New file |
| | |
| | | package com.ycl.jxkg.job; |
| | | |
| | | import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; |
| | | import com.ycl.jxkg.domain.entity.Exam; |
| | | import com.ycl.jxkg.enums.general.ExamStatusEnum; |
| | | import com.ycl.jxkg.mapper.ExamMapper; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author:xp |
| | | * @date:2024/7/1 11:06 |
| | | */ |
| | | @Component |
| | | @RequiredArgsConstructor |
| | | public class ExamJob { |
| | | |
| | | private final ExamMapper examMapper; |
| | | |
| | | |
| | | @Scheduled(fixedRate = 120000) // 两分钟执行一次,定时任务作为mq消费失败的保底 |
| | | private void updateExamStatus() { |
| | | List<Exam> notFinishedExams = new LambdaQueryChainWrapper<>(examMapper) |
| | | .select(Exam::getId, Exam::getStatus, Exam::getStartTime, Exam::getEndTime) |
| | | .ne(Exam::getStatus, ExamStatusEnum.FINISHED) |
| | | .list(); |
| | | List<Exam> notStartExams = notFinishedExams.stream().filter(item -> ExamStatusEnum.NOT_START.equals(item.getStatus())).collect(Collectors.toList()); |
| | | List<Exam> ingExams = notFinishedExams.stream().filter(item -> ExamStatusEnum.ING.equals(item.getStatus())).collect(Collectors.toList()); |
| | | Date now = new Date(); |
| | | // 未开始的状态 到 进行中 |
| | | for (Exam exam : notStartExams) { |
| | | if (now.after(exam.getStartTime()) && now.before(exam.getEndTime())) { |
| | | exam.setStatus(ExamStatusEnum.ING); |
| | | examMapper.updateById(exam); |
| | | } |
| | | } |
| | | // 从进行中状态 到 结束 |
| | | for (Exam ingExam : ingExams) { |
| | | if (now.after(ingExam.getEndTime())) { |
| | | ingExam.setStatus(ExamStatusEnum.FINISHED); |
| | | examMapper.updateById(ingExam); |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | } |