UnexecutedInspectionTaskJob.java 10.8 KB
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));

    }

}