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

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.jsa.vo.QhseJsaPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.jsa.vo.QhseJsaRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.qhse.jsa.vo.QhseJsaSaveReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.qhse.jsa.QhseJsaDO;
import cn.iocoder.yudao.module.pms.service.qhse.jsa.QhseJsaService;
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_JSA工作安全分析")
@RestController
@RequestMapping("/rq/qhse-jsa")
@Validated
public class QhseJsaController {

    @Resource
    private QhseJsaService qhseJsaService;

    @PostMapping("/create")
    @Operation(summary = "创建QHSE_JSA工作安全分析")
    @PreAuthorize("@ss.hasPermission('rq:qhse-jsa:create')")
    public CommonResult<Long> createQhseJsa(@Valid @RequestBody QhseJsaSaveReqVO createReqVO) {
        return success(qhseJsaService.createQhseJsa(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新QHSE_JSA工作安全分析")
    @PreAuthorize("@ss.hasPermission('rq:qhse-jsa:update')")
    public CommonResult<Boolean> updateQhseJsa(@Valid @RequestBody QhseJsaSaveReqVO updateReqVO) {
        qhseJsaService.updateQhseJsa(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除QHSE_JSA工作安全分析")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rq:qhse-jsa:delete')")
    public CommonResult<Boolean> deleteQhseJsa(@RequestParam("id") Long id) {
        qhseJsaService.deleteQhseJsa(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得QHSE_JSA工作安全分析")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rq:qhse-jsa:query')")
    public CommonResult<QhseJsaRespVO> getQhseJsa(@RequestParam("id") Long id) {
        QhseJsaDO qhseJsa = qhseJsaService.getQhseJsa(id);
        return success(BeanUtils.toBean(qhseJsa, QhseJsaRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得QHSE_JSA工作安全分析分页")
    @PreAuthorize("@ss.hasPermission('rq:qhse-jsa:query')")
    public CommonResult<PageResult<QhseJsaRespVO>> getQhseJsaPage(@Valid QhseJsaPageReqVO pageReqVO) {
        PageResult<QhseJsaDO> pageResult = qhseJsaService.getQhseJsaPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, QhseJsaRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出QHSE_JSA工作安全分析 Excel")
    @PreAuthorize("@ss.hasPermission('rq:qhse-jsa:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportQhseJsaExcel(@Valid QhseJsaPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QhseJsaDO> list = qhseJsaService.getQhseJsaPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "QHSE_JSA工作安全分析.xls", "数据", QhseJsaRespVO.class,
                        BeanUtils.toBean(list, QhseJsaRespVO.class));
    }

}