|
|
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.zksy.property.domain.ARentalContract;
|
|
|
+import com.zksy.property.domain.bo.RentalTempBo;
|
|
|
import com.zksy.property.mapper.ARentalContractMapper;
|
|
|
import com.zksy.property.service.ARentalContentService;
|
|
|
import com.zksy.property.service.ARentalContractService;
|
|
|
@@ -15,10 +16,12 @@ import org.springframework.core.io.ClassPathResource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
-import java.util.ArrayList;
|
|
|
+import java.lang.reflect.Method;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
@@ -56,220 +59,86 @@ public class ARentalContractServiceImpl extends ServiceImpl<ARentalContractMappe
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public String generatorRental(String mark) {
|
|
|
- switch (mark){
|
|
|
- case "1":{
|
|
|
- HashMap<String, String> contentMap = new HashMap<>();
|
|
|
- contentMap.put("a1", "张三");
|
|
|
- contentMap.put("a2", "11111111");
|
|
|
- contentMap.put("a3", "2222222222");
|
|
|
- contentMap.put("a4", "3333333");
|
|
|
- contentMap.put("a5", "555555");
|
|
|
- contentMap.put("a6", "6666666");
|
|
|
- contentMap.put("a7", "777777");
|
|
|
- contentMap.put("a8", "8888888");
|
|
|
- contentMap.put("a9", "111100099999");
|
|
|
-
|
|
|
- try (InputStream inputStream = new ClassPathResource("templates/1.docx").getInputStream()) {
|
|
|
- XWPFDocument document = new XWPFDocument(inputStream);
|
|
|
-
|
|
|
- // 遍历所有段落并进行占位符替换
|
|
|
- for (XWPFParagraph paragraph : document.getParagraphs()) {
|
|
|
- List<XWPFRun> runs = paragraph.getRuns();
|
|
|
- if (runs == null || runs.isEmpty()) continue;
|
|
|
-
|
|
|
- List<XWPFRun> modifiableRuns = new ArrayList<>(runs);
|
|
|
- int index = 0;
|
|
|
-
|
|
|
- while (index < modifiableRuns.size()) {
|
|
|
- XWPFRun run = modifiableRuns.get(index);
|
|
|
-
|
|
|
- // 检查 run 是否有效
|
|
|
- if (run.getParent() == null) {
|
|
|
- index++;
|
|
|
- continue;
|
|
|
- }
|
|
|
+ public String generatorRental(String mark, RentalTempBo bo) {
|
|
|
+ Map<String, Integer> markToFieldCount = new HashMap<>();
|
|
|
+ markToFieldCount.put("1", 9);
|
|
|
+ markToFieldCount.put("2", 54);
|
|
|
+ markToFieldCount.put("3", 27);
|
|
|
+
|
|
|
+ if (!markToFieldCount.containsKey(mark)) {
|
|
|
+ throw new IllegalArgumentException("不支持的 mark 类型:" + mark);
|
|
|
+ }
|
|
|
|
|
|
- String text;
|
|
|
- try {
|
|
|
- text = run.getText(0);
|
|
|
- } catch (Exception e) {
|
|
|
- index++;
|
|
|
- continue;
|
|
|
- }
|
|
|
+ int fieldCount = markToFieldCount.get(mark);
|
|
|
+ String prefix;
|
|
|
+ switch (mark) {
|
|
|
+ case "1": prefix = "a"; break;
|
|
|
+ case "2": prefix = "b"; break;
|
|
|
+ case "3": prefix = "c"; break;
|
|
|
+ default: throw new IllegalStateException("非法 mark 值");
|
|
|
+ }
|
|
|
|
|
|
- if (text == null || text.isEmpty()) {
|
|
|
- index++;
|
|
|
- continue;
|
|
|
- }
|
|
|
+ String templatePath = "templates/" + mark + ".docx";
|
|
|
+ String outputPath = "C:\\Users\\hxb\\Downloads\\已替换合同.docx";
|
|
|
|
|
|
- Matcher matcher = Pattern.compile("\\$\\{(.+?)\\}").matcher(text);
|
|
|
- if (matcher.find()) {
|
|
|
- // 存在占位符,进行替换
|
|
|
- do {
|
|
|
- String key = matcher.group(1);
|
|
|
- String replacement = contentMap.getOrDefault(key, "未知字段");
|
|
|
+ Map<String, String> contentMap = buildContentMap(bo, prefix, fieldCount);
|
|
|
|
|
|
- // 替换当前 Run 中的占位符内容
|
|
|
- text = text.replace("${" + key + "}", replacement);
|
|
|
- } while (matcher.find());
|
|
|
+ try (InputStream inputStream = new ClassPathResource(templatePath).getInputStream();
|
|
|
+ XWPFDocument document = new XWPFDocument(inputStream);
|
|
|
+ FileOutputStream outputStream = new FileOutputStream(outputPath)) {
|
|
|
|
|
|
- // 只更新当前 Run 的文本内容,不删除或清空整个段落
|
|
|
- run.setText(text, 0);
|
|
|
- }
|
|
|
+ replacePlaceholders(document, contentMap);
|
|
|
+ document.write(outputStream);
|
|
|
|
|
|
- index++;
|
|
|
- }
|
|
|
- }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException("处理文档时发生错误", e);
|
|
|
+ }
|
|
|
|
|
|
- // 可选:保存修改后的文档到新文件
|
|
|
- try (FileOutputStream outputStream = new FileOutputStream("C:\\Users\\hxb\\Downloads\\已替换合同.docx")) {
|
|
|
- document.write(outputStream);
|
|
|
- }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- break;
|
|
|
+ private Map<String, String> buildContentMap(RentalTempBo bo, String prefix, int count) {
|
|
|
+ Map<String, String> contentMap = new HashMap<>();
|
|
|
+ for (int i = 1; i <= count; i++) {
|
|
|
+ String methodName = "get" + prefix.toUpperCase() + i;
|
|
|
+ try {
|
|
|
+ Method method = bo.getClass().getMethod(methodName);
|
|
|
+ String value = (String) method.invoke(bo);
|
|
|
+ contentMap.put(prefix + i, value != null ? value : " ");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace(); // 可替换为日志记录
|
|
|
+ contentMap.put(prefix + i, " ");
|
|
|
}
|
|
|
- case "2":{
|
|
|
- HashMap<String, String> contentMap = new HashMap<>();
|
|
|
- contentMap.put("b1", "张三");
|
|
|
- contentMap.put("b2", "123456789");
|
|
|
-
|
|
|
- try (InputStream inputStream = new ClassPathResource("templates/2.docx").getInputStream()) {
|
|
|
- XWPFDocument document = new XWPFDocument(inputStream);
|
|
|
-
|
|
|
- // 遍历所有段落并进行占位符替换
|
|
|
- for (XWPFParagraph paragraph : document.getParagraphs()) {
|
|
|
- List<XWPFRun> runs = paragraph.getRuns();
|
|
|
- if (runs == null || runs.isEmpty()) continue;
|
|
|
-
|
|
|
- List<XWPFRun> modifiableRuns = new ArrayList<>(runs);
|
|
|
- int index = 0;
|
|
|
-
|
|
|
- while (index < modifiableRuns.size()) {
|
|
|
- XWPFRun run = modifiableRuns.get(index);
|
|
|
-
|
|
|
- // 检查 run 是否有效
|
|
|
- if (run.getParent() == null) {
|
|
|
- index++;
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- String text;
|
|
|
- try {
|
|
|
- text = run.getText(0);
|
|
|
- } catch (Exception e) {
|
|
|
- index++;
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (text == null || text.isEmpty()) {
|
|
|
- index++;
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- Matcher matcher = Pattern.compile("\\$\\{(.+?)\\}").matcher(text);
|
|
|
- if (matcher.find()) {
|
|
|
- // 存在占位符,进行替换
|
|
|
- do {
|
|
|
- String key = matcher.group(1);
|
|
|
- String replacement = contentMap.getOrDefault(key, "未知字段");
|
|
|
-
|
|
|
- // 替换当前 Run 中的占位符内容
|
|
|
- text = text.replace("${" + key + "}", replacement);
|
|
|
- } while (matcher.find());
|
|
|
-
|
|
|
- // 只更新当前 Run 的文本内容,不删除或清空整个段落
|
|
|
- run.setText(text, 0);
|
|
|
- }
|
|
|
-
|
|
|
- index++;
|
|
|
- }
|
|
|
- }
|
|
|
+ }
|
|
|
+ return contentMap;
|
|
|
+ }
|
|
|
|
|
|
- // 可选:保存修改后的文档到新文件
|
|
|
- try (FileOutputStream outputStream = new FileOutputStream("C:\\Users\\hxb\\Downloads\\已替换合同.docx")) {
|
|
|
- document.write(outputStream);
|
|
|
+ private void replacePlaceholders(XWPFDocument document, Map<String, String> contentMap) {
|
|
|
+ for (XWPFParagraph paragraph : document.getParagraphs()) {
|
|
|
+ for (XWPFRun run : paragraph.getRuns()) {
|
|
|
+ if (run == null || run.getText(0) == null) continue;
|
|
|
+ String text = run.getText(0);
|
|
|
+
|
|
|
+ Matcher matcher = Pattern.compile("\\$\\{(.+?)\\}").matcher(text);
|
|
|
+ boolean found = false;
|
|
|
+
|
|
|
+ while (matcher.find()) {
|
|
|
+ found = true;
|
|
|
+ String key = matcher.group(1);
|
|
|
+ String replacement = contentMap.getOrDefault(key, "未知字段");
|
|
|
+ if(replacement == null) {
|
|
|
+ replacement = " ";
|
|
|
}
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ text = text.replace("${" + key + "}", replacement);
|
|
|
}
|
|
|
- break;
|
|
|
- }
|
|
|
- case "3":{
|
|
|
- HashMap<String, String> contentMap = new HashMap<>();
|
|
|
- contentMap.put("c1", "张三");
|
|
|
- contentMap.put("c2", "123456789");
|
|
|
-
|
|
|
- try (InputStream inputStream = new ClassPathResource("templates/3.docx").getInputStream()) {
|
|
|
- XWPFDocument document = new XWPFDocument(inputStream);
|
|
|
-
|
|
|
- // 遍历所有段落并进行占位符替换
|
|
|
- for (XWPFParagraph paragraph : document.getParagraphs()) {
|
|
|
- List<XWPFRun> runs = paragraph.getRuns();
|
|
|
- if (runs == null || runs.isEmpty()) continue;
|
|
|
-
|
|
|
- List<XWPFRun> modifiableRuns = new ArrayList<>(runs);
|
|
|
- int index = 0;
|
|
|
-
|
|
|
- while (index < modifiableRuns.size()) {
|
|
|
- XWPFRun run = modifiableRuns.get(index);
|
|
|
-
|
|
|
- // 检查 run 是否有效
|
|
|
- if (run.getParent() == null) {
|
|
|
- index++;
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- String text;
|
|
|
- try {
|
|
|
- text = run.getText(0);
|
|
|
- } catch (Exception e) {
|
|
|
- index++;
|
|
|
- continue;
|
|
|
- }
|
|
|
|
|
|
- if (text == null || text.isEmpty()) {
|
|
|
- index++;
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- Matcher matcher = Pattern.compile("\\$\\{(.+?)\\}").matcher(text);
|
|
|
- if (matcher.find()) {
|
|
|
- // 存在占位符,进行替换
|
|
|
- do {
|
|
|
- String key = matcher.group(1);
|
|
|
- String replacement = contentMap.getOrDefault(key, "未知字段");
|
|
|
-
|
|
|
- // 替换当前 Run 中的占位符内容
|
|
|
- text = text.replace("${" + key + "}", replacement);
|
|
|
- } while (matcher.find());
|
|
|
-
|
|
|
- // 只更新当前 Run 的文本内容,不删除或清空整个段落
|
|
|
- run.setText(text, 0);
|
|
|
- }
|
|
|
-
|
|
|
- index++;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 可选:保存修改后的文档到新文件
|
|
|
- try (FileOutputStream outputStream = new FileOutputStream("C:\\Users\\hxb\\Downloads\\已替换合同.docx")) {
|
|
|
- document.write(outputStream);
|
|
|
- }
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ if (found) {
|
|
|
+ run.setText(text, 0);
|
|
|
}
|
|
|
- break;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
- return "";
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|