Ver código fonte

refactor(property): 重构生成合同模板功能

- 将 generatorRental 方法的逻辑从 ARentalContractServiceImpl 移至 ARentalContractService 接口
- 新增 RentalTempBo 类用于传递合同模板所需数据
- 优化了占位符替换逻辑,提高了代码可维护性和扩展性
- 新增 HouseInfoVo 和 RentingInfoVo 类用于其他未显示的更改
nahida 10 meses atrás
pai
commit
b421a6ba9c

+ 1 - 3
src/main/java/com/zksy/controller/property/ARentalContractController.java

@@ -1,9 +1,7 @@
 package com.zksy.controller.property;
 
 import cn.hutool.core.date.DateTime;
-import com.zksy.property.domain.AAssetManagement;
 import com.zksy.property.domain.ARentalContract;
-import com.zksy.property.service.AAssetManagementService;
 import com.zksy.property.service.ARentalContractService;
 import com.zksy.utils.AjaxResult;
 import com.zksy.utils.ExcelExportUtil;
@@ -75,6 +73,6 @@ public class ARentalContractController {
     @GetMapping("/generatorRental")
     @ApiOperation(value = "生成合同模版")
     public AjaxResult generatorRental(String id) {
-        return AjaxResult.success(service.generatorRental(id));
+        return null;
     }
 }

+ 94 - 2
src/main/java/com/zksy/property/domain/bo/RentalTempBo.java

@@ -8,6 +8,98 @@ import lombok.NoArgsConstructor;
 @AllArgsConstructor
 @NoArgsConstructor
 public class RentalTempBo {
-    private String tenantName;
-    //todo
+
+    private String a1;
+    private String a2;
+    private String a3;
+    private String a4;
+    private String a5;
+    private String a6;
+    private String a7;
+    private String a8;
+    private String a9;
+
+    private String b1;
+    private String b2;
+    private String b3;
+    private String b4;
+    private String b5;
+    private String b6;
+    private String b7;
+    private String b8;
+    private String b9;
+    private String b10;
+    private String b11;
+    private String b12;
+    private String b13;
+    private String b14;
+    private String b15;
+    private String b16;
+    private String b17;
+    private String b18;
+    private String b19;
+    private String b20;
+    private String b21;
+    private String b22;
+    private String b23;
+    private String b24;
+    private String b25;
+    private String b26;
+    private String b27;
+    private String b28;
+    private String b29;
+    private String b30;
+    private String b31;
+    private String b32;
+    private String b33;
+    private String b34;
+    private String b35;
+    private String b36;
+    private String b37;
+    private String b38;
+    private String b39;
+    private String b40;
+    private String b41;
+    private String b42;
+    private String b43;
+    private String b44;
+    private String b45;
+    private String b46;
+    private String b47;
+    private String b48;
+    private String b49;
+    private String b50;
+    private String b51;
+    private String b52;
+    private String b53;
+    private String b54;
+
+    private String c1;
+    private String c2;
+    private String c3;
+    private String c4;
+    private String c5;
+    private String c6;
+    private String c7;
+    private String c8;
+    private String c9;
+    private String c10;
+    private String c11;
+    private String c12;
+    private String c13;
+    private String c14;
+    private String c15;
+    private String c16;
+    private String c17;
+    private String c18;
+    private String c19;
+    private String c20;
+    private String c21;
+    private String c22;
+    private String c23;
+    private String c24;
+    private String c25;
+    private String c26;
+    private String c27;
 }
+

+ 75 - 0
src/main/java/com/zksy/property/domain/vo/HouseInfoVo.java

@@ -0,0 +1,75 @@
+package com.zksy.property.domain.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class HouseInfoVo {
+    private String houseName;//房间名称 (A栋101)
+    private String address;//地址
+    private Boolean status ;//状态 (未租/已租)
+    private String rentRange ;//租金范围
+
+    @Data
+    @NoArgsConstructor
+    @AllArgsConstructor
+    public static class TenantInfo{
+        private String tenantName;//租户名称
+        private String tenantNumber;//联系电话
+        private String tenantIdCard;//身份证号
+        private String tenantInDate;//入住时间
+        private String tenantTime;//租期
+        private String tenantRent;//月租金
+    }
+
+    @Data
+    @NoArgsConstructor
+    @AllArgsConstructor
+    public static class HouseInfo{
+        private String area;//面积
+        private String floor;//楼层
+        private String introduce;//介绍
+        private String houseType;//房型
+    }
+
+    @Data
+    @NoArgsConstructor
+    @AllArgsConstructor
+    public static class DeviceInfo{
+        private String deviceName;//设备名称
+        private String deviceType;//设备类型
+        private String deviceBrand;//设备品牌
+        private String deviceNumber;//设备型号
+        private String devicePrice;//设备价格
+    }
+
+    @Data
+    @NoArgsConstructor
+    @AllArgsConstructor
+    public static class MaintenanceRecords{
+        private String maintenanceDate;//维护时间
+        private String maintenanceContent;//维护内容
+        private String maintenanceType;//维护类型
+        private String maintenanceCost;//维护费用
+        private String maintenancePerson;//维护人员
+        private String maintenanceStatus;//维护状态
+    }
+
+    @Data
+    @NoArgsConstructor
+    @AllArgsConstructor
+    public static class ContractInfo{
+        private String ContractNumber;//合同编号
+        private String ContractDate;//签约日期
+        private String ContractTime;//合同期限
+        private String ContractExpirationDate;//到期日期
+        private String ContractDeposit;//押金
+        private String ContractStatus;//合同状态
+    }
+
+}
+
+

