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.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 0/20 * * * ?") // @Scheduled(cron = "0 0/1 * * * ?") // @Scheduled(cron = "0/20 * * * * ?") public void resetWork(){ logger.info("【】检索时间异常作业"); System.out.println("【】检索时间异常作业"); Date date= new Date(); Date errorTime = new Date(date.getTime() - 10 * 60 * 1000L); List 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); //如果距离下个周期还有一小时以上,重置为上一个周期,立即调度任务;否则只重置时间到下一个周期 if (resetTime.getTime() - date.getTime() > 60 * 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("异常作业已重置"); }else { //重置作业时间 int result = preventDangerCheckWorkService.resetErrorWork(resetTime, workById.getId()); if (result < 1){ throw new BusinessException(E.DATA_PARAM_CHECK_INVALID ,"重置异常作业时间失败"); } } System.out.println("重置异常作业完成"); } }else { logger.info("【】无异常需要处理"); System.out.println("【】无异常需要处理"); } } }