liujie
2026-01-21 8d1d546ad729da0e809e17e8161376adcc662c96
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
269
270
271
272
273
274
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.business.device.DeviceType;
import com.estate.expert.admin.domain.exam.eval.fuzzy.FuzzyResult;
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.standard.hierachy.HierarchyData;
import com.estate.expert.admin.utils.tree.TreeNode;
import com.estate.expert.admin.utils.tree.TreeRoot;
import lombok.experimental.SuperBuilder;
import org.springframework.util.StringUtils;
 
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
 
/**
 * 设备风险评估报告数据
 */
@SuperBuilder
public abstract class DeviceReportData extends ReportDataBase {
 
    List<ErrorRecord> records;
 
    protected Map<Integer, FuzzyResult> deviceResMap;
 
    TreeRoot<HierarchyData> deviceHierarchy;
 
    /**
     * 生成报告数据
     * @return 报告数据
     */
    public JSONObject generate() {
        var result = buildReportInfo();
        result.put("工程概况", buildProjectInfo());
        result.put("设备信息", buildDeviceInfo());
        result.put("评估结果", buildEvalInfo());
        return result;
    }
 
    /**
     * 构建评估信息
     * @return 评估信息
     */
    @Override
    protected JSONObject buildEvalInfo() {
        JSONObject evalInfo = new JSONObject();
 
        evalInfo.put("分类评估", buildDeviceTypeCheckItem());
        evalInfo.put("隐患等级分布汇总", buildDeviceErrorLevels());
        return evalInfo;
    }
 
    // 按照设备类型分组
    /**
     * 构建设备类型检查项
     * @return 设备类型检查项
     */
    private JSONObject buildDeviceTypeCheckItem()
    {
        JSONObject result = new JSONObject();
        var details = new ArrayList<>();
        float totalScore = 0;
        int totalTypes = 0;
        int totalDevices = 0;
        for (var deviceType : DeviceType.values()) {
            var detail = buildDeviceTypeCheckDetail(deviceType);
            if (detail != null) {
                details.add(detail);
                totalTypes++;
                totalScore += Float.parseFloat((String) detail.get("分数"));
                totalDevices += (int) detail.get("数量");
            }
        }
        result.put("详情", details);
        result.put("总数", totalTypes);
        result.put("设备总数", totalDevices);
        result.put("总分", formatFloat(totalScore / totalTypes));
        return result;
    }
 
 
    /**
     * 获取设备分数
     * @param deviceId 设备ID
     * @return 设备分数
     */
    protected abstract float getDeviceScore(Integer deviceId);
 
    /**
     * 构建设备类型检查项详情
     * @param deviceType 设备类型
     * @return 设备类型检查项详情
     */
    private JSONObject buildDeviceTypeCheckDetail(DeviceType deviceType) {
        int totalDevice = 0;
        float totalScore = 0;
        List<Object> deviceCheckItems = new ArrayList<>();
        JSONArray records = new JSONArray();
        int [] totalErrors = new int[4];
 
        for(var deviceId: deviceResMap.keySet()){
            var device = devices.stream().filter(d -> d.getId().equals(deviceId)).findFirst().orElse(null);
            if (device == null || device.getType() != deviceType)
                continue;
 
            // 汇总分数
            var deviceData = deviceResMap.get(deviceId).root().getData();
            totalScore += getDeviceScore(deviceId);
            totalDevice++;
            for(int i = 0; i < 4; i++){
                totalErrors[i] += deviceData.getTotalErrorCounts()[i];
            }
 
            var c = buildDeviceErrorLevels(device);
            if(c != null){
                deviceCheckItems.addAll(c);
            }
 
            var r = buildDeviceErrorRecords(device);
            if(r != null){
                records.addAll(r);
            }
        }
 
        if(totalDevice > 0){
            JSONObject info = new JSONObject();
            info.put("设备类型", deviceType.getDesc());
            info.put("数量", totalDevice);
            info.put("分数", formatFloat(totalScore / totalDevice));
            info.put("分项隐患", deviceCheckItems);
            info.put("分项问题", records);
            info.put("一级隐患", totalErrors[0]);
            info.put("二级隐患", totalErrors[1]);
            info.put("三级隐患", totalErrors[2]);
            info.put("四级隐患", totalErrors[3]);
            return info;
        }
        return null;
    }
 
