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<AlarmInspection> inspectionList = new ArrayList<>();
|
//查询新的电源告警
|
List<AlarmInspection> powerAlarmList = powerAlarmService.getListGreatThan(alarmInspectionId.getPowerAlarmId());
|
//查询新的设备告警
|
List<AlarmInspection> devAlarmList = devAlarmService.getListGreatThan(alarmInspectionId.getDevAlarmId());
|
//查询新的电池告警
|
List<AlarmInspection> 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<AlarmInspection> alarmExistList =inspectionService.getAlarmExistList();
|
//按电源,设备,电池告警进行分类,再分别进行查询
|
List<AlarmInspection> powerAlarmInspectionList = alarmExistList.stream().filter(alarmInspection -> alarmInspection.getType() == 1).collect(Collectors.toList());
|
List<Long> powerAlarmNumList = powerAlarmInspectionList.stream().map(AlarmInspection::getAlmNum).collect(Collectors.toList());
|
|
List<AlarmInspection> devAlarmInspectionList = alarmExistList.stream().filter(alarmInspection -> alarmInspection.getType() == 2).collect(Collectors.toList());
|
List<Long> devAlarmNumList = devAlarmInspectionList.stream().map(AlarmInspection::getAlmNum).collect(Collectors.toList());
|
|
List<AlarmInspection> battAlarmInspectionList = alarmExistList.stream().filter(alarmInspection -> alarmInspection.getType() == 3).collect(Collectors.toList());
|
List<Long> battAlarmNumList = battAlarmInspectionList.stream().map(AlarmInspection::getAlmNum).collect(Collectors.toList());
|
|
//告警消失了的巡检实时表id
|
List<Long> notExistIdList = new ArrayList<>();
|
if(powerAlarmNumList.size() != 0){
|
//查询id是否还存在
|
List<Long> 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<Long> 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<Long> 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);
|
}
|
|
}
|
|
|
}
|