Prechádzať zdrojové kódy

feat(property): 新增合同模板生成功能

- 在 ARentalContractController 中添加 generatorRental 接口
- 在 ARentalContractService接口中定义 generatorRental 方法- 实现 ARentalContractServiceImpl 中的 generatorRental 方法,支持三种合同模板的生成
- 添加相关测试用例
- 新增 RentalTempBo 类用于后续优化
- 更新 pom.xml,添加必要的依赖
nahida 10 mesiacov pred
rodič
commit
b7c2a99495

+ 14 - 0
pom.xml

@@ -53,6 +53,10 @@
             <artifactId>spring-boot-starter-data-redis</artifactId>
             <version>3.0.5</version>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>
@@ -75,5 +79,15 @@
             <artifactId>easyexcel</artifactId>
             <version>3.3.2</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+            <version>4.1.2</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-scratchpad</artifactId>
+            <version>4.1.2</version>
+        </dependency>
     </dependencies>
 </project>

+ 5 - 0
src/main/java/com/zksy/controller/property/ARentalContractController.java

@@ -72,4 +72,9 @@ public class ARentalContractController {
         ExcelExportUtil.exportExcel(response,service.getARentalContractList(tenantName,contractNumber,contractStatus), ARentalContract.class, "租赁合同管理","租赁合同管理");
     }
 
+    @GetMapping("/generatorRental")
+    @ApiOperation(value = "生成合同模版")
+    public AjaxResult generatorRental(String id) {
+        return AjaxResult.success(service.generatorRental(id));
+    }
 }

+ 13 - 0
src/main/java/com/zksy/property/domain/bo/RentalTempBo.java

@@ -0,0 +1,13 @@
+package com.zksy.property.domain.bo;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class RentalTempBo {
+    private String tenantName;
+    //todo
+}

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

@@ -3,6 +3,7 @@ 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 java.util.List;
 
@@ -14,4 +15,6 @@ import java.util.List;
 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);
 }

+ 227 - 0
src/main/java/com/zksy/property/service/impl/ARentalContractServiceImpl.java

@@ -3,12 +3,27 @@ package com.zksy.property.service.impl;
 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.ARentalContent;
 import com.zksy.property.domain.ARentalContract;
+import com.zksy.property.service.ARentalContentService;
 import com.zksy.property.service.ARentalContractService;
 import com.zksy.property.mapper.ARentalContractMapper;
+import org.apache.poi.hwpf.HWPFDocument;
+import org.apache.poi.hwpf.usermodel.Bookmarks;
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.apache.poi.xwpf.usermodel.XWPFParagraph;
+import org.apache.poi.xwpf.usermodel.XWPFRun;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.stereotype.Service;
 
+import java.io.*;
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
 * @author Administrator
@@ -19,6 +34,9 @@ import java.util.List;
 public class ARentalContractServiceImpl extends ServiceImpl<ARentalContractMapper, ARentalContract>
     implements ARentalContractService{
 
+    @Autowired
+    private ARentalContentService aRentalContentService;
+
     @Override
     public Page<ARentalContract> findByPage(long pageNum, long pageSize, String tenantName, String contractNumber, String contractStatus) {
         Page<ARentalContract> page = new Page<>(pageNum,pageSize);
@@ -39,6 +57,215 @@ public class ARentalContractServiceImpl extends ServiceImpl<ARentalContractMappe
         List<ARentalContract> list = this.list(queryWrapper);
         return list;
     }
+
+    @Override
+    public String generatorRental(String mark) {
+        switch ( mark){
+            case "1":{
+                HashMap<String, String> contentMap = new HashMap<>();
+                contentMap.put("a1", "张三");
+                contentMap.put("a2", "123456789");
+
+                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;
+                            }
+
+                            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);
+                }
+                break;
+            }
+            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++;
+                        }
+                    }
+
+                    // 可选:保存修改后的文档到新文件
+                    try (FileOutputStream outputStream = new FileOutputStream("C:\\Users\\hxb\\Downloads\\已替换合同.docx")) {
+                        document.write(outputStream);
+                    }
+
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+                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);
+                }
+                break;
+            }
+
+        }
+        return "";
+    }
 }
 
 

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


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


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


+ 18 - 0
src/test/java/com/zksy/property/service/impl/ARentalContractServiceImplTest.java

@@ -0,0 +1,18 @@
+package com.zksy.property.service.impl;
+
+import com.zksy.property.service.ARentalContractService;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+@SpringBootTest
+class ARentalContractServiceImplTest {
+
+    @Autowired
+    private ARentalContractService aRentalContractService;
+
+    @Test
+    void generatorRental() {
+        aRentalContractService.generatorRental("3");
+    }
+}