From cfd0903dc0cfc7ffd39c2caa101f31a50441d39c Mon Sep 17 00:00:00 2001
From: 6c <420680616@qq.com>
Date: 星期五, 31 十月 2025 17:29:59 +0800
Subject: [PATCH] 已送审保养打印; 列表批量打印
---
src/main/java/com/product/print/service/PrintRealizeService.java | 241 ++++++++++++++++++++++++++++++------------------
1 files changed, 150 insertions(+), 91 deletions(-)
diff --git a/src/main/java/com/product/print/service/PrintRealizeService.java b/src/main/java/com/product/print/service/PrintRealizeService.java
index 4687f24..4c7feda 100644
--- a/src/main/java/com/product/print/service/PrintRealizeService.java
+++ b/src/main/java/com/product/print/service/PrintRealizeService.java
@@ -10,11 +10,11 @@
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
-import com.deepoove.poi.render.RenderContext;
-import com.deepoove.poi.util.RegexUtils;
+import com.google.common.collect.Lists;
import com.product.common.lang.StringUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.config.Global;
+import com.product.core.entity.DataEntity;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
@@ -24,22 +24,22 @@
import com.product.print.config.CmnCode;
import com.product.print.config.CmnConst;
import com.product.print.service.ide.IPrintRealizeService;
+import com.product.print.util.CustomPictureRenderPolicy;
import com.product.print.util.DynamicTableRenderPolicy;
import com.product.print.util.FlowOpinionRenderPolicy;
import com.product.print.util.TableEmptyHandler;
import com.product.tool.flow.service.FlowDetailService;
import com.product.util.BaseUtil;
-
import com.product.util.SystemParamReplace;
-import org.apache.poi.xwpf.usermodel.XWPFTable;
+import org.apache.pdfbox.io.MemoryUsageSetting;
+import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
-import java.io.*;
+import java.io.File;
+import java.io.OutputStream;
import java.util.*;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -120,17 +120,32 @@
/**
* 鎵撳嵃浼犺緭pdf娴佸埌鍓嶇
*
- * @param fse 鎵撳嵃鏁版嵁
+ * @param fseOrDte 鎵撳嵃鏁版嵁
* @param response 鍝嶅簲
* @param isConvertPdf 鏄惁杞崲涓簆df
* @throws BaseException
*/
@Override
- public void print(FieldSetEntity fse, HttpServletResponse response, boolean isConvertPdf) throws BaseException {
- //鎵撳嵃閰嶇疆
- FieldSetEntity printConfig = getPrintConfig(fse.getString("~" + CmnConst.PRINT_TEMP + "~"));
+ public void print(DataEntity fseOrDte, HttpServletResponse response, boolean isConvertPdf) throws BaseException {
+ FieldSetEntity fse;
+ DataTableEntity dte;
+ //鎵撳嵃閰嶇疆
+ FieldSetEntity printConfig;
+ if (fseOrDte.isFieldsetEntity()) {
+ fse = (FieldSetEntity) fseOrDte;
+ printConfig = getPrintConfig(getPrintTemplateField(fse));
+ } else if (fseOrDte.isDataTableEntity()) {
+ dte = (DataTableEntity) fseOrDte;
+ fse = dte.getFieldSetEntity(0);
+ printConfig = getPrintConfig(getPrintTemplateField(fse));
+ } else {
+ throw new BaseException(CmnCode.GET_BEAN_FAIL);
+ }
//鑾峰彇鍒版浛鎹㈠悗鐨勬枃浠惰矾寰� 锛坧df鏂囦欢鎴栬�厀ord鏂囦欢锛�
- String tempPdfFilePath = replaceTemplateFileOut(printConfig, fse, isConvertPdf);
+ String tempPdfFilePath = replaceTemplateFileOut(printConfig, fseOrDte, isConvertPdf);
+ if (StringUtils.isEmpty(tempPdfFilePath)) {
+ throw new BaseException(CmnCode.DEALT_FILE_PATH_IS_EMPTY);
+ }
//鑾峰彇鏂囦欢鍚�
String fileName = BaseUtil.ifNull(printConfig.getString(CmnConst.PRINT_FILE_NAME), printConfig.getString(CmnConst.PRINT_NAME));
if (isConvertPdf) {
@@ -145,11 +160,27 @@
// 璁剧疆鑷畾涔夊ごContent-Disposition
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncodeUtil.encode(fileName));
- try (
- OutputStream out = response.getOutputStream();
- ) {
- FileUtil.writeToStream(new File(tempPdfFilePath), out);
- } catch (Exception e) {
+ try (OutputStream out = response.getOutputStream();) {
+ if (tempPdfFilePath.contains(",")) {
+ // 鏇撮珮鏁堢殑鏂瑰紡锛氱洿鎺ヤ粠鏂囦欢鍚堝苟鍒拌緭鍑烘祦
+ PDFMergerUtility merger = new PDFMergerUtility();
+ merger.setDestinationStream(response.getOutputStream());
+
+ String[] tempPdfFilePathArr = tempPdfFilePath.split(",");
+ for (int i = 0; i < tempPdfFilePathArr.length; i++) {
+ String singleTempPdfFilePath = tempPdfFilePathArr[i];
+ File file = new File(singleTempPdfFilePath);
+ if (file.exists()) {
+ merger.addSource(file); // 鐩存帴娣诲姞鏂囦欢婧�
+ }
+ }
+
+ // 鎵ц鍚堝苟
+ merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
+ } else {
+ FileUtil.writeToStream(new File(tempPdfFilePath), out);
+ }
+ } catch (Exception e) {
e.printStackTrace();
throw new BaseException(CmnCode.PRINT_CONTENT_FAIL, e);
} finally {
@@ -158,8 +189,23 @@
System.out.println(absolutePath);
file.delete();
}
-
}
+
+ /**
+ * 鑾峰彇鎵撳嵃妯℃澘瀛楁
+ * @param fse
+ * @return
+ */
+ private String getPrintTemplateField(FieldSetEntity fse) {
+ if (FieldSetEntity.isEmpty(fse)) {
+ return null;
+ }
+ String printTemplateField = fse.getString("~" + CmnConst.PRINT_TEMP + "~");
+ if (StringUtils.isEmpty(printTemplateField)) {
+ printTemplateField = fse.getString("~print_template~");
+ }
+ return printTemplateField;
+ }
/**
* 鑾峰彇鎵撳嵃閰嶇疆
@@ -182,74 +228,97 @@
* 鏇挎崲妯℃澘鏂囦欢骞惰緭鍑�
*
* @param printConf 鎵撳嵃閰嶇疆
- * @param fse 鏇挎崲鏁版嵁
+ * @param fseOrDte 鏇挎崲鏁版嵁
* @param isConvertPdf 鏄惁杞崲涓簆df
* @return 鏇挎崲鍚庣殑鏂囦欢璺緞
* @throws BaseException 寮傚父
*/
- private String replaceTemplateFileOut(FieldSetEntity printConf, FieldSetEntity fse, boolean isConvertPdf) throws BaseException {
+ private String replaceTemplateFileOut(FieldSetEntity printConf, DataEntity fseOrDte, boolean isConvertPdf) throws BaseException {
+ FieldSetEntity fse = null;
+ DataTableEntity dte = null;
+ if (fseOrDte.isFieldsetEntity()) {
+ fse = (FieldSetEntity) fseOrDte;
+ } else if (fseOrDte.isDataTableEntity()) {
+ dte = (DataTableEntity) fseOrDte;
+ } else {
+ throw new BaseException(CmnCode.GET_BEAN_FAIL);
+ }
+ if (fse != null) {
+ dte = new DataTableEntity();
+ dte.addFieldSetEntity(fse);
+ }
- Object[] fields = fse.getFields();
- for (int i = 0; i < fields.length; i++) {
- String field = fields[i].toString();
- FieldSetEntity metaEntity = fse.getMeta().getFieldMeta(field);
- if (metaEntity == null) {
- continue;
- }
- String fieldType = metaEntity.getString("field_type");
- //鍒ゆ柇鏄惁鎷ユ湁娴佺▼鏍囪瘑
- if ("flowsign".equals(fieldType)) {
- if (StringUtils.equalsAny(fse.getString(field), "1", "2")) {
- //娴佺▼鍔炵悊涓垨鍔炵粨 鑾峰彇娴佺▼鎰忚
- FieldSetEntity flowTask = getBaseDao().getFieldSetByFilter("product_sys_flow_task", "table_name=? and record_uuid=?", new Object[]{fse.getTableName(), fse.getUUID()}, false);
- if (flowTask != null) {
- String taskUuid = flowTask.getString(CmnConst.UUID);
- //鑾峰彇娴佺▼鎰忚
- FlowDetailService flowDetailService = SpringUtil.getBean(FlowDetailService.class);
- JSONArray opinion = flowDetailService.getHistoryInfo(taskUuid);
- fse.setValue("~flow_opinion~", opinion);
- }
- }
- break;
- }
- }
+ List<String> localTempPathList = Lists.newArrayList();
+ if (!DataTableEntity.isEmpty(dte)) {
+ for (int m = 0; m < dte.getRows(); m++) {
+ fse = dte.getFieldSetEntity(m);
- // 鎵撳嵃妯℃澘闄勪欢uuid
- String template_uuid = printConf.getString(CmnConst.PRINT_TEMPLATE);
- // 鑾峰彇鎵撳嵃妯℃澘
- File file = getTemplateFile(template_uuid);
- // 鍔犺浇鍙傜収 鎵撳嵃鏃朵娇鐢ㄦ樉绀哄�� 鑰屼笉鏄疄闄呭��
- getBaseDao().loadPromptData(fse);
- // 鏂囦欢鍚嶅墠缂�閮ㄥ垎
- Object tempKey = UUID.randomUUID();
- // 鏇挎崲鍚庣殑word涓存椂璺緞
- String localTempPathWord = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".docx";
- replaceWord(localTempPathWord, file.getPath(), fse);
- file.delete();
+ Object[] fields = fse.getFields();
+ for (int i = 0; i < fields.length; i++) {
+ String field = fields[i].toString();
+ FieldSetEntity metaEntity = fse.getMeta().getFieldMeta(field);
+ if (metaEntity == null) {
+ continue;
+ }
+ String fieldType = metaEntity.getString("field_type");
+ //鍒ゆ柇鏄惁鎷ユ湁娴佺▼鏍囪瘑
+ if ("flowsign".equals(fieldType)) {
+ if (StringUtils.equalsAny(fse.getString(field), "1", "2")) {
+ //娴佺▼鍔炵悊涓垨鍔炵粨 鑾峰彇娴佺▼鎰忚
+ FieldSetEntity flowTask = getBaseDao().getFieldSetByFilter("product_sys_flow_task", "table_name=? and record_uuid=?", new Object[]{fse.getTableName(), fse.getUUID()}, false);
+ if (flowTask != null) {
+ String taskUuid = flowTask.getString(CmnConst.UUID);
+ //鑾峰彇娴佺▼鎰忚
+ FlowDetailService flowDetailService = SpringUtil.getBean(FlowDetailService.class);
+ JSONArray opinion = flowDetailService.getHistoryInfo(taskUuid);
+ fse.setValue("~flow_opinion~", opinion);
+ }
+ }
+ break;
+ }
+ }
- String replaceParams = SystemParamReplace.replaceParams(Optional.ofNullable(printConf.getString(CmnConst.PRINT_FILE_NAME)).orElse(printConf.getString(CmnConst.PRINT_NAME)), fse);
- printConf.setValue(CmnConst.PRINT_FILE_NAME, replaceParams);
- if (isConvertPdf) {
- try {
- // 鏇挎崲鍚庣殑pdf涓存椂璺緞
- String localTempPathPdf = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".pdf";
- //妫�鏌ユ枃浠舵槸鍚﹀瓨鍦ㄤ笉瀛樺垯鍒涘缓
- FileUtil.touch(localTempPathPdf);
- // 杞崲pdf
- PdfConcurrenceUtil.convertToPdf(localTempPathWord, localTempPathPdf, "docx");
- // 鍒犻櫎word涓存椂鏂囦欢
- File wordTemp = new File(localTempPathWord);
- if (wordTemp.exists()) {
- wordTemp.delete();
- }
- return localTempPathPdf;
- } catch (Exception e) {
- throw new BaseException(CmnCode.CONVERT_PDF_ERROR, e);
- } finally {
- FileUtil.del(localTempPathWord);
- }
- }
- return localTempPathWord;
+ // 鎵撳嵃妯℃澘闄勪欢uuid
+ String template_uuid = printConf.getString(CmnConst.PRINT_TEMPLATE);
+ // 鑾峰彇鎵撳嵃妯℃澘
+ File file = getTemplateFile(template_uuid);
+ // 鍔犺浇鍙傜収 鎵撳嵃鏃朵娇鐢ㄦ樉绀哄�� 鑰屼笉鏄疄闄呭��
+ getBaseDao().loadPromptData(fse);
+ // 鏂囦欢鍚嶅墠缂�閮ㄥ垎
+ Object tempKey = UUID.randomUUID();
+ // 鏇挎崲鍚庣殑word涓存椂璺緞
+ String localTempPathWord = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".docx";
+
+ replaceWord(localTempPathWord, file.getPath(), fse);
+ file.delete();
+
+ String replaceParams = SystemParamReplace.replaceParams(Optional.ofNullable(printConf.getString(CmnConst.PRINT_FILE_NAME)).orElse(printConf.getString(CmnConst.PRINT_NAME)), fse);
+ printConf.setValue(CmnConst.PRINT_FILE_NAME, replaceParams);
+ if (isConvertPdf) {
+ try {
+ // 鏇挎崲鍚庣殑pdf涓存椂璺緞
+ String localTempPathPdf = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".pdf";
+ //妫�鏌ユ枃浠舵槸鍚﹀瓨鍦ㄤ笉瀛樺垯鍒涘缓
+ FileUtil.touch(localTempPathPdf);
+ // 杞崲pdf
+ PdfConcurrenceUtil.convertToPdf(localTempPathWord, localTempPathPdf, "docx");
+ // 鍒犻櫎word涓存椂鏂囦欢
+ File wordTemp = new File(localTempPathWord);
+ if (wordTemp.exists()) {
+ wordTemp.delete();
+ }
+ localTempPathList.add(localTempPathPdf);
+ } catch (Exception e) {
+ throw new BaseException(CmnCode.CONVERT_PDF_ERROR, e);
+ } finally {
+ FileUtil.del(localTempPathWord);
+ }
+ } else {
+ localTempPathList.add(localTempPathWord);
+ }
+ }
+ }
+ return BaseUtil.collection2String(localTempPathList);
}
/**
@@ -270,19 +339,9 @@
Map<String, Object> cloneValues = new HashMap(dataFse.getValues());
//鑾峰彇瀛愯〃鏁版嵁
Map<String, DataTableEntity> subDataMap = dataFse.getSubData();
- ConfigureBuilder config = Configure.createDefault().builder();
+ ConfigureBuilder config = Configure.builder();
config.addPlugin('@', new FlowOpinionRenderPolicy(flowOpinion));
- config.addPlugin('$', new com.deepoove.poi.policy.DynamicTableRenderPolicy() {
- @Override
- public void doRender(RenderContext<Object> context) throws Exception {
- return;
- }
-
- @Override
- public void render(XWPFTable table, Object data) throws Exception {
- return;
- }
- });
+ config.addPlugin('&', new CustomPictureRenderPolicy());
config.buildGrammerRegex("(#)?([\\w\\u4e00-\\u9fa5]+)(\\.?[\\w\\u4e00-\\u9fa5\\|]*)*(#)?");
TableEmptyHandler tableEmptyHandler = new TableEmptyHandler();
if (!CollectionUtil.isEmpty(subDataMap)) {
--
Gitblit v1.9.2