浏览代码

bugFix 催缴单导出功能

xiahan 4 月之前
父节点
当前提交
3af194f8c1

+ 6 - 0
zhsw-common/pom.xml

@@ -64,6 +64,12 @@
             <artifactId>ooxml-schemas</artifactId>
             <version>1.4</version>
         </dependency>
+
+        <dependency>
+            <groupId>com.aspose</groupId>
+            <artifactId>aspose-words</artifactId>
+            <version>21.1.0</version>
+        </dependency>
         <!--        <dependency>-->
 <!--            <groupId>org.docx4j</groupId>-->
 <!--            <artifactId>docx4j</artifactId>-->

+ 2 - 0
zhsw-common/src/main/java/com/rongwei/zhsw/system/service/SwCollectionNoticeService.java

@@ -11,4 +11,6 @@ public interface SwCollectionNoticeService {
 
     void printCollectionBill(List<Map<String, String>> data, HttpServletResponse response) throws IOException;
 
+
+    void generatePdf(List<Map<String, String>> data,HttpServletResponse response);
 }

+ 66 - 12
zhsw-common/src/main/java/com/rongwei/zhsw/system/service/impl/SwCollectionNoticeServiceImpl.java

@@ -1,9 +1,14 @@
 package com.rongwei.zhsw.system.service.impl;
 
+import com.aspose.words.ImportFormatMode;
+import com.aspose.words.SaveFormat;
+import com.rongwe.zhsw.system.domain.SwEnterpriseConfigInfoDo;
+import com.rongwei.rwcommon.base.exception.CustomException;
 import com.rongwei.rwcommon.utils.StringUtils;
 import com.rongwei.rwcommoncomponent.file.service.SysFileItemService;
 import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
 import com.rongwei.zhsw.system.service.SwCollectionNoticeService;
+import com.rongwei.zhsw.system.service.SwEnterpriseConfigInfoService;
 import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.Units;
 import org.apache.poi.xwpf.usermodel.*;
@@ -11,25 +16,27 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
 import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.*;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
+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;
-// 必须导入的类(POI 4.1.1)
-import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTPosH;
-import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTPosV;
-import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.STRelFromH;
-import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.STRelFromV;
+
 import javax.servlet.http.HttpServletResponse;
 import java.io.*;
-import java.util.List;
-import java.util.Map;
+import java.text.SimpleDateFormat;
+import java.util.*;
 
 @Service
 public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService {
+    private static final Logger log = LoggerFactory.getLogger(SwCollectionNoticeServiceImpl.class);
     @Autowired
     private SysFileItemService sysFileItemService;
+    @Autowired
+    private SwEnterpriseConfigInfoService swEnterpriseConfigInfoService;
 
     @Override
-    public void printCollectionBill(List<Map<String, String>> userList,HttpServletResponse response) {
+    public void printCollectionBill(List<Map<String, String>> userList, HttpServletResponse response) {
 
 
         // 1. 获取模板文件
@@ -95,7 +102,7 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
                     for (XWPFParagraph srcPara : document.getParagraphs()) {
                         XWPFParagraph newPara = mergedDocument.createParagraph();
 
-                        copyParagraphStyle(srcPara,newPara);
+                        copyParagraphStyle(srcPara, newPara);
 
 
                         for (XWPFRun srcRun : srcPara.getRuns()) {
@@ -119,8 +126,7 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
                                     drawing.setAnchorArray(new CTAnchor[]{anchor});
                                     configureAnchor(anchor, newPara);
                                 }
-                            }
-                            else {
+                            } else {
                                 // 复制普通文本
                                 XWPFRun newRun = newPara.createRun();
                                 String text = srcRun.getText(0);
@@ -134,7 +140,7 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
 
                     }
                     // 分页符
-                    if (i < userList.size() -   1) {
+                    if (i < userList.size() - 1) {
                         mergedDocument.createParagraph().createRun().addBreak(BreakType.PAGE);
                     }
                 }
@@ -186,6 +192,7 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
             }
         }
     }
+
     private void configureAnchor(CTAnchor anchor, XWPFParagraph paragraph) {
         // 尺寸
         CTPositiveSize2D extent = anchor.getExtent();
@@ -305,7 +312,54 @@ public class SwCollectionNoticeServiceImpl implements SwCollectionNoticeService
         }
     }
 
+    @Override
+    public void generatePdf(List<Map<String, String>> dataList, HttpServletResponse response) {
+
+        SwEnterpriseConfigInfoDo swEnterpriseConfigInfoDo = swEnterpriseConfigInfoService.list().get(0);
+        String tempName = swEnterpriseConfigInfoDo.getTempName();
+        if (StringUtils.isBlank(tempName)) {
+            log.error("暂未配置催缴通知单模板");
+        }
+        if (dataList.isEmpty()) {
+            throw new CustomException("数据异常");
+        }
+
+        ClassPathResource classPathResource = new ClassPathResource("temp/" + tempName);
+        try {
+            Date now = new Date();
+            Calendar instance = Calendar.getInstance();
+            instance.setTime(now);
+            instance.add(Calendar.DAY_OF_MONTH, swEnterpriseConfigInfoDo.getNotificationdays());
+            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<>();
+            for (Map<String, String> dataMapp : dataList) {
+                com.aspose.words.Document doc = temp.deepClone(); // 深拷贝模板
+                doc.getMailMerge().execute(new String[]{"USERNAME", "USERNUMBER", "YEAR", "QYMC", "RQ"},
+                        new Object[]{dataMapp.getOrDefault("USERNAME", ""),
+                                dataMapp.getOrDefault("USERNUMBER", ""), dateStr,
+                                swEnterpriseConfigInfoDo.getEnterprisename(), nowStr});
+                docList.add(doc);
+            }
+            // 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);
+            }
 
+            response.setContentType("application/octet-stream;charset=ISO8859-1");
+            response.setHeader("Content-Disposition", "inline; filename=催收单.pdf");
+            // 保存合并后的 PDF
+            finalDoc.save(response.getOutputStream(), SaveFormat.PDF);
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("催缴单生成失败");
+        }
+
+    }
 }
 
 

+ 4 - 0
zhsw-entity/src/main/java/com/rongwe/zhsw/system/domain/SwEnterpriseConfigInfoDo.java

@@ -125,4 +125,8 @@ public class SwEnterpriseConfigInfoDo extends BaseDo implements Serializable {
      * 模板数据配置
      */
     private String templateconfiguration;
+
+    private String tempName;
+
+    private int notificationdays;
 }

+ 4 - 8
zhsw-server/src/main/java/com/rongwei/zhsw/system/controller/SwCollectionNoticeController.java

@@ -2,10 +2,6 @@ package com.rongwei.zhsw.system.controller;
 
 import com.rongwei.zhsw.system.service.SwCollectionNoticeService;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.MediaType;
-import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -22,10 +18,10 @@ public class SwCollectionNoticeController {
     SwCollectionNoticeService swCollectionNoticeService;
 
 
-@PostMapping("/receiveData")
-public void receiveData(@RequestBody List<Map<String, String>> data, HttpServletResponse response) throws Exception {
-    swCollectionNoticeService.printCollectionBill(data, response);
-}
+    @PostMapping("/receiveData")
+    public void receiveData(@RequestBody List<Map<String, String>> data, HttpServletResponse response) throws Exception {
+        swCollectionNoticeService.generatePdf(data, response);
+    }
 
 }
 

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