|
@@ -0,0 +1,602 @@
|
|
|
+package com.rongwei.sfcommon.sys.service.impl;
|
|
|
+
|
|
|
+import com.aspose.cells.DocumentPropertyCollection;
|
|
|
+import com.aspose.words.Document;
|
|
|
+import com.aspose.words.DocumentBuilder;
|
|
|
+import com.aspose.words.MailMergeCleanupOptions;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.rongwe.scentity.domian.*;
|
|
|
+import com.rongwei.commonservice.service.dao.FileItemDao;
|
|
|
+import com.rongwei.rwadmincommon.system.dao.SysDictDao;
|
|
|
+import com.rongwei.rwadmincommon.system.domain.SysDictDo;
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
+import com.rongwei.rwcommon.utils.StringUtils;
|
|
|
+import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
|
|
|
+import com.rongwei.sfcommon.sys.service.*;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.io.ClassPathResource;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import com.rongwei.rwcommon.base.BaseDo;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class RiskJobServiceImpl implements RiskJobService {
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(RiskJobServiceImpl.class.getName());
|
|
|
+ public static final String DEFAULT_FONT_NAME = "仿宋";
|
|
|
+
|
|
|
+ public static final Map<String, String> CHECK_BOX= new HashMap<String, String>() {{
|
|
|
+ put("DHFS", "dhfs");
|
|
|
+
|
|
|
+ }};
|
|
|
+
|
|
|
+
|
|
|
+ public static final List<String> IMG_SUFFIX = new ArrayList<String>() {{
|
|
|
+ add("png");
|
|
|
+ add("jpg");
|
|
|
+ add("jpeg");
|
|
|
+ add("bmp");
|
|
|
+ add("gif");
|
|
|
+ add("svg");
|
|
|
+ add("webp");
|
|
|
+ }};
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AspJobConfigFxbsService aspJobConfigFxbsService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AspDhzyService dhzyService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AspCombustibleGasDetectionService aspCombustibleGasDetectionService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AspJobSjzyService aspJobSjzyService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysDictDao sysDictDao;
|
|
|
+ @Autowired
|
|
|
+ private FileItemDao fileItemDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ AspInspectionInformationTableService aspInspectionInformationTableService;
|
|
|
+ @Autowired
|
|
|
+ AspSpecialPersonalInformationService aspSpecialPersonalInformationService;
|
|
|
+/*
|
|
|
+
|
|
|
+ private Map<String, IService<?>> jobTypeServiceMap = new HashMap<>();
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ jobTypeServiceMap.put("动火作业", dhzyService);
|
|
|
+// jobTypeServiceMap.put("高处作业", gczyService);
|
|
|
+// jobTypeServiceMap.put("受限空间", sxkjzyService);
|
|
|
+// jobTypeServiceMap.put("吊装作业", dzzyService);
|
|
|
+// jobTypeServiceMap.put("临时用电", lsydzyService);
|
|
|
+// jobTypeServiceMap.put("断路作业", dlzyService);
|
|
|
+// jobTypeServiceMap.put("动土作业", dtzyService);
|
|
|
+// jobTypeServiceMap.put("盲板作业", mbzyService);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void riskJobExport2(String id, String jobtype, HttpServletResponse response) {
|
|
|
+ IService<?> service = jobTypeServiceMap.get(jobtype);
|
|
|
+ if (service == null) {
|
|
|
+ throw new RuntimeException("不支持的作业类型: " + jobtype);
|
|
|
+ }
|
|
|
+
|
|
|
+ Object jobDo = service.getById(id);
|
|
|
+ if (jobDo == null) {
|
|
|
+ throw new RuntimeException("未查询到作业数据");
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, String> wordMap = extractWordMap(jobDo);
|
|
|
+
|
|
|
+ List<Map<String, String>> fxbsList = queryFxbsList(jobDo);
|
|
|
+ List<Map<String, String>> qtjcList = queryQtjcList(jobDo);
|
|
|
+
|
|
|
+ // 调用你原有的 response 导出方法
|
|
|
+ response(wordMap, response, qtjcList);
|
|
|
+ }
|
|
|
+ private Map<String, String> extractWordMap(Object jobDo) {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ if (jobDo instanceof DhzyDo) {
|
|
|
+ DhzyDo dhzy = (DhzyDo) jobDo;
|
|
|
+ map.put("SQDW", defaultEmptyStr(dhzy.getSqdw()));
|
|
|
+ map.put("SQR", defaultEmptyStr(dhzy.getSqr()));
|
|
|
+ map.put("ZYNR", defaultEmptyStr(dhzy.getZynr()));
|
|
|
+ map.put("ZYZBH", defaultEmptyStr(dhzy.getZyzbh()));
|
|
|
+ map.put("DHZYJB", defaultEmptyStr(dhzy.getDhzyjb()));
|
|
|
+ map.put("DHFS", defaultEmptyStr(dhzy.getDhfs()));
|
|
|
+ // 动火特有字段...
|
|
|
+ } else if (jobDo instanceof GczyDo) {
|
|
|
+ GczyDo gczy = (GczyDo) jobDo;
|
|
|
+ map.put("SQDW", defaultEmptyStr(gczy.getSqdw()));
|
|
|
+ map.put("SQR", defaultEmptyStr(gczy.getSqr()));
|
|
|
+ map.put("ZYNR", defaultEmptyStr(gczy.getZynr()));
|
|
|
+ map.put("ZYZBH", defaultEmptyStr(gczy.getZyzbh()));
|
|
|
+ map.put("GCJB", defaultEmptyStr(gczy.getGcjb()));
|
|
|
+ map.put("GCDW", defaultEmptyStr(gczy.getGcdw()));
|
|
|
+ // 高处特有字段...
|
|
|
+ }
|
|
|
+ // 受限空间、吊装作业... 依次补充
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+//风险辨识
|
|
|
+ private List<Map<String, String>> queryFxbsList(Object jobDo) {
|
|
|
+ String tenantId = "";
|
|
|
+ String jobType = "";
|
|
|
+
|
|
|
+ // 根据 jobDo 类型判断并获取 tenantId 和 jobType
|
|
|
+ if (jobDo instanceof DhzyDo ) {
|
|
|
+ DhzyDo dhzy = (DhzyDo) jobDo;
|
|
|
+ tenantId = dhzy.getTenantid();
|
|
|
+ jobType = dhzy.getJobtype();
|
|
|
+ } else if (jobDo instanceof GczyDo gczy) {
|
|
|
+
|
|
|
+ tenantId = gczy.getTenantid();
|
|
|
+ jobType = gczy.getJobtype();
|
|
|
+ }
|
|
|
+ // 可以继续添加更多 else if 来支持其他类型,比如:受限空间、吊装作业等
|
|
|
+
|
|
|
+ // 查询风险辨识数据
|
|
|
+ QueryWrapper<AspJobConfigFxbsDo> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("tenantid", tenantId);
|
|
|
+ queryWrapper.eq("jobtype", jobType);
|
|
|
+ queryWrapper.eq("deleted", '0');
|
|
|
+
|
|
|
+ // 获取风险辨识数据列表
|
|
|
+ List<AspJobConfigFxbsDo> fxbsList = aspJobConfigFxbsService.list(queryWrapper);
|
|
|
+
|
|
|
+ List<Map<String, String>> fxbs = new ArrayList<>();
|
|
|
+ for (AspJobConfigFxbsDo item : fxbsList) {
|
|
|
+ Map<String, String> subItem = new HashMap<>();
|
|
|
+ subItem.put("FXBSX", defaultEmptyStr(item.getFxbsx()));
|
|
|
+ fxbs.add(subItem);
|
|
|
+ }
|
|
|
+ return fxbs;
|
|
|
+ }
|
|
|
+
|
|
|
+ //气体检测
|
|
|
+ private List<Map<String, String>> queryQtjcList(Object jobDo) {
|
|
|
+ String tenantId = "";
|
|
|
+ String jobType = "";
|
|
|
+
|
|
|
+ // 根据 jobDo 类型判断并获取 tenantId 和 jobType
|
|
|
+ if (jobDo instanceof DhzyDo ) {
|
|
|
+ DhzyDo dhzy = (DhzyDo) jobDo;
|
|
|
+ tenantId = dhzy.getTenantid();
|
|
|
+ jobType = dhzy.getJobtype();
|
|
|
+ } else if (jobDo instanceof GczyDo ) {
|
|
|
+ tenantId = gczy.getTenantid();
|
|
|
+ jobType = gczy.getJobtype();
|
|
|
+ }
|
|
|
+ // 可以继续添加更多 else if 来支持其他类型
|
|
|
+
|
|
|
+ // 查询气体检测数据
|
|
|
+ QueryWrapper<AspCombustibleGasDetectionDo> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("tenantid", tenantId);
|
|
|
+ queryWrapper.eq("jobtype", jobType);
|
|
|
+ queryWrapper.eq("deleted", '0');
|
|
|
+
|
|
|
+ // 获取气体检测数据列表
|
|
|
+ List<AspCombustibleGasDetectionDo> qtjcList = aspCombustibleGasDetectionService.list(queryWrapper);
|
|
|
+
|
|
|
+ List<Map<String, String>> qtjc = new ArrayList<>();
|
|
|
+ for (AspCombustibleGasDetectionDo item : qtjcList) {
|
|
|
+ Map<String, String> subItem = new HashMap<>();
|
|
|
+ subItem.put("COMBUSTIBLEGASLETCONTENT", defaultEmptyStr(item.getCombustiblegasletcontent()));
|
|
|
+ subItem.put("OXYGENCONTENT", defaultEmptyStr(item.getOxygencontent()));
|
|
|
+ subItem.put("TOXICGAS", defaultEmptyStr(item.getToxicgas()));
|
|
|
+ subItem.put("SAMPLINGTIME", defaultEmptyStr(item.getSamplingtime()));
|
|
|
+ subItem.put("LELSAMPLINGSITE", defaultEmptyStr(item.getLelsamplingsite()));
|
|
|
+ subItem.put("ANALYST", defaultEmptyStr(item.getAnalyst()));
|
|
|
+ qtjc.add(subItem);
|
|
|
+ }
|
|
|
+ return qtjc;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// private String defaultEmptyStr(Object obj) {
|
|
|
+// return obj == null ? "" : obj.toString();
|
|
|
+// }
|
|
|
+
|
|
|
+*/
|
|
|
+
|
|
|
+ public void riskJobExport2(String id, String jobtype, HttpServletResponse response){
|
|
|
+ Map<String, String> wordMap = new HashMap<>();
|
|
|
+
|
|
|
+ AspDhzyDo dhzyDo = dhzyService.getById(id);
|
|
|
+
|
|
|
+ List<SysDictDo> sysDictDoList = sysDictDao.selectList(new LambdaQueryWrapper<SysDictDo>()
|
|
|
+ .eq(BaseDo::getDeleted, "0")
|
|
|
+ .in(SysDictDo::getDicttype, "dhfs","dhzyjb"));
|
|
|
+ SysDictDo sysDictDo = null;
|
|
|
+
|
|
|
+ //动火方式
|
|
|
+ String dhfs = dhzyDo.getDhfs();
|
|
|
+ String[] dhfsValues = dhfs.split(",");
|
|
|
+ Map<String, String> dictValueNameMap = sysDictDoList.stream()
|
|
|
+ .filter(dict -> "dhfs".equals(dict.getDicttype()))
|
|
|
+ .collect(Collectors.toMap(SysDictDo::getValue, SysDictDo::getName));
|
|
|
+ List<String> dhfsNames = Arrays.stream(dhfsValues)
|
|
|
+ .map(String::trim)
|
|
|
+ .map(dictValueNameMap::get)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ String DHFS = String.join(",", dhfsNames);
|
|
|
+
|
|
|
+ //动火作业级别
|
|
|
+ String dhzyjb = dhzyDo.getDhzyjb();
|
|
|
+ sysDictDo = sysDictDoList.stream()
|
|
|
+ .filter(dict -> "dhzyjb".equals(dict.getDicttype()) &&
|
|
|
+ dict.getValue().equals(dhzyjb))
|
|
|
+ .findFirst()
|
|
|
+ .orElse(null);
|
|
|
+ String DHZYJB = sysDictDo == null ? "" : sysDictDo.getName();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ QueryWrapper<AspJobConfigFxbsDo> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.eq("tenantid", dhzyDo.getTenantid());
|
|
|
+ queryWrapper.eq("jobtype", dhzyDo.getJobtype());
|
|
|
+ queryWrapper.eq("deleted", '0');
|
|
|
+ List<AspJobConfigFxbsDo> fxbsList = aspJobConfigFxbsService.list(queryWrapper);
|
|
|
+ // 创建一个与 fxbsList 大小相同的数组
|
|
|
+ String[] fxbs = new String[fxbsList.size()];
|
|
|
+ // 将 fxbsList 中的元素复制到数组中
|
|
|
+ for (int i = 0; i < fxbsList.size(); i++) {
|
|
|
+ fxbs[i] = fxbsList.get(i).getFxbsx();
|
|
|
+ }
|
|
|
+// List<Map<String, String>> fxbs = new ArrayList<>();
|
|
|
+// for (AspJobConfigFxbsDo item : fxbsList) {
|
|
|
+// Map<String, String> subItem = new HashMap<>();
|
|
|
+// // 根据实际情况添加属性到 Map 中
|
|
|
+// subItem.put("FXBSX", defaultEmptyStr(item.getFxbsx()));
|
|
|
+// fxbs.add(subItem);
|
|
|
+// }
|
|
|
+
|
|
|
+//气体检查
|
|
|
+ QueryWrapper<AspCombustibleGasDetectionDo> queryWrapper1 = new QueryWrapper<>();
|
|
|
+ queryWrapper1.eq("jobid", dhzyDo.getId());
|
|
|
+ queryWrapper1.eq("deleted", '0');
|
|
|
+ List<AspCombustibleGasDetectionDo> qtjcList = aspCombustibleGasDetectionService.list(queryWrapper1);
|
|
|
+
|
|
|
+ List<Map<String, String>> subList1 = new ArrayList<>();
|
|
|
+ for (AspCombustibleGasDetectionDo item : qtjcList) {
|
|
|
+ Map<String, String> subItem = new HashMap<>();
|
|
|
+ // 根据实际情况添加属性到 Map 中
|
|
|
+ subItem.put("COMBUSTIBLEGASLETCONTENT", defaultEmptyStr(item.getCombustiblegasletcontent()));
|
|
|
+ subItem.put("OXYGENCONTENT",defaultEmptyStr(item.getOxygencontent()));
|
|
|
+ subItem.put("TOXICGAS", defaultEmptyStr(item.getToxicgas()));
|
|
|
+ subItem.put("SAMPLINGTIME", defaultEmptyStr(item.getSamplingtime()));
|
|
|
+ subItem.put("LELSAMPLINGSITE", defaultEmptyStr(item.getLelsamplingsite()));
|
|
|
+ subItem.put("ANALYST", defaultEmptyStr(item.getAnalyst()));
|
|
|
+ subList1.add(subItem);
|
|
|
+ }
|
|
|
+
|
|
|
+ //关联作业查询
|
|
|
+ QueryWrapper<AspJobSjzyDo> queryWrapper2 = new QueryWrapper<>();
|
|
|
+ queryWrapper2.eq("JOBID",dhzyDo.getId());
|
|
|
+ queryWrapper2.eq("DELETED","0");
|
|
|
+ List<AspJobSjzyDo> resultList = aspJobSjzyService.list(queryWrapper2);
|
|
|
+ // JOBTYPE SJZYZBH
|
|
|
+ if (!resultList.isEmpty()) {
|
|
|
+ String jobTypeString = resultList.stream()
|
|
|
+ .map(AspJobSjzyDo::getJobtype)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.joining(","));
|
|
|
+ String sjzyzbhString = resultList.stream()
|
|
|
+ .map(AspJobSjzyDo::getSjzyzbh)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.joining(","));
|
|
|
+ wordMap.put("SJQTZYLX",defaultEmptyStr(jobTypeString) );
|
|
|
+ wordMap.put("SJQTTSZYZBH", defaultEmptyStr(sjzyzbhString));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+//安全措施
|
|
|
+ QueryWrapper<AspInspectionInformationTableDo> queryWrapper3 = new QueryWrapper<>();
|
|
|
+
|
|
|
+ queryWrapper3.eq("jobid", dhzyDo.getId());
|
|
|
+ queryWrapper3.eq("deleted", '0');
|
|
|
+ List<AspInspectionInformationTableDo> aqcsList = aspInspectionInformationTableService.list(queryWrapper3);
|
|
|
+ List<Map<String, String>> subList2 = new ArrayList<>();
|
|
|
+ for (AspInspectionInformationTableDo item : aqcsList) {
|
|
|
+ Map<String, String> subItem2 = new HashMap<>();
|
|
|
+ // 根据实际情况添加属性到 Map 中
|
|
|
+ subItem2.put("MAINSAFETYMEASURES", defaultEmptyStr(item.getMainsafetymeasures()));
|
|
|
+ subItem2.put("WHETHERITINVOLVES",defaultEmptyStr(item.getWhetheritinvolves()));
|
|
|
+ subItem2.put("CHECKUSER", defaultEmptyStr(item.getCheckuser()));
|
|
|
+ subList2.add(subItem2);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+// 动火人以及证书编号
|
|
|
+// aspSpecialPersonalInformationService
|
|
|
+ QueryWrapper<AspSpecialPersonalInformationDo> queryWrapper4 = new QueryWrapper<>();
|
|
|
+ queryWrapper3.eq("jobid", dhzyDo.getId());
|
|
|
+ queryWrapper3.eq("deleted", '0');
|
|
|
+ List<AspSpecialPersonalInformationDo> dhrList = aspSpecialPersonalInformationService.list(queryWrapper4);
|
|
|
+
|
|
|
+ String dhrxx = dhrList.stream()
|
|
|
+ .map(item -> item.getName() + "," + item.getCertificateno())
|
|
|
+ .collect(Collectors.joining(" "));
|
|
|
+ if (!resultList.isEmpty()) {
|
|
|
+ wordMap.put("DHRJZSBH",defaultEmptyStr(dhrxx) );
|
|
|
+ }
|
|
|
+// CERTIFICATENO name 小王,123 小明456
|
|
|
+// List<Map<String, String>> subList2 = new ArrayList<>();
|
|
|
+// for (AspInspectionInformationTableDo item : aqcsList) {
|
|
|
+// Map<String, String> subItem2 = new HashMap<>();
|
|
|
+// // 根据实际情况添加属性到 Map 中
|
|
|
+// subItem2.put("MAINSAFETYMEASURES", defaultEmptyStr(item.getMainsafetymeasures()));
|
|
|
+// subItem2.put("WHETHERITINVOLVES",defaultEmptyStr(item.getWhetheritinvolves()));
|
|
|
+// subItem2.put("CHECKUSER", defaultEmptyStr(item.getCheckuser()));
|
|
|
+// subList2.add(subItem2);
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+//
|
|
|
+// dhzyDo.getFxbs();//风险辨识
|
|
|
+
|
|
|
+
|
|
|
+ wordMap.put("SQDW", defaultEmptyStr( dhzyDo.getSqdw()));
|
|
|
+ wordMap.put("SQR", defaultEmptyStr( dhzyDo.getSqr()));
|
|
|
+ wordMap.put("ZYZBH", defaultEmptyStr( dhzyDo.getZyzbh()));
|
|
|
+ wordMap.put("ZYNR", defaultEmptyStr( dhzyDo.getZynr()));
|
|
|
+
|
|
|
+ wordMap.put("DHZYJB", defaultEmptyStr( DHZYJB));
|
|
|
+// wordMap.put("DHFS",dhfs);
|
|
|
+ wordMap.put("DHFS",DHFS);
|
|
|
+
|
|
|
+ wordMap.put("DHDD", defaultEmptyStr( dhzyDo.getDhdd()));
|
|
|
+ wordMap.put("ZYWZ", defaultEmptyStr( dhzyDo.getZywz()));
|
|
|
+ wordMap.put("ZYDWMC", defaultEmptyStr( dhzyDo.getZydwmc()));
|
|
|
+ wordMap.put("ZYFZR", defaultEmptyStr( dhzyDo.getZyfzr()));
|
|
|
+ wordMap.put("ZYKSSJ", defaultEmptyStr( dhzyDo.getZykssj()));
|
|
|
+ wordMap.put("ZYJSSJ", defaultEmptyStr( dhzyDo.getZyjssj()));
|
|
|
+
|
|
|
+ wordMap.put("JHRQM", defaultEmptyStr( dhzyDo.getJhrqm()));
|
|
|
+ wordMap.put("AQJDRQM", defaultEmptyStr( dhzyDo.getAqjdrqm()));
|
|
|
+ wordMap.put("JSJDRQM", defaultEmptyStr( dhzyDo.getJsjdrqm()));
|
|
|
+ wordMap.put("SDBMFZRQZ", defaultEmptyStr( dhzyDo.getSdbmfzrqz()));
|
|
|
+
|
|
|
+ String fxbsx = defaultEmptyStr( dhzyDo.getFxbs());
|
|
|
+
|
|
|
+ response(wordMap, response ,subList1,subList2,fxbs ,fxbsx , sysDictDoList );
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public String defaultEmptyStr(Object str) {
|
|
|
+ if (str == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ if (str instanceof Date) {
|
|
|
+// return new SimpleDateFormat("yyyy-MM-dd").format(str);
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) str);
|
|
|
+ }
|
|
|
+ return str.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void checkBoxDispose(Map<String, String> wordMap, Document document, String[] fxbs, String fxbsx, List<SysDictDo> sysDictDoList) {
|
|
|
+ try {
|
|
|
+ DocumentBuilder builder = new DocumentBuilder(document);
|
|
|
+ if (fxbs.length > 0) {
|
|
|
+ String bsx = "FXBS";
|
|
|
+ builder.moveToMergeField(bsx);
|
|
|
+ for (String info : fxbs) {
|
|
|
+ builder.getFont().setName(DEFAULT_FONT_NAME);
|
|
|
+ builder.write(info);
|
|
|
+ builder.getFont().setName("Wingdings");
|
|
|
+ if (StringUtils.isBlank(fxbsx)) {
|
|
|
+ // 如果 wordMap 中 FXBS 键的值为空,复选框不勾选
|
|
|
+ builder.write("\u00A8");
|
|
|
+ } else {
|
|
|
+ String[] values = fxbsx.split(",");
|
|
|
+ boolean isChecked = false;
|
|
|
+ for (String value : values) {
|
|
|
+ if (value.trim().equals(info)) {
|
|
|
+ // 如果 FXBS 键的值中包含当前选项,复选框勾选
|
|
|
+ isChecked = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ builder.write(isChecked ? "\u00FE" : "\u00A8");
|
|
|
+ }
|
|
|
+ builder.write(" ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Map.Entry<String, String> entry : CHECK_BOX.entrySet()) {
|
|
|
+ String k = entry.getKey();
|
|
|
+ String v = entry.getValue();
|
|
|
+ builder.moveToMergeField(k);
|
|
|
+ List<SysDictDo> dicts = sysDictDoList.stream().filter(dict -> v.equals(dict.getDicttype()) && !dict.getPid().equals("-1"))
|
|
|
+ .sorted(Comparator.comparing(SysDictDo::getSort).reversed())
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ for (SysDictDo info : dicts) {
|
|
|
+ builder.getFont().setName(DEFAULT_FONT_NAME);
|
|
|
+ builder.write(info.getName());
|
|
|
+ builder.getFont().setName("Wingdings");
|
|
|
+ builder.write(StringUtils.isBlank(wordMap.get(k)) ? "\u00A8" : wordMap.get(k).contains(info.getName()) ? "\u00FE" : "\u00A8");
|
|
|
+ builder.write(" ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+//
|
|
|
+// public void response(Map<String, String> wordMap, HttpServletResponse response) {
|
|
|
+// try (InputStream inputStream = new ClassPathResource("导出PDF(动火作业待修改).docx").getInputStream();
|
|
|
+// OutputStream outputStream = response.getOutputStream()) {
|
|
|
+// // 加载 Word 文档模板
|
|
|
+// Document doc = new Document(inputStream);
|
|
|
+// // 如果 wordMap 不为空,执行邮件合并操作
|
|
|
+// if (wordMap != null) {
|
|
|
+// doc.getMailMerge().execute(wordMap.keySet().toArray(new String[0]), wordMap.values().toArray(new String[0]));
|
|
|
+// }
|
|
|
+// // 设置清理选项,移除未使用的字段和空表格行
|
|
|
+// doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS);
|
|
|
+// doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS);
|
|
|
+// // 删除文档中的字段
|
|
|
+// doc.getMailMerge().deleteFields();
|
|
|
+//
|
|
|
+// // 设置响应头,表明返回的是 PDF 文件
|
|
|
+// response.setContentType("application/pdf");
|
|
|
+// // 设置响应头,指定文件名和下载方式
|
|
|
+// response.setHeader("Content-Disposition", "attachment;filename=ComplaintForm.pdf");
|
|
|
+//
|
|
|
+// // 将文档保存为 PDF 格式并输出到响应流
|
|
|
+// doc.save(outputStream, SaveFormat.PDF);
|
|
|
+// outputStream.flush();
|
|
|
+// } catch (IOException e) {
|
|
|
+// // 抛出自定义异常
|
|
|
+// throw new CustomException("文件导出异常");
|
|
|
+// } catch (Exception e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+ public void response(Map<String, String> wordMap, HttpServletResponse response , List<Map<String, String>> subList1,List<Map<String, String>> subList2, String[] fxbs, String fxbsx, List<SysDictDo> sysDictDoList) {
|
|
|
+ try (InputStream inputStream = new ClassPathResource("导出PDF(动火作业待修改).docx").getInputStream();
|
|
|
+ OutputStream outputStream = response.getOutputStream()) {
|
|
|
+ if (inputStream == null) {
|
|
|
+ log.error("无法找到指定的 Word 模板文件");
|
|
|
+ throw new FileNotFoundException("无法找到 Word 模板文件");
|
|
|
+ }
|
|
|
+ Document doc = new Document(inputStream);
|
|
|
+ log.info("文档加载成功");
|
|
|
+ if (wordMap != null) {
|
|
|
+ log.info("执行邮件合并");
|
|
|
+ checkBoxDispose(wordMap, doc, fxbs ,fxbsx,sysDictDoList );
|
|
|
+ // 删除图片字段,防止插入为 ID
|
|
|
+ List<String> imageBookmarks = Arrays.asList("JHRQM", "AQJDRQM", "JSJDRQM","SDBMFZRQZ");
|
|
|
+ Map<String, String> textMap = new HashMap<>(wordMap);
|
|
|
+ imageBookmarks.forEach(textMap::remove);
|
|
|
+// doc.getMailMerge().execute(wordMap.keySet().toArray(new String[0]), wordMap.values().toArray(new String[0]));
|
|
|
+ doc.getMailMerge().execute(textMap.keySet().toArray(new String[0]), textMap.values().toArray(new String[0]));
|
|
|
+ // 插入图片签名
|
|
|
+ insertImageToBookmark(doc, "SDBMFZRQZ", wordMap.get("SDBMFZRQZ"));
|
|
|
+ insertImageToBookmark(doc, "JHRQM", wordMap.get("JHRQM"));
|
|
|
+ insertImageToBookmark(doc, "AQJDRQM", wordMap.get("AQJDRQM"));
|
|
|
+ insertImageToBookmark(doc, "JSJDRQM", wordMap.get("JSJDRQM"));
|
|
|
+ if (!subList1.isEmpty()) {
|
|
|
+ log.info("执行 List1 区域合并");
|
|
|
+ CustomMailMergeDataSource dataSource = new CustomMailMergeDataSource(subList1,"list1");
|
|
|
+ // 执行嵌套邮件合并
|
|
|
+ doc.getMailMerge().executeWithRegions(dataSource);
|
|
|
+ }
|
|
|
+ if (!subList2.isEmpty()) {
|
|
|
+ log.info("执行 List2 区域合并");
|
|
|
+ CustomMailMergeDataSource dataSource2 = new CustomMailMergeDataSource(subList2,"list2");
|
|
|
+ // 执行嵌套邮件合并
|
|
|
+ doc.getMailMerge().executeWithRegions(dataSource2);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ doc.getMailMerge().setCleanupOptions(MailMergeCleanupOptions.REMOVE_UNUSED_FIELDS | MailMergeCleanupOptions.REMOVE_EMPTY_TABLE_ROWS);
|
|
|
+ doc.getMailMerge().deleteFields();
|
|
|
+
|
|
|
+ // 设置返回 PDF 的响应头
|
|
|
+ response.setContentType("application/pdf");
|
|
|
+ String fileName = URLEncoder.encode("导出结果.pdf", "UTF-8").replaceAll("\\+", "%20");
|
|
|
+ response.setHeader("Content-Disposition", "inline; filename*=UTF-8''" + fileName);
|
|
|
+
|
|
|
+ log.info("开始输出 PDF 至前端");
|
|
|
+
|
|
|
+ // 输出为 PDF 流
|
|
|
+ doc.save(response.getOutputStream(), com.aspose.words.SaveFormat.PDF);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+/* log.info("开始保存为 PDF");
|
|
|
+
|
|
|
+// response.setContentType("application/octet-stream;charset=ISO8859-1");
|
|
|
+// response.setHeader("Content-Disposition", "inline; filename=exported_file.pdf");
|
|
|
+ // 如果需要本地保存
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
|
|
+ String formattedTime = now.format(formatter);
|
|
|
+ String fileName = "dh" + formattedTime + ".docx";
|
|
|
+ String localPath = "D:\\test\\" + fileName;
|
|
|
+ File file = new File(localPath);
|
|
|
+ if (!file.getParentFile().exists()) {
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+// doc.save(localPath,com.aspose.words.SaveFormat.DOCX);
|
|
|
+ try {
|
|
|
+ doc.save(localPath,com.aspose.words.SaveFormat.DOCX);
|
|
|
+// doc.save(response.getOutputStream(), com.aspose.words.SaveFormat.DOCX);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("保存 PDF 时出错", e);
|
|
|
+ throw new RuntimeException("文件导出异常", e);
|
|
|
+ }*/
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("文件导出过程中出现 IO 异常", e);
|
|
|
+ throw new CustomException("文件导出异常");
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("文件导出过程中出现未知异常", e);
|
|
|
+ throw new RuntimeException("文件导出过程中出现未知异常", e);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public void insertImageToBookmark(Document doc, String bookmark, String fileId) {
|
|
|
+ if (StringUtils.isBlank(fileId)) return;
|
|
|
+
|
|
|
+ SysFileItemDo file = fileItemDao.selectById(fileId);
|
|
|
+ if (file == null || !IMG_SUFFIX.contains(file.getFiletype().toLowerCase())) return;
|
|
|
+//
|
|
|
+ try (InputStream inputStream = Files.newInputStream( Paths.get(file.getFullpath()) ) ) {
|
|
|
+ DocumentBuilder builder = new DocumentBuilder(doc);
|
|
|
+ builder.moveToMergeField(bookmark); // 移动到书签
|
|
|
+ builder.insertImage(inputStream, 100, 50); // 自行设置图片大小
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|