kongzy
2024-06-24 4910e41f81a85c03a9dfc83f8ec9c1e71c123d49
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
package com.gkhy.exam.system.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.exam.common.exception.ApiException;
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.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;
 
/**
 * <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(studentAnswer.getAnswer().equals(question.getAnswer())){
            studentAnswer.setPassed(1);
        }else{
            studentAnswer.setPassed(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(paperStudent.getCompleted()==1){
            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);
    }
}