package cn.iocoder.yudao.module.pms.controller.admin.iotryhidailyreport;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotryhidailyreport.vo.IotRyHiDailyReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotryhidailyreport.vo.IotRyHiDailyReportRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotryhidailyreport.vo.IotRyHiDailyReportSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotryhidailyreport.IotRyHiDailyReportDO;
import cn.iocoder.yudao.module.pms.service.iotryhidailyreport.IotRyHiDailyReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 瑞鹰历史日报")
@RestController
@RequestMapping("/pms/iot-ry-hi-daily-report")
@Validated
public class IotRyHiDailyReportController {

    @Resource
    private IotRyHiDailyReportService iotRyHiDailyReportService;

    @PostMapping("/create")
    @Operation(summary = "创建瑞鹰历史日报")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-hi-daily-report:create')")
    public CommonResult<Long> createIotRyHiDailyReport(@Valid @RequestBody IotRyHiDailyReportSaveReqVO createReqVO) {
        return success(iotRyHiDailyReportService.createIotRyHiDailyReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新瑞鹰历史日报")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-hi-daily-report:update')")
    public CommonResult<Boolean> updateIotRyHiDailyReport(@Valid @RequestBody IotRyHiDailyReportSaveReqVO updateReqVO) {
        iotRyHiDailyReportService.updateIotRyHiDailyReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除瑞鹰历史日报")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-hi-daily-report:delete')")
    public CommonResult<Boolean> deleteIotRyHiDailyReport(@RequestParam("id") Long id) {
        iotRyHiDailyReportService.deleteIotRyHiDailyReport(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得瑞鹰历史日报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-hi-daily-report:query')")
    public CommonResult<IotRyHiDailyReportRespVO> getIotRyHiDailyReport(@RequestParam("id") Long id) {
        IotRyHiDailyReportDO iotRyHiDailyReport = iotRyHiDailyReportService.getIotRyHiDailyReport(id);
        return success(BeanUtils.toBean(iotRyHiDailyReport, IotRyHiDailyReportRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得瑞鹰历史日报分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-hi-daily-report:query')")
    public CommonResult<PageResult<IotRyHiDailyReportRespVO>> getIotRyHiDailyReportPage(@Valid IotRyHiDailyReportPageReqVO pageReqVO) {
        PageResult<IotRyHiDailyReportDO> pageResult = iotRyHiDailyReportService.getIotRyHiDailyReportPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotRyHiDailyReportRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出瑞鹰历史日报 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-hi-daily-report:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotRyHiDailyReportExcel(@Valid IotRyHiDailyReportPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotRyHiDailyReportDO> list = iotRyHiDailyReportService.getIotRyHiDailyReportPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "瑞鹰历史日报.xls", "数据", IotRyHiDailyReportRespVO.class,
                        BeanUtils.toBean(list, IotRyHiDailyReportRespVO.class));
    }

}