From a2cfd417f673603633c6d46713d320bb1501f88c Mon Sep 17 00:00:00 2001
From: 许鹏程 <1821349743@qq.com>
Date: 星期二, 08 八月 2023 16:03:30 +0800
Subject: [PATCH] xn commit

---
 src/main/java/com/product/print/util/DynamicTableRenderPolicy.java |  332 +++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 246 insertions(+), 86 deletions(-)

diff --git a/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java b/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
index 9590e86..daa0044 100644
--- a/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
+++ b/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
@@ -1,17 +1,23 @@
 package com.product.print.util;
 
+import cn.hutool.core.collection.CollectionUtil;
 import com.deepoove.poi.exception.RenderException;
 import com.deepoove.poi.render.RenderContext;
+import com.deepoove.poi.template.ElementTemplate;
+import com.deepoove.poi.template.MetaTemplate;
 import com.deepoove.poi.template.run.RunTemplate;
 import com.deepoove.poi.util.TableTools;
 import com.product.common.lang.StringUtils;
 import com.product.core.exception.BaseException;
 import com.product.print.config.CmnCode;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
+import org.apache.poi.util.Units;
 import org.apache.poi.xwpf.usermodel.*;
 
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -26,12 +32,14 @@
 
 	private final String indexKey = "~index~";
 
-	public DynamicTableRenderPolicy(String replaceKey) {
+	public 	DynamicTableRenderPolicy(String replaceKey) {
 		this.replaceKey = replaceKey;
 
 	}
 
-	private XWPFRun run;
+	private List<MetaTemplate> metaTemplateList;
+
+
 
 	@Override
 	public void doRender(RenderContext<Object> context) throws Exception {
@@ -44,8 +52,13 @@
 			}
 			XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
 			XWPFTable table = cell.getTableRow().getTable();
+			this.metaTemplateList = context.getTemplate().getElementTemplates();
+			ElementTemplate eleTemplate = context.getEleTemplate();
+			int index = metaTemplateList.indexOf(eleTemplate);
+			this.metaTemplateList = metaTemplateList.subList(index + 1, metaTemplateList.size());
 			render(table, context.getData());
 		} catch (Exception e) {
+			e.printStackTrace();
 			throw new RenderException("Dynamic render table error:" + e.getMessage(), e);
 		}
 	}
@@ -58,108 +71,134 @@
 		List<Map<String, Object>> subTableData = (List<Map<String, Object>>) data;
 		List<XWPFTableRow> rows = xwpfTable.getRows();
 		//璇诲彇rows涓殑鍐呭
-		//鑾峰彇鏈�鍚庝竴琛�
-		XWPFTableRow xwpfTableRow = rows.get(rows.size() - 1);
 		String tableExpression = "{{" + this.replaceKey + "}}";
 		//鑾峰彇琛ㄦ牸璧峰琛屽拰缁撴潫琛�
 		int startRowIndex = -1;
-		int endRowIndex = -1;
 		rows:
 		for (int i = 0; i < rows.size(); i++) {
 			XWPFTableRow row = rows.get(i);
 			for (int j = 0; j < row.getTableCells().size(); j++) {
-				if (startRowIndex > -1 && endRowIndex > -1) {
-					break rows;
-				}
 				XWPFTableCell cell = row.getTableCells().get(j);
 				String text = cell.getText();
 				if (tableExpression.equals(text)) {
 					startRowIndex = i;
-				}
-				if ("{{$~end~}}".equals(text)) {
-					endRowIndex = i;
+					break rows;
 				}
 
 			}
 		}
