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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
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.iotryimprovedailyreport.vo.IotRyImproveDailyReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotryimprovedailyreport.vo.IotRyImproveDailyReportRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotryimprovedailyreport.vo.IotRyImproveDailyReportSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotryimprovedailyreportdetail.vo.IotRyImproveDailyReportDetailPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotryimprovedailyreportdetail.vo.IotRyImproveDailyReportDetailRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotryimprovedailyreport.IotRyImproveDailyReportDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotryimprovedailyreportdetail.IotRyImproveDailyReportDetailDO;
import cn.iocoder.yudao.module.pms.service.iotryimprovedailyreport.IotRyImproveDailyReportService;
import cn.iocoder.yudao.module.pms.service.iotryimprovedailyreportdetail.IotRyImproveDailyReportDetailService;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
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.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen;

@Tag(name = "管理后台 - 瑞鹰项目启动设备整改")
@RestController
@RequestMapping("/pms/iot-ry-improve-daily-report")
@Validated
public class IotRyImproveDailyReportController {

    @Resource
    private IotRyImproveDailyReportService iotRyImproveDailyReportService;
    @Resource
    private DeptService deptService;
    @Resource
    private IotRyImproveDailyReportDetailService iotRyImproveDailyReportDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建瑞鹰项目启动设备整改")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report:create')")
    public CommonResult<Long> createIotRyImproveDailyReport(@Valid @RequestBody IotRyImproveDailyReportSaveReqVO createReqVO) {
        return success(iotRyImproveDailyReportService.createIotRyImproveDailyReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新瑞鹰项目启动设备整改")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report:update')")
    public CommonResult<Boolean> updateIotRyImproveDailyReport(@Valid @RequestBody IotRyImproveDailyReportSaveReqVO updateReqVO) {
        iotRyImproveDailyReportService.updateIotRyImproveDailyReport(updateReqVO);
        return success(true);
    }

    @PutMapping("/approval")
    @Operation(summary = "审批日报")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report:update')")
    public CommonResult<Boolean> approvalImproveDailyReport(@Valid @RequestBody IotRyImproveDailyReportSaveReqVO updateReqVO) {
        iotRyImproveDailyReportService.approvalImproveDailyReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除瑞鹰项目启动设备整改")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report:delete')")
    public CommonResult<Boolean> deleteIotRyImproveDailyReport(@RequestParam("id") Long id) {
        iotRyImproveDailyReportService.deleteIotRyImproveDailyReport(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得瑞鹰项目启动设备整改")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report:query')")
    public CommonResult<IotRyImproveDailyReportRespVO> getIotRyImproveDailyReport(@RequestParam("id") Long id) {
        IotRyImproveDailyReportRespVO result = new IotRyImproveDailyReportRespVO();
        IotRyImproveDailyReportDO iotRyImproveDailyReport = iotRyImproveDailyReportService.getIotRyImproveDailyReport(id);
        if (ObjUtil.isEmpty(iotRyImproveDailyReport)) {
            return success(result);
        }
        result = BeanUtils.toBean(iotRyImproveDailyReport, IotRyImproveDailyReportRespVO.class);
        // 设置 设备整改日报明细
        IotRyImproveDailyReportDetailPageReqVO pageReqVO = new IotRyImproveDailyReportDetailPageReqVO();
        pageReqVO.setReportId(result.getId());
        PageResult<IotRyImproveDailyReportDetailDO> pageResult = iotRyImproveDailyReportDetailService.getIotRyImproveDailyReportDetailPage(pageReqVO);
        List<IotRyImproveDailyReportDetailDO> reports = pageResult.getList();
        List<IotRyImproveDailyReportDetailRespVO> details = new ArrayList<>();
        if (CollUtil.isNotEmpty(reports)) {
            details = BeanUtils.toBean(reports, IotRyImproveDailyReportDetailRespVO.class);
        }
        result.setImproveReportDetails(details);
        return success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "获得瑞鹰项目启动设备整改分页")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report:query')")
    public CommonResult<PageResult<IotRyImproveDailyReportRespVO>> getIotRyImproveDailyReportPage(@Valid IotRyImproveDailyReportPageReqVO pageReqVO) {
        PageResult<IotRyImproveDailyReportDO> pageResult = iotRyImproveDailyReportService.getIotRyImproveDailyReportPage(pageReqVO);
        return success(new PageResult<>(buildRyDailyReports(pageResult.getList()), pageResult.getTotal()));
        // return success(BeanUtils.toBean(pageResult, IotRyImproveDailyReportRespVO.class));
    }

    /**
     * 瑞恒日报分页 设置关联查询信息
     * @param reports
     * @return
     */
    private List<IotRyImproveDailyReportRespVO> buildRyDailyReports(List<IotRyImproveDailyReportDO> reports) {
        if (CollUtil.isEmpty(reports)) {
            return Collections.emptyList();
        }
        // 设备部门信息
        Map<Long, DeptDO> deptMap = deptService.getDeptMap(convertList(reports, IotRyImproveDailyReportDO::getDeptId));
        // 2. 拼接数据
        return BeanUtils.toBean(reports, IotRyImproveDailyReportRespVO.class, (reportVO) -> {
            // 2.1 拼接部门信息
            findAndThen(deptMap, reportVO.getDeptId(), dept -> reportVO.setDeptName(dept.getName()));

        });
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出瑞鹰项目启动设备整改 Excel")
    @PreAuthorize("@ss.hasPermission('pms:iot-ry-improve-daily-report:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotRyImproveDailyReportExcel(@Valid IotRyImproveDailyReportPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotRyImproveDailyReportDO> list = iotRyImproveDailyReportService.getIotRyImproveDailyReportPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "瑞鹰项目启动设备整改.xls", "数据", IotRyImproveDailyReportRespVO.class,
                        BeanUtils.toBean(list, IotRyImproveDailyReportRespVO.class));
    }

}