package com.ruoyi.web.controller.task; import cn.hutool.core.date.DateUtil; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.qdtl.domain.PlanUser; import com.ruoyi.qdtl.domain.TlInspectionPlan; import com.ruoyi.qdtl.domain.TlPlanRecord; 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; /** * @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 RedisCache redisCache; /** * 根据巡检计划生成下一日巡检计划数据 */ public void createPlan() { // 查询符合条件的计划。根据当前时间点,如果是晚20点~24点之间,就去生成第二天的计划,如果是凌晨0点~1点,就生成当天的计划 // 生成过的计划,标记已生成放到redis中 int hour = DateUtil.hour(new Date(), true); String queryDate; 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) { // 查询计划的人员.根据计划id查询人员以及设备号 List userList = planService.queryPlanUser(inspectionPlan.getId()); if (userList != null && userList.size() > 0) { // 组装计划数据 for (PlanUser planUser : userList) { TlPlanRecord planRecord = new TlPlanRecord(); planRecord.setPlanId(planUser.getPlanId()); planRecord.setPlanDate(queryDate); planRecord.setPlanName(inspectionPlan.getPlanName()); planRecord.setUserId(planUser.getUserId()); planRecord.setNickName(planUser.getNickName()); planRecord.setCard(planUser.getCard()); planRecord.setScore(inspectionPlan.getScore()); planRecords.add(planRecord); } } } if (planRecords.size() > 0) { planRecordService.batchInsert(planRecords); redisCache.setCacheObject("tl:planrecord:" + queryDate, true); } } } }