“djh”
4 天以前 f99d902db3c31e57229439962abd2746bb06868d
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
package com.gkhy.exam.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.enums.PaperStudentStateEnum;
import com.gkhy.exam.common.exception.ApiException;
import com.gkhy.exam.system.domain.ExPaperStudent;
import com.gkhy.exam.system.domain.ExStudentAnswer;
import com.gkhy.exam.system.mapper.ExPaperStudentMapper;
import com.gkhy.exam.system.mapper.ExQuestionMapper;
import com.gkhy.exam.system.mapper.ExStudentAnswerMapper;
import com.gkhy.exam.system.service.ExStudentAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Objects;
 
/**
 * <p>
 * 学员与考试题目关系表 服务实现类
 * </p>
 *
 * @author kzy
 * @since 2024-06-13 17:47:56
 */
@Service
public class ExStudentAnswerServiceImpl extends ServiceImpl<ExStudentAnswerMapper, ExStudentAnswer> implements ExStudentAnswerService {
    @Autowired
    private ExQuestionMapper questionMapper;
    @Autowired
    private ExPaperStudentMapper paperStudentMapper;
    @Override
    public int addStudentAnswer(ExStudentAnswer studentAnswer) {
        int row=0;
        validData(studentAnswer);
//        ExQuestion question=questionMapper.selectById(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();
//            }else{
//                studentAnswer.setPassed(StudentAnswerPassEnum.ERROR.getCode());
//                studentAnswer.setScore(0);
//            }
//        }
        ExStudentAnswer existAnswer= baseMapper.getStudentAnswer(studentAnswer);
        if(existAnswer!=null){
            studentAnswer.setId(existAnswer.getId());
            row=baseMapper.updateById(studentAnswer);
        }else{
            row=baseMapper.insert(studentAnswer);
        }
        if(row<1){
            throw new ApiException("提交答题失败");
        }
        return row;
    }
 
    public void validData(ExStudentAnswer studentAnswer){
        ExPaperStudent paperStudent=paperStudentMapper.selectByPaperStudentId(new ExPaperStudent().setPaperId(studentAnswer.getPaperId()).setStudentId(studentAnswer.getStudentId()));
        if(!Objects.equals(paperStudent.getState(), PaperStudentStateEnum.WAIT_EXAM.getCode())){
            throw new ApiException("考试已完成,不能再作答");
        }
 
    }
 
    @Override
    public ExStudentAnswer getStudentAnswer(ExStudentAnswer studentAnswer) {
        if(studentAnswer.getPaperId()==null||studentAnswer.getStudentId()==null||studentAnswer.getQuestionId()==null){
            throw new ApiException("考卷Id、学生id、试题id不能为空");
        }
        return baseMapper.getStudentAnswer(studentAnswer);
    }
}