liujie
2026-05-14 38346fde0ab9a9ce98ed83bea612cd6825378812
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.estate.expert.admin.stat.report.builder;
 
import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.estate.expert.admin.service.UserFileService;
import com.estate.expert.admin.stat.report.render.CustomPictureRenderPolicy;
import com.estate.expert.admin.stat.report.render.DeviceTablePolicy;
import org.springframework.core.io.ClassPathResource;
 
import java.io.IOException;
import java.util.Map;
 
 
/**
 * 设备风险评估报告构建器
 */
public class DeviceReportBuilder implements ReportBuilderBase {
 
    private static final String DEFAULT_TEMPLATE_FILE = "设备风险评估报告.docx";
 
    protected UserFileService userFileService;
    private final String templateFileName;
 
    public DeviceReportBuilder(UserFileService userFileService) {
        this(userFileService, DEFAULT_TEMPLATE_FILE);
    }
 
    public DeviceReportBuilder(UserFileService userFileService, String templateFileName) {
        this.userFileService = userFileService;
        this.templateFileName = templateFileName;
    }
 
    /**
     * 构建设备风险评估报告
     * @param data 数据
     * @param templateConfig 模板配置
     * @return 设备风险评估报告
     */
    @Override
    public XWPFTemplate buildReport(JSONObject data, Map<String, Integer> templateConfig) {
        var res = new ClassPathResource("templates/" + templateFileName);
 
        if (res.getPath().contains("new")) {
            return buildNewReport(res, data);
        }
        return buildDefaultReport(res, data);
    }
 
    private XWPFTemplate buildDefaultReport(ClassPathResource res, JSONObject data) {
        // 将data.评估结果.分类评估.详情 复制
        // 避免渲染的时候的策略冲突
        copyCategoryDetails(data);
 
        ConfigureBuilder builder = Configure.builder();
        builder.bind("data.设备信息", new LoopRowTableRenderPolicy());
        builder.bind("data.评估结果.分类评估", new LoopRowTableRenderPolicy());
        builder.bind("data.评估结果.分类评估.详情2", new LoopRowTableRenderPolicy());
        builder.bind("data.评估结果.隐患等级分布汇总.分项", new LoopRowTableRenderPolicy());
        builder.bind("分项隐患", new LoopRowTableRenderPolicy());
        builder.bind("分项问题", new DeviceTablePolicy());
//        builder.bind("data.设备信息", new DeviceInfoTablePolicy());
 
        builder.addPlugin('%', new CustomPictureRenderPolicy(userFileService));
 
        return compileAndRender(res, builder, data);
    }
 
    private XWPFTemplate buildNewReport(ClassPathResource res, JSONObject data) {
        copyCategoryDetails(data);
 
        ConfigureBuilder builder = Configure.builder();
        builder.bind("data.设备信息", new LoopRowTableRenderPolicy());
        builder.bind("data.设备分组信息", new LoopRowTableRenderPolicy());
        builder.bind("data.评估结果.设备得分情况", new LoopRowTableRenderPolicy());
        builder.bind("详情", new LoopRowTableRenderPolicy());
        builder.bind("data.评估结果.分类评估", new LoopRowTableRenderPolicy());
        builder.bind("data.评估结果.分类评估.详情2", new LoopRowTableRenderPolicy());
        builder.bind("data.评估结果.隐患等级分布汇总.分项", new LoopRowTableRenderPolicy());
        builder.bind("分项隐患", new LoopRowTableRenderPolicy());
        builder.bind("分项问题", new DeviceTablePolicy());
        builder.addPlugin('%', new CustomPictureRenderPolicy(userFileService));
 
        return compileAndRender(res, builder, data);
    }
 
    private void copyCategoryDetails(JSONObject data) {
        if (data.containsKey("评估结果") && data.getJSONObject("评估结果").containsKey("分类评估")) {
            var evalResult = data.getJSONObject("评估结果").getJSONObject("分类评估");
            var details = evalResult.getJSONArray("详情");
            evalResult.put("详情2", details);
        }
    }
 
    private XWPFTemplate compileAndRender(ClassPathResource res, ConfigureBuilder builder, JSONObject data) {
        try {
            return XWPFTemplate.compile(res.getInputStream(), builder.build()).render(
                    Map.of("data", data)
            );
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}