liujie
2026-05-20 7dae6f1ae2a3c6ba6c27ca7508e4d23ddb08cede
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
package com.estate.expert.admin.io.processor.impl.standard;
 
import com.estate.expert.admin.domain.standard.problem.Problem;
import com.estate.expert.admin.domain.standard.problem.detail.NumberFactor;
import com.estate.expert.admin.domain.standard.problem.detail.ScaleFactor;
import com.estate.expert.admin.domain.standard.problem.enums.IncidentType;
import com.estate.expert.admin.io.processor.RowDataProcessorBase;
import com.estate.expert.admin.service.HierarchyService;
import com.estate.expert.admin.utils.ExcelParseUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
 
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 问题处理器
 * 
 * 用于处理问题信息的读取和写入
 * - 综合体系(ID=1):第6层叶子节点
 * - 其他体系(ID!=1):第4层叶子节点
 * 
 * Excel 格式(参考 ImportProblemJob.java,共41列):
 * 0-9列:基础信息(10列)
 * 10-14列:数量因子名称(5列)
 * 15-19列:数量因子对应数值(5列)
 * 20-24列:数量因子扣除分数(5列)
 * 25-29列:数量因子附加分数(5列)
 * 30-34列:预留(5列)
 * 35-37列:比例因子名称(3列)
 * 38-40列:比例因子系数(3列)
 */
@AllArgsConstructor
public class ProblemProcessor extends RowDataProcessorBase<Problem> {
 
    private HierarchyService hierarchyService;
    private Integer hierarchyId;
 
    @Override
    protected String[] getHeaders() {
        return new String[]{
                // 基础信息(0-9列)
                "描述", "规范与条款", "建议措施", "数值", "事故类型", 
                "点评范式", "责任人", "责任部门", "事故案例", "正确做法",
                
                // 数量因子(10-29列)
                // 问题数量集合(10-14列)
                "问题数量集合", "问题数量集合", "问题数量集合", "问题数量集合", "问题数量集合",
                // 对应数值(Rn)(15-19列)
                "对应数值(Rn)", "对应数值(Rn)", "对应数值(Rn)", "对应数值(Rn)", "对应数值(Rn)",
                // 扣除分数(20-24列)
                "扣除分数", "扣除分数", "扣除分数", "扣除分数", "扣除分数",
                // 附加分数(25-29列)
                "附加分数", "附加分数", "附加分数", "附加分数", "附加分数",
                
                // 预留(30-34列)
                "", "", "", "", "",
                
                // 比例因子(35-40列)
                // 对应规模集合/规模名称(35-37列)
                "规模名称", "规模名称", "规模名称",
                // 对应规模系数(38-40列)
                "对应规模系数", "对应规模系数", "对应规模系数"
        };
    }
 
    @Override
    public String getName() {
        return "问题信息";
    }
 
    @Override
    protected List<String> buildRowData(Problem data) {
        List<String> row = new ArrayList<>();
        
        // 基础信息(0-9列)
        row.add(data.getDescription() != null ? data.getDescription() : "");
        row.add(data.getSpecificTerms() != null && !data.getSpecificTerms().isEmpty() ? String.join("; ", data.getSpecificTerms()) : "");
        row.add(data.getSuggestActions() != null && !data.getSuggestActions().isEmpty() ? String.join("; ", data.getSuggestActions()) : "");
        row.add(data.getNumValue() != null ? data.getNumValue().toString() : "");
        row.add(data.getIncidentTypes() != null && !data.getIncidentTypes().isEmpty() 
                ? data.getIncidentTypes().stream().map(IncidentType::getName).collect(Collectors.joining("; ")) : "");
        row.add(data.getReviewParadigm() != null ? data.getReviewParadigm() : "");
        row.add(data.getResponsiblePerson() != null ? data.getResponsiblePerson() : "");
        row.add(data.getResponsibleDepartment() != null ? data.getResponsibleDepartment() : "");
        row.add(""); // 事故案例(暂不支持)
        row.add(data.getCorrectPractice() != null ? data.getCorrectPractice() : "");
        
        // 数量因子(10-29列)- 最多5组
        List<NumberFactor> numberFactors = data.getNumberFactors() != null ? data.getNumberFactors() : new ArrayList<>();
        // 名称(10-14列)
        for(int i = 0; i < 5; i++) {
            row.add(i < numberFactors.size() ? numberFactors.get(i).getName() : "");
        }
        // 对应数值(15-19列)
        for(int i = 0; i < 5; i++) {
            row.add(i < numberFactors.size() ? String.valueOf(numberFactors.get(i).getValue()) : "");
        }
        // 扣除分数(20-24列)
        for(int i = 0; i < 5; i++) {
            row.add(i < numberFactors.size() ? String.valueOf(numberFactors.get(i).getDeductPoint()) : "");
        }
        // 附加分数(25-29列)
        for(int i = 0; i < 5; i++) {
            row.add(i < numberFactors.size() ? String.valueOf(numberFactors.get(i).getAdditionPoint()) : "");
        }
        
        // 预留(30-34列)
        for(int i = 0; i < 5; i++) {
            row.add("");
        }
        
        // 比例因子(35-40列)- 最多3组
        List<ScaleFactor> scaleFactors = data.getScaleFactors() != null ? data.getScaleFactors() : new ArrayList<>();
        // 名称(35-37列)
        for(int i = 0; i < 3; i++) {
            row.add(i < scaleFactors.size() ? scaleFactors.get(i).getName() : "");
        }
        // 系数(38-40列)
        for(int i = 0; i < 3; i++) {
            row.add(i < scaleFactors.size() ? String.valueOf(scaleFactors.get(i).getFactor()) : "");
        }
        
        return row;
    }
 
