|
@@ -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("催缴单生成失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|