shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
package com.iplatform.scheduler.service;
 
import com.iplatform.model.po.S_scheduler;
import com.iplatform.scheduler.Constants;
import com.walker.db.page.GenericPager;
import com.walker.db.page.ListPageContext;
import com.walker.db.page.PageSearch;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.jdbc.service.BaseServiceImpl;
import org.springframework.stereotype.Service;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 调度任务数据库操作。
 * @author 时克英
 * @date 2022-09-07
 */
@Service
public class SchedulerServiceImpl extends BaseServiceImpl {
 
    private static final String SQL_PAGE_LIST_SEARCH = "select * from s_scheduler where dept=0 or dept=:dept order by create_time desc";
    private static final String SQL_PAGE_LIST = "select * from s_scheduler order by create_time desc";
 
    public GenericPager<S_scheduler> queryPageSchedulerList(long dept){
        PageSearch pageSearch = ListPageContext.getPageSearch();
        if(dept < 0){
            return this.selectSplit(SQL_PAGE_LIST, new Object[]{}, pageSearch.getPageIndex(), pageSearch.getPageSize(), new S_scheduler());
        }
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("dept", dept);
        return this.selectSplit(SQL_PAGE_LIST_SEARCH, parameters, pageSearch.getPageIndex(), pageSearch.getPageSize(), new S_scheduler());
    }
 
    /**
     * 查询正在运行(包含:暂停的)状态的调度器记录
     * @return
     */
    public List<S_scheduler> queryRunningSchedulers(){
        return this.select(new S_scheduler(), "where status >= ? and status <= ?", new Object[]{1,2});
    }
 
    /**
     * 调度器启动,状态更新
     * @param id
     */
    public void execUpdateSchedulerStarted(int id){
        S_scheduler e = new S_scheduler();
        e.setStatus(Constants.STATUS_START);
        e.setStart_time(Long.parseLong(DateUtils.getDateTimeSecondForShow()));
        this.update(e, "where id=?", new Object[]{id});
    }
 
    /**
     * 调度器(手动)停止,状态更新
     * @param id
     */
    public void execUpdateSchedulerStopped(int id){
        S_scheduler e = new S_scheduler();
        e.setStatus(Constants.STATUS_TERMINATE);
        e.setEnd_time(Long.parseLong(DateUtils.getDateTimeSecondForShow()));
        this.update(e, "where id=?", new Object[]{id});
    }
 
    /**
     * 调度器暂停,状态更新
     * @param id
     */
    public void execUpdateSchedulerPause(int id){
        S_scheduler e = new S_scheduler();
        e.setStatus(Constants.STATUS_PAUSE);
        e.setPause_time(Long.parseLong(DateUtils.getDateTimeSecondForShow()));
        this.update(e, "where id=?", new Object[]{id});
    }
 
    /**
     * 调度器运行完成,状态更新
     * @param id
     */
    public void execUpdateSchedulerDone(int id){
        S_scheduler e = new S_scheduler();
        e.setStatus(Constants.STATUS_END);
        e.setEnd_time(Long.parseLong(DateUtils.getDateTimeSecondForShow()));
        this.update(e, "where id=?", new Object[]{id});
    }
 
    public void execTestTransaction(boolean throwError){
        this.execUpdateSchedulerPause(1);
        if(throwError){
            throw new IllegalArgumentException("抛出测试异常,第一条数据应当无法更新");
        }
        this.execUpdateSchedulerPause(2);
    }
 
//    private static final String SQL_UPD_STATUS_STARTED = "update s_scheduler set status=1, start_time=? where id=?";
//    private static final String SQL_UPD_STATUS_STOPED = "update s_scheduler set status=-1, end_time=? where id=?";
//    private static final String SQL_UPD_STATUS_PAUSE = "update s_scheduler set status=2, pause_time=? where id=?";
//    private static final String SQL_UPD_STATUS_DONE = "update s_scheduler set status=9, end_time=? where id=?";
}