项目中有一个要求,添加一个计划,计划又开始时间,结束时间,跳过日期(选择的日期和周六,周天),根据任务周期,来生成定时任务
1、两个数组处理逻辑(存在数组a(从小到大),b,当b的数据处于a的i和i+1之间时,给a的i+1到最后一个数据的值增1,)
public static void main(String[] args) {
ArrayList<Integer> integers = new ArrayList<>();
integers.add(2);
integers.add(4);
integers.add(6);
integers.add(8);
integers.add(10);
ArrayList<Integer> integers1 = new ArrayList<>();
integers1.add(3);
integers1.add(6);
integers1.add(9);
for (int i = 0; i < integers1.size(); i++) {
Integer integer = integers1.get(i);
for (int j = 0; j <integers.size() ; j++) {
if (integers.size() >j+1) {
Integer integer1 = integers.get(j);
Integer integer2 = integers.get(j + 1);
if (integer1 < integer && integer <integer2){
for (int k = j+1; k <integers.size() ; k++) {
Integer integer3 = integers.get(k);
System.out.println(integer3);
integers.set(k,integer3+1);
}
}
continue;
}
}
}
System.out.println(integers);
}
运行结果:
[2, 5, 8, 11, 13]
2、项目中时间代码处理逻辑:
a、跳过日期:
//查询开启的保养计划,且开始时间小于当前时间(取当前日期的最后一秒)的
LambdaQueryWrapper<UpKeepPlan> planWrapper = new LambdaQueryWrapper<>();
planWrapper.eq(UpKeepPlan::getDelFlag,"0");
planWrapper.eq(UpKeepPlan::getType,"0");
planWrapper.eq(UpKeepPlan::getIsEnable,"2");
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format1 = format.format(date);
String substring = format1.substring(0, 10);
String substring2= substring +"23:59:59";
//获取开始时间小于当前之前的时间
planWrapper.le(UpKeepPlan::getStartTime,substring2);
//结束时间大于当前时间的
planWrapper.ge(UpKeepPlan::getEndTime,substring2);
List<UpKeepPlan> list = upKeepPlanService.list(planWrapper);
for (int i = 0; i <list.size() ; i++) {
UpKeepPlan upKeepPlan = list.get(i);
//当前时间 - 任务开始时间 < 24小时则添加对应计划下的实体和标准的任务
//当前时间戳
long l = System.currentTimeMillis();
//计划开启时间
long startTime = upKeepPlan.getStartTime().getTime();
//计划的结束时间
long endTime = upKeepPlan.getEndTime().getTime();
//一、处理所有跳过的日期
//1.1跳过日期的集合
String skipTime = upKeepPlan.getSkipTime();
List<String> skipTimes= Arrays.asList(skipTime.split(","));
//1.2获取开始时间到结束时间的所有周天和周日的日期
HashMap<String, List<String>> weekend = this.getWeekend(upKeepPlan.getStartTime(), upKeepPlan.getEndTime());
//1.21获取开始时间到结束时间的所有周六
List<String> list2 = weekend.get("6");
//1.22获取开始时间到结束时间的所有周天
List<String> list3 = weekend.get("7");
//1.3如果周六跳过,则把日期内所有周六加到跳过集合内容,
if (upKeepPlan.getIsSkipSaturday().equals("2")){
skipTimes.addAll(list2);
}
//1.3如果周天跳过则把所有周天日期加到跳过集合集合内
if (upKeepPlan.getIsSkipSunday().equals("2")){
skipTimes.addAll(list3);
}
//1.4处理开始时间到时间跳过所有时间段转换为日期,去重
ArrayList<Date> skipDates = new ArrayList<>();
for (int j = 0; j <skipTimes.size() ; j++) {
String s = skipTimes.get(j);
String format2 = format.format(s);
skipDates.add(format.parse(format2));
}
//1.5去重
Set<Date> hashSet = new HashSet<>(skipDates);
List<Date> skipTimeList = new ArrayList<>(hashSet);
//二、处理开始时间到结束时间的分区
//2.1结束时间戳 - 开始时间戳 = 中间的时间戳
Long gapTime = endTime - startTime;
//周期单位
String unit = upKeepPlan.getUnit();
//周期数据
Long cycle = upKeepPlan.getCycle();
if (unit.equals("0")){
}else if (unit.equals("1")){
//周
cycle= cycle*7;
}else if (unit.equals("2")){
//月
cycle= cycle* 30;
}else if (unit.equals("3")){
//季度
cycle= cycle*120;
}else if (unit.equals("4")){
//年
cycle= cycle*365;
}
//周期转换成时间戳
cycle= cycle* 1000*60*60*24;
//任务轮数n
int n = (int) Math.ceil((double)gapTime/cycle);
ArrayList<Long> times = new ArrayList<>();
for (int n1 = n; n1 > 0; n1--) {
startTime = startTime +n*1000*60*60*24;
n= n -1;
times.add(0,startTime);
}
//时间分区和跳过时间处理
for (int m = 0; m < skipTimeList.size(); m++) {
Date date1 = skipTimeList.get(m);
long time = date1.getTime();
for (int j = 0; j <times.size() ; j++) {
if (times.size() >j+1) {
Long time1 = times.get(j);
Long time2 = times.get(j + 1);
if (time1 < time && time <time2){
for (int k = j+1; k <times.size() ; k++) {
Long integer3 = times.get(k);
System.out.println(integer3);
times.set(k,integer3+1000*60*60*24);
}
}
}
}
}
//把结束时间添加到时间分区的最后一个
times.add(endTime);
HashMap<Integer, HashMap<Long, Long>> timeHashMap = new HashMap<>();
for (int j = 0; j < times.size(); j++) {
HashMap<Long, Long> longLongHashMap = new HashMap<>();
longLongHashMap.put(times.get(j),times.get(j+1));
timeHashMap.put(j,longLongHashMap);
}
for (int j = 0; j <times.size() ; j++) {
Long startTime1 = times.get(i);
Date date1 = new Date();
//当前时间戳
long time = date1.getTime();
if (j+1<times.size()){
//当前时间 - 开始时间 < 24小时时间戳
Long oneDayTimes =1000*60*60*24L;
Long nextTime = times.get(j + 1);
//如果当前时间大于开始时间,且小于下一个时间节点,并且 当前时间戳-开始时间戳<一天的时间戳
if (startTime1< time && time <nextTime && (time-startTime1) <oneDayTimes){
//查询计划下的资源类下的资源实体ID和保养标准ID集合
LambdaQueryWrapper<PlanRelation> relationWrapper = new LambdaQueryWrapper<>();
relationWrapper.eq(PlanRelation::getPlanId,upKeepPlan.getPlanId());
List<PlanRelation> list1 = relationService.list(relationWrapper);
for (int m = 0; m < list1.size(); m++) {
PlanRelation planRelation = list1.get(m);
UpKeepTask upKeepTask = new UpKeepTask();
//任务执行人
upKeepTask.setUserId(upKeepPlan.getHead());
//资源实体ID
upKeepTask.setObjectId(planRelation.getObjectId());
//保养标准ID
upKeepTask.setCriterionId(planRelation.getCriterionId());
//任务开始时间(当前日期,如果是周六,且周六周末跳过则加2天,如果是周六,周末跳过加一天)
String weekOfDate = this.getWeekOfDate(new Date());
if (upKeepPlan.getIsSkipSaturday().equals("2") && upKeepPlan.getIsSkipSunday().equals("2")){
//如果今天是周六 开始时间加两天
if (weekOfDate.equals("6")){
startTime1 = startTime1 +2*oneDayTimes;
}
//如果今天是周天,开始时间加一天
else if (weekOfDate.equals("7")){
startTime1= startTime1 +oneDayTimes;
}
}//周六跳过,周天不跳过
else if (upKeepPlan.getIsSkipSaturday().equals("2") && !upKeepPlan.getIsSkipSunday().equals("2")){
//今日是周六
if (weekOfDate.equals("6")){
startTime1 = startTime1 +oneDayTimes;
}
}//周天跳过,周六不跳过
else if (!upKeepPlan.getIsSkipSaturday().equals("2") && upKeepPlan.getIsSkipSunday().equals("2")){
if (weekOfDate.equals("7")){
startTime1 = startTime1 +oneDayTimes;
}
}
upKeepTask.setStartTime(new Date(startTime1));
//结束时间
upKeepTask.setEndTime(new Date(nextTime));
//任务轮数
upKeepTask.setCriterionOrder(m+1);
//任务总轮数
upKeepTask.setNumber(times.size());
//添加任务
Boolean aBoolean = taskService.addUpKeepTask(upKeepTask);
System.out.println(aBoolean+":"+upKeepTask);
}
}
}else {
continue;
}
}
}