| package com.doumee.service.timer; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| import com.baomidou.mybatisplus.core.metadata.IPage; | 
| import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
| import com.doumee.core.constants.ResponseStatus; | 
| import com.doumee.core.exception.BusinessException; | 
| import com.doumee.core.model.PageData; | 
| import com.doumee.core.model.PageWrap; | 
| import com.doumee.core.utils.Utils; | 
| import com.doumee.dao.timer.entity.JobState; | 
| import com.doumee.dao.timer.entity.QuartzJob; | 
| import com.doumee.dao.timer.mapper.QuartzJobMapper; | 
| import com.doumee.dao.timer.scheduler.QuartzManage; | 
| import org.quartz.CronTrigger; | 
| import org.springframework.stereotype.Service; | 
|   | 
| import javax.annotation.PostConstruct; | 
| import javax.annotation.Resource; | 
| import java.util.List; | 
| import java.util.Objects; | 
|   | 
| /** | 
|  * @author 公众号:知了一笑 | 
|  * @since 2023-07-26 11:08 | 
|  */ | 
| @Service | 
| public class QuartzJobService { | 
|   | 
|     @Resource | 
|     private QuartzJobMapper quartzJobMapper ; | 
|   | 
|     @Resource | 
|     private QuartzManage quartzManage; | 
|   | 
|     /** | 
|      * 任务主键查询 | 
|      */ | 
|     public QuartzJob getById(Integer id) { | 
|         return quartzJobMapper.selectById(id) ; | 
|     } | 
|   | 
|     /** | 
|      * 新增任务 | 
|      */ | 
|     public int insert(QuartzJob quartzJob) { | 
|         int flag = quartzJobMapper.insert(quartzJob) ; | 
|         if (flag > 0){ | 
|             quartzManage.createJob(quartzJob) ; | 
|         } | 
|         return flag; | 
|     } | 
|   | 
|     /** | 
|      * 更新任务 | 
|      */ | 
|     public int update(QuartzJob quartzJob) { | 
|         int flag = quartzJobMapper.updateById(quartzJob); | 
|         if (flag > 0){ | 
|             quartzManage.updateJob(quartzJob); | 
|         } | 
|         return flag ; | 
|     } | 
|   | 
|     /** | 
|      * 暂停任务 | 
|      */ | 
|     public void pause(Integer id) { | 
|         QuartzJob quartzJob = quartzJobMapper.selectById(id) ; | 
|         if (!Objects.isNull(quartzJob)){ | 
|             quartzJob.setState(JobState.JOB_STOP.getStatus()); | 
|             if (quartzJobMapper.updateById(quartzJob)>0){ | 
|                 quartzManage.checkStop(quartzJob); | 
|             } | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 恢复任务 | 
|      */ | 
|     public void resume(Integer id) { | 
|         QuartzJob quartzJob = quartzJobMapper.selectById(id) ; | 
|         if (!Objects.isNull(quartzJob)){ | 
|             quartzJob.setState(JobState.JOB_RUN.getStatus()); | 
|             if (quartzJobMapper.updateById(quartzJob)>0){ | 
|                 quartzManage.resumeJob(id); | 
|             } | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 执行任务一次 | 
|      */ | 
|     public void runOnce(Integer id) { | 
|         QuartzJob quartzJob = quartzJobMapper.selectById(id) ; | 
|         if (!Objects.isNull(quartzJob) && quartzJob.getState() != JobState.JOB_DEL.getStatus()){ | 
|             quartzManage.run(quartzJob); | 
|         } | 
|     } | 
|   | 
|     public PageData<QuartzJob> findPage(PageWrap<QuartzJob> pageWrap) { | 
|         IPage<QuartzJob> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); | 
|         QueryWrapper<QuartzJob> queryWrapper = new QueryWrapper<>(); | 
|         Utils.MP.blankToNull(pageWrap.getModel()); | 
|         queryWrapper.lambda() | 
|                 .eq(pageWrap.getModel().getId() != null, QuartzJob::getId, pageWrap.getModel().getId()) | 
|                 .eq(pageWrap.getModel().getParams() != null, QuartzJob::getParams, pageWrap.getModel().getParams()) | 
|                 .like(pageWrap.getModel().getBeanName() != null, QuartzJob::getBeanName, pageWrap.getModel().getBeanName()) | 
|                 .eq(pageWrap.getModel().getState() != null, QuartzJob::getState, pageWrap.getModel().getState()) | 
|                 .eq(pageWrap.getModel().getRemark() != null, QuartzJob::getRemark, pageWrap.getModel().getRemark()) | 
|                 .like(pageWrap.getModel().getModule() != null, QuartzJob::getModule, pageWrap.getModel().getModule()) | 
|         ; | 
|         return PageData.from(quartzJobMapper.selectPage(page, queryWrapper)); | 
|     } | 
|   | 
|   | 
|     public void updateById(QuartzJob model) { | 
|         QuartzJob quartzJob = quartzJobMapper.selectById(model.getId()) ; | 
|         if (Objects.isNull(quartzJob)) { | 
|             throw new BusinessException(ResponseStatus.DATA_EMPTY); | 
|         } | 
|         pause(model.getId());//暂停 | 
|         //先删除就任务 | 
|         quartzManage.deleteJob(model.getId()); | 
|         quartzJob.setState(JobState.JOB_STOP.getStatus()); | 
|         quartzJobMapper.updateById(quartzJob) ; | 
|     } | 
| } |