Browse Source

feature 催缴单功能优化

xiahan 4 months ago
parent
commit
21f9e2912d

+ 36 - 15
zhsw-common/src/main/java/com/rongwei/zhsw/system/service/impl/SwCollectionNoticeServiceImpl.java

@@ -2,7 +2,10 @@ package com.rongwei.zhsw.system.service.impl;
 
 import com.aspose.words.ImportFormatMode;
 import com.aspose.words.SaveFormat;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.rongwe.zhsw.system.domain.SwBusinesshallDo;
 import com.rongwe.zhsw.system.domain.SwEnterpriseConfigInfoDo;
+import com.rongwei.rwcommon.base.BaseDo;
 import com.rongwei.rwcommon.base.exception.CustomException;
 import com.rongwei.rwcommon.utils.StringUtils;
 import com.rongwei.rwcommoncomponent.file.service.SysFileItemService;
@@ -26,6 +29,7 @@ import javax.servlet.http.HttpServletResponse;
 import java.io.*;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.stream.Collectors;
 
 @Service
 public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService {
@@ -34,6 +38,8 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
     private SysFileItemService sysFileItemService;
     @Autowired
     private SwEnterpriseConfigInfoService swEnterpriseConfigInfoService;
+    @Autowired
+    private SwBusinesshallServiceImpl swBusinesshallService;
 
     @Override
     public void printCollectionBill(List<Map<String, String>> userList, HttpServletResponse response) {
@@ -315,7 +321,11 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
     @Override
     public void generatePdf(List<Map<String, String>> dataList, HttpServletResponse response) {
 
-        SwEnterpriseConfigInfoDo swEnterpriseConfigInfoDo = swEnterpriseConfigInfoService.list().get(0);
+        SwEnterpriseConfigInfoDo swEnterpriseConfigInfoDo = swEnterpriseConfigInfoService.getOne(new LambdaQueryWrapper<SwEnterpriseConfigInfoDo>()
+                .eq(BaseDo::getDeleted,"0"));
+        if(swEnterpriseConfigInfoDo==null){
+            throw new CustomException("无法获取企业配置信息");
+        }
         String tempName = swEnterpriseConfigInfoDo.getTempName();
         if (StringUtils.isBlank(tempName)) {
             log.error("暂未配置催缴通知单模板");
@@ -323,9 +333,12 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
         if (dataList.isEmpty()) {
             throw new CustomException("数据异常");
         }
+        List<SwBusinesshallDo> list = swBusinesshallService.list(new LambdaQueryWrapper<SwBusinesshallDo>().eq(BaseDo::getDeleted, "0")
+                .orderByAsc(SwBusinesshallDo::getOrdernumber));
+        String addresStr = list.stream().map(data -> data.getAddress()).collect(Collectors.joining("\n"));
 
         ClassPathResource classPathResource = new ClassPathResource("temp/" + tempName);
-        try {
+        try(InputStream templateStream = classPathResource.getInputStream()) {
             Date now = new Date();
             Calendar instance = Calendar.getInstance();
             instance.setTime(now);
@@ -333,27 +346,35 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
             SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
             String dateStr = formatter.format(instance.getTime());
             String nowStr = formatter.format(now);
-            com.aspose.words.Document temp = new com.aspose.words.Document(classPathResource.getInputStream());
-            List<com.aspose.words.Document> docList = new ArrayList<>();
+            // 模板
+            com.aspose.words.Document temp = new com.aspose.words.Document(templateStream);
+            // 合并的word
+            com.aspose.words.Document mergedDoc = null;
+
             for (Map<String, String> dataMapp : dataList) {
-                com.aspose.words.Document doc = temp.deepClone(); // 深拷贝模板
-                doc.getMailMerge().execute(new String[]{"USERNAME", "USERNUMBER", "YEAR", "QYMC", "RQ"},
+                com.aspose.words.Document currentDoc = temp.deepClone(); // 深拷贝模板
+                currentDoc.getMailMerge().execute(new String[]{"USERNAME", "USERNUMBER", "YEAR", "QYMC", "RQ","sfdt"},
                         new Object[]{dataMapp.getOrDefault("USERNAME", ""),
                                 dataMapp.getOrDefault("USERNUMBER", ""), dateStr,
-                                swEnterpriseConfigInfoDo.getEnterprisename(), nowStr});
-                docList.add(doc);
+                                swEnterpriseConfigInfoDo.getEnterprisename(), nowStr,addresStr});
+                if (mergedDoc == null) {
+                    mergedDoc = currentDoc;
+                } else {
+                    mergedDoc.appendDocument(currentDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
+                    currentDoc = null; // 加速内存回收
+                }
             }
             // 4. 合并为 PDF
-            com.aspose.words.Document finalDoc = docList.get(0).deepClone();
-            docList = docList.subList(0, 1);
-            for (com.aspose.words.Document doc : docList) {
-                finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
-            }
+            // com.aspose.words.Document finalDoc = docList.get(0).deepClone();
+            // docList = docList.subList(0, 1);
+            // for (com.aspose.words.Document doc : docList) {
+            //     finalDoc.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
+            // }
 
             response.setContentType("application/octet-stream;charset=ISO8859-1");
-            response.setHeader("Content-Disposition", "inline; filename=催收单.pdf");
+            response.setHeader("Content-Disposition", "inline; filename=");
             // 保存合并后的 PDF
-            finalDoc.save(response.getOutputStream(), SaveFormat.PDF);
+            mergedDoc.save(response.getOutputStream(), SaveFormat.PDF);
         } catch (Exception e) {
             e.printStackTrace();
             log.error("催缴单生成失败");

BIN
zhsw-server/src/main/resources/temp/徐州大吴水务催缴单.docx