|
|
@@ -1,11 +1,12 @@
|
|
|
package nju.seec.helper.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.google.common.collect.Maps;
|
|
|
import nju.seec.helper.aspect.auth.LoginUser;
|
|
|
import nju.seec.helper.dao.QuizDAO;
|
|
|
import nju.seec.helper.dao.QuizStudentAnswerDAO;
|
|
|
import nju.seec.helper.dao.UserDAO;
|
|
|
-import nju.seec.helper.dto.quiz.StudentAnswerDTO;
|
|
|
+import nju.seec.helper.dto.question.StudentAnswerDTO;
|
|
|
import nju.seec.helper.entity.Quiz;
|
|
|
import nju.seec.helper.entity.QuizStudentAnswer;
|
|
|
import nju.seec.helper.entity.User;
|
|
|
@@ -26,7 +27,6 @@ import java.math.RoundingMode;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author cst
|
|
|
@@ -53,7 +53,7 @@ public class QuizStudentAnswerServiceImpl implements QuizStudentAnswerService {
|
|
|
|
|
|
switch (quiz.getState()) {
|
|
|
case NOT_STARTED:
|
|
|
- throw HelperException.of(ExceptionType.FORBIDDEN, "测试暂未开始");
|
|
|
+ throw HelperException.of(ExceptionType.FORBIDDEN, "测试未开始,无法提交");
|
|
|
case CLOSED:
|
|
|
throw HelperException.of(ExceptionType.FORBIDDEN, "测试已关闭,无法提交");
|
|
|
default:
|
|
|
@@ -65,7 +65,7 @@ public class QuizStudentAnswerServiceImpl implements QuizStudentAnswerService {
|
|
|
QuizStudentAnswer quizStudentAnswer = quizStudentAnswerDAO.findByQuizAndStudent(quiz, student).orElse(new QuizStudentAnswer());
|
|
|
|
|
|
if (quizStudentAnswer.getSubmitNumber() >= quiz.getMaxSubmitNumber()) {
|
|
|
- throw HelperException.of(ExceptionType.FORBIDDEN, String.format("已达最大提交次数%d次,无法再提交", quiz.getMaxSubmitNumber()));
|
|
|
+ throw HelperException.of(ExceptionType.FORBIDDEN, StrUtil.format("已达最大提交次数{}次,无法再提交", quiz.getMaxSubmitNumber()));
|
|
|
}
|
|
|
|
|
|
// 计算得分
|
|
|
@@ -75,10 +75,17 @@ public class QuizStudentAnswerServiceImpl implements QuizStudentAnswerService {
|
|
|
|
|
|
Map<String, StudentAnswerDTO.AnswerRecordDTO> records = studentAnswerDTO.getRecords();
|
|
|
questionVOList.forEach(baseQuestionVO -> {
|
|
|
+ String questionId = baseQuestionVO.getId();
|
|
|
Object answer;
|
|
|
- if ((answer = records.get(baseQuestionVO.getId()).getAnswer()) != null && baseQuestionVO.pass(answer)) {
|
|
|
+ if (records.get(questionId) != null) {
|
|
|
+ answer = records.get(questionId).getAnswer();
|
|
|
+ } else {
|
|
|
+ answer = quizStudentAnswer.getAnswers().get(questionId);
|
|
|
+ }
|
|
|
+ if (baseQuestionVO.pass(answer)) {
|
|
|
correct.getAndIncrement();
|
|
|
}
|
|
|
+ quizStudentAnswer.getAnswers().put(questionId, answer);
|
|
|
});
|
|
|
|
|
|
BigDecimal score = BigDecimal.valueOf(correct.get() * 100).divide(BigDecimal.valueOf(questionVOList.size()), 2, RoundingMode.HALF_UP);
|
|
|
@@ -87,7 +94,6 @@ public class QuizStudentAnswerServiceImpl implements QuizStudentAnswerService {
|
|
|
quizStudentAnswer.setQuiz(quiz)
|
|
|
.setStudent(student)
|
|
|
.setSubmitNumber(quizStudentAnswer.getSubmitNumber() + 1)
|
|
|
- .setAnswers(records.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, recordEntry -> recordEntry.getValue().getAnswer())))
|
|
|
.setScore(score);
|
|
|
|
|
|
questionService.record(user, studentAnswerDTO);
|