package com.ruoyi.web.controller.task; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.core.text.Convert; import com.ruoyi.qdtl.domain.PlanUser; import com.ruoyi.qdtl.domain.TlInspectionLocation; import com.ruoyi.qdtl.domain.TlInspectionPlan; import com.ruoyi.qdtl.domain.TlPlanRecord; import com.ruoyi.qdtl.service.ITlInspectionLocationService; import com.ruoyi.qdtl.service.ITlInspectionPlanService; import com.ruoyi.qdtl.service.ITlPlanRecordService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.concurrent.TimeUnit; /** * @Description: 巡检计划任务 * @Author: huangcheng * @Date: 2022/3/30 * @Version V1.0 */ @Component("planTask") public class PlanTask { @Autowired private ITlInspectionPlanService planService; @Autowired private ITlPlanRecordService planRecordService; @Autowired private ITlInspectionLocationService locationService; @Autowired private RedisCache redisCache; /** * 根据巡检计划生成下一日巡检计划数据 * params参数:如果传了参数,就根据参数的日期来生成计划(为了测试方便) */ public void createPlan(String params) { // 查询符合条件的计划。根据当前时间点,如果是晚20点~24点之间,就去生成第二天的计划,如果是凌晨0点~1点,就生成当天的计划 // 生成过的计划,标记已生成放到redis中 String queryDate; if (StrUtil.isNotBlank(params)) { queryDate = params; } else { int hour = DateUtil.hour(new Date(), true); if (20 <= hour && hour <= 24) { queryDate = DateUtil.formatDate(DateUtil.tomorrow()); } else if (0 <= hour && hour <= 1) { queryDate = DateUtil.today(); } else { return; } } Boolean recordFlag = redisCache.getCacheObject("tl:planrecord:" + queryDate); if (recordFlag != null && recordFlag) { return; } List planList = planService.queryPlanByDate(queryDate); if (planList != null && planList.size() > 0) { List planRecords = new ArrayList<>(); for (TlInspectionPlan inspectionPlan : planList) { // 查询是否满足日期的要求 int week = DateUtil.dayOfWeek(DateUtil.parseDate(queryDate)); if (!CollUtil.contains(StrUtil.split(inspectionPlan.getWeeks(), ","), Convert.toStr(week))) { continue; } // 查询计划的人员.根据计划id查询人员以及设备号 List userList = planService.queryPlanUser(inspectionPlan.getId()); // 查询计划的线路及点位 List lineLocations = locationService.queryLocationByLineId(inspectionPlan.getLineId()); if (userList != null && userList.size() > 0) { // 组装计划数据 for (PlanUser planUser : userList) { for (TlInspectionLocation lineLocation : lineLocations) { for (String s : StrUtil.split(inspectionPlan.getTimeUnit(), ",")) { for (int i = 0; i < inspectionPlan.getXunNum(); i++) { TlPlanRecord planRecord = new TlPlanRecord(); planRecord.setPlanId(planUser.getPlanId()); planRecord.setPlanDate(queryDate); planRecord.setStartTime(s.split("~")[0]); planRecord.setEndTime(s.split("~")[1]); planRecord.setPlanName(inspectionPlan.getPlanName()); planRecord.setUserId(planUser.getUserId()); planRecord.setNickName(planUser.getNickName()); planRecord.setCard(planUser.getCard()); planRecord.setScore(inspectionPlan.getScore()); planRecord.setCheckpointCard(lineLocation.getLocationCode()); planRecord.setLineId(inspectionPlan.getLineId()); planRecord.setLineName(""); planRecord.setAreaId(lineLocation.getAreaId()); planRecords.add(planRecord); } } } } } } if (planRecords.size() > 0) { planRecordService.batchInsert(planRecords); redisCache.setCacheObject("tl:planrecord:" + queryDate, true, 2, TimeUnit.DAYS); } } } }