Browse Source

bug修复

master
luoweijian 2 months ago
parent
commit
eacb61b38c
  1. 63
      src/main/java/com/project/classicpaper/application/impl/ClassicPaperSetApplicationServiceImpl.java

63
src/main/java/com/project/classicpaper/application/impl/ClassicPaperSetApplicationServiceImpl.java

@ -22,6 +22,7 @@ import com.project.classicpaper.domain.entity.ClassicCategoryEntity;
import com.project.classicpaper.domain.service.*; import com.project.classicpaper.domain.service.*;
import com.project.question.domain.entity.QuestionEntity; import com.project.question.domain.entity.QuestionEntity;
import com.project.question.domain.service.QuestionBaseService; import com.project.question.domain.service.QuestionBaseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -34,6 +35,7 @@ import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@Service @Service
@Slf4j
public class ClassicPaperSetApplicationServiceImpl implements ClassicPaperSetApplicationService { public class ClassicPaperSetApplicationServiceImpl implements ClassicPaperSetApplicationService {
@Autowired @Autowired
@ -63,9 +65,6 @@ public class ClassicPaperSetApplicationServiceImpl implements ClassicPaperSetApp
@Autowired @Autowired
private ClassicCategoryBaseService classicCategoryBaseService; private ClassicCategoryBaseService classicCategoryBaseService;
@Autowired
private com.project.task.config.ExamScoreRatioConfig examScoreRatioConfig;
@Override @Override
public Result<ClassicPaperSetDTO> generate(ClassicPaperSetDTO request) throws Exception { public Result<ClassicPaperSetDTO> generate(ClassicPaperSetDTO request) throws Exception {
return generateClassicPaperDomainService.generate(request); return generateClassicPaperDomainService.generate(request);
@ -259,39 +258,39 @@ public class ClassicPaperSetApplicationServiceImpl implements ClassicPaperSetApp
} }
/** /**
* 计算分值比例并填充到 DTO 的四个独立字段 * scoreRatio JSON 解析分值比例并填充到 DTO 的四个独立字段
* 优先取实体已存储的 scoreRatio JSON否则从 ExamScoreRatioConfig 计算 * 没有 scoreRatio 或解析失败时不设值前端自己处理
* 题型数量为 0 时对应字段设为 null
*/ */
private void computeScoreRatio(ClassicPaperSetEntity paperSet, ClassicPaperSetDTO dto) { private void computeScoreRatio(ClassicPaperSetEntity paperSet, ClassicPaperSetDTO dto) {
Map<String, Integer> ratioMap = null; if (StrUtil.isBlank(paperSet.getScoreRatio())) {
if (StrUtil.isNotBlank(paperSet.getScoreRatio())) { return;
try {
ratioMap = new ObjectMapper().readValue(paperSet.getScoreRatio(),
Map.class);
} catch (Exception e) {
// 解析失败时使用配置默认值
}
} }
if (ratioMap == null) { Map<String, Integer> ratioMap;
ratioMap = new LinkedHashMap<>(); try {
String scoreRatio = paperSet.getScoreRatio();
// DB 中存的是带转义符的 JSON 字符串,需先还原
scoreRatio = scoreRatio.replace("\\\"", "\"");
// 可能有外层引号,去掉
if (scoreRatio.startsWith("\"") && scoreRatio.endsWith("\"")) {
scoreRatio = scoreRatio.substring(1, scoreRatio.length() - 1);
}
ratioMap = new ObjectMapper().readValue(scoreRatio, Map.class);
} catch (Exception e) {
log.warn(">>> [经典套题] scoreRatio 解析失败, setId={}", paperSet.getId(), e);
return;
} }
dto.setSingleChoiceScoreRatio( if (paperSet.getSingleChoiceNum() != null && paperSet.getSingleChoiceNum() > 0) {
paperSet.getSingleChoiceNum() != null && paperSet.getSingleChoiceNum() > 0 dto.setSingleChoiceScoreRatio(ratioMap.get("single"));
? ratioMap.getOrDefault("single", examScoreRatioConfig.getSingle()) }
: null); if (paperSet.getMultipleChoiceNum() != null && paperSet.getMultipleChoiceNum() > 0) {
dto.setMultipleChoiceScoreRatio( dto.setMultipleChoiceScoreRatio(ratioMap.get("multiple"));
paperSet.getMultipleChoiceNum() != null && paperSet.getMultipleChoiceNum() > 0 }
? ratioMap.getOrDefault("multiple", examScoreRatioConfig.getMultiple()) if (paperSet.getTrueFalseNum() != null && paperSet.getTrueFalseNum() > 0) {
: null); dto.setTrueFalseScoreRatio(ratioMap.get("trueFalse"));
dto.setTrueFalseScoreRatio( }
paperSet.getTrueFalseNum() != null && paperSet.getTrueFalseNum() > 0 if (paperSet.getShortAnswerNum() != null && paperSet.getShortAnswerNum() > 0) {
? ratioMap.getOrDefault("trueFalse", examScoreRatioConfig.getTrueFalse()) dto.setShortAnswerScoreRatio(ratioMap.get("shortAnswer"));
: null); }
dto.setShortAnswerScoreRatio(
paperSet.getShortAnswerNum() != null && paperSet.getShortAnswerNum() > 0
? ratioMap.getOrDefault("shortAnswer", examScoreRatioConfig.getShortAnswer())
: null);
} }
} }

Loading…
Cancel
Save