package com.gkhy.exam.framework.event; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.gkhy.exam.common.enums.PaperStudentStateEnum; import com.gkhy.exam.common.enums.QuestionTypeEnum; import com.gkhy.exam.common.enums.StudentAnswerPassEnum; import com.gkhy.exam.system.domain.ExExamPaper; import com.gkhy.exam.system.domain.ExPaperStudent; import com.gkhy.exam.system.domain.ExQuestion; import com.gkhy.exam.system.domain.ExStudentAnswer; import com.gkhy.exam.system.service.ExExamPaperService; import com.gkhy.exam.system.service.ExPaperStudentService; import com.gkhy.exam.system.service.ExQuestionService; import com.gkhy.exam.system.service.ExStudentAnswerService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; @Slf4j @Component public class PaperStudentListener { @Autowired private ExExamPaperService examPaperService; @Autowired private ExStudentAnswerService studentAnswerService; @Autowired private ExQuestionService questionService; @Autowired private ExPaperStudentService paperStudentService; @EventListener public void PaperStudentListener(ExPaperStudent paperStudent){ ExExamPaper examPaper=examPaperService.getById(paperStudent.getPaperId()); List studentAnswerList=studentAnswerService.list(Wrappers.lambdaQuery() .eq(true,ExStudentAnswer::getStudentId,paperStudent.getStudentId()) .eq(true,ExStudentAnswer::getPaperId,paperStudent.getPaperId())); List questionList=questionService.selectQuestionByPaperId(paperStudent.getPaperId()); Map collectMap = questionList.stream().collect(Collectors.toMap(ExQuestion::getId, k -> k)); Integer totalScore=0; for(ExStudentAnswer studentAnswer:studentAnswerList){ ExQuestion question=collectMap.get(studentAnswer.getQuestionId()); if(question.getQuestionType().equals(QuestionTypeEnum.EASY.getCode())){ studentAnswer.setPassed(StudentAnswerPassEnum.WAIT_REVIEW.getCode()); }else{ if(studentAnswer.getAnswer().equals(question.getAnswer())){ studentAnswer.setPassed(StudentAnswerPassEnum.CORRECT.getCode()); studentAnswer.setScore(getScore(examPaper,question.getQuestionType())); totalScore=totalScore+studentAnswer.getScore(); }else{ studentAnswer.setPassed(StudentAnswerPassEnum.ERROR.getCode()); studentAnswer.setScore(0); } } } studentAnswerService.updateBatchById(studentAnswerList); if(Optional.ofNullable(examPaper.getEasyNum()).orElse(0)<=0){ //没有简答题,直接批改试卷 paperStudent.setScore(totalScore); paperStudent.setState(PaperStudentStateEnum.DONE_REVIEW.getCode()); paperStudentService.updateById(paperStudent); } } private Integer getScore(ExExamPaper examPaper,Integer questionType){ if(questionType.equals(QuestionTypeEnum.SINGLE.getCode())){ return examPaper.getSingleScore(); }else if(questionType.equals(QuestionTypeEnum.MULTI.getCode())){ return examPaper.getMultiScore(); }else if(questionType.equals(QuestionTypeEnum.JUDGE.getCode())){ return examPaper.getJudgeScore(); }else if(questionType.equals(QuestionTypeEnum.EASY.getCode())){ return examPaper.getEasyScore(); } return 0; } }