package cn.iocoder.yudao.module.mes.service.safety;
import cn.idev.excel.FastExcelFactory;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.module.mes.controller.admin.safety.vo.ledger.SafetyLedgerExportReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.safety.*;
import cn.iocoder.yudao.module.mes.enums.safety.*;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.mes.enums.SafetyErrorCodeConstants.SAFETY_LEDGER_DATE_RANGE_INVALID;
/**
* 安全生产 - 电力行业标准化台账导出 Service
*
*
按「全套」导出时一次生成多 Sheet 工作簿,覆盖安全标准化检查所需的全部台账:
* 组织架构、特种作业证、巡检记录、隐患台账、作业票、监护记录、物资台账、
* 出入库流水、培训记录、演练记录、事故登记。
*
* @author 超级管理员
*/
@Service
public class SafetyLedgerExportService {
@Resource
private SafetyOrgService orgService;
@Resource
private SafetyCertificateService certificateService;
@Resource
private SafetyInspectionService inspectionService;
@Resource
private SafetyHazardService hazardService;
@Resource
private SafetyWorkPermitService permitService;
@Resource
private SafetyMaterialService materialService;
@Resource
private SafetyTrainingService trainingService;
/**
* 导出标准化台账
*/
public void export(SafetyLedgerExportReqVO reqVO, HttpServletResponse response) throws IOException {
LocalDate start = reqVO.getStartDate();
LocalDate end = reqVO.getEndDate();
if (start != null && end != null && start.isAfter(end)) {
throw exception(SAFETY_LEDGER_DATE_RANGE_INVALID);
}
String type = reqVO.getType() == null ? "ALL" : reqVO.getType();
List sheets = new ArrayList<>();
if ("ALL".equals(type) || "CERT".equals(type)) {
sheets.add(certSheet());
}
if ("ALL".equals(type) || "INSPECTION".equals(type)) {
sheets.add(inspectionSheet(start, end));
}
if ("ALL".equals(type) || "HAZARD".equals(type)) {
sheets.add(hazardSheet(start, end));
}
if ("ALL".equals(type) || "PERMIT".equals(type)) {
sheets.add(permitSheet(start, end));
sheets.add(monitorSheet());
}
if ("ALL".equals(type) || "MATERIAL".equals(type)) {
sheets.add(materialSheet());
sheets.add(stockSheet(start, end));
}
if ("ALL".equals(type) || "TRAINING".equals(type)) {
sheets.add(trainingSheet(start, end));
}
if ("ALL".equals(type) || "DRILL".equals(type)) {
sheets.add(drillSheet(start, end));
}
if ("ALL".equals(type) || "ACCIDENT".equals(type)) {
sheets.add(accidentSheet(start, end));
}
String filename = switch (type) {
case "CERT" -> "特种作业证台账.xlsx";
case "INSPECTION" -> "安全巡检台账.xlsx";
case "HAZARD" -> "隐患台账.xlsx";
case "PERMIT" -> "高危作业票台账.xlsx";
case "MATERIAL" -> "应急物资台账.xlsx";
case "TRAINING" -> "安全培训台账.xlsx";
case "DRILL" -> "消防演练台账.xlsx";
case "ACCIDENT" -> "事故登记台账.xlsx";
default -> "安全生产标准化台账.xlsx";
};
write(response, filename, sheets);
}
// ==================== 各 Sheet ====================
private Sheet certSheet() {
List headers = List.of("持证人", "工号", "部门", "证件类型", "证件编号", "发证机关",
"发证日期", "有效期至", "下次复审", "状态", "预警天数", "备注");
var pageReqVO = new cn.iocoder.yudao.module.mes.controller.admin.safety.vo.cert.SafetyCertificatePageReqVO();
pageReqVO.setPageNo(1);
pageReqVO.setPageSize(50_000);
var certs = certificateService.getCertificatePage(pageReqVO).getList();
// 导出时用员工档案的最新姓名/工号/部门
certificateService.fillEmployeeInfo(certs);
List> rows = certs.stream()
.map(c -> Arrays.