doum
2025-09-29 1d064aa5ec2556155bbf116cef1d6d0ac5007acc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.doumee.service.timer;
 
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.springframework.stereotype.Service;
 
import javax.annotation.Resource;
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) {
        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) ;
        }
        return flag;
    }
 
    /**
     * 更新任务
     */
    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);
    }
        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())
                .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) ;
    }
}