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

import cn.iocoder.yudao.module.pms.controller.admin.qhse.accident.vo.IotAccidentReportProcessPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.accident.vo.IotAccidentReportProcessRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.accident.vo.IotAccidentReportProcessSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.accident.IotAccidentReportProcessDO;
import cn.iocoder.yudao.module.pms.service.qhse.accident.IotAccidentReportProcessService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


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

    @Resource
    private IotAccidentReportProcessService iotAccidentReportProcessService;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE事故上报过程明细")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report-process:create')")
    public CommonResult<Long> createIotAccidentReportProcess(@Valid @RequestBody IotAccidentReportProcessSaveReqVO createReqVO) {
        return success(iotAccidentReportProcessService.createIotAccidentReportProcess(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新QHSE事故上报过程明细")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report-process:update')")
    public CommonResult<Boolean> updateIotAccidentReportProcess(@Valid @RequestBody IotAccidentReportProcessSaveReqVO updateReqVO) {
        iotAccidentReportProcessService.updateIotAccidentReportProcess(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得QHSE事故上报过程明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report-process:query')")
    public CommonResult<IotAccidentReportProcessRespVO> getIotAccidentReportProcess(@RequestParam("id") Long id) {
        IotAccidentReportProcessDO iotAccidentReportProcess = iotAccidentReportProcessService.getIotAccidentReportProcess(id);
        return success(BeanUtils.toBean(iotAccidentReportProcess, IotAccidentReportProcessRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE事故上报过程明细分页")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report-process:query')")
    public CommonResult<PageResult<IotAccidentReportProcessRespVO>> getIotAccidentReportProcessPage(@Valid IotAccidentReportProcessPageReqVO pageReqVO) {
        PageResult<IotAccidentReportProcessDO> pageResult = iotAccidentReportProcessService.getIotAccidentReportProcessPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, IotAccidentReportProcessRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE事故上报过程明细 Excel")
    @PreAuthorize("@ss.hasPermission('rq:iot-accident-report-process:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportIotAccidentReportProcessExcel(@Valid IotAccidentReportProcessPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<IotAccidentReportProcessDO> list = iotAccidentReportProcessService.getIotAccidentReportProcessPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE事故上报过程明细.xls", "数据", IotAccidentReportProcessRespVO.class,
                        BeanUtils.toBean(list, IotAccidentReportProcessRespVO.class));
    }

}