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

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.cert.vo.QhseCertPersonPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.cert.vo.QhseCertPersonRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.cert.vo.QhseCertPersonSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.cert.QhseCertPersonDO;
import cn.iocoder.yudao.module.pms.service.qhse.cert.QhseCertPersonService;
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 = "管理后台 - QHSE-证书人员")
@RestController
@RequestMapping("/rq/qhse-cert-person")
@Validated
public class QhseCertPersonController {

    @Resource
    private QhseCertPersonService qhseCertPersonService;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE-证书人员")
    @PreAuthorize("@ss.hasPermission('rq:qhse-cert-person:create')")
    public CommonResult<Long> createQhseCertPerson(@Valid @RequestBody QhseCertPersonSaveReqVO createReqVO) {
        return success(qhseCertPersonService.createQhseCertPerson(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新QHSE-证书人员")
    @PreAuthorize("@ss.hasPermission('rq:qhse-cert-person:update')")
    public CommonResult<Boolean> updateQhseCertPerson(@Valid @RequestBody QhseCertPersonSaveReqVO updateReqVO) {
        qhseCertPersonService.updateQhseCertPerson(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QHSE-证书人员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:qhse-cert-person:delete')")
    public CommonResult<Boolean> deleteQhseCertPerson(@RequestParam("id") Long id) {
        qhseCertPersonService.deleteQhseCertPerson(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QHSE-证书人员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:qhse-cert-person:query')")
    public CommonResult<QhseCertPersonRespVO> getQhseCertPerson(@RequestParam("id") Long id) {
        QhseCertPersonDO qhseCertPerson = qhseCertPersonService.getQhseCertPerson(id);
        return success(BeanUtils.toBean(qhseCertPerson, QhseCertPersonRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE-证书人员分页")
    @PreAuthorize("@ss.hasPermission('rq:qhse-cert-person:query')")
    public CommonResult<PageResult<QhseCertPersonRespVO>> getQhseCertPersonPage(@Valid QhseCertPersonPageReqVO pageReqVO) {
        PageResult<QhseCertPersonDO> pageResult = qhseCertPersonService.getQhseCertPersonPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QhseCertPersonRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE-证书人员 Excel")
    @PreAuthorize("@ss.hasPermission('rq:qhse-cert-person:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQhseCertPersonExcel(@Valid QhseCertPersonPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QhseCertPersonDO> list = qhseCertPersonService.getQhseCertPersonPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE-证书人员.xls", "数据", QhseCertPersonRespVO.class,
                        BeanUtils.toBean(list, QhseCertPersonRespVO.class));
    }

}