|
@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUnit;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.IdUtil;
|
|
|
+import cn.hutool.core.util.NumberUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
@@ -184,7 +185,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
UserExam userExam = new UserExam();
|
|
|
userExam.setId(IdUtil.getSnowflake(1, 1).nextIdStr());
|
|
|
userExam.setExamId(exam.getId());
|
|
|
- userExam.setUserScore(0);
|
|
|
+ userExam.setUserScore(0.0);
|
|
|
userExam.setUserId(userId);
|
|
|
userExam.setPaperId(paper.getId());
|
|
|
userExam.setState(PaperState.ING);
|
|
@@ -194,6 +195,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
|
|
|
//保存考试关联的试题列表
|
|
|
if (!CollectionUtils.isEmpty(paperQuestionList)) {
|
|
|
+ //保存用户考试题目,如果有组合题,则也要保存子题目
|
|
|
this.savePaperQuestion(paper.getId(), userExam.getId(), paperQuestionList, joinType);
|
|
|
//保存用户考试的题目答案
|
|
|
this.saveQuestionAnswer(paper.getId(), userExam.getId(), exam, paperQuestionList);
|
|
@@ -238,7 +240,8 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void genQuestionAnswer(String userExamId, Exam exam, Question question, List<UserExamQuestionAnswer> userExamQuestionAnswerList) {
|
|
|
+ public void genQuestionAnswer(String userExamId, Exam exam, Question question,
|
|
|
+ List<UserExamQuestionAnswer> userExamQuestionAnswerList) {
|
|
|
List<QuestionAnswer> answerList = question.getAnswerList();
|
|
|
if (!CollectionUtils.isEmpty(answerList)) {
|
|
|
//答案乱序(支持单选题,多选题)
|
|
@@ -299,7 +302,9 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 保存试卷试题列表
|
|
|
+ * 保存用户考试题目,如果有组合题,则也要保存子题目
|
|
|
+ *
|
|
|
+ * @param paperQuestionList 主题目
|
|
|
*/
|
|
|
private void savePaperQuestion(String paperId, String userExamId, List<PaperQuestion> paperQuestionList, Integer joinType) {
|
|
|
paperQuestionList = paperQuestionList.stream().sorted(Comparator.comparing(PaperQuestion::getQuestionType)
|
|
@@ -336,7 +341,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
|
|
|
//配置子题目
|
|
|
configUserExamSubQuestion(userExamId, paperSubQuestionList, groupMap, userExamQuestionList);
|
|
|
-
|
|
|
+ //保存用户考试题目,如果有组合题,则也要保存子题目
|
|
|
userExamQuestionService.saveBatch(userExamQuestionList);
|
|
|
}
|
|
|
|
|
@@ -347,17 +352,18 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
* @param parent
|
|
|
* @return
|
|
|
*/
|
|
|
- public Integer calcSubQuestionScore(Map<String, Long> subQuestionCountMap, PaperQuestion parent) {
|
|
|
- int subQuestionScore = 0;
|
|
|
+ public Double calcSubQuestionScore(Map<String, Long> subQuestionCountMap, PaperQuestion parent) {
|
|
|
+ double subQuestionScore = 0;
|
|
|
if (!Objects.isNull(parent) && !Objects.isNull(subQuestionCountMap)
|
|
|
&& !Objects.isNull(subQuestionCountMap.get(parent.getQuestionId()))) {
|
|
|
//TODO 分数改为double
|
|
|
- subQuestionScore = parent.getQuestionScore() / subQuestionCountMap.get(parent.getQuestionId()).intValue();
|
|
|
+ subQuestionScore = NumberUtil.div(parent.getQuestionScore().doubleValue(),
|
|
|
+ subQuestionCountMap.get(parent.getQuestionId()).doubleValue(), 2);
|
|
|
}
|
|
|
return subQuestionScore;
|
|
|
}
|
|
|
|
|
|
- private static PaperQuestion getPaperQuestion(String paperId, Question sub, PaperQuestion parent, int subQuestionScore) {
|
|
|
+ private PaperQuestion getPaperQuestion(String paperId, Question sub, PaperQuestion parent, double subQuestionScore) {
|
|
|
PaperQuestion paperSubQuestion = new PaperQuestion();
|
|
|
paperSubQuestion.setGroupId(!Objects.isNull(parent) ? parent.getGroupId() : null);
|
|
|
paperSubQuestion.setPaperId(paperId);
|
|
@@ -616,7 +622,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
answerCard.setQuestionType(item.getKey());
|
|
|
//题数和分数
|
|
|
answerCard.setQuestionCount(questions.size());
|
|
|
- answerCard.setQuestionScore(questions.stream().mapToInt(UserExamQuestion::getQuestionScore).sum());
|
|
|
+ answerCard.setQuestionScore(questions.stream().mapToDouble(UserExamQuestion::getQuestionScore).sum());
|
|
|
//答题卡数字面板
|
|
|
answerCard.setIndexList(questions.stream().map(UserExamQuestion::getQuestionIndex).collect(Collectors.toList()));
|
|
|
Map<String, AnswerCardDTO> answerCardMap = new LinkedHashMap<>();
|
|
@@ -667,7 +673,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
Map<Integer, UserExamQuestion> userExamQuestionMap = rootQuestionList.stream().collect(Collectors.toMap(UserExamQuestion::getQuestionIndex, a -> a, (k1, k2) -> k1));
|
|
|
|
|
|
//计算客观题得分
|
|
|
- int score = calcObjectiveScore(examAnswers, userExamQuestionMap, rootQuestionList);
|
|
|
+ double score = calcObjectiveScore(examAnswers, userExamQuestionMap, rootQuestionList);
|
|
|
//更新用户考试信息和成绩
|
|
|
updateUserExamAndResult(score, userExam);
|
|
|
}
|
|
@@ -811,7 +817,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
* @param userExamQuestionMap
|
|
|
* @return
|
|
|
*/
|
|
|
- public int calcObjectiveScore(List<ExamAnswerDTO> examAnswers, Map<Integer, UserExamQuestion> userExamQuestionMap, List<UserExamQuestion> rootQuestionList) {
|
|
|
+ public double calcObjectiveScore(List<ExamAnswerDTO> examAnswers, Map<Integer, UserExamQuestion> userExamQuestionMap, List<UserExamQuestion> rootQuestionList) {
|
|
|
if (!CollectionUtils.isEmpty(examAnswers)) {
|
|
|
//对比/计算
|
|
|
for (ExamAnswerDTO o : examAnswers) {
|
|
@@ -826,7 +832,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
root.setAnswered(true);
|
|
|
}
|
|
|
root.setActualScore(root.getSubQuestionList().stream().filter(o ->
|
|
|
- !Objects.isNull(o.getActualScore())).mapToInt(UserExamQuestion::getActualScore).sum());
|
|
|
+ !Objects.isNull(o.getActualScore())).mapToDouble(UserExamQuestion::getActualScore).sum());
|
|
|
resultList.add(root);
|
|
|
root.getSubQuestionList().forEach(subQuestion -> {
|
|
|
//是否是没回答的客观题,如果是,是否正确设置为false
|
|
@@ -856,7 +862,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
}
|
|
|
//最终得分
|
|
|
return rootQuestionList.stream().filter(o -> !Objects.isNull(o.getActualScore()))
|
|
|
- .mapToInt(UserExamQuestion::getActualScore).sum();
|
|
|
+ .mapToDouble(UserExamQuestion::getActualScore).sum();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -953,7 +959,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
*
|
|
|
* @return
|
|
|
*/
|
|
|
- public Integer compareBlankToScore(UserExamQuestion userExamQuestion, String userAnswerStr, Question question, Boolean canBlankOption) {
|
|
|
+ public Double compareBlankToScore(UserExamQuestion userExamQuestion, String userAnswerStr, Question question, Boolean canBlankOption) {
|
|
|
List<QuestionAnswer> answerList = question.getAnswerList();
|
|
|
//深拷贝答案
|
|
|
List<QuestionAnswer> deepList = deepCopyAnswerList(answerList);
|
|
@@ -989,7 +995,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return 0;
|
|
|
+ return 0.0;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -1000,22 +1006,22 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
* @param dtoList
|
|
|
* @return
|
|
|
*/
|
|
|
- public Integer calcBlankScoreWhenFalse(UserExamQuestion userExamQuestion, Boolean canBlankOption, List<BlankAnswerResultDTO> dtoList) {
|
|
|
+ public Double calcBlankScoreWhenFalse(UserExamQuestion userExamQuestion, Boolean canBlankOption, List<BlankAnswerResultDTO> dtoList) {
|
|
|
if (!Objects.isNull(canBlankOption) && canBlankOption) {//是否允许按空得分
|
|
|
if (dtoList.stream().filter(d -> !Objects.isNull(d.getIsRight()) && d.getIsRight()).count() == 0) {//全部错
|
|
|
userExamQuestion.setIsRight(false);
|
|
|
- userExamQuestion.setActualScore(0);
|
|
|
- return 0;
|
|
|
+ userExamQuestion.setActualScore(0.0);
|
|
|
+ return 0.0;
|
|
|
}
|
|
|
//有答对的,部分得分
|
|
|
- int actualScore = sumRightAnswerScore(dtoList);
|
|
|
+ double actualScore = sumRightAnswerScore(dtoList);
|
|
|
userExamQuestion.setIsRight(true);
|
|
|
userExamQuestion.setActualScore(actualScore);
|
|
|
return actualScore;
|
|
|
} else {
|
|
|
userExamQuestion.setIsRight(false);
|
|
|
- userExamQuestion.setActualScore(0);
|
|
|
- return 0;
|
|
|
+ userExamQuestion.setActualScore(0.0);
|
|
|
+ return 0.0;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1025,8 +1031,8 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
* @param dtoList
|
|
|
* @return
|
|
|
*/
|
|
|
- public int sumRightAnswerScore(List<BlankAnswerResultDTO> dtoList) {
|
|
|
- return dtoList.stream().filter(d -> !Objects.isNull(d.getIsRight()) && d.getIsRight() && !Objects.isNull(d.getScore())).mapToInt(BlankAnswerResultDTO::getScore).sum();
|
|
|
+ public double sumRightAnswerScore(List<BlankAnswerResultDTO> dtoList) {
|
|
|
+ return dtoList.stream().filter(d -> !Objects.isNull(d.getIsRight()) && d.getIsRight() && !Objects.isNull(d.getScore())).mapToDouble(BlankAnswerResultDTO::getScore).sum();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -1062,7 +1068,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
if (!Objects.isNull(answerArray) && answerArray.length > 0) {
|
|
|
List<String> asList = Arrays.asList(answerArray);
|
|
|
if (asList.contains(userAnswer.getContent().trim())) {
|
|
|
- dtoList.add(new BlankAnswerResultDTO(userAnswer.getTag(), userAnswer.getContent().trim(), true, !Objects.isNull(tmp.getPathScore()) ? tmp.getPathScore().intValue() : null));
|
|
|
+ dtoList.add(new BlankAnswerResultDTO(userAnswer.getTag(), userAnswer.getContent().trim(), true, !Objects.isNull(tmp.getPathScore()) ? tmp.getPathScore().doubleValue() : null));
|
|
|
} else {
|
|
|
dtoList.add(new BlankAnswerResultDTO(userAnswer.getTag(), userAnswer.getContent().trim(), false, null));
|
|
|
}
|
|
@@ -1086,7 +1092,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
if (!Objects.isNull(answerArray) && answerArray.length > 0) {
|
|
|
List<String> asList = Arrays.asList(answerArray);
|
|
|
if (asList.contains(userAnswer.getContent().trim())) {
|
|
|
- dtoList.add(new BlankAnswerResultDTO(userAnswer.getTag(), userAnswer.getContent().trim(), true, !Objects.isNull(qa.getPathScore()) ? qa.getPathScore().intValue() : null));
|
|
|
+ dtoList.add(new BlankAnswerResultDTO(userAnswer.getTag(), userAnswer.getContent().trim(), true, !Objects.isNull(qa.getPathScore()) ? qa.getPathScore().doubleValue() : null));
|
|
|
isTrue = true;
|
|
|
iterator.remove();
|
|
|
break;
|
|
@@ -1106,8 +1112,8 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
* @param question
|
|
|
* @return
|
|
|
*/
|
|
|
- public Integer compareMultiToScore(UserExamQuestion userExamQuestion, String[] answers, Question question) {
|
|
|
- Result<Integer> result = compareMultiAnswer(userExamQuestion, answers, question);
|
|
|
+ public Double compareMultiToScore(UserExamQuestion userExamQuestion, String[] answers, Question question) {
|
|
|
+ Result<Double> result = compareMultiAnswer(userExamQuestion, answers, question);
|
|
|
if (Objects.isNull(result)) {
|
|
|
return null;
|
|
|
}
|
|
@@ -1132,7 +1138,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
* @param answers
|
|
|
* @return
|
|
|
*/
|
|
|
- public Result<Integer> compareMultiAnswer(UserExamQuestion userExamQuestion, String[] answers, Question question) {
|
|
|
+ public Result<Double> compareMultiAnswer(UserExamQuestion userExamQuestion, String[] answers, Question question) {
|
|
|
List<QuestionAnswer> answerList = question.getAnswerList();
|
|
|
if (!Objects.isNull(answers) && answers.length > 0 && !CollectionUtils.isEmpty(answerList)) {
|
|
|
//找出正确答案
|
|
@@ -1148,7 +1154,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
if (CollectionUtils.isEmpty(reduceList)) {
|
|
|
return Result.ok(userExamQuestion.getQuestionScore());//给整题的分
|
|
|
} else {
|
|
|
- return Result.error("答题错误", 0);//有错误项,给0分
|
|
|
+ return Result.error("答题错误", 0.0);//有错误项,给0分
|
|
|
}
|
|
|
} else {
|
|
|
//去重并集,取交集,再取差集
|
|
@@ -1161,12 +1167,12 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
//如果用户给的答案中有错误答案的话,给0分
|
|
|
List<QuestionAnswer> falseList = reduceAnswers.stream().filter(o -> !Objects.isNull(o.getIsRight()) && !o.getIsRight()).collect(Collectors.toList());
|
|
|
if (!CollectionUtils.isEmpty(falseList)) {
|
|
|
- return Result.error("答题错误", 0);
|
|
|
+ return Result.error("答题错误", 0.0);
|
|
|
} else {
|
|
|
//漏选项单独算分
|
|
|
List<QuestionAnswer> trueList = answerList.stream().filter(o -> userAnswerList.contains(o.getId())).collect(Collectors.toList());
|
|
|
double sum = trueList.stream().filter(q -> !Objects.isNull(q.getPathScore()) && q.getPathScore() > 0).mapToDouble(QuestionAnswer::getPathScore).sum();
|
|
|
- return Result.ok((int) sum);
|
|
|
+ return Result.ok(sum);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -1205,7 +1211,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
* @param question
|
|
|
* @return
|
|
|
*/
|
|
|
- public Integer compareToScore(UserExamQuestion userExamQuestion, String[] answers, Question question) {
|
|
|
+ public Double compareToScore(UserExamQuestion userExamQuestion, String[] answers, Question question) {
|
|
|
boolean succeed = compareAnswer(answers, question);
|
|
|
if (succeed) {//回答正确
|
|
|
userExamQuestion.setIsRight(true);
|
|
@@ -1213,8 +1219,8 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
return userExamQuestion.getQuestionScore();
|
|
|
} else {
|
|
|
userExamQuestion.setIsRight(false);
|
|
|
- userExamQuestion.setActualScore(0);
|
|
|
- return 0;
|
|
|
+ userExamQuestion.setActualScore(0.0);
|
|
|
+ return 0.0;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -1233,7 +1239,7 @@ public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements IE
|
|
|
*
|
|
|
* @param score
|
|
|
*/
|
|
|
- public void updateUserExamAndResult(int score, UserExam userExam) {
|
|
|
+ public void updateUserExamAndResult(double score, UserExam userExam) {
|
|
|
userExam.setUserScore(score);//最终得分
|
|
|
userExam.setObjectiveScore(score);//客观题得分
|
|
|
userExam.setCommitTime(new Date());
|