From 6d6e9ab8c8de4087a333bba38b5529a26df2ced1 Mon Sep 17 00:00:00 2001 From: aipper Date: Mon, 3 Nov 2025 15:09:53 +0800 Subject: [PATCH] test --- .../fourCheck/service/FourCheckService.java | 179 ++++++++++++++++-- .../service/ReceiveInformationService.java | 7 +- .../webapp/pdfxml/四性检测报表.ureport.xml | 2 +- 3 files changed, 167 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/point/strategy/fourCheck/service/FourCheckService.java b/src/main/java/com/point/strategy/fourCheck/service/FourCheckService.java index 11dd31a..658b74b 100644 --- a/src/main/java/com/point/strategy/fourCheck/service/FourCheckService.java +++ b/src/main/java/com/point/strategy/fourCheck/service/FourCheckService.java @@ -7,6 +7,8 @@ import com.bstek.ureport.Utils; import com.bstek.ureport.export.ExportConfigure; import com.bstek.ureport.export.ExportConfigureImpl; import com.bstek.ureport.export.ExportManager; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.point.strategy.archiveNoSet.bean.ArchiveNoFormat; @@ -80,9 +82,35 @@ public class FourCheckService { @Autowired MetadataStandardMapper metadataStandardMapper; + private ObjectMapper objectMapper = new ObjectMapper(); private static final Logger logger = LoggerFactory.getLogger(FourCheckService.class); + /** + * 对超长字符串进行软换行,避免在PDF中单行过宽/过高造成分页异常。 + * 简单实现:按固定列宽断行,同时保留已有的换行符。 + */ + private static List softWrap(String text, int maxCols) { + List lines = new ArrayList<>(); + if (text == null) { + return lines; + } + String[] rawLines = text.replace("\r\n", "\n").replace('\r', '\n').split("\n", -1); + for (String raw : rawLines) { + if (raw.length() <= maxCols) { + lines.add(raw); + } else { + int start = 0; + while (start < raw.length()) { + int end = Math.min(start + maxCols, raw.length()); + lines.add(raw.substring(start, end)); + start = end; + } + } + } + return lines; + } + @Transactional public AjaxJson addEntity(FourCheck entity) { AjaxJson result = null; @@ -210,11 +238,55 @@ public class FourCheckService { String pass = "通过"; if ("0".equals((new StringBuilder()).append(map1.get("pass")).append("").toString())) pass = "不通过"; - map1.put("pass", pass); - map1.put("checkLink", checkLink); - map1.put("dataFormat", dataFormat); - map1.put("filePath", fourCheck.getFilePath()); - returnList.add(map1); + + Object msgObj = map1.get("msg"); + // 将可能很长的列表消息拆分为多行/多条记录,避免单行过高导致 iText 无限循环异常 + List msgLines = new ArrayList<>(); + if (msgObj instanceof List) { + for (Object o : (List) msgObj) { + // 对每条内容进行软换行展开 + msgLines.addAll(softWrap(String.valueOf(o), 60)); + } + } else if (msgObj instanceof com.alibaba.fastjson.JSONArray) { + com.alibaba.fastjson.JSONArray arr = (com.alibaba.fastjson.JSONArray) msgObj; + for (Object o : arr) { + msgLines.addAll(softWrap(String.valueOf(o), 60)); + } + } else if (msgObj != null) { + msgLines.addAll(softWrap(String.valueOf(msgObj), 60)); + } + + // 分块,每块最多 N 行,避免某一行高度超过页面 + final int CHUNK = 15; + if (msgLines.isEmpty()) { + Map row = new HashMap<>(); + row.putAll(map1); + row.put("type", typeChn); + row.put("pass", pass); + row.put("checkLink", checkLink); + row.put("dataFormat", dataFormat); + row.put("filePath", fourCheck.getFilePath()); + row.put("createdDate", dateFormat.format(fourCheck.getCreatedDate())); + row.put("userChnName", fourCheck.getCreatedBy()); + row.put("msg", ""); + returnList.add(row); + } else { + for (int i = 0; i < msgLines.size(); i += CHUNK) { + int end = Math.min(i + CHUNK, msgLines.size()); + String chunkText = String.join("\n", msgLines.subList(i, end)); + Map row = new HashMap<>(); + row.putAll(map1); + row.put("type", typeChn); + row.put("pass", pass); + row.put("checkLink", checkLink); + row.put("dataFormat", dataFormat); + row.put("filePath", fourCheck.getFilePath()); + row.put("createdDate", dateFormat.format(fourCheck.getCreatedDate())); + row.put("userChnName", fourCheck.getCreatedBy()); + row.put("msg", chunkText); + returnList.add(row); + } + } } // Collections.sort(returnList, (Comparator>)new Object()); // String pathName = "E:/zzrsda/point-strategy/src/main/webapp/pdffile"; @@ -236,13 +308,18 @@ public class FourCheckService { try { SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String currentName = "fourcheck_" + df.format(new Date()); + // 确保输出目录存在,避免因目录缺失导致 FileNotFoundException + File outDir = new File(pathName); + if (!outDir.exists()) { + outDir.mkdirs(); + } outputStream = new FileOutputStream(new File(pathName + File.separator + currentName + ".pdf")); ExportConfigureImpl exportConfigureImpl = new ExportConfigureImpl("file:四性检测报表.ureport.xml", map, outputStream); ExportManager exportManager = (ExportManager) Utils.getApplicationContext().getBean("ureport.exportManager"); exportManager.exportPdf((ExportConfigure)exportConfigureImpl); return currentName; } catch (Exception e) { - + logger.error("导出四性检测报表PDF失败(findResultById-默认路径)", e); } return null; } @@ -303,11 +380,48 @@ public class FourCheckService { String pass = "通过"; if ("0".equals((new StringBuilder()).append(map1.get("pass")).append("").toString())) pass = "不通过"; - map1.put("pass", pass); - map1.put("checkLink", checkLink); - map1.put("dataFormat", dataFormat); - map1.put("filePath", fourCheck.getFilePath()); - returnList.add(map1); + + Object msgObj = map1.get("msg"); + List msgLines = new ArrayList<>(); + if (msgObj instanceof List) { + for (Object o : (List) msgObj) msgLines.addAll(softWrap(String.valueOf(o), 60)); + } else if (msgObj instanceof com.alibaba.fastjson.JSONArray) { + com.alibaba.fastjson.JSONArray arr = (com.alibaba.fastjson.JSONArray) msgObj; + for (Object o : arr) msgLines.addAll(softWrap(String.valueOf(o), 60)); + } else if (msgObj != null) { + msgLines.addAll(softWrap(String.valueOf(msgObj), 60)); + } + + final int CHUNK = 15; + if (msgLines.isEmpty()) { + Map row = new HashMap<>(); + row.putAll(map1); + row.put("type", typeChn); + row.put("pass", pass); + row.put("checkLink", checkLink); + row.put("dataFormat", dataFormat); + row.put("filePath", fourCheck.getFilePath()); + row.put("createdDate", dateFormat.format(fourCheck.getCreatedDate())); + row.put("userChnName", fourCheck.getCreatedBy()); + row.put("msg", ""); + returnList.add(row); + } else { + for (int i = 0; i < msgLines.size(); i += CHUNK) { + int end = Math.min(i + CHUNK, msgLines.size()); + String chunkText = String.join("\n", msgLines.subList(i, end)); + Map row = new HashMap<>(); + row.putAll(map1); + row.put("type", typeChn); + row.put("pass", pass); + row.put("checkLink", checkLink); + row.put("dataFormat", dataFormat); + row.put("filePath", fourCheck.getFilePath()); + row.put("createdDate", dateFormat.format(fourCheck.getCreatedDate())); + row.put("userChnName", fourCheck.getCreatedBy()); + row.put("msg", chunkText); + returnList.add(row); + } + } } // Collections.sort(returnList, (Comparator>)new Object()); // String pathName = "E:/zzrsda/point-strategy/src/main/webapp/pdffile"; @@ -323,13 +437,18 @@ public class FourCheckService { try { SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS"); String currentName = "fourcheck_" + df.format(new Date()); + // 确保输出目录存在 + File outDir = new File(filePath); + if (!outDir.exists()) { + outDir.mkdirs(); + } outputStream = new FileOutputStream(new File(filePath + File.separator + currentName + ".pdf")); ExportConfigureImpl exportConfigureImpl = new ExportConfigureImpl("file:四性检测报表.ureport.xml", map, outputStream); ExportManager exportManager = (ExportManager) Utils.getApplicationContext().getBean("ureport.exportManager"); exportManager.exportPdf((ExportConfigure)exportConfigureImpl); return currentName; } catch (Exception e) { - + logger.error("导出四性检测报表PDF失败(findResultById-指定路径)", e); } return null; } @@ -575,7 +694,11 @@ public class FourCheckService { map.put("link",checkItem); map.put("describe",testItems); if(CollectionUtils.isNotEmpty(archivesNosTwo)){ - map.put("msg",archivesNosTwo); + try { + map.put("msg",objectMapper.writeValueAsString(archivesNosTwo)); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } }else{ map.put("msg",""); } @@ -588,7 +711,11 @@ public class FourCheckService { map.put("link",checkItem); map.put("describe",testItems); if(CollectionUtils.isNotEmpty(archivesNosThree)){ - map.put("msg",archivesNosThree); + try { + map.put("msg",objectMapper.writeValueAsString(archivesNosThree)); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } }else{ map.put("msg",""); } @@ -600,7 +727,11 @@ public class FourCheckService { map.put("link",checkItem); map.put("describe",testItems); if(CollectionUtils.isNotEmpty(archivesNosFour)){ - map.put("msg",archivesNosFour); + try { + map.put("msg",objectMapper.writeValueAsString(archivesNosFour)); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } }else{ map.put("msg",""); } @@ -612,7 +743,11 @@ public class FourCheckService { map.put("link",checkItem); map.put("describe",testItems); if(CollectionUtils.isNotEmpty(archivesNos)){ - map.put("msg",archivesNos); + try { + map.put("msg",objectMapper.writeValueAsString(archivesNos)); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } }else{ map.put("msg",""); } @@ -626,7 +761,11 @@ public class FourCheckService { map.put("link",checkItem); map.put("describe",testItems); if(CollectionUtils.isNotEmpty(archivesNosOne)){ - map.put("msg",archivesNosOne); + try { + map.put("msg",objectMapper.writeValueAsString(archivesNosOne)); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } }else{ map.put("msg",""); } @@ -638,7 +777,11 @@ public class FourCheckService { map.put("link",checkItem); map.put("describe",testItems); if(!StringUtils.isEmpty(archivesNosFive)){ - map.put("msg",archivesNosFive); + try { + map.put("msg",objectMapper.writeValueAsString(archivesNosFive)); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } }else{ map.put("msg",""); } diff --git a/src/main/java/com/point/strategy/receiveInformation/service/ReceiveInformationService.java b/src/main/java/com/point/strategy/receiveInformation/service/ReceiveInformationService.java index 9ad5753..e02c80a 100644 --- a/src/main/java/com/point/strategy/receiveInformation/service/ReceiveInformationService.java +++ b/src/main/java/com/point/strategy/receiveInformation/service/ReceiveInformationService.java @@ -588,9 +588,12 @@ public class ReceiveInformationService { map.put("fondscode",fondscode); map.put("detection",detection); String fileName = fourCheckService.check(map); - if (fileName.equals("请先设置四性检测条目")){ + // 避免对 null 调用 equals,先判空 + if ("请先设置四性检测条目".equals(fileName)){ json = AjaxJson.returnExceptionInfo(fileName); - }else { + } else if (fileName == null) { + json = AjaxJson.returnExceptionInfo("四性检测报表生成失败"); + } else { // //修改检测结果 // ReceiveInformation information1 = new ReceiveInformation(); // information1.setDetectionresult(2); diff --git a/src/main/webapp/pdfxml/四性检测报表.ureport.xml b/src/main/webapp/pdfxml/四性检测报表.ureport.xml index 3013e1a..c650c14 100644 --- a/src/main/webapp/pdfxml/四性检测报表.ureport.xml +++ b/src/main/webapp/pdfxml/四性检测报表.ureport.xml @@ -144,7 +144,7 @@ - +