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

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.qhse.ptw.vo.QhsePtwPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.ptw.vo.QhsePtwRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.ptw.vo.QhsePtwSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.ptw.QhsePtwDO;
import cn.iocoder.yudao.module.pms.dal.mysql.qhse.cert.IotMeasureCertMapper;
import cn.iocoder.yudao.module.pms.service.qhse.ptw.QhsePtwService;
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 static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - QHSE_PTW作业许可")
@RestController
@RequestMapping("/rq/qhse-ptw")
@Validated
public class QhsePtwController {

    @Resource
    private QhsePtwService qhsePtwService;
    @Autowired
    private IotMeasureCertMapper iotMeasureCertMapper;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE_PTW作业许可")
    @PreAuthorize("@ss.hasPermission('rq:qhse-ptw:create')")
    public CommonResult<Long> createQhsePtw(@Valid @RequestBody QhsePtwSaveReqVO createReqVO) {
        return success(qhsePtwService.createQhsePtw(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新QHSE_PTW作业许可")
    @PreAuthorize("@ss.hasPermission('rq:qhse-ptw:update')")
    public CommonResult<Boolean> updateQhsePtw(@Valid @RequestBody QhsePtwSaveReqVO updateReqVO) {
        qhsePtwService.updateQhsePtw(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QHSE_PTW作业许可")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:qhse-ptw:delete')")
    public CommonResult<Boolean> deleteQhsePtw(@RequestParam("id") Long id) {
        qhsePtwService.deleteQhsePtw(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QHSE_PTW作业许可")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:qhse-ptw:query')")
    public CommonResult<QhsePtwRespVO> getQhsePtw(@RequestParam("id") Long id) {
        QhsePtwDO qhsePtw = qhsePtwService.getQhsePtw(id);
        return success(BeanUtils.toBean(qhsePtw, QhsePtwRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE_PTW作业许可分页")
    @PreAuthorize("@ss.hasPermission('rq:qhse-ptw:query')")
    public CommonResult<PageResult<QhsePtwRespVO>> getQhsePtwPage(@Valid QhsePtwPageReqVO pageReqVO) {
        PageResult<QhsePtwDO> pageResult = qhsePtwService.getQhsePtwPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QhsePtwRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE_PTW作业许可 Excel")
    @PreAuthorize("@ss.hasPermission('rq:qhse-ptw:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQhsePtwExcel(@Valid QhsePtwPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QhsePtwDO> list = qhsePtwService.getQhsePtwPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE_PTW作业许可.xls", "数据", QhsePtwRespVO.class,
                        BeanUtils.toBean(list, QhsePtwRespVO.class));
    }

//
//    @GetMapping("/cert-owner")
//    @Operation(summary = "获得QHSE证书相关人员")
//    @PreAuthorize("@ss.hasPermission('rq:qhse-ptw:query')")
//    public CommonResult<PageResult<QhsePtwRespVO>> getPtwCertOwner() {
//        Long loginUserDeptId = SecurityFrameworkUtils.getLoginUserDeptId();
//        List<IotMeasureCertDO> iotMeasureCertDOS = iotMeasureCertMapper.selectList("dept_id", loginUserDeptId);
//        iotMeasureCertDOS.stream()
//        return null;
//    }
}