package com.maibu.scheduled; import com.maibu.common.NettyCacheKey; import com.maibu.core.redis.RedisCache; import com.maibu.domain.DevicePlan; import com.maibu.domain.DeviceStatusRecordDTO; import com.maibu.mapper.DeviceStatusHistoryMapper; import com.maibu.memory.GlobalMemory; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Map; @Component public class ScheduledTask { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Autowired private RedisCache redisCache; @Autowired private DeviceStatusHistoryMapper deviceStatusHistoryMapper; //整点更新设备状态 @Scheduled(cron = "0 0 * * * ?") // @Scheduled(cron = "0 * * * * ?") public void executeHourlyTask() { System.out.println("整点定时任务执行时间: " + dateFormat.format(new Date())); String key = NettyCacheKey.deviceRunningStatusKey; // Collection keys = redisCache.keys(key); Collection keys = redisCache.getListKeyByPrefix(key); if (!CollectionUtils.isEmpty(keys)) { keys.forEach(k -> { DeviceStatusRecordDTO recordDTO = redisCache.getCacheObject(k); if (recordDTO != null && !CollectionUtils.isEmpty(recordDTO.getHistories())) { // recordDTO.getHistories().forEach(x -> { // deviceStatusHistoryMapper.insert(x); // }); deviceStatusHistoryMapper.insertBatch(recordDTO.getHistories()); } redisCache.deleteObject(k); }); } System.out.println("任务执行完成"); } //0点刷新任务创建状态 @Scheduled(cron = "0 0 0 * * ?") public void refreshDevicePlanTask() { System.out.println("0点定时刷新状态"); Map> map = GlobalMemory.devicePlanMap; if (!CollectionUtils.isEmpty(map.keySet())) { map.values().forEach(x -> { if (!CollectionUtils.isEmpty(x)) { x.forEach(y -> { y.setDayTriggerFlag(false); }); } }); } System.out.println("0点定时刷新状态执行完成"); } }