From 6cf1b91b85598821535bcfb95803fb8bc5a01ed1 Mon Sep 17 00:00:00 2001 From: Lqc Date: Fri, 29 Aug 2025 15:59:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E8=80=83=E8=AF=95=E6=97=B6?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E9=80=89=E6=8B=A9=E9=A2=98=EF=BC=8C=E5=A4=9A?= =?UTF-8?q?=E9=80=89=E9=A2=98=EF=BC=8C=E5=88=A4=E6=96=AD=E9=A2=98=E5=BE=97?= =?UTF-8?q?=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../biz/controller/ExamBizController.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/jeecg-boot/jeecg-boot-module/jeecg-module-learn/src/main/java/org/jeecg/modules/biz/controller/ExamBizController.java b/jeecg-boot/jeecg-boot-module/jeecg-module-learn/src/main/java/org/jeecg/modules/biz/controller/ExamBizController.java index 690645f1..6835cf5a 100644 --- a/jeecg-boot/jeecg-boot-module/jeecg-module-learn/src/main/java/org/jeecg/modules/biz/controller/ExamBizController.java +++ b/jeecg-boot/jeecg-boot-module/jeecg-module-learn/src/main/java/org/jeecg/modules/biz/controller/ExamBizController.java @@ -20,12 +20,16 @@ import org.jeecg.modules.gen.paper.entity.Paper; import org.jeecg.modules.gen.paper.service.IPaperService; import org.jeecg.modules.gen.paperquestion.entity.PaperQuestion; import org.jeecg.modules.gen.paperquestion.service.IPaperQuestionService; +import org.jeecg.modules.gen.question.controller.QuestionController; import org.jeecg.modules.gen.question.entity.Question; import org.jeecg.modules.gen.question.entity.QuestionRequest; import org.jeecg.modules.gen.question.service.IQuestionService; +import org.jeecg.modules.gen.questionoption.entity.QuestionOption; +import org.jeecg.modules.gen.questionoption.service.IQuestionOptionService; import org.jeecg.modules.gen.questionrepo.entity.QuestionRepo; import org.jeecg.modules.gen.questionrepo.service.IQuestionRepoService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; @@ -51,10 +55,13 @@ public class ExamBizController { private IQuestionService questionService; @Autowired private IQuestionRepoService questionRepoService; + @Autowired + private IQuestionOptionService questionOptionService; //获取考试试题 @RequestMapping("/getExamQuestions/{examId}") @Operation(summary = "获取考试试题") + @Transactional public Result getExamQuestions(@PathVariable String examId, @RequestParam String studentId) { Exam exam = examService.getById(examId); if(exam.getPaperId().isEmpty()){ @@ -170,6 +177,7 @@ public class ExamBizController { @PostMapping("/submitExam") @Operation(summary = "提交考试") + @Transactional public Result submitExam(@RequestBody ExamRecord examRecord, HttpServletRequest req) { examRecord.setIpAddress(getClientIp(req)); @@ -188,6 +196,9 @@ public class ExamBizController { if (examRecord.getDeviceInfo() != null) { updateWrapper.set(ExamRecord::getDeviceInfo, examRecord.getDeviceInfo()); } + // 阅卷 + List gradedAnswers = gradeExam(examRecord.getExamId(), examRecord.getUserId()); + examAnswerService.updateBatchById(gradedAnswers); // 更新考试状态,提交时间 updateWrapper. set(ExamRecord::getStatus,1). @@ -282,6 +293,90 @@ public class ExamBizController { return ip; } + /** + * 批量阅卷 + * @param examId 考试ID + * @param userId 用户ID + * @return 阅卷后的答题列表 + */ + private List gradeExam(String examId, String userId) { + // 获取学生的答题列表 + List examAnswerList = examAnswerService.list(new LambdaQueryWrapper() + .eq(ExamAnswer::getExamId, examId) + .eq(ExamAnswer::getUserId, userId) + ); + + // 提取所有题目ID + List questionIds = examAnswerList.stream() + .map(ExamAnswer::getQuestionId) + .collect(Collectors.toList()); + + // 查询题目 + List questions = questionService.list(new LambdaQueryWrapper() + .in(Question::getId, questionIds) + .lt(Question::getType, 3) + ); + + // 创建题目ID到题目的映射 + Map questionMap = questions.stream() + .collect(Collectors.toMap(Question::getId, question -> question)); + + // 获取 选择、多选、判断 题目ID列表 + List questionIdsFromQuestions = questions.stream() + .map(Question::getId) + .collect(Collectors.toList()); + + // 查询这些题目的正确选项 + List questionOptions = questionOptionService.list(new LambdaQueryWrapper() + .in(QuestionOption::getQuestionId, questionIdsFromQuestions) + .eq(QuestionOption::getIzCorrent, 1) + ); + + // 将选项转换为Map,结构为:题目ID -> 正确答案选项ID列表 + Map> correctAnswerMap = questionOptions.stream() + .collect(Collectors.groupingBy( + QuestionOption::getQuestionId, + Collectors.mapping(QuestionOption::getId, Collectors.toList()) + )); + + // 遍历学生的答案,进行评分 + for (ExamAnswer examAnswer : examAnswerList) { + String studentAnswer = examAnswer.getAnswer(); + Question question = questionMap.get(examAnswer.getQuestionId()); + List correctAnswers = correctAnswerMap.get(examAnswer.getQuestionId()); + + // 比较答案并设置分数 + if (studentAnswer != null && question != null && correctAnswers != null) { + // 将学生答案按逗号分割成列表 + List studentAnswers = Arrays.asList(studentAnswer.split(",")); + double score = 0.0; + + // 根据题目类型进行评分 + if (question.getType() == 1 || question.getType() == 2) { // 单选题或判断题 + if (studentAnswers.get(0).equals(correctAnswers.get(0))) { + score = question.getScore(); // 使用题目设定的分值 + } + } else if (question.getType() == 3) { // 多选题 + // 检查学生答案数量是否正确 + if (studentAnswers.size() == correctAnswers.size()) { + // 检查每个答案是否都正确 + boolean allCorrect = studentAnswers.stream() + .allMatch(correctAnswers::contains); + + if (allCorrect) { + score = question.getScore(); // 使用题目设定的分值 + } + } + } + examAnswer.setIzCorrect(score> 0.0 ? 1 : 0); + examAnswer.setScore(score); + } else { + examAnswer.setScore(0.0); // 答案为空或题目不存在 + } + } + return examAnswerList; + } + //获取设备信息 public String getDeviceInfo(HttpServletRequest request) { // 获取User-Agent