郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.gk.hotwork.doublePrevention.scheduls;
 
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.Exception.E;
import com.gk.hotwork.doublePrevention.entity.PreventDangerCheckWork;
import com.gk.hotwork.doublePrevention.service.DangerService;
import com.gk.hotwork.doublePrevention.service.baseService.PreventDangerCheckTaskService;
import com.gk.hotwork.doublePrevention.service.baseService.PreventDangerCheckWorkService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.PostConstruct;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
@Component
public class PreventResetWorkTimeSchedule {
 
    private static final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(PreventResetWorkTimeSchedule.class);
 
    @Autowired
    private PreventDangerCheckWorkService preventDangerCheckWorkService;
    @Autowired
    private PreventDangerCheckTaskService preventDangerCheckTaskService;
    @Autowired
    private DangerService dangerService;
 
 
//    @Transactional
//    @Scheduled(cron = "0/40 * * * * ?")
//    @Scheduled(cron = "0 0/1 * * * ?")
//    @Scheduled(cron = "0 0/30 * * * ?")
//    @Scheduled(cron = "0 0/20 * * * ?")
    @Scheduled(cron = "0 0 0/1 * * ?")  // 小时
 
    public void resetWork(){
 
        logger.info("【】检索时间异常作业");
        System.out.println("【】检索时间异常作业");
 
        Date date= new Date();
        Date errorTime = new Date(date.getTime() - 10 * 60 * 1000L);
 
        List<PreventDangerCheckWork> errorWorkList = preventDangerCheckWorkService.listErrorCheckWork(errorTime);
        if (ObjectUtils.isNotEmpty(errorWorkList)){
 
            System.out.println("处理异常作业");
 
            for (PreventDangerCheckWork workById : errorWorkList) {
 
                Long validTimeSecond = null;
                Long  cycleSecond  = null;
                Date resetTime  = null;
                Date startTime = null;
//                Date validTime = null;
 
                //如果是第一次调度,以firstTime为标准;否则以nextTime为标准
                if (workById.getValidTimeUnit() == 1) {
                    //如果时间单位是分钟
                    validTimeSecond = workById.getValidTime() * 60 * 1000L;
                }else if (workById.getValidTimeUnit() == 2) {
                    //如果时间单位是小时
                    validTimeSecond = workById.getValidTime() * 60 * 60 * 1000L;
                }else if (workById.getValidTimeUnit() == 3) {
                    //如果时间单位是日
                    validTimeSecond =  workById.getValidTime() * 24 * 60 * 60 * 1000L;
                }else if (workById.getValidTimeUnit() == 4) {
                    //如果时间单位是月
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(workById.getNextCheckTime());//设置起时间
                    calendar.add(Calendar.MONTH, workById.getValidTime());//增加N个月
                    Date validTime = calendar.getTime();
                    if (ObjectUtils.isNotEmpty(workById.getLastCheckTime())){
                        validTimeSecond =validTime.getTime() - workById.getLastCheckTime() .getTime();
                    }
                    validTimeSecond =validTime.getTime() - workById.getFirstStartTime() .getTime();
                }
 
                //解析调度周期时间间隔,work的下次执行时间
                if (workById.getCheckCycleUnit() == 1) {
                    //如果时间单位是分钟
                    cycleSecond = workById.getCheckCycle() * 60 * 1000L;
                }else if (workById.getCheckCycleUnit() == 2) {
                    //如果时间单位是小时
                    cycleSecond = workById.getCheckCycle() * 60 * 60 * 1000L;
                }else if (workById.getCheckCycleUnit() == 3) {
                    //如果时间单位是日
                    cycleSecond = workById.getCheckCycle() * 24 * 60 * 60 * 1000L;
                }else if (workById.getCheckCycleUnit() == 4) {
 
//                    cycleSecond =  workById.getCheckCycle() * 30 * 24 * 60 * 60 * 1000L;
                    //如果时间单位是月
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(workById.getNextCheckTime());//设置起时间
                    calendar.add(Calendar.MONTH, workById.getCheckCycle());//增加N个月
                    cycleSecond = calendar.getTime().getTime();
                }
                //任务周期 毫秒数
                Long workCycle = cycleSecond + validTimeSecond;
 
                //定位重置任务周期的初始时间
                resetTime = workById.getFirstStartTime();
 
               // System.out.println("周期解析完成,设置上次执行时间");
 
                //循环定位任务周期,时间定位到当前时间之后,最近的一个周期点
                while (resetTime.getTime() < date.getTime()){
                    resetTime = new Date(resetTime.getTime() + workCycle);
                }
                //重置时间回滚一个周期,定位为当前任务创建时间
                startTime =new Date(resetTime.getTime() - workCycle);
//
//                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
//
//                System.out.println("任务开始时间" + dateFormat.format(startTime));
//
//                System.out.println("任务重置时间" + dateFormat.format(resetTime));
//
//                System.out.println(resetTime.getTime() - date.getTime());
 
                long time =  resetTime.getTime() - date.getTime();
 
                //如果距离下个周期还有一小时以上,重置为上一个周期,立即调度任务;否则只重置时间到下一个周期,先设为1分钟
                if (time > 60 * 1000L ){
                    //System.out.println("定位任务下一周期完成");
                    //重置作业
                    int step = 1;
                    int result = preventDangerCheckWorkService.resetErrorWork(startTime, workById.getId());
                    if (result == 1){
                        step ++;
                    }else {
                        throw new BusinessException(E.DATA_PARAM_CHECK_INVALID ,"重置异常作业时间失败");
                    }
                    //   立即生成一次任务
                    if (step == 2){
                        dangerService.createAutoTask(workById.getId());
                        logger.info("异常任务已重置,立即生成一条可执行任务");
                        System.out.println("异常任务已重置,立即生成一条可执行任务");
                        System.out.println("已重置");
                    }
//                System.out.println("异常作业已重置");
                }else {
                    //重置作业时间
                    int result = preventDangerCheckWorkService.resetErrorWork(resetTime,  workById.getId());
                    if (result < 1){
                        throw new BusinessException(E.DATA_PARAM_CHECK_INVALID ,"重置异常作业时间失败");
                    }
                }
                //System.out.println("定位任务下一周期完成");
                //重置作业
                int step = 1;
                int result = preventDangerCheckWorkService.resetErrorWork(startTime, workById.getId());
                if (result == 1){
                    step ++;
                }else {
                    throw new BusinessException(E.DATA_PARAM_CHECK_INVALID ,"重置异常作业时间失败");
                }
                //   立即生成一次任务
//                dangerService.createAutoTask(workById.getId());
//                logger.info("异常任务已重置,立即生成一条可执行任务");
//                System.out.println("异常任务已重置,立即生成一条可执行任务");
                System.out.println("重置异常作业完成");
                }
 
        }else {
            logger.info("【】无异常需要处理");
            System.out.println("【】无异常需要处理");
        }
    }
}