heheng
6 天以前 46ecbba9e60627c48881f9d91dff168ff25a4e32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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<ExStudentAnswer> studentAnswerList=studentAnswerService.list(Wrappers.<ExStudentAnswer>lambdaQuery()
                .eq(true,ExStudentAnswer::getStudentId,paperStudent.getStudentId())
                .eq(true,ExStudentAnswer::getPaperId,paperStudent.getPaperId()));
        List<ExQuestion> questionList=questionService.selectQuestionByPaperId(paperStudent.getPaperId());
        Map<Long, ExQuestion> 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;
    }
}