liujie
2026-09-20 25999c575dd42b4715e49d3fe57e73c2c3204ccb
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
package com.estate.expert.admin.stat.report.data;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.estate.expert.admin.domain.exam.eval.fuzzy.FuzzyResult;
import com.estate.expert.admin.domain.exam.eval.fuzzy.FuzzyValue;
import com.estate.expert.admin.domain.exam.eval.score.ScoreResult;
import com.estate.expert.admin.domain.exam.eval.score.ScoreValue;
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 lombok.extern.slf4j.Slf4j;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * 第三方评估报告数据
 */
@Slf4j
@SuperBuilder
public abstract class SystemReportData extends ReportDataBase {
 
    List<ErrorRecord> records;
 
    TreeRoot<HierarchyData> systemHierarchy;
 
    protected FuzzyResult sysResult;
 
    protected ScoreResult scoreResult;
 
    Map<Integer, Long> problemHazardCounts;
 
    long projectCheckCount;
 
    // 评估结果
    @Override
    protected JSONObject buildEvalInfo() {
        JSONObject evalInfo = new JSONObject();
 
        evalInfo.put("综合信息", buildAbstractInfo());
        evalInfo.put("检查项", buildCheckItems());
        evalInfo.put("隐患等级分布", buildErrorLevel(getErrorLevelCounts()));
 
        return evalInfo;
    }
 
    /**
     * 获取报告使用的隐患等级计数。
     * 默认使用模糊结果,评分报告可按需使用评分结果。
     */
    protected int[] getErrorLevelCounts() {
        var root = getFuzzyRoot();
        if (root == null || root.getData() == null) {
            return null;
        }
        return root.getData().getTotalErrorCounts();
    }
 
    /**
     * 获取模糊根节点
     * @return 模糊根节点
     */
    protected TreeNode<FuzzyValue> getFuzzyRoot(){
        if(sysResult == null)
            return null;
        return sysResult.root();
    }
 
    protected TreeNode<ScoreValue> getScoreRoot(){
        if(scoreResult == null)
            return null;
        return scoreResult.root();
    }
 
    /**
     * 根据路径获取对应的模糊节点
     * @param path 节点路径
     * @return 模糊节点,如果不存在则返回null
     */
    protected TreeNode<FuzzyValue> getFuzzyNodeByPath(String path) {
        if(sysResult == null || path == null)
            return null;
        return sysResult.get(path);
    }
 
    /**
     * 构建摘要信息
     * @return 摘要信息
     */
    protected abstract JSONObject buildAbstractInfo();
 
    /**
     * 构建节点
     * @param info 信息
     * @param node 节点
     */
    protected abstract void buildNode(JSONObject info, TreeNode<ScoreValue> node);
 
    /**
     * 构建检查项
     * @return 检查项
     */
    protected JSONArray buildCheckItems(){
        var root = getScoreRoot();
        var result = new JSONArray();
        if(root == null)
            return result;
        root.getChildren().values().forEach(node -> {
            var item = buildCheckItem(node);
            if(item!=null)
                result.add(item);
        });
        return result;
    }
 
