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

import com.alibaba.fastjson.JSONArray;
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.datapermission.core.annotation.DeptDataPermissionIgnore;
import com.zt.plat.framework.excel.core.util.ExcelUtils;
import com.zt.plat.module.qms.business.bus.dal.dataobject.BusinessAssayReportDataDO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentDataPageReqVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentDataRespVO;
import com.zt.plat.module.qms.business.reportdoc.controller.vo.ReportDocumentDataSaveReqVO;
import com.zt.plat.module.qms.business.reportdoc.dal.dataobject.ReportDocumentDataDO;
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.ReportDocumentDataService;
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.common.data.service.DataTemplateService;
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.HashMap;
import java.util.List;
import java.util.Map;

import static com.zt.plat.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.zt.plat.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 检测报告明细")
@RestController
@RequestMapping("/qms/report-document-data")
@Validated
@DeptDataPermissionIgnore(enable = "true")
@FileUploadController(source = "qms.reportdocumentdata")
public class ReportDocumentDataController extends AbstractFileUploadController implements BusinessControllerMarker{

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

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


    @PostMapping("/assembleByReportData")
    @Operation(summary = "创建检测报告明细")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-data:create')")
    public CommonResult<JSONArray> assembleByReportData(@RequestBody JSONObject jsonObject) {
//        Long mainId = jsonObject.getLong("mainId");

        return reportDocumentDataService.assembleByReportData(jsonObject);
    }

    /*
    * 查询报告明细数据，返回结果包含表头、数据、检出限等。会按报告配置进行“换行”处理。*/
    @GetMapping("/queryReportDetail")
    @Operation(summary = "查询报告明细数据")
    @Parameter(name = "mainId", description = "报告id", required = true, example = "1024")
    public CommonResult<?> queryReportDetail(@RequestParam("mainId") Long mainId, @RequestParam(name = "pageFlag", required = false) String pageFlag) {

        ReportDocumentMainDO mainDO = reportDocumentMainService.getReportDocumentMain(mainId);
        Long typeId = mainDO.getReportDocumentTypeId();
        ReportDocumentTypeDO typeDO = reportDocumentTypeService.getReportDocumentType(typeId);
        CommonResult<JSONArray> result = reportDocumentDataService.assembleDynamicData(mainDO, typeDO, pageFlag);
        return result;
    }

    @GetMapping("/queryAddedData")
    @Operation(summary = "查询已编制报表数据")
    @Parameter(name = "mainId", description = "报告id", required = true, example = "1024")
    public CommonResult<List<ReportDocumentDataRespVO>> queryAddedData(@RequestParam("mainId") Long mainId) {

        ReportDocumentMainDO mainDO = reportDocumentMainService.getReportDocumentMain(mainId);
        Long typeId = mainDO.getReportDocumentTypeId();
        ReportDocumentTypeDO typeDO = reportDocumentTypeService.getReportDocumentType(typeId);
        Long confId = typeDO.getConfigReportTypeId();
        Map<String, Object> param = new HashMap<>();

        List<ReportDocumentDataDO> list = reportDocumentDataService.listByMainDataId(mainId).getData();
        return success(BeanUtils.toBean(list, ReportDocumentDataRespVO.class));
    }


    @PostMapping("/create")
    @Operation(summary = "创建检测报告明细")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-data:create')")
    public CommonResult<ReportDocumentDataRespVO> createReportDocumentData(@Valid @RequestBody ReportDocumentDataSaveReqVO createReqVO) {
        return success(reportDocumentDataService.createReportDocumentData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新检测报告明细")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-data:update')")
    public CommonResult<Boolean> updateReportDocumentData(@Valid @RequestBody ReportDocumentDataSaveReqVO updateReqVO) {
        reportDocumentDataService.updateReportDocumentData(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得检测报告明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-data:query')")
    public CommonResult<ReportDocumentDataRespVO> getReportDocumentData(@RequestParam("id") Long id) {
        ReportDocumentDataDO reportDocumentData = reportDocumentDataService.getReportDocumentData(id);
        return success(BeanUtils.toBean(reportDocumentData, ReportDocumentDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得检测报告明细分页")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-data:query')")
    public CommonResult<PageResult<ReportDocumentDataRespVO>> getReportDocumentDataPage(@Valid ReportDocumentDataPageReqVO pageReqVO) {
        PageResult<ReportDocumentDataDO> pageResult = reportDocumentDataService.getReportDocumentDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ReportDocumentDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出检测报告明细 Excel")
    //@PreAuthorize("@ss.hasPermission('qms:report-document-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportReportDocumentDataExcel(@Valid ReportDocumentDataPageReqVO pageReqVO,
                                              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ReportDocumentDataDO> list = reportDocumentDataService.getReportDocumentDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "检测报告明细.xls", "数据", ReportDocumentDataRespVO.class,
                BeanUtils.toBean(list, ReportDocumentDataRespVO.class));
    }

}