whycxzp
2025-06-12 1757c698bc9f37844d27e6e8f1eaa5232d2d670c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
        }
 
    }
 
 
}