    /**
     * 构建设备错误级别
     * @return 设备错误级别
     */
    private JSONObject buildDeviceErrorLevels(){
        int totalErrors = 0;
        var totalErrorCounts = new int[4];
 
        for (var device : devices) {
            var deviceRes = deviceResMap.get(device.getId());
            if (deviceRes == null) {
                continue;
            }
            var deviceErrorCounts = deviceRes.root().getData().getTotalErrorCounts();
            for (int i = 0; i < 4; i++) {
                totalErrorCounts[i] += deviceErrorCounts[i];
                totalErrors += deviceErrorCounts[i];
            }
        }
 
        List<Object> errorInfos = new ArrayList<>();
        for (int i = 0; i< 4; i++) {
            var errorLevel = ErrorLevel.values()[i];
            var count = totalErrorCounts[i];
            var info = new TreeMap<>();
            info.put("隐患等级", errorLevel.getDesc());
            info.put("隐患程度", errorLevel.getDesc2());
            info.put("数量", count);
            info.put("比例", formatPercent(count * 1.0 / totalErrors));
            errorInfos.add(info);
        }
 
        JSONObject result = new JSONObject();
        result.put("隐患数量", totalErrors);
        result.put("分项", errorInfos);
        return result;
    }
 
    // 按照设备分组
    /**
     * 构建设备错误级别
     * @param device 设备
     * @return 设备错误级别
     */
    private List<Object> buildDeviceErrorLevels(Device device){
        var deviceRes = deviceResMap.get(device.getId());
        if (deviceRes == null) {
            return null;
        }
        List<Object> result = new ArrayList<>();
 
        JSONObject info = new JSONObject();
        info.put("设备编号", device.getManufacturerNumber());
 
        var errorCounts = deviceRes.root().getData().getTotalErrorCounts();
        info.put("一级隐患", errorCounts[0]);
        info.put("二级隐患", errorCounts[1]);
        info.put("三级隐患", errorCounts[2]);
        info.put("四级隐患", errorCounts[3]);
        result.add(info);
        return result;
    }
 
    // 按照设备分组
    /**
     * 构建设备错误记录
     * @param device 设备
     * @return 设备错误记录
     */
    private List<Object> buildDeviceErrorRecords(Device device){
        var deviceRes = deviceResMap.get(device.getId());
        if (deviceRes == null) {
            return null;
        }
        List<Object> result = new ArrayList<>();
 
        JSONObject info = new JSONObject();
        info.put("设备编号", device.getManufacturerNumber());
 
        info.put("问题", buildDeviceErrorInfo(device));
        result.add(info);
        return result;
    }
 
    /**
     * 构建设备错误信息
     * @param device 设备
     * @return 设备错误信息
     */
    private List<Object> buildDeviceErrorInfo(Device device){
        var deviceRecords = buildDeviceRecords(device);
        List<Object> result = new ArrayList<>();
        for (var record : deviceRecords) {
            JSONObject info = new JSONObject();
            // 检查问题列:默认为问题备注,当问题备注为空时,填写问题描述(与安全报告一致)
            info.put("检查问题", StringUtils.hasText(record.getRemark()) ? record.getRemark() : record.getProblem().getDescription());
            info.put("隐患等级", record.getLevel().getDesc());
            var subItem =  buildRecordDetail(record);
            subItem.put("项目级别", errorNode(record).getName());
            info.put("问题详情", subItem);
            result.add(info);
        }
        return result;
    }
 
    /**
     * 构建设备记录
     * @param device 设备
     * @return 设备记录
     */
    private List<ErrorRecord> buildDeviceRecords(Device device){
        return records.stream()
                .filter(r -> r.getDevice().getId().equals(device.getId()))
                .sorted(Comparator.comparing(r -> r.getLevel() != null ? r.getLevel().ordinal() : ErrorLevel.P4.ordinal()))
                .toList();
    }
 
 
    /**
     * 构建错误节点
     * @param record 错误记录
     * @return 错误节点
     */
    protected TreeNode<HierarchyData> errorNode(ErrorRecord record){
        var hierarchyPath = record.getProblem().getHierarchyPath();
        return deviceHierarchy.get(hierarchyPath);
    }
}