liujie
2026-04-01 4b33796be3887c81fa40fba2f55dce4489cff041
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package com.estate.expert.admin.stat.report.data;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.estate.expert.admin.domain.business.device.Device;
import com.estate.expert.admin.domain.exam.eval.fuzzy.FuzzyValue;
import com.estate.expert.admin.domain.exam.record.error.ErrorLevel;
import com.estate.expert.admin.domain.exam.record.error.ErrorRecord;
import com.estate.expert.admin.domain.misc.UserFile;
import com.estate.expert.admin.domain.project.Project;
import com.estate.expert.admin.domain.project.info.ProjectInfo;
import com.estate.expert.admin.domain.report.ReportTemplate;
import com.estate.expert.admin.utils.tree.TreeNode;
import lombok.experimental.SuperBuilder;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 报告数据基类
 * 这里将原始的数据,通过JSON的形式进行组织
 * 便于后续的模板渲染
 */
@SuperBuilder
public abstract class ReportDataBase {
 
    ReportTemplate template;
 
    List<Device> devices;
 
    Project project;
 
    ProjectInfo projectInfo;
 
    private int reportId;
 
    /**
     * 生成报告数据
     * @return 报告数据
     */
    public JSONObject generate() {
        var result = buildReportInfo();
        result.put("工程概况", buildProjectInfo());
        result.put("设备信息", buildDeviceInfo());
        result.put("评估结果", buildEvalInfo());
        return result;
    }
 
    /**
     * 构建评估信息
     * @return 评估信息
     */
    protected abstract JSONObject buildEvalInfo();
 
    /**
     * 构建报告信息
     * @return 报告信息
     */
    protected JSONObject buildReportInfo() {
        JSONObject result = new JSONObject();
        result.put("项目名称", project.getName());
        result.put("报告编号", template.getNumber() + "-" + String.format("%08d", reportId));
        result.put("报告日期", LocalDate.now());
        result.put("公司名称", "四川信立隆第三方评估咨询有限公司");
        return result;
    }
 
    /**
     * 构建设备信息
     * @return 设备信息
     */
    protected List<Object> buildDeviceInfo() {
        List<Object> deviceInfos = new ArrayList<>();
        // 按照设备类型排序
        var sortedDevices = devices.stream()
                .sorted(Comparator.comparing(device -> device.getType() != null ? device.getType().ordinal() : Integer.MAX_VALUE))
                .collect(Collectors.toList());
        for (var device : sortedDevices) {
            JSONObject deviceInfo = new JSONObject();
            deviceInfo.put("设备类型", device.getType().getDesc());
            deviceInfo.put("设备编号", device.getManufacturerNumber());
            deviceInfo.put("设备租赁单位", device.getProperties().get("工程起重机械租赁单位"));
            deviceInfo.put("设备使用单位", device.getProperties().get("使用单位"));
            deviceInfo.put("安拆单位", device.getProperties().get("工程起重机械安拆单位"));
            deviceInfo.put("检测单位", device.getProperties().get("工程起重机械维保单位"));
            deviceInfos.add(deviceInfo);
        }
        return deviceInfos;
    }
 
    /**
     * 构建错误级别
     * @param node 节点
     * @return 错误级别
     */
    protected JSONObject buildErrorLevel(TreeNode<FuzzyValue> node) {
        JSONObject result = new JSONObject();
        if(node == null)
            return result;
 
        int totalErrors = Arrays.stream(node.getData().getTotalErrorCounts()).sum();
        result.put("隐患数量", totalErrors);
        List<Object> errorInfos = new ArrayList<>();
        for (int i = 0; i<4 ; i++) {
            var level = ErrorLevel.values()[i];
            var count = node.getData().getTotalErrorCounts()[i];
            var info = new JSONObject();
            info.put("隐患等级", level.getDesc());
            info.put("隐患程度", level.getDesc2());
            info.put("数量", count);
            info.put("比例", formatPercent(count * 1.0 / totalErrors));
            errorInfos.add(info);
        }
        result.put("分项", errorInfos);
        return result;
    }
 
