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;
/**
*
* 学员与考试题目关系表 服务实现类
*
*
* @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(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);
}
}