Commit bffd50a7 authored by wujipeng's avatar wujipeng
Browse files

修复-答案与选项不匹配未识别异常

parent 12e76975
...@@ -232,7 +232,7 @@ public class TheoreticalLibraryImportPreviewServiceImpl ...@@ -232,7 +232,7 @@ public class TheoreticalLibraryImportPreviewServiceImpl
if (options.stream().anyMatch(option -> isBlank(option.getContent()))) { if (options.stream().anyMatch(option -> isBlank(option.getContent()))) {
throw new BusinessException("第 " + question.getSourceRow() + " 行选项内容不能为空"); throw new BusinessException("第 " + question.getSourceRow() + " 行选项内容不能为空");
} }
for (char answer : Objects.toString(question.getAnswer(), "") for (char answer : answerForOptionValidation(question)
.toUpperCase(Locale.ROOT).toCharArray()) { .toUpperCase(Locale.ROOT).toCharArray()) {
// 答案解析沿用旧导入逻辑,可能得到 A~E 之外的错误字母(如 P)。 // 答案解析沿用旧导入逻辑,可能得到 A~E 之外的错误字母(如 P)。
// 此处必须校验全部英文字母,否则管理员即使没有真正修正答案,也能让异常题通过确认。 // 此处必须校验全部英文字母,否则管理员即使没有真正修正答案,也能让异常题通过确认。
...@@ -243,6 +243,19 @@ public class TheoreticalLibraryImportPreviewServiceImpl ...@@ -243,6 +243,19 @@ public class TheoreticalLibraryImportPreviewServiceImpl
} }
} }
/**
* 返回确认阶段应检查的完整答案。
*
* <p>预览对象中的单选题答案需要兼容旧接口,可能已经由 ABD 截成 A;确认处理时必须再次
* 从原题干恢复完整括号内容,避免用户原样提交 A/B/C 三个选项后绕过 D 缺失校验。</p>
*/
private String answerForOptionValidation(TheoreticalLibraryImportQuestionVO question) {
if ("单选题".equals(question.getType())) {
return legacySingleAnswerContent(Objects.toString(question.getRawProblem(), ""));
}
return Objects.toString(question.getAnswer(), "");
}
/** 跳过题只清空题干及紧随其后的选项行,层级标题和题型标题保持不变。 */ /** 跳过题只清空题干及紧随其后的选项行,层级标题和题型标题保持不变。 */
private void clearQuestion(Sheet sheet, TheoreticalLibraryImportQuestionVO question) { private void clearQuestion(Sheet sheet, TheoreticalLibraryImportQuestionVO question) {
int questionRowIndex = question.getSourceRow() - 1; int questionRowIndex = question.getSourceRow() - 1;
...@@ -419,9 +432,16 @@ public class TheoreticalLibraryImportPreviewServiceImpl ...@@ -419,9 +432,16 @@ public class TheoreticalLibraryImportPreviewServiceImpl
if ("单选题".equals(type)) { if ("单选题".equals(type)) {
result.answer = legacySingleAnswer(rawProblem); result.answer = legacySingleAnswer(rawProblem);
/*
* 旧正式导入器只取最后一个全角括号后的首字符,因此单选题“(ABD)”的展示答案
* 仍然是 A。异常校验不能复用这个被截断的值,否则实际不存在的 D 选项会被漏掉。
* validationAnswer 专门保留答案括号中的完整内容,只用于预览和确认阶段的一致性校验。
*/
result.validationAnswer = legacySingleAnswerContent(rawProblem);
} else { } else {
Matcher answerMatcher = MULTIPLE_ANSWER.matcher(rawProblem); Matcher answerMatcher = MULTIPLE_ANSWER.matcher(rawProblem);
result.answer = answerMatcher.find() ? answerMatcher.group(1) : ""; result.answer = answerMatcher.find() ? answerMatcher.group(1) : "";
result.validationAnswer = result.answer;
} }
if (!isBlank(result.answer)) { if (!isBlank(result.answer)) {
result.problem = rawProblem.replace("(" + result.answer + ")", "( )"); result.problem = rawProblem.replace("(" + result.answer + ")", "( )");
...@@ -472,7 +492,7 @@ public class TheoreticalLibraryImportPreviewServiceImpl ...@@ -472,7 +492,7 @@ public class TheoreticalLibraryImportPreviewServiceImpl
addIssue(result.issues, "OPTION_LABEL_DISCONTINUOUS", addIssue(result.issues, "OPTION_LABEL_DISCONTINUOUS",
"选项序号不连续,当前解析结果为 " + String.join("、", labels)); "选项序号不连续,当前解析结果为 " + String.join("、", labels));
} }
for (char answer : Objects.toString(result.answer, "") for (char answer : Objects.toString(result.validationAnswer, "")
.toUpperCase(Locale.ROOT).toCharArray()) { .toUpperCase(Locale.ROOT).toCharArray()) {
// 不能只检查合法范围 A~E:P 等错误答案字母恰恰是需要在预览阶段暴露的异常。 // 不能只检查合法范围 A~E:P 等错误答案字母恰恰是需要在预览阶段暴露的异常。
// 非字母字符交由原有答案解析规则处理,避免把空格、符号误报为“缺少选项”。 // 非字母字符交由原有答案解析规则处理,避免把空格、符号误报为“缺少选项”。
...@@ -611,6 +631,18 @@ public class TheoreticalLibraryImportPreviewServiceImpl ...@@ -611,6 +631,18 @@ public class TheoreticalLibraryImportPreviewServiceImpl
? String.valueOf(text.charAt(index + 1)) : ""; ? String.valueOf(text.charAt(index + 1)) : "";
} }
/**
* 读取旧单选题最后一个全角括号中的完整内容。
*
* <p>该值不改变旧导入器最终保存的答案,仅用于发现“展示答案已被截成 A,但原答案 ABD
* 中的 D 没有对应选项”这类数据异常。括号不完整时返回空串,继续沿用原有容错行为。</p>
*/
private String legacySingleAnswerContent(String text) {
int start = text.lastIndexOf('(');
int end = start < 0 ? -1 : text.indexOf(')', start + 1);
return start >= 0 && end > start ? text.substring(start + 1, end) : "";
}
private String duplicateKey(String scope, TheoreticalLibraryImportQuestionVO question) { private String duplicateKey(String scope, TheoreticalLibraryImportQuestionVO question) {
return key(scope, question.getFirstLevelName(), question.getSecondLevelName(), return key(scope, question.getFirstLevelName(), question.getSecondLevelName(),
question.getThirdLevelName(), question.getFourthLevelName(), question.getThirdLevelName(), question.getFourthLevelName(),
...@@ -660,6 +692,8 @@ public class TheoreticalLibraryImportPreviewServiceImpl ...@@ -660,6 +692,8 @@ public class TheoreticalLibraryImportPreviewServiceImpl
private static class ParseResult { private static class ParseResult {
private String problem; private String problem;
private String answer; private String answer;
/** 未截断的答案文本,仅用于校验答案中的每个字母是否都有对应选项。 */
private String validationAnswer;
private boolean consumedOptionRow; private boolean consumedOptionRow;
private final List<TheoreticalLibraryImportOptionVO> options = new ArrayList<>(); private final List<TheoreticalLibraryImportOptionVO> options = new ArrayList<>();
private final List<TheoreticalLibraryImportBracketVO> answerCandidates = new ArrayList<>(); private final List<TheoreticalLibraryImportBracketVO> answerCandidates = new ArrayList<>();
......
...@@ -120,6 +120,32 @@ class TheoreticalLibraryImportPreviewServiceImplTest { ...@@ -120,6 +120,32 @@ class TheoreticalLibraryImportPreviewServiceImplTest {
&& issue.getMessage().contains("P"))); && issue.getMessage().contains("P")));
} }
@Test
void previewShouldCheckCompleteSingleChoiceAnswerBeforeLegacyTruncation() throws Exception {
when(theoreticalLibraryMapper.selectList(any())).thenReturn(Collections.emptyList());
MockMultipartFile file = workbook("污泥四级题库.xlsx", sheet -> {
hierarchy(sheet, 4, 0, "第一章");
hierarchy(sheet, 5, 1, "第一节");
hierarchy(sheet, 6, 2, "第一目");
hierarchy(sheet, 7, 3, "知识点");
cell(sheet, 8, "单选题");
// 精确复现问题:旧单选题解析会把(ABD)截成 A,但异常校验必须继续检查 B、D。
cell(sheet, 9, "影响污泥龄的主要因素包括(ABD)。");
cell(sheet, 10, "(A)曝气池溶解氧水平 (B)剩余污泥排放量 (C)进水水温");
});
TheoreticalLibraryImportPreviewVO result = service.preview(file);
assertEquals(1, result.getAbnormalCount());
assertEquals(0, result.getNormalCount());
TheoreticalLibraryImportQuestionVO question = result.getAbnormalQuestions().get(0);
// answer 字段保留旧导入兼容结果,避免预览与最终入库行为出现额外的不兼容变化。
assertEquals("A", question.getAnswer());
assertTrue(question.getIssues().stream().anyMatch(issue ->
"ANSWER_OPTION_MISSING".equals(issue.getCode())
&& issue.getMessage().contains("D")));
}
@Test @Test
void previewShouldRejectFilenameThatOriginalImporterCannotRoute() throws Exception { void previewShouldRejectFilenameThatOriginalImporterCannotRoute() throws Exception {
MockMultipartFile file = workbook("理论题库.xlsx", sheet -> cell(sheet, 4, "判断题")); MockMultipartFile file = workbook("理论题库.xlsx", sheet -> cell(sheet, 4, "判断题"));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment