|  |  | 
 |  |  | 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.service.business.third.model.PageData; | 
 |  |  | import com.doumee.service.business.third.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; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  | 
 |  |  |     private QuartzManage quartzManage; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 初始化加载定时任务 | 
 |  |  |      */ | 
 |  |  |     @PostConstruct | 
 |  |  |     public void init () { | 
 |  |  |         LambdaQueryWrapper<QuartzJob> queryWrapper = new LambdaQueryWrapper<>() ; | 
 |  |  |         queryWrapper.in(QuartzJob::getState, JobState.JOB_RUN.getStatus(),JobState.JOB_STOP.getStatus()); | 
 |  |  |         List<QuartzJob> jobList = quartzJobMapper.selectList(queryWrapper); | 
 |  |  |         jobList.forEach(quartzJob -> { | 
 |  |  |             CronTrigger cronTrigger = quartzManage.getCronTrigger(quartzJob.getId()) ; | 
 |  |  |             if (Objects.isNull(cronTrigger)){ | 
 |  |  |                 quartzManage.createJob(quartzJob); | 
 |  |  |             } else { | 
 |  |  |                 quartzManage.updateJob(quartzJob); | 
 |  |  |             } | 
 |  |  |         }); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 任务主键查询 | 
 |  |  |      */ | 
 |  |  |     public QuartzJob getById(Integer id) { | 
 |  |  | 
 |  |  |      * 新增任务 | 
 |  |  |      */ | 
 |  |  |     public int insert(QuartzJob quartzJob) { | 
 |  |  |         if(quartzJobMapper.selectCount(new QueryWrapper<QuartzJob>().lambda() | 
 |  |  |                 .eq(QuartzJob::getModule,quartzJob.getModule()) | 
 |  |  |                 .eq(QuartzJob::getBeanName,quartzJob.getBeanName())) >0){ | 
 |  |  |             throw  new BusinessException(ResponseStatus.DATA_EXISTS); | 
 |  |  |         } | 
 |  |  |         int flag = quartzJobMapper.insert(quartzJob) ; | 
 |  |  |         if (flag > 0){ | 
 |  |  |             quartzManage.createJob(quartzJob) ; | 
 |  |  | 
 |  |  |      * 更新任务 | 
 |  |  |      */ | 
 |  |  |     public int update(QuartzJob quartzJob) { | 
 |  |  |         if(quartzJobMapper.selectCount(new QueryWrapper<QuartzJob>().lambda() | 
 |  |  |                 .eq(QuartzJob::getModule,quartzJob.getModule()) | 
 |  |  |                 .ne(QuartzJob::getId,quartzJob.getId()) | 
 |  |  |                 .eq(QuartzJob::getBeanName,quartzJob.getBeanName())) >0){ | 
 |  |  |             throw  new BusinessException(ResponseStatus.DATA_EXISTS); | 
 |  |  |         } | 
 |  |  |         int flag = quartzJobMapper.updateById(quartzJob); | 
 |  |  |         if (flag > 0){ | 
 |  |  |             quartzManage.updateJob(quartzJob); | 
 |  |  | 
 |  |  |             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()) | 
 |  |  |                 .like(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) ; | 
 |  |  |     } | 
 |  |  | } |