package cn.iocoder.yudao.module.pms.controller.admin.qhse.accident;

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.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.accident.vo.IotAccidentReportPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.accident.vo.IotAccidentReportRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.accident.vo.IotAccidentReportSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.accident.IotAccidentReportDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.accident.IotAccidentReportProcessDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.accident.IotAccidentReportProcessMapper;
import cn.iocoder.yudao.module.pms.service.qhse.accident.IotAccidentReportService;
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.beans.factory.annotation.Autowired;
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 java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

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


@Tag(name = "管理后台 - QHSE事故上报")
@RestController
@RequestMapping("/rq/iot-accident-report")
@Validated
public class IotAccidentReportController {

    @Resource
    private IotAccidentReportService iotAccidentReportService;
    @Autowired
    private IotAccidentReportProcessMapper iotAccidentReportProcessMapper;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE事故上报")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report:create')")
    public CommonResult<Long> createIotAccidentReport(@Valid @RequestBody IotAccidentReportSaveReqVO createReqVO) {
        return success(iotAccidentReportService.createIotAccidentReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新QHSE事故上报")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report:update')")
    public CommonResult<Boolean> updateIotAccidentReport(@Valid @RequestBody IotAccidentReportSaveReqVO updateReqVO) {
        iotAccidentReportService.updateIotAccidentReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QHSE事故上报")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report:delete')")
    public CommonResult<Boolean> deleteIotAccidentReport(@RequestParam("id") Long id) {
        iotAccidentReportService.deleteIotAccidentReport(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QHSE事故上报")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report:query')")
    public CommonResult<IotAccidentReportRespVO> getIotAccidentReport(@RequestParam("id") Long id) {
        IotAccidentReportDO iotAccidentReport = iotAccidentReportService.getIotAccidentReport(id);
        return success(BeanUtils.toBean(iotAccidentReport, IotAccidentReportRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE事故上报分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report:query')")
    public CommonResult<PageResult<IotAccidentReportRespVO>> getIotAccidentReportPage(@Valid IotAccidentReportPageReqVO pageReqVO) {
        PageResult<IotAccidentReportDO> pageResult = iotAccidentReportService.getIotAccidentReportPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotAccidentReportRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE事故上报 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotAccidentReportExcel(@Valid IotAccidentReportPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotAccidentReportDO> list = iotAccidentReportService.getIotAccidentReportPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE事故上报.xls", "数据", IotAccidentReportRespVO.class,
                        BeanUtils.toBean(list, IotAccidentReportRespVO.class));
    }


    @GetMapping("/approval/page")
    @Operation(summary = "获得QHSE事故审批分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report:query')")
    public CommonResult<PageResult<IotAccidentReportRespVO>> getIotAccidentReportApprovalPage(@Valid IotAccidentReportPageReqVO pageReqVO) {
        //查询当前处理人为登录人的上报信息
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        List<IotAccidentReportProcessDO> processes = iotAccidentReportProcessMapper.selectList("creator", loginUserId);
        Set<Long> accidentIds = processes.stream().map(IotAccidentReportProcessDO::getAccidentId).collect(Collectors.toSet());
        pageReqVO.setIds(accidentIds);
        pageReqVO.setCurrentPerson(loginUserId);
        PageResult<IotAccidentReportDO> pageResult = iotAccidentReportService.getIotAccidentReportApprovalPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotAccidentReportRespVO.class));
    }

    @GetMapping("/approval")
    @Operation(summary = "上报操作")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report:approval')")
    public CommonResult<String> approvalReport(Long id, String suggestion) {
        assert Objects.nonNull(id);
        iotAccidentReportService.approvalAccident(id, suggestion);
        return success("成功");
    }
}