-		//琛ㄦ牸璧峰琛屼笉鍦ㄧ涓�琛屼笖娌℃湁鎵惧埌缁撴潫鏍囪
-		if (startRowIndex > 0 && endRowIndex == -1) {
-			throw new BaseException(CmnCode.NOT_FIND_END_FLAG);
+		if (startRowIndex == -1) {
+			throw new BaseException(CmnCode.NOT_FIND_START_FLAG);
 		}
 
-		if (startRowIndex == 0 && endRowIndex > 0) {
-			//鍒犻櫎缁撴潫鏍囪鎵�鍦ㄨ
-			xwpfTable.removeRow(endRowIndex);
+		//鍦ㄨ〃鏍间腑鏌ユ壘瀛愯〃瀛楁浠{$寮�澶磢}浠}缁撳熬鐨勫唴瀹�
+		String regex = "\\{\\{\\$[a-zA-Z0-9_]+\\}\\}";
+		Pattern pattern = Pattern.compile(regex);
+		//瀛楁鎵�鍦ㄨ
+		XWPFTableRow fieldRow = null;
+		for (int i = startRowIndex; i < rows.size(); i++) {
+			XWPFTableRow row = rows.get(i);
+			for (int j = 0; j < row.getTableCells().size(); j++) {
+				XWPFTableCell cell = row.getTableCells().get(j);
+				String text = cell.getText();
+				Matcher matcher = pattern.matcher(text);
+				if (matcher.find()) {
+					fieldRow = row;
+					i = rows.size();
+					break;
+				}
+			}
+		}
+		if (fieldRow == null) {
+			throw new BaseException(CmnCode.NOT_FIND_CHILD_TABLE_FIELD);
 		}
 
 		//璇诲彇鏈�鍚庤姣忎釜鍗曞厓鏍肩殑鍊硷紝璋冪敤getReplaceKey鏂规硶鑾峰彇琛ㄨ揪寮忎腑鐨勫��
-		String[] fieldNames = new String[xwpfTableRow.getTableCells().size()];
-//		for (int i = 0; i < xwpfTableRow.getTableCells().size(); i++) {
-//			XWPFTableCell cell = xwpfTableRow.getTableCells().get(i);
-//			String text = cell.getText();
-//			String replaceKey = getReplaceKey(text);
-//			//鑾峰彇琛ㄨ揪寮忎腑鐨勫��
-//			if (StringUtils.isEmpty(replaceKey)) {
-//				//璁剧疆鍗曞厓鏍间负绌哄��
-//				replaceKey = "";
-//			}
-//			fieldNames[i] = replaceKey;
-//		}
-//		//璇诲彇瀹屾瘯鍚庡垹闄ゆ渶鍚庝竴琛�
-//		xwpfTable.removeRow(rows.size() - 1);
-		//鑾峰彇鎵�鏈夌殑琛屽垽鏂崟鍏冩牸鏄惁鏈夊�兼病鏈夊氨鍒犻櫎璇ヨ
-//		List<XWPFTableRow> tableRows = xwpfTable.getRows();
-//		List<XWPFTableRow> deleteRows = new ArrayList<>();
-//		for (int i = 0; i < tableRows.size(); i++) {
-//			XWPFTableRow row = tableRows.get(i);
-//			boolean flag = false;
-//			for (int j = 0; j < row.getTableCells().size(); j++) {
-//				XWPFTableCell cell = row.getTableCells().get(j);
-//				if (StringUtils.isNotEmpty(cell.getText())) {
-//					flag = true;
-//					break;
-//				}
-//			}
-//			if (!flag) {
-//				deleteRows.add(row);
-//			}
-//		}
-//		//閬嶅巻瑕佸垹闄ょ殑琛岃幏鍙栨墍鍦ㄧ殑涓嬫爣杩涜鍒犻櫎
-//		for (int i = 0; i < deleteRows.size(); i++) {
-//			XWPFTableRow row = deleteRows.get(i);
-//			int index = xwpfTable.getRows().indexOf(row);
-//			xwpfTable.removeRow(index);
-//		}
-//		//閬嶅巻鏁版嵁闆嗗悎锛屾瘡涓猰ap瀵瑰簲涓�琛屾暟鎹�
-//		for (int i = 0; i < subTableData.size(); i++) {
-//			Map<String, Object> map = subTableData.get(i);
-//			//鍒涘缓涓�琛�
-//			XWPFTableRow row = xwpfTable.createRow();
-//			//閬嶅巻瀛楁姣忎釜瀛楁鍒涘缓涓�涓崟鍏冩牸
-//			for (int j = 0; j < fieldNames.length; j++) {
-//				//褰撳墠鍗曞厓鏍�
-//				XWPFTableCell cell;
-//				//鍒ゆ柇row涓j涓崟鍏冩牸鏄惁瀛樺湪
-//				if (row.getTableCells().size() > j) {
-//					cell = row.getTableCells().get(j);
-//				} else {
-//					cell = row.createCell();
-//				}
-//				//璁剧疆鍗曞厓鏍肩殑鍊间粠map涓彇鍑�
-//				//鍒ゆ柇鏄惁鏄簭鍙峰垪
-//				if (indexKey.equals(fieldNames[j])) {
-//					cell.setText(String.valueOf(i + 1));
-//					continue;
-//				}
-//				Object value = map.get(fieldNames[j]);
-//				if (value == null) {
-//					value = "";
-//				}
-//				//璁剧疆鍗曞厓鏍肩殑鍊�
-//				cell.setText(value.toString());
-//			}
-//		}
+		String[] fieldNames = new String[fieldRow.getTableCells().size()];
+		for (int i = 0; i < fieldRow.getTableCells().size(); i++) {
+			XWPFTableCell cell = fieldRow.getTableCells().get(i);
+			String text = cell.getText();
+			String replaceKey = getReplaceKey(text);
+			//鑾峰彇琛ㄨ揪寮忎腑鐨勫��
+			if (StringUtils.isEmpty(replaceKey)) {
+				//璁剧疆鍗曞厓鏍间负绌哄��
+				replaceKey = "";
+			}
+			fieldNames[i] = replaceKey;
+		}
 
+		if (subTableData != null && subTableData.size() > 0) {
+			for (int i = 0; i < subTableData.size(); i++) {
+				Map<String, Object> map = subTableData.get(i);
+				XWPFTableRow row;
+				if (i == 0) {
+					row = fieldRow;
+				} else {
+					//鍒涘缓涓�琛屽湪fieldRowIndex涓嬮潰
+					row = xwpfTable.insertNewTableRow(xwpfTable.getRows().size());
+					PrintPoiUtil.copyTableRow(row, fieldRow);
+				}
+				//閬嶅巻瀛楁姣忎釜瀛楁鍒涘缓涓�涓崟鍏冩牸
+				for (int j = 0; j < fieldNames.length; j++) {
+					//褰撳墠鍗曞厓鏍�
+					XWPFTableCell cell = row.getCell(j);
+					//娓呯┖鍗曞厓鏍煎唴瀹�
+					XWPFParagraph xwpfParagraph = cell.addParagraph();
+					int index = cell.getParagraphs().indexOf(xwpfParagraph);
+					PrintPoiUtil.copyParagraph(xwpfParagraph, cell.getParagraphs().get(0));
+					//鍒犻櫎鎵�鏈夌殑run
+					for (int k = xwpfParagraph.getRuns().size() - 1; k >= 0; k--) {
+						xwpfParagraph.removeRun(k);
+					}
+					XWPFRun xwpfRun = xwpfParagraph.createRun();
+					PrintPoiUtil.copyRun(xwpfRun, cell.getParagraphs().get(0).getRuns().get(0));
+					//鍒犻櫎cell涓殑娈佃惤浣嗘帓闄ゆ柊澧炵殑娈佃惤
+					for (int k = cell.getParagraphs().size() - 1; k >= 0; k--) {
+						if (k < index) {
+							cell.removeParagraph(k);
+						}
+					}
+					//鍒ゆ柇鏄惁鏄簭鍙峰垪
+					if (indexKey.equals(fieldNames[j])) {
+						cell.setText(String.valueOf(i + 1));
+						continue;
+					}
+					Object value = map.get(fieldNames[j]);
+					if (value == null) {
+						value = "";
+					}
+					xwpfRun.setText(value.toString(), 0);
+					if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldNames[j]) && !StringUtils.isEmpty(map.get("sign_attachment_uuid"))) {
+						//鎰忚妗� 鎻掑叆绛惧悕鍥剧墖鍒板崟鍏冩牸鍙充笅瑙掍綅缃�
+						xwpfParagraph = cell.addParagraph();
+						xwpfParagraph.setAlignment(ParagraphAlignment.RIGHT);
+						XWPFRun run = xwpfParagraph.createRun();
+						String signAttachmentBase64 = map.get("sign_attachment_uuid").toString();
+						//灏哹ase64杞崲瀛楄妭娴�
+						byte[] bytes = Base64.getDecoder().decode(signAttachmentBase64.split(",")[1]);
+						//灏嗗瓧鑺傛祦杞崲涓鸿緭鍏ユ祦
+						InputStream inputStream = new ByteArrayInputStream(bytes);
+						try {
+							//鎹㈣鎻掑叆鍥剧墖
+							run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG, "sign.png", Units.toEMU(50), Units.toEMU(20));
+							inputStream.close();
+						} catch (InvalidFormatException e) {
+							e.printStackTrace();
+						} catch (IOException e) {
+							e.printStackTrace();
+						}
+					}
+
+				}
+			}
+		} else {
+
+//			鍒犻櫎fieldRow
+			int index = xwpfTable.getRows().indexOf(fieldRow);
+			xwpfTable.removeRow(index);
+		}
+		//鍒犻櫎璧峰琛�
+		xwpfTable.removeRow(startRowIndex);
 	}
 
 	/**
@@ -174,4 +213,125 @@
 		}
 		return null;
 	}
+
+	/**
+	 * 澶嶅埗琛岋紝浠巗ource鍒皌arget
+	 *
+	 * @param target
+	 * @param source
+	 */
+	public void copyTableRow(XWPFTableRow target, XWPFTableRow source) {
+		// 澶嶅埗鏍峰紡
+		if (source.getCtRow() != null) {
+			target.getCtRow().setTrPr(source.getCtRow().getTrPr());
+		}
+		// 澶嶅埗鍗曞厓鏍�
+		for (int i = 0; i < source.getTableCells().size(); i++) {
+			XWPFTableCell cell1 = target.getCell(i);
+			XWPFTableCell cell2 = source.getCell(i);
+			if (cell1 == null) {
+				cell1 = target.addNewTableCell();
+			}
+			copyTableCell(cell1, cell2);
+		}
+
+	}
+
+	/**
+	 * 澶嶅埗鍗曞厓鏍硷紝浠巗ource鍒皌arget
+	 *
+	 * @param target
+	 * @param source
+	 */
+	public void copyTableCell(XWPFTableCell target, XWPFTableCell source) {
+		// 鍒楀睘鎬�
+		if (source.getCTTc() != null) {
+			target.getCTTc().setTcPr(source.getCTTc().getTcPr());
+		}
+		// 鍒犻櫎娈佃惤
+		for (int pos = 0; pos < target.getParagraphs().size(); pos++) {
+			target.removeParagraph(pos);
+		}
+		// 娣诲姞娈佃惤
+		for (XWPFParagraph sp : source.getParagraphs()) {
+			XWPFParagraph targetP = target.addParagraph();
+			copyParagraph(targetP, sp);
+		}
+	}
+
+	/**
+	 * 澶嶅埗鍥剧墖鍒皌arget
+	 *
+	 * @param target
+	 * @param picture
+	 * @throws IOException
+	 * @throws InvalidFormatException
+	 */
+	public void copyPicture(XWPFRun target, XWPFPicture picture) throws IOException, InvalidFormatException {
+
+		String filename = picture.getPictureData().getFileName();
+		InputStream pictureData = new ByteArrayInputStream(picture
+				.getPictureData().getData());
+		int pictureType = picture.getPictureData().getPictureType();
+		int width = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
+				.getCx();
+
+		int height = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
+				.getCy();
+
+		// target.addBreak();
+		target.addPicture(pictureData, pictureType, filename, width, height);
+		// target.addBreak(BreakType.PAGE);
+	}
+
+	/**
+	 * 澶嶅埗娈佃惤锛屼粠source鍒皌arget
+	 *
+	 * @param target
+	 * @param source
+	 */
+	public void copyParagraph(XWPFParagraph target, XWPFParagraph source) {
+
+		// 璁剧疆娈佃惤鏍峰紡
+		target.getCTP().setPPr(source.getCTP().getPPr());
+
+		// 绉婚櫎鎵�鏈夌殑run
+		for (int pos = target.getRuns().size() - 1; pos >= 0; pos--) {
+			target.removeRun(pos);
+		}
+
+		// copy 鏂扮殑run
+		for (XWPFRun s : source.getRuns()) {
+			XWPFRun targetrun = target.createRun();
+			copyRun(targetrun, s);
+		}
+
+
+	}
+
+	/**
+	 * 澶嶅埗RUN锛屼粠source鍒皌arget
+	 *
+	 * @param target
+	 * @param source
+	 */
+	public void copyRun(XWPFRun target, XWPFRun source) {
+		// 璁剧疆run灞炴��
+		target.getCTR().setRPr(source.getCTR().getRPr());
+		// 璁剧疆鏂囨湰
+		target.setText(source.text());
+		// 澶勭悊鍥剧墖
+		List<XWPFPicture> pictures = source.getEmbeddedPictures();
+
+		for (XWPFPicture picture : pictures) {
+			try {
+				copyPicture(target, picture);
+			} catch (InvalidFormatException e) {
+				logger.error("copyRun", e);
+			} catch (IOException e) {
+				logger.error("copyRun", e);
+			}
+		}
+	}
+
 }

--
Gitblit v1.9.2