UnexecutedInspectionTaskJob.java
10.8 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.skua.modules.inspection.quartz;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.core.util.DateUtils;
import com.skua.modules.inspection.entity.InspectionPlan;
import com.skua.modules.inspection.entity.InspectionTask;
import com.skua.modules.inspection.service.IInspectionPlanService;
import com.skua.modules.inspection.service.IInspectionTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* <pre>
* 巡检未执行任务-定时任务
* </pre>
*
* @author Xie Chao
* @version 1.0,4/17/23 3:40 PM
*/
@Slf4j
public class UnexecutedInspectionTaskJob implements Job {
@Autowired
private IInspectionTaskService inspectionTaskService;
@Autowired
private IInspectionPlanService inspectionPlanService;
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
log.info("巡检未执行任务-开始");
Date date = new Date();
// Date date = DateUtils.parseDate(startDate,"yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR, -1);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date nowDate = calendar.getTime();
//得到符合时间的巡检计划
QueryWrapper<InspectionPlan> inspectionPlanQueryWrapper = new QueryWrapper<>();
inspectionPlanQueryWrapper.eq("del_flag", 0);
List<InspectionPlan> inspectionPlanList = inspectionPlanService.list(inspectionPlanQueryWrapper);
InspectionTask inspectionTask = null;
for (InspectionPlan inspectionPlan : inspectionPlanList) {
inspectionTask = new InspectionTask();
//判断当天的开始时间
String startTime = new SimpleDateFormat("yyyy-MM-dd").format(inspectionPlan.getPlanStartDate()) + " " + inspectionPlan.getPlanTaskStartTime() + ":00";
String endTime = new SimpleDateFormat("yyyy-MM-dd").format(inspectionPlan.getPlanEndDate()) + " " + "23:59:59";
//判断当前时间是否在巡检计划的有效时间
if (isEffectiveDate(nowDate, DateUtils.parse(startTime, "yyyy-MM-dd HH:mm:ss"), DateUtils.parse(endTime, "yyyy-MM-dd HH:mm:ss"))) {
//根据持续时长、计划开始时间和计划结束时间,得到巡检时间的时间段分组
List<String[]> cycleList = new ArrayList<>();
if (StringUtils.isNotBlank(inspectionPlan.getContinueTime()) && Double.parseDouble(inspectionPlan.getContinueTime()) != 0) {
cycleList = getCycleList(Integer.parseInt(inspectionPlan.getContinueTime()), startTime, endTime);
}
//根据当前时间得到该时间所属的时间段
SimpleDateFormat taskSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
String nowTaskTime = taskSimpleDateFormat.format(nowDate);
//当前时间段对应的巡检周期
String[] nowPlanTaskDateArr = null;
int inspectionCycleIndex = 0;
for (String[] cycleArr : cycleList) {
inspectionCycleIndex++;
if (isEffectiveDate(taskSimpleDateFormat.parse(nowTaskTime), taskSimpleDateFormat.parse(cycleArr[0]), taskSimpleDateFormat.parse(cycleArr[1]))) {
nowPlanTaskDateArr = cycleArr;
break;
}
}
//如果不设置时间间隔
if (nowPlanTaskDateArr == null) {
nowPlanTaskDateArr = new String[2];
nowPlanTaskDateArr[0] = startTime;
nowPlanTaskDateArr[1] = endTime;
}
//通过巡检计划ID,得到该计划的上一次任务执行时间
QueryWrapper<InspectionTask> inspectionTaskQueryWrapper = new QueryWrapper<>();
inspectionTaskQueryWrapper.eq("inspection_plan_id", inspectionPlan.getId());
inspectionTaskQueryWrapper.eq("inspection_cycle_index", inspectionCycleIndex);
inspectionTaskQueryWrapper.eq("inspection_cycle_start_date", nowPlanTaskDateArr[0]);
InspectionTask lastInspectionTask = inspectionTaskService.getOne(inspectionTaskQueryWrapper);
//判断该计划的上次任务开始周期和当前日期的是否相同
if (lastInspectionTask != null) {
continue;
}
if (inspectionPlan.getDelFlag() == 1) {
continue;
}
//如果计划为单次常规任务,并且上次任务状态为完成,则不在需要显示该任务
if ("2".equals(inspectionPlan.getTaskType())) {
inspectionTaskQueryWrapper = new QueryWrapper<>();
inspectionTaskQueryWrapper.eq("inspection_plan_id", inspectionPlan.getId());
lastInspectionTask = inspectionTaskService.getOne(inspectionTaskQueryWrapper);
if (lastInspectionTask != null) {
continue;
}
}
if (date.after(DateUtils.parse(nowPlanTaskDateArr[1],"yyyy-MM-dd HH:mm:ss"))) {
inspectionTask.setId("");
inspectionTask.setInspectionPlanId(inspectionPlan.getId());
inspectionTask.setTaskName(inspectionPlan.getPlanName());
inspectionTask.setTaskState("3");
if (nowPlanTaskDateArr != null) {
inspectionTask.setInspectionCycleStartDate(taskSimpleDateFormat.parse(nowPlanTaskDateArr[0]));
}
inspectionTask.setTaskDuration(inspectionPlan.getTaskDuration());
inspectionTask.setDepartId(inspectionPlan.getDepartId());
inspectionTask.setInspectionWorkingGroupId(inspectionPlan.getInspectionWorkingGroupId());
inspectionTask.setTaskType(inspectionPlan.getTaskType());
inspectionTask.setInspectionCycleIndex(inspectionCycleIndex);
inspectionTask.setPlanStartDate(inspectionPlan.getPlanStartDate());
inspectionTask.setPlanEndDate(inspectionPlan.getPlanEndDate());
inspectionTask.setPlanTaskStartTime(inspectionPlan.getPlanTaskStartTime());
inspectionTask.setContinueTime(inspectionPlan.getContinueTime());
inspectionTask.setInspectionWorkingGroupName("");
inspectionTask.setModuleType("1");
inspectionTask.setDelFlag("1");
//userid为null,标识任务是未执行
inspectionTaskService.addInpsectionTask(inspectionPlan.getId(),null,inspectionCycleIndex,nowPlanTaskDateArr[0]);
}
}
}
log.info("巡检未执行任务-结束");
} catch (Exception e) {
e.printStackTrace();
log.error("巡检未执行任务-失败",e.getMessage());
}
}
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
public static List<String[]> getCycleList(int cycleNum, String startTime, String endTime) throws
ParseException {
System.out.println("统计的时间段为:从" + startTime + "到" + endTime);
Date endParse = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(endTime);
Long endLong = endParse.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//这个是你要转成后的时间的格式
endTime = sdf.format(new Date(endLong));
System.out.println("结束时间是:" + endTime);
int daysBetweenNum = daysBetween(startTime, endTime);
int cycleForNum = daysBetweenNum % cycleNum == 0 ? (daysBetweenNum / cycleNum) : (daysBetweenNum / cycleNum) + 1;
System.out.println("两日期间相隔的天数为:" + daysBetweenNum);
System.out.println("周期选择是:" + cycleNum + "小时一周期, 则切割出来的周期存在个数:" + cycleForNum);
Date startParse = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(startTime);
Long startLong = startParse.getTime();
Long calculationLong = startLong;
List<String[]> cycleList = new ArrayList<>();
String[] cycleArr = null;
for (int i = 1; i <= cycleForNum; i++) {
String startStr = sdf.format(new Date(calculationLong));
Date startStrParse = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(startStr);
Long startStrLong = startStrParse.getTime();
Long cycleLong = cycleNum * 3600000L;
calculationLong = startStrLong + cycleLong;
String endStr = endTime;
if (calculationLong <= endLong) {
endStr = sdf.format(new Date(calculationLong));
}
cycleArr = new String[]{startStr+":00", endStr+":00"};
cycleList.add(cycleArr);
}
return cycleList;
}
public static int daysBetween(String smdate, String bdate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar cal = Calendar.getInstance();
cal.setTime(sdf.parse(smdate));
long time1 = cal.getTimeInMillis();
cal.setTime(sdf.parse(bdate));
long time2 = cal.getTimeInMillis();
long between_days = (time2 - time1) / (1000 * 3600);
return Integer.parseInt(String.valueOf(between_days));
}
}