This commit is contained in:
=
2026-05-13 14:28:57 +08:00
parent 9f818e1cde
commit ba33a89cee
@@ -239,29 +239,9 @@ public class QsvSurveyServiceImpl extends BaseServiceImpl<QsvUserAnswerRecord> i
// 构建Excel导出实体 // 构建Excel导出实体
List<ExcelExportEntity> excelExportEntities = buildExcelExportEntities(subjects); List<ExcelExportEntity> excelExportEntities = buildExcelExportEntities(subjects);
// 构建答题记录列表 // 构建答题记录列表
List<NutMap> list = answerRecords.stream().map(record -> { List<NutMap> list = answerRecords.stream()
NutMap map = NutMap.NEW() .map(record -> buildAnswerRecordMap(record, subjectMap, options))
.addv("id", record.getString("id")) .toList();
.addv("loginName", record.getString("loginName"))
.addv("userName", record.getString("userName"))
.addv("unitName", record.getString("unitName"))
.addv("unionName", record.getString("unionName"));
JSONObject extJson = record.getAs("extJson", JSONObject.class);
extJson.forEach((k, v) -> {
JSONObject jsonVal = (JSONObject) v;
String type = subjectMap.get(k).getType();
if (type.equals("text")) {
map.addv(k, jsonVal.getStr("text"));
} else if (type.equals("radio") || type.equals("checkbox")) {
JSONArray optionIds = jsonVal.getJSONArray("optionIds");
String selectOptionTexts = options.stream().filter(option -> optionIds.contains(option.getId()))
.map(QsvOption::getText).collect(Collectors.joining(";"));
map.addv(k, selectOptionTexts);
}
});
return map;
}).toList();
pagination.setList(list); pagination.setList(list);
@@ -312,28 +292,60 @@ public class QsvSurveyServiceImpl extends BaseServiceImpl<QsvUserAnswerRecord> i
} }
private List<NutMap> buildAnswerRecords(List<QsvUserAnswerRecord> answerRecords, Map<String, QsvSubject> subjectMap, List<QsvOption> options) { private List<NutMap> buildAnswerRecords(List<QsvUserAnswerRecord> answerRecords, Map<String, QsvSubject> subjectMap, List<QsvOption> options) {
return answerRecords.stream().map(record -> { return answerRecords.stream()
NutMap map = NutMap.NEW() .map(record -> buildAnswerRecordMap(record, subjectMap, options))
.addv("id", record.getId()) .collect(Collectors.toList());
.addv("loginName", record.getLoginName()) }
.addv("userName", record.getUserName())
.addv("unitName", record.getUnitName())
.addv("unionName", record.getUnionName());
JSONObject extJson = record.getExtJson();
extJson.forEach((k, v) -> {
JSONObject jsonVal = (JSONObject) v;
String type = subjectMap.get(k).getType(); private NutMap buildAnswerRecordMap(NutMap record, Map<String, QsvSubject> subjectMap, List<QsvOption> options) {
if (type.equals("text")) { NutMap map = NutMap.NEW()
map.addv(k, jsonVal.getStr("text")); .addv("id", record.getString("id"))
} else if (type.equals("radio") || type.equals("checkbox")) { .addv("loginName", record.getString("loginName"))
JSONArray optionIds = jsonVal.getJSONArray("optionIds"); .addv("userName", record.getString("userName"))
String selectOptionTexts = options.stream().filter(option -> optionIds.contains(option.getId())) .addv("unitName", record.getString("unitName"))
.map(QsvOption::getText).collect(Collectors.joining(";")); .addv("unionName", record.getString("unionName"));
map.addv(k, selectOptionTexts); fillSubjectAnswers(map, record.getAs("extJson", JSONObject.class), subjectMap, options);
return map;
}
private NutMap buildAnswerRecordMap(QsvUserAnswerRecord record, Map<String, QsvSubject> subjectMap, List<QsvOption> options) {
NutMap map = NutMap.NEW()
.addv("id", record.getId())
.addv("loginName", record.getLoginName())
.addv("userName", record.getUserName())
.addv("unitName", record.getUnitName())
.addv("unionName", record.getUnionName());
fillSubjectAnswers(map, record.getExtJson(), subjectMap, options);
return map;
}
private void fillSubjectAnswers(NutMap map, JSONObject extJson, Map<String, QsvSubject> subjectMap, List<QsvOption> options) {
if (extJson == null) {
return;
}
extJson.forEach((k, v) -> {
QsvSubject subject = subjectMap.get(k);
JSONObject jsonVal = extJson.get(k, JSONObject.class);
// 兼容历史答卷中保留了已删除题目或异常答案结构的情况,避免导出时中断。
if (subject == null || jsonVal == null) {
return;
}
String type = subject.getType();
if ("text".equals(type)) {
map.addv(k, jsonVal.getStr("text"));
} else if ("radio".equals(type) || "checkbox".equals(type)) {
JSONArray optionIds = jsonVal.getJSONArray("optionIds");
if (optionIds == null) {
map.addv(k, "");
return;
} }
}); String selectOptionTexts = options.stream()
return map; .filter(option -> optionIds.contains(option.getId()))
}).collect(Collectors.toList()); .map(QsvOption::getText)
.collect(Collectors.joining(";"));
map.addv(k, selectOptionTexts);
}
});
} }
} }