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

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.iotprojectdailyreport.vo.IotProjectDailyReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojectdailyreport.vo.IotProjectDailyReportRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotprojectdailyreport.vo.IotProjectDailyReportSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotprojectdailyreport.IotProjectDailyReportDO;
import cn.iocoder.yudao.module.pms.service.iotprojectdailyreport.IotProjectDailyReportService;
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("/rq/iot-project-daily-report")
@Validated
public class IotProjectDailyReportController {

    @Resource
    private IotProjectDailyReportService iotProjectDailyReportService;

    @PostMapping("/create")
    @Operation(summary = "创建项目日报")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-daily-report:create')")
    public CommonResult<Long> createIotProjectDailyReport(@Valid @RequestBody IotProjectDailyReportSaveReqVO createReqVO) {
        return success(iotProjectDailyReportService.createIotProjectDailyReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目日报")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-daily-report:update')")
    public CommonResult<Boolean> updateIotProjectDailyReport(@Valid @RequestBody IotProjectDailyReportSaveReqVO updateReqVO) {
        iotProjectDailyReportService.updateIotProjectDailyReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目日报")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-project-daily-report:delete')")
    public CommonResult<Boolean> deleteIotProjectDailyReport(@RequestParam("id") Long id) {
        iotProjectDailyReportService.deleteIotProjectDailyReport(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目日报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-daily-report:query')")
    public CommonResult<IotProjectDailyReportRespVO> getIotProjectDailyReport(@RequestParam("id") Long id) {
        IotProjectDailyReportDO iotProjectDailyReport = iotProjectDailyReportService.getIotProjectDailyReport(id);
        return success(BeanUtils.toBean(iotProjectDailyReport, IotProjectDailyReportRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目日报分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-daily-report:query')")
    public CommonResult<PageResult<IotProjectDailyReportRespVO>> getIotProjectDailyReportPage(@Valid IotProjectDailyReportPageReqVO pageReqVO) {
        PageResult<IotProjectDailyReportDO> pageResult = iotProjectDailyReportService.getIotProjectDailyReportPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotProjectDailyReportRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出项目日报 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-project-daily-report:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotProjectDailyReportExcel(@Valid IotProjectDailyReportPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotProjectDailyReportDO> list = iotProjectDailyReportService.getIotProjectDailyReportPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "项目日报.xls", "数据", IotProjectDailyReportRespVO.class,
                        BeanUtils.toBean(list, IotProjectDailyReportRespVO.class));
    }

}