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

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.emergency.vo.QhseEmergencyCertPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.emergency.vo.QhseEmergencyCertRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.emergency.vo.QhseEmergencyCertSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.emergency.QhseEmergencyCertDO;
import cn.iocoder.yudao.module.pms.service.qhse.emergency.QhseEmergencyCertService;
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-emergency-cert")
@Validated
public class QhseEmergencyCertController {

    @Resource
    private QhseEmergencyCertService qhseEmergencyCertService;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE应急物资检测证书")
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-cert:create')")
    public CommonResult<Long> createQhseEmergencyCert(@Valid @RequestBody QhseEmergencyCertSaveReqVO createReqVO) {
        return success(qhseEmergencyCertService.createQhseEmergencyCert(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新QHSE应急物资检测证书")
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-cert:update')")
    public CommonResult<Boolean> updateQhseEmergencyCert(@Valid @RequestBody QhseEmergencyCertSaveReqVO updateReqVO) {
        qhseEmergencyCertService.updateQhseEmergencyCert(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QHSE应急物资检测证书")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-cert:delete')")
    public CommonResult<Boolean> deleteQhseEmergencyCert(@RequestParam("id") Long id) {
        qhseEmergencyCertService.deleteQhseEmergencyCert(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QHSE应急物资检测证书")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-cert:query')")
    public CommonResult<QhseEmergencyCertRespVO> getQhseEmergencyCert(@RequestParam("id") Long id) {
        QhseEmergencyCertDO qhseEmergencyCert = qhseEmergencyCertService.getQhseEmergencyCert(id);
        return success(BeanUtils.toBean(qhseEmergencyCert, QhseEmergencyCertRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE应急物资检测证书分页")
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-cert:query')")
    public CommonResult<PageResult<QhseEmergencyCertRespVO>> getQhseEmergencyCertPage(@Valid QhseEmergencyCertPageReqVO pageReqVO) {
        PageResult<QhseEmergencyCertDO> pageResult = qhseEmergencyCertService.getQhseEmergencyCertPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QhseEmergencyCertRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE应急物资检测证书 Excel")
    @PreAuthorize("@ss.hasPermission('rq:qhse-emergency-cert:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQhseEmergencyCertExcel(@Valid QhseEmergencyCertPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QhseEmergencyCertDO> list = qhseEmergencyCertService.getQhseEmergencyCertPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE应急物资检测证书.xls", "数据", QhseEmergencyCertRespVO.class,
                        BeanUtils.toBean(list, QhseEmergencyCertRespVO.class));
    }

}