    @Override
    protected Problem parseRowData(int rowIndex, List<String> rowData) {
        var index = 0;
        var problem = new Problem();
 
        // 基础信息(0-9列)
        problem.setDescription(ExcelParseUtils.parseString(rowData.get(index++)));
        
        // 规范与条款(支持分号分隔多个)
        String specificTerms = ExcelParseUtils.parseString(rowData.get(index++));
        if(StringUtils.isNotBlank(specificTerms)) {
            problem.setSpecificTerms(List.of(specificTerms.split(";")));
        }
        
        // 建议措施(支持分号分隔多个)
        String suggestActions = ExcelParseUtils.parseString(rowData.get(index++));
        if(StringUtils.isNotBlank(suggestActions)) {
            problem.setSuggestActions(List.of(suggestActions.split(";")));
        }
        
        // 数值
        String numValueStr = ExcelParseUtils.parseString(rowData.get(index++));
        if(StringUtils.isNotBlank(numValueStr)) {
            try {
                problem.setNumValue(Float.parseFloat(numValueStr));
            } catch (NumberFormatException e) {
                problem.setNumValue(1.0f); // 默认为1
            }
        } else {
            problem.setNumValue(1.0f);
        }
        
        // 事故类型
        String incidentTypes = ExcelParseUtils.parseString(rowData.get(index++));
        if(StringUtils.isNotBlank(incidentTypes)) {
            problem.setIncidentTypes(IncidentType.parse(incidentTypes));
        }
        
        problem.setReviewParadigm(ExcelParseUtils.parseString(rowData.get(index++)));
        problem.setResponsiblePerson(ExcelParseUtils.parseString(rowData.get(index++)));
        problem.setResponsibleDepartment(ExcelParseUtils.parseString(rowData.get(index++)));
        
        // 事故案例(跳过)
        index++;
        
        problem.setCorrectPractice(ExcelParseUtils.parseString(rowData.get(index++)));
        
        // 数量因子(10-29列)- 最多5组
        List<NumberFactor> numberFactors = new ArrayList<>();
        // 先读取所有名称(10-14列)
        String[] factorNames = new String[5];
        for(int i = 0; i < 5; i++) {
            factorNames[i] = ExcelParseUtils.parseString(rowData.get(index++));
        }
        // 读取对应数值(15-19列)
        Float[] factorValues = new Float[5];
        for(int i = 0; i < 5; i++) {
            String val = ExcelParseUtils.parseString(rowData.get(index++));
            if(StringUtils.isNotBlank(val)) {
                try {
                    factorValues[i] = Float.parseFloat(val);
                } catch (NumberFormatException e) {
                    factorValues[i] = null;
                }
            }
        }
        // 读取扣除分数(20-24列)
        Float[] deductPoints = new Float[5];
        for(int i = 0; i < 5; i++) {
            String val = ExcelParseUtils.parseString(rowData.get(index++));
            if(StringUtils.isNotBlank(val)) {
                try {
                    deductPoints[i] = Float.parseFloat(val);
                } catch (NumberFormatException e) {
                    deductPoints[i] = null;
                }
            }
        }
        // 读取附加分数(25-29列)
        Float[] additionPoints = new Float[5];
        for(int i = 0; i < 5; i++) {
            String val = ExcelParseUtils.parseString(rowData.get(index++));
            if(StringUtils.isNotBlank(val)) {
                try {
                    additionPoints[i] = Float.parseFloat(val);
                } catch (NumberFormatException e) {
                    additionPoints[i] = null;
                }
            }
        }
        // 组装数量因子
        for(int i = 0; i < 5; i++) {
            if(StringUtils.isNotBlank(factorNames[i])) {
                NumberFactor factor = new NumberFactor();
                factor.setName(factorNames[i]);
                factor.setValue(factorValues[i] != null ? factorValues[i] : 0f);
                factor.setDeductPoint(deductPoints[i] != null ? deductPoints[i] : 0f);
                factor.setAdditionPoint(additionPoints[i] != null ? additionPoints[i] : 0f);
                numberFactors.add(factor);
            }
        }
        problem.setNumberFactors(numberFactors);
        
        // 预留(30-34列)
        for(int i = 0; i < 5; i++) {
            index++;
        }
        
        // 比例因子(35-40列)- 最多3组
        List<ScaleFactor> scaleFactors = new ArrayList<>();
        // 先读取所有名称(35-37列)
        String[] scaleNames = new String[3];
        for(int i = 0; i < 3; i++) {
            scaleNames[i] = ExcelParseUtils.parseString(rowData.get(index++));
        }
        // 读取系数(38-40列)
        Float[] scaleValues = new Float[3];
        for(int i = 0; i < 3; i++) {
            String val = ExcelParseUtils.parseString(rowData.get(index++));
            if(StringUtils.isNotBlank(val)) {
                try {
                    scaleValues[i] = Float.parseFloat(val);
                } catch (NumberFormatException e) {
                    scaleValues[i] = null;
                }
            }
        }
        // 组装比例因子
        for(int i = 0; i < 3; i++) {
            if(StringUtils.isNotBlank(scaleNames[i])) {
                ScaleFactor factor = new ScaleFactor();
                factor.setName(scaleNames[i]);
                factor.setFactor(scaleValues[i] != null ? scaleValues[i] : 1f);
                scaleFactors.add(factor);
            }
        }
        problem.setScaleFactors(scaleFactors);
        
        // 设置体系信息(不从Excel读取,由参数指定)
        problem.setHierarchyId(hierarchyId);
 
        return problem;
    }
}