| | |
| | | import com.ycl.jxkg.enums.general.ExamStatusEnum; |
| | | import com.ycl.jxkg.enums.general.ExamSubmitTempStatusEnum; |
| | | import com.ycl.jxkg.mapper.*; |
| | | import com.ycl.jxkg.rabbitmq.msg.ExamStatusMsg; |
| | | import com.ycl.jxkg.rabbitmq.product.Producer; |
| | | import com.ycl.jxkg.server.WebsocketServer; |
| | | import com.ycl.jxkg.service.ExamPaperScoreService; |
| | | import com.ycl.jxkg.service.ExamPaperService; |
| | | import com.ycl.jxkg.service.ExamService; |
| | | import com.ycl.jxkg.utils.DateTimeUtil; |
| | | import com.ycl.jxkg.utils.PageUtil; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.BeanUtils; |
| | |
| | | @RequiredArgsConstructor |
| | | public class ExamServiceImpl extends ServiceImpl<ExamMapper, Exam> implements ExamService { |
| | | |
| | | private static final String ANSWER_SPLIT = ","; |
| | | |
| | | private final ExamMapper examMapper; |
| | | private final WebContext webContext; |
| | | private final QuestionMapper questionMapper; |
| | |
| | | private final UserMapper userMapper; |
| | | private final ExamPaperScoreMapper examPaperScoreMapper; |
| | | private final ExamPaperScoreService examPaperScoreService; |
| | | private static final String ANSWER_SPLIT = ","; |
| | | |
| | | private final Producer producer; |
| | | |
| | | /** |
| | | * 添加 |
| | |
| | | Exam entity = ExamForm.getEntityByForm(form, null); |
| | | entity.setStatus(ExamStatusEnum.getStatusByTime(form.getStartTime(), form.getEndTime(), null)); |
| | | entity.setTeacherId(webContext.getCurrentUser().getId()); |
| | | baseMapper.insert(entity); |
| | | // 设置乐观锁版本 |
| | | entity.setUpdateVersion(0); |
| | | if (baseMapper.insert(entity) > 0) { |
| | | this.sendMQ(entity, 0); |
| | | } |
| | | return Result.ok("添加成功"); |
| | | } |
| | | |
| | |
| | | // 为空抛IllegalArgumentException,做全局异常处理 |
| | | Assert.notNull(entity, "记录不存在"); |
| | | // 判断考试状态 |
| | | if (!ExamStatusEnum.NOT_START.equals(entity.getStatus())) { |
| | | if (! ExamStatusEnum.NOT_START.equals(entity.getStatus())) { |
| | | throw new RuntimeException("只能修改还未开始的考试"); |
| | | } |
| | | BeanUtils.copyProperties(form, entity); |
| | | entity.setStatus(ExamStatusEnum.getStatusByTime(form.getStartTime(), form.getEndTime(), null)); |
| | | baseMapper.updateById(entity); |
| | | // 如果修改成功发送mq消息 |
| | | if (baseMapper.updateById(entity) > 0) { |
| | | this.sendMQ(entity, entity.getUpdateVersion() + 1); |
| | | } |
| | | return Result.ok("修改成功"); |
| | | } |
| | | |
| | | /** |
| | | * 发送mq消息 |
| | | * |
| | | * @param entity 考试实体类 |
| | | * @param version 乐观锁版本 |
| | | */ |
| | | public void sendMQ(Exam entity, Integer version) { |
| | | // 如果当前状态为未开始,则发送两条mq消息,一条设置状态为进行中,一条设置状态为已结束 |
| | | if (ExamStatusEnum.NOT_START.equals(entity.getStatus())) { |
| | | // 进行状态消息 |
| | | ExamStatusMsg ingMsg = new ExamStatusMsg(); |
| | | ingMsg.setVersion(version); |
| | | ingMsg.setExamId(entity.getId()); |
| | | ingMsg.setTargetStatus(ExamStatusEnum.ING); |
| | | producer.examMsg(entity.getId(), JSON.toJSONString(ingMsg), DateTimeUtil.getTwoTimeDiffMS(entity.getStartTime(), new Date())); |
| | | // 结束状态消息 |
| | | ExamStatusMsg finishedMsg = new ExamStatusMsg(); |
| | | finishedMsg.setVersion(version); |
| | | finishedMsg.setExamId(entity.getId()); |
| | | finishedMsg.setTargetStatus(ExamStatusEnum.FINISHED); |
| | | producer.examMsg(entity.getId(), JSON.toJSONString(finishedMsg), DateTimeUtil.getTwoTimeDiffMS(entity.getEndTime(), new Date())); |
| | | } else if (ExamStatusEnum.ING.equals(entity.getStatus())) { // 当前是进行中状态则只需发送结束消息 |
| | | // 结束状态消息 |
| | | ExamStatusMsg finishedMsg = new ExamStatusMsg(); |
| | | finishedMsg.setVersion(0); |
| | | finishedMsg.setExamId(entity.getId()); |
| | | finishedMsg.setTargetStatus(ExamStatusEnum.FINISHED); |
| | | producer.examMsg(entity.getId(), JSON.toJSONString(finishedMsg), DateTimeUtil.getTwoTimeDiffMS(entity.getEndTime(), new Date())); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据考试的当前状态,得到下一个状态 |
| | | * |
| | | * @param currentStatus |
| | | * @return |
| | | */ |
| | | public ExamStatusEnum getNextStatus(ExamStatusEnum currentStatus) { |
| | | if (ExamStatusEnum.NOT_START.equals(currentStatus)) { |
| | | return ExamStatusEnum.ING; |
| | | } else { |
| | | return ExamStatusEnum.FINISHED; |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | // 阅卷后才往exam_paper_answer保存考试成绩、以及保存到exam_paper_customer_answer |
| | | // 现在只需要保存到一张临时表 |
| | | // 该接口是主动提交,所以状态都设置为完成,以便后续老师阅卷 |
| | | //TODO:暂时改为temp方便测试 |
| | | saveTempExam(submitData, ExamSubmitTempStatusEnum.temp); |
| | | saveTempExam(submitData, ExamSubmitTempStatusEnum.finish); |
| | | |
| | | //TODO:考试状态设定为结束 |
| | | |
| | | return Result.ok(); |
| | | } |
| | | |
| | |
| | | throw new RuntimeException("考试试卷不存在"); |
| | | } |
| | | List<ExamSubmitTemp> examSubmitTempList = new LambdaQueryChainWrapper<>(examSubmitTempMapper) |
| | | .eq(ExamSubmitTemp::getDeleted, 0) |
| | | .eq(ExamSubmitTemp::getDeleted,0) |
| | | .eq(ExamSubmitTemp::getExamId, id) |
| | | .list(); |
| | | // 参考人数 |