liujie
2026-05-16 2e2ecd28d1bb9c976aa163af2f201af0a3320f3f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.estate.expert.admin.service;
 
import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.estate.expert.admin.domain.report.ReportTemplateType;
import com.estate.expert.admin.stat.report.builder.DeviceReportBuilder;
import com.estate.expert.admin.stat.report.builder.SystemReportBuilder;
import com.estate.expert.admin.stat.report.render.CheckInfoTablePolicy;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Map;
 
@Slf4j
@SpringBootTest
@ExtendWith(SpringExtension.class)
@Transactional
public class ReportBuilderTest {
 
    @Resource
    UserFileService userFileService;
 
    @Test
    public void testDoc() throws IOException {
        // load file from resource
        var resource = getClass().getClassLoader().getResourceAsStream("templates/test.docx");
 
        ConfigureBuilder builder = Configure.builder();
        builder.bind("detail_table", new CheckInfoTablePolicy());
 
        var data = JSONObject.parseObject(Files.readString(new File("/Volumes/Works/tmp/system_report.json").toPath()));
        var checkInfos = data.getJSONObject("评估结果").getJSONObject("安全检查").getJSONArray("分项评估");
 
        XWPFTemplate template = XWPFTemplate.compile(resource, builder.build()).render(
                Map.of("detail_table", checkInfos)
        );
        template.writeAndClose(new FileOutputStream("/Volumes/Works/tmp/output.docx"));
    }
 
    @Test
    public void testSystemReport() throws IOException {
        var data = JSONObject.parseObject(Files.readString(new File("/Volumes/Works/tmp/system_report.json").toPath()));
        XWPFTemplate template = new SystemReportBuilder(userFileService).buildReport(data, ReportTemplateType.ThirdPartyReport.getConfig());;
        template.writeAndClose(new FileOutputStream("/Volumes/Works/tmp/system_report.docx"));
    }
 
    @Test
    public void testDeviceReport() throws IOException {
        var data = JSONObject.parseObject(Files.readString(new File("/Volumes/Works/tmp/device_report.json").toPath()));
        XWPFTemplate template = new DeviceReportBuilder(userFileService).buildReport(data, null);
        template.writeAndClose(new FileOutputStream("/Volumes/Works/tmp/device_report.docx"));
    }
 
    @Test
    public void testEmptyDeviceReport() throws IOException {
        XWPFTemplate template = new DeviceReportBuilder(userFileService).buildReport(new JSONObject(), null);
        template.writeAndClose(new FileOutputStream("/Volumes/Works/tmp/empty_device_report.docx"));
    }
}