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;
/**
*
* 学员与考试题目关系表 服务实现类
*
*
* @author kzy
* @since 2024-06-13 17:47:56
*/
@Service
public class ExStudentAnswerServiceImpl extends ServiceImpl 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);
}
}