    /**
     * 构建项目信息
     * @return 项目信息
     */
    protected JSONObject buildProjectInfo() {
        JSONObject info = new JSONObject();
        info.put("工程名称", project.getName());
        if(project.getAddressInfo() != null)
            info.put("工程地址", project.getAddressInfo().getFullAddress());
 
        if(project.getDevelopmentCompanyInfo() != null) {
            info.put("建设单位", project.getDevelopmentCompanyInfo().getName());
            info.put("项目负责人", project.getDevelopmentCompanyInfo().getLeader());
        }
 
        if(project.getSupervisorCompanyInfo() != null){
            info.put("监理单位", project.getSupervisorCompanyInfo().getName());
            info.put("项目总监", project.getSupervisorCompanyInfo().getLeader());
        }
 
        if(project.getConstructionCompanyInfo() != null){
            info.put("施工单位", project.getConstructionCompanyInfo().getName());
            info.put("项目经理", project.getConstructionCompanyInfo().getLeader());
        }
 
        info.put("工程类型", project.getType().getDesc());
        info.put("工程规模", project.getProjectCost());
 
        if (projectInfo != null) {
            info.put("项目简介", projectInfo.getDetail());
            if(projectInfo.getDangerousOptions() != null) {
                var dangerStr = "";
 
                var d0 = Arrays.stream(projectInfo.getDangerousOptions()).map(s -> s.getDesc() + ",").collect(Collectors.joining());
                if(StringUtils.hasText(d0))
                    dangerStr += "危险性较大工程:[" + d0 + "]";
 
                var d1 = Arrays.stream(projectInfo.getSuperDangerousOptions()).map(s -> s.getDesc() + ",").collect(Collectors.joining());
                if(StringUtils.hasText(d1))
                    dangerStr += "\n超过一定规模的危险性较大工程:[" + d1 + "]";
 
                dangerStr = dangerStr.replace(",]", "]");
                info.put("危险性", dangerStr);
            }
            info.put("近一季度施工进展情况", projectInfo.getProgress());
        }
 
        return info;
    }
 
 
    /**
     * 去除字符串首尾空白字符
     */
    private String trimText(String text) {
        return StringUtils.hasText(text) ? text.trim() : text;
    }
 
    /**
     * 构建记录详情
     * @param record 记录
     * @return 记录详情
     */
    protected JSONObject buildRecordDetail(ErrorRecord record) {
        var problem = record.getProblem();
        JSONObject subItem = new JSONObject();
        if(record.getDevice() != null)
            subItem.put("设备编号", record.getDevice().getManufacturerNumber());
        subItem.put("隐患等级", record.getLevel().getDesc());
        subItem.put("问题部位", record.getProblemPart());
        subItem.put("问题备注", trimText(StringUtils.hasText(record.getRemark()) ? record.getRemark() : problem.getDescription()));
        subItem.put("建议措施", trimText(record.getSuggestion()));
        subItem.put("问题图片", buildImage(record.getImages()));
        subItem.put("问题描述", trimText(StringUtils.hasText(problem.getDescription()) ? problem.getDescription() : record.getRemark()));
 
        if(!CollectionUtils.isEmpty(problem.getSpecificTerms())){
            StringBuilder sb = new StringBuilder();
            var terms = CollectionUtils.isEmpty(record.getSpecificTermIndexes())
                    ? problem.getSpecificTerms()
                    : record.getSpecificTermIndexes().stream()
                    .filter(idx -> idx < problem.getSpecificTerms().size())
                    .map(problem.getSpecificTerms()::get)
                    .collect(Collectors.toList());
 
            for(int i = 0; i < terms.size(); i++) {
                sb.append(terms.get(i));
                if(i < terms.size() - 1) {
                    sb.append("\n");
                }
            }
            subItem.put("违反条款", sb.toString());
        }
 
        if(!problem.getNumberFactors().isEmpty()){
            var numberFactorIndex = Math.min(record.getNumberFactorIndex(), problem.getNumberFactors().size() - 1);
            subItem.put("问题数量", problem.getNumberFactors().get(numberFactorIndex).getName());
        }
 
        if(!problem.getScaleFactors().isEmpty()){
            var scaleFactorIndex = Math.min(record.getScaleFactorIndex(), problem.getScaleFactors().size() - 1);
            subItem.put("问题规模", problem.getScaleFactors().get(scaleFactorIndex).getName());
        }
        return subItem;
    }
 
    /**
     * 构建图片
     * @param images 图片
     * @return 图片
     */
    private JSONArray buildImage(List<UserFile> images) {
        if(CollectionUtils.isEmpty(images))
            return new JSONArray();
        return images.stream().map(s -> {
            Map<String, Integer> map = new HashMap<>();
            map.put("ID", s.getId());
            return map;
        }).collect(Collectors.toCollection(JSONArray::new));
    }
 
    /**
     * 格式化百分比
     * @param value 值
     * @return 百分比
     */
    protected String formatPercent(double value) {
        return String.format("%.2f%%", value * 100);
    }
 
    /**
     * 格式化浮点数
     * @param value 值
     * @return 浮点数
     */
    protected String formatFloat(double value) {
        return String.format("%.2f", value);
    }
 
    /**
     * 格式化浮点数
     * @param value 值
     * @return 浮点数
     */
    protected String formatFloatF4(double value) {
        return String.format("%.4f", value);
    }
 
}