package com.zt.plat.module.qms.business.reportdoc.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.zt.plat.framework.apilog.core.annotation.ApiAccessLog;
import com.zt.plat.framework.business.annotation.FileUploadController;
import com.zt.plat.framework.business.controller.AbstractFileUploadController;
import com.zt.plat.framework.business.interceptor.BusinessControllerMarker;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.framework.common.pojo.PageParam;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.pojo.vo.BatchDeleteReqVO;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.*;
import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentMainDO;
import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentTypeDO;
import com.zt.plat.module.qms.business.reportdoc.service.ReportDocumentMainService;
import com.zt.plat.module.qms.business.reportdoc.service.ReportDocumentTypeService;
import com.zt.plat.module.qms.enums.QmsCommonConstant;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.error;
import static com.zt.plat.framework.common.pojo.CommonResult.success;
import static com.zt.plat.module.qms.enums.ErrorCodeConstants.REPORT_DOCUMENT_TYPE_NOT_EXISTS;

@Tag(name = "管理后台 - 检测报告业务")
@RestController
@RequestMapping("/qms/report-document-main")
@Validated
@FileUploadController(source = "qms.reportdocumentmain")
public class ReportDocumentMainController extends AbstractFileUploadController implements BusinessControllerMarker{

    static {
        FileUploadController annotation = ReportDocumentMainController.class.getAnnotation(FileUploadController.class);
        if (annotation != null) {
            setFileUploadInfo(annotation);
        }
    }

    @Resource private ReportDocumentMainService reportDocumentMainService;
    @Resource private ReportDocumentTypeService reportDocumentTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建检测报告")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main:create')")
    public CommonResult<ReportDocumentMainRespVO> createReportDocumentMain(@Valid @RequestBody ReportDocumentMainSaveReqVO createReqVO) {
        return success(reportDocumentMainService.createReportDocumentMain(createReqVO));
    }

    @PostMapping("/createTempData")
    @Operation(summary = "创建临时数据")
    public CommonResult<ReportDocumentMainRespVO> createTempData(@RequestBody ReportDocumentMainSaveReqVO vo) {
        Long reportDocumentTypeId = vo.getReportDocumentTypeId();
        if(reportDocumentTypeId == null){
            return error(REPORT_DOCUMENT_TYPE_NOT_EXISTS, "报告类型ID不能为空，请刷新后重试");
        }
        ReportDocumentTypeDO typeDO = reportDocumentTypeService.getReportDocumentType(reportDocumentTypeId);
        if(typeDO == null){
            return error(REPORT_DOCUMENT_TYPE_NOT_EXISTS, "报告类型不存在，请刷新后重试");
        }
        vo.setFlowKey(typeDO.getFlowKey());
        vo.setDocumentType(typeDO.getDocumentType());
        vo.setConfigReportTypeId(typeDO.getConfigReportTypeId());
        vo.setDocumentConfig(JSONObject.toJSONString(typeDO));
        vo.setFlowStatus(QmsCommonConstant.NOT_START);
        vo.setCancelFlag("-1");
        return success(reportDocumentMainService.createReportDocumentMain(vo));
    }

    @PostMapping("/createProcessInstance")
    @Operation(summary = "发起流程")
    public CommonResult<ReportDocumentMainRespVO> createProcessInstance(@Valid @RequestBody ReportDocumentMainSaveReqVO createReqVO) {
        return reportDocumentMainService.createProcessInstance(createReqVO);
    }

    @PostMapping("/execAddData")
    @Operation(summary = "增加数据")
    public CommonResult<String> execAddData(@RequestBody ReportDocumentMainSaveReqVO vo) {
        return reportDocumentMainService.execAddData(vo);
    }

    @PostMapping("/execRemoveData")
    @Operation(summary = "移除数据")
    public CommonResult<String> execRemoveData(@RequestBody ReportDocumentMainSaveReqVO vo) {
        return reportDocumentMainService.execRemoveData(vo);
    }

    @PutMapping("/update")
    @Operation(summary = "更新检测报告业务")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main:update')")
    public CommonResult<Boolean> updateReportDocumentMain(@Valid @RequestBody ReportDocumentMainSaveReqVO updateReqVO) {
        updateReqVO.setCancelFlag("0");
        reportDocumentMainService.updateReportDocumentMain(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除检测报告业务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qms:report-document-main:delete')")
    public CommonResult<Boolean> deleteReportDocumentMain(@RequestParam("id") Long id) {
        reportDocumentMainService.deleteReportDocumentMain(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除检测报告业务")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main:delete')")
    public CommonResult<Boolean> deleteReportDocumentMainList(@RequestBody BatchDeleteReqVO req) {
        reportDocumentMainService.deleteReportDocumentMainListByIds(req.getIds());
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得检测报告业务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main:query')")
    public CommonResult<ReportDocumentMainRespVO> getReportDocumentMain(@RequestParam("id") Long id) {
        ReportDocumentMainDO reportDocumentMain = reportDocumentMainService.getReportDocumentMain(id);
        return success(BeanUtils.toBean(reportDocumentMain, ReportDocumentMainRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检测报告业务分页")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main:query')")
    public CommonResult<PageResult<ReportDocumentMainRespVO>> getReportDocumentMainPage(@Valid ReportDocumentMainPageReqVO pageReqVO) {
        PageResult<ReportDocumentMainDO> pageResult = reportDocumentMainService.getReportDocumentMainPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReportDocumentMainRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检测报告业务 Excel")
    @PreAuthorize("@ss.hasPermission('qms:report-document-main:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportDocumentMainExcel(@Valid ReportDocumentMainPageReqVO pageReqVO,
                                              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReportDocumentMainDO> list = reportDocumentMainService.getReportDocumentMainPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "检测报告业务.xls", "数据", ReportDocumentMainRespVO.class,
                BeanUtils.toBean(list, ReportDocumentMainRespVO.class));
    }

}