+ 21 - 0
src/main/java/com/zksy/property/domain/vo/RentingInfoVo.java

@@ -0,0 +1,21 @@
+package com.zksy.property.domain.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class RentingInfoVo {
+
+    private String houseName;//房间名称 (A栋101)
+    private String address;//地址
+    private String area;//面积
+    private String floor;//楼层
+    private String introduce;//介绍(例如装修情况)
+    private Boolean status ;//状态 (未租/已租)
+    private String rentRange ;//租金范围
+
+
+}

+ 3 - 3
src/main/java/com/zksy/property/service/ARentalContractService.java

@@ -1,9 +1,9 @@
 package com.zksy.property.service;
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import com.zksy.property.domain.ARentalContract;
 import com.baomidou.mybatisplus.extension.service.IService;
-import com.zksy.utils.AjaxResult;
+import com.zksy.property.domain.ARentalContract;
+import com.zksy.property.domain.bo.RentalTempBo;
 
 import java.util.List;
 
@@ -16,5 +16,5 @@ public interface ARentalContractService extends IService<ARentalContract> {
     Page<ARentalContract> findByPage(long pageNum, long pageSize, String tenantName, String contractNumber, String contractStatus);
     List<ARentalContract> getARentalContractList(String tenantName, String contractNumber, String contractStatus);
 
-    String generatorRental(String id);
+    String generatorRental(String id, RentalTempBo bo);
 }

+ 67 - 198
src/main/java/com/zksy/property/service/impl/ARentalContractServiceImpl.java

@@ -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 "";
     }
+
 }
 
 

BIN
src/main/resources/templates/2.docx


BIN
src/main/resources/templates/3.docx


+ 105 - 1
src/test/java/com/zksy/property/service/impl/ARentalContractServiceImplTest.java

@@ -1,5 +1,6 @@
 package com.zksy.property.service.impl;
 
+import com.zksy.property.domain.bo.RentalTempBo;
 import com.zksy.property.service.ARentalContractService;
 import org.junit.jupiter.api.Test;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -13,6 +14,109 @@ class ARentalContractServiceImplTest {
 
     @Test
     void generatorRental() {
-        aRentalContractService.generatorRental("1");
+        var t = new RentalTempBo();
+        t.setA1("1");
+        t.setA2("2");
+        t.setA3("3");
+        t.setA4("4");
+        t.setA5("5");
+        t.setA6("6");
+        t.setA7("7");
+        t.setA8("8");
+        t.setA9("9");
+        aRentalContractService.generatorRental("1",t);
+    }
+
+    @Test
+    void generatorRental2() {
+        var t = new RentalTempBo();
+        t.setB1("1");
+        t.setB2("2");
+        t.setB3("3");
+        t.setB4("4");
+        t.setB5("5");
+        t.setB6("6");
+        t.setB7("7");
+        t.setB8("8");
+        t.setB9("9");
+        t.setB10("10");
+        t.setB11("11");
+        t.setB12("12");
+        t.setB13("13");
+        t.setB14("14");
+        t.setB15("15");
+        t.setB16("16");
+        t.setB17("17");
+        t.setB18("18");
+        t.setB19("19");
+        t.setB20("20");
+        t.setB21("21");
+        t.setB22("22");
+        t.setB23("23");
+        t.setB24("24");
+        t.setB25("25");
+        t.setB26("26");
+        t.setB27("27");
+        t.setB28("28");
+        t.setB29("29");
+        t.setB30("30");
+        t.setB31("31");
+        t.setB32("32");
+        t.setB33("33");
+        t.setB34("34");
+        t.setB35("35");
+        t.setB36("36");
+        t.setB37("37");
+        t.setB38("38");
+        t.setB39("39");
+        t.setB40("40");
+        t.setB41("41");
+        t.setB42("42");
+        t.setB43("43");
+        t.setB44("44");
+        t.setB45("45");
+        t.setB46("46");
+        t.setB47("47");
+        t.setB48("48");
+        t.setB49("49");
+        t.setB50("50");
+        t.setB51("51");
+        t.setB52("52");
+        t.setB53("53");
+        t.setB54("54");
+        aRentalContractService.generatorRental("2",t);
+    }
+
+    @Test
+    void generatorRental3() {
+        var t = new RentalTempBo();
+        t.setC1("1");
+        t.setC2("2");
+        t.setC3("3");
+        t.setC4("4");
+        t.setC5("5");
+        t.setC6("6");
+        t.setC7("7");
+        t.setC8("8");
+        t.setC9("9");
+        t.setC10("10");
+        t.setC11("11");
+        t.setC12("12");
+        t.setC13("13");
+        t.setC14("14");
+        t.setC15("15");
+        t.setC16("16");
+        t.setC17("17");
+        t.setC18("18");
+        t.setC19("19");
+        t.setC20("20");
+        t.setC21("21");
+        t.setC22("22");
+        t.setC23("23");
+        t.setC24("24");
+        t.setC25("25");
+        t.setC26("26");
+        t.setC27("27");
+        aRentalContractService.generatorRental("3",t);
     }
 }