package com.sandu.ximon.admin.schedule; import com.alibaba.fastjson.JSONObject; import com.sandu.common.util.SpringContextHolder; import com.sandu.ximon.admin.service.LightService; import com.sandu.ximon.admin.service.LightTaskPoleRelationService; import com.sandu.ximon.dao.domain.Light; import com.sandu.ximon.dao.domain.LightTask; import com.sandu.ximon.dao.domain.LightTaskPoleRelation; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.util.*; import java.util.stream.Collectors; /** * 单灯同步时间定时 */ @Component @Slf4j @AllArgsConstructor @EnableScheduling public class LightTimeSynchronizationSchedule { private final LightService lightService; private final LightTaskPoleRelationService lightTaskPoleRelationService; //每天02:00 开始执行 @Scheduled(cron = "0 0 2 * * ? ") public void UserSubjectRefund() { //所有单灯数据 List lightList = lightService.list(); //单灯任务 List lightTaskPoleRelationList = lightTaskPoleRelationService.list(); /** * 去除无效数据 */ lightTaskPoleRelationList = lightTaskPoleRelationList.stream().filter(bean -> bean.getDeviceScheduled() != null).collect(Collectors.toList()); Map map = new HashMap<>(); for (LightTaskPoleRelation bean : lightTaskPoleRelationList) { LightTask lightTask = JSONObject.parseObject(bean.getDeviceScheduled(), LightTask.class); map.put(bean.getDeviceCode() + bean.getLightAddress(), lightTask); } for (Light light : lightList) { //TODO if (!light.getDeviceCode().equals("32313243305007ff8711ffff")) { continue; } if (map.get(light.getDeviceCode() + "0001") != null) { /** * 参数1:单灯信息 * 参数2:单灯任务 */ lightService.timeSynchronization(light, "0001", map.get(light.getDeviceCode() + "0001")); } else { /** * 参数1:单灯信息 * 参数2:任务为null */ lightService.timeSynchronization(light, "0001", null); } if (map.get(light.getDeviceCode() + "0002") != null) { /** * 参数1:单灯信息 * 参数2:单灯任务 */ lightService.timeSynchronization(light, "0002", map.get(light.getDeviceCode() + "0002")); } else { /** * 参数1:单灯信息 * 参数2:任务为null */ lightService.timeSynchronization(light, "0002", null); } } } }