    // 处理检查项
    /**
     * 构建检查项
     * @param node 节点
     * @return 检查项
     */
    private JSONObject buildCheckItem(TreeNode<ScoreValue> node){
        if(node == null || node.getData().getTotalErrorCounts() == null)
            return null;
 
        var totalErrors = Arrays.stream(node.getData().getTotalErrorCounts()).sum();
        if(totalErrors == 0)
            return null;
 
        JSONObject result = new JSONObject();
        var name = node.getName();
        result.put("名称", name);
        result.put("问题清单标题", buildProblemListTitle(name));
        buildNode(result, node);
 
        var cfuzzyNode = getFuzzyNodeByPath(node.getPath());
        if(cfuzzyNode != null){
            result.put("模糊评价", cfuzzyNode.getData().getRiskLevel().getDesc());
            result.put("模糊得分", formatFloat(cfuzzyNode.getData().getScore()));
        }
 
        var categoryItems = new ArrayList<>();
        AtomicInteger index = new AtomicInteger();
        List<Object> itemInfos = new ArrayList<>();
 
        for(var child : node.getChildren().values()) {
            for(var c : child.getChildren().values()) {
                for(var n : c.getChildren().values()) {
                    var errorInfos = buildErrorInfo(n.getPath());
//                    if(CollectionUtils.isEmpty(errorInfos))
//                        continue;
 
                    JSONObject info = new JSONObject();
                    info.put("类别", n.getName());
                    info.put("序号", index.getAndIncrement() + 1);
                    buildNode(info, n);
 
                    // 从fuzzyNode中获取评价
                    var fuzzyNode = getFuzzyNodeByPath(n.getPath());
                    if(fuzzyNode != null && fuzzyNode.getData() != null && fuzzyNode.getData().getRiskLevel() != null) {
                        info.put("评价", fuzzyNode.getData().getRiskLevel().getDesc());
                        info.put("模糊得分", formatFloat(fuzzyNode.getData().getScore()));
                    } else {
                        info.put("评价", n.getData().getScore());
                        info.put("模糊得分", "-");
                    }
 
                    int errorCnt = Arrays.stream(n.getData().getTotalErrorCounts()).sum();
                    info.put("问题数量", errorCnt);
                    info.put("问题百分比", formatPercent(errorCnt * 1.0 / totalErrors));
 
                    info.put("问题", errorInfos);
                    itemInfos.add(info);
                }
            }
        }
 
        JSONObject categoryItem = new JSONObject();
        categoryItem.put("类别", "合并项目");
        categoryItem.put("分项", itemInfos);
        categoryItems.add(categoryItem);
 
        result.put("问题合计数量", totalErrors);
        result.put("分项评估", categoryItems);
        return result;
    }
 
    private String buildProblemListTitle(String name) {
        if (name == null || name.isBlank()) {
            return "问题清单";
        }
        if ("安全检查".equals(name)) {
            return "安全问题清单";
        }
        return name + "问题清单";
    }
 
    /**
     * 构建错误信息
     * @param nodePath 节点路径
     * @return 错误信息
     */
    protected List<Object> buildErrorInfo(String nodePath) {
        var suffix = nodePath + "/";
        var errors = new ArrayList<>();
        var nodeRecords = records.stream()
                .filter(record -> record.getProblem() != null &&
                        record.getProblem().getHierarchyPath() != null &&
                        record.getProblem().getHierarchyPath().startsWith(suffix))
                // 按照record.errorLevel排序
                .sorted(Comparator.comparing(ErrorRecord::getLevel, Comparator.nullsLast(Comparator.naturalOrder())))
                .toList();
 
        for(var record : nodeRecords){
            var subItem = buildRecordDetail(record);
            subItem.put("项目级别", errorNodeName(record));
            subItem.put("同类隐患出现频次", getProblemHazardCount(record));
            subItem.put("检查频次", getProblemCheckCount(record));
            errors.add(subItem);
        }
        return errors;
    }
 
    private long getProblemHazardCount(ErrorRecord record) {
        var problemId = record != null && record.getProblem() != null ? record.getProblem().getId() : null;
        if (problemId == null || problemHazardCounts == null) {
            return 0L;
        }
        return problemHazardCounts.getOrDefault(problemId, 0L);
    }
 
    private long getProblemCheckCount(ErrorRecord record) {
        return Math.max(projectCheckCount, 0L);
    }
 
    /**
     * 构建错误节点名称
     * @param record 记录
     * @return 错误节点名称
     */
    protected String errorNodeName(ErrorRecord record){
        var hierarchyPath = record.getProblem().getHierarchyPath();
        return systemHierarchy.get(hierarchyPath).getName();
    }
}