package com.whyc.schedule; import com.whyc.pojo.db_pwrdev_alarm.PwrdevAlarm; import com.whyc.pojo.web_site.AlarmInspection; import com.whyc.pojo.web_site.AlarmInspectionId; import com.whyc.service.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; /** * 有两个定时任务:1.告警产生,加入到巡检实时表,2.告警是否消失,同步到巡检实时表 */ @EnableScheduling @Component public class AlarmInspectionSchedule { @Autowired private AlarmInspectionService inspectionService; @Autowired private AlarmInspectionIdService alarmIdService; @Autowired private PwrdevAlarmService powerAlarmService; @Autowired private DevalarmDataService devAlarmService; @Autowired private BattalarmDataService battAlarmService; /** * 数据4秒钟获取一次 * 告警产生,加入到巡检实时表 * */ @Scheduled(cron = "0/4 * * * * ? ") @Transactional public void getAndRecord(){ AlarmInspectionId alarmInspectionId = alarmIdService.get(); List inspectionList = new ArrayList<>(); //查询新的电源告警 List powerAlarmList = powerAlarmService.getListGreatThan(alarmInspectionId.getPowerAlarmId()); //查询新的设备告警 List devAlarmList = devAlarmService.getListGreatThan(alarmInspectionId.getDevAlarmId()); //查询新的电池告警 List battAlarmList = battAlarmService.getListGreatThan(alarmInspectionId.getBattAlarmId()); inspectionList.addAll(powerAlarmList); inspectionList.addAll(devAlarmList); inspectionList.addAll(battAlarmList); if(inspectionList.size() != 0) { //插入到巡检实时表 inspectionService.addBatch(inspectionList); //获取本次截止的告警id,并更新巡检告警的告警id Long powerAlmId = powerAlarmList.get(powerAlarmList.size() - 1).getAlmNum(); Long devAlmId = devAlarmList.get(devAlarmList.size() - 1).getAlmNum(); Long battAlmId = battAlarmList.get(battAlarmList.size() - 1).getAlmNum(); alarmInspectionId.setPowerAlarmId(powerAlmId); alarmInspectionId.setDevAlarmId(devAlmId); alarmInspectionId.setBattAlarmId(battAlmId); alarmIdService.update(alarmInspectionId); } } /** * 数据4秒钟获取一次 * 告警是否消失,同步到巡检实时表 * */ @Scheduled(cron = "0/4 * * * * ? ") public void checkAlarm(){ List alarmExistList =inspectionService.getAlarmExistList(); //按电源,设备,电池告警进行分类,再分别进行查询 List powerAlarmInspectionList = alarmExistList.stream().filter(alarmInspection -> alarmInspection.getType() == 1).collect(Collectors.toList()); List powerAlarmNumList = powerAlarmInspectionList.stream().map(AlarmInspection::getAlmNum).collect(Collectors.toList()); List devAlarmInspectionList = alarmExistList.stream().filter(alarmInspection -> alarmInspection.getType() == 2).collect(Collectors.toList()); List devAlarmNumList = devAlarmInspectionList.stream().map(AlarmInspection::getAlmNum).collect(Collectors.toList()); List battAlarmInspectionList = alarmExistList.stream().filter(alarmInspection -> alarmInspection.getType() == 3).collect(Collectors.toList()); List battAlarmNumList = battAlarmInspectionList.stream().map(AlarmInspection::getAlmNum).collect(Collectors.toList()); //告警消失了的巡检实时表id List notExistIdList = new ArrayList<>(); if(powerAlarmNumList.size() != 0){ //查询id是否还存在 List powerNumListInDB = powerAlarmService.getNumListInDB(powerAlarmNumList); for (AlarmInspection alarm : powerAlarmInspectionList) { Long almNum = alarm.getAlmNum(); if(!powerNumListInDB.contains(almNum)){ notExistIdList.add(alarm.getId()); } } } if(devAlarmNumList.size() != 0){ //查询id是否还存在 List devNumListInDB = devAlarmService.getNumListInDB(devAlarmNumList); for (AlarmInspection alarm : devAlarmInspectionList) { Long almNum = alarm.getAlmNum(); if(!devNumListInDB.contains(almNum)){ notExistIdList.add(alarm.getId()); } } } if(battAlarmNumList.size() != 0){ //查询id是否还存在 List battNumListInDB = battAlarmService.getNumListInDB(battAlarmNumList); for (AlarmInspection alarm : battAlarmInspectionList) { Long almNum = alarm.getAlmNum(); if(!battNumListInDB.contains(almNum)){ notExistIdList.add(alarm.getId()); } } } if(notExistIdList.size() != 0){ //更新巡检实时表 inspectionService.updateIsExist(notExistIdList); } } }