zxl
2026-03-25 8819762ad58f77e606431fca4072c19e542e6055
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.tievd.jyz.service.impl;
 
import cn.hutool.core.date.DateUtil;
import com.tievd.jyz.dto.AlarmStatDTO;
import com.tievd.jyz.dto.AlarmTypeStatDTO;
import com.tievd.jyz.dto.DeviceStatusStatDTO;
import com.tievd.jyz.entity.vo.AlarmEventVO;
import com.tievd.jyz.entity.vo.AlarmOverviewVO;
import com.tievd.jyz.entity.vo.OrgAlarmCountVO;
import com.tievd.jyz.entity.vo.PartitionAlarmStatVO;
import com.tievd.jyz.mapper.CameraMapper;
import com.tievd.jyz.mapper.DeviceMapper;
import com.tievd.jyz.mapper.OilEventMapper;
import com.tievd.jyz.mapper.OiloutEventMapper;
import com.tievd.jyz.service.AlarmDataService;
import com.tievd.jyz.util.PercentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * @author yang'zhi'shui
 */
@Service
public class AlarmDataServiceImpl implements AlarmDataService {
 
    @Autowired
    private OilEventMapper oilEventMapper;
 
    @Autowired
    private OiloutEventMapper oiloutEventMapper;
 
    @Autowired
    private CameraMapper cameraMapper;
 
    @Autowired
    private DeviceMapper deviceMapper;
 
    @Override
    public AlarmOverviewVO overview(String today, String orgCode) {
        int oilMonthlyAlarmCount = oilEventMapper.monthlyAlarmCount(DateUtil.format(new Date(), "yyyy-MM"), orgCode);
        int oiloutMonthlyAlarmCount = oiloutEventMapper.monthlyAlarmCount(DateUtil.format(new Date(), "yyyy-MM"), orgCode);
        AlarmOverviewVO oilAlarmOverviewVO = oilEventMapper.alarmStat(today, orgCode);
        AlarmOverviewVO oiloutAlarmOverviewVO = oiloutEventMapper.alarmStat(today, orgCode);
        AlarmOverviewVO alarmOverviewVO = new AlarmOverviewVO();
        alarmOverviewVO.setMonthlyAlarmCount(oilMonthlyAlarmCount + oiloutMonthlyAlarmCount);
        alarmOverviewVO.setAlarmCount(oilAlarmOverviewVO.getAlarmCount() + oiloutAlarmOverviewVO.getAlarmCount());
        alarmOverviewVO.setDealedAlarmCount(oilAlarmOverviewVO.getDealedAlarmCount() + oiloutAlarmOverviewVO.getDealedAlarmCount());
        alarmOverviewVO.setConfirmedAlarmCount(oilAlarmOverviewVO.getConfirmedAlarmCount() + oiloutAlarmOverviewVO.getConfirmedAlarmCount());
        return alarmOverviewVO;
    }
 
    @Override
    public Map<String, List> alarmStat(int intervalDay, String endDay, String orgCode) {
        List<AlarmStatDTO> oilEventAlarmStatList = oilEventMapper.alarmStatRange(intervalDay, endDay, orgCode);
        List<AlarmStatDTO> oiloutEventAlarmStatList = oiloutEventMapper.alarmStatRange(intervalDay, endDay, orgCode);
        Map<String, AlarmStatDTO> oiloutMap = oiloutEventAlarmStatList.stream().collect(Collectors.toMap(AlarmStatDTO::getDate, Function.identity()));
        List<String> dateList = new ArrayList();
        List<Integer> alarmCountList = new ArrayList();
        List<Integer> confirmedCountList = new ArrayList();
        for (int i = 0; i < oilEventAlarmStatList.size(); i++) {
            AlarmStatDTO oildto = oilEventAlarmStatList.get(i);
            AlarmStatDTO oiloutDTO = oiloutMap.get(oildto.getDate());
            dateList.add(i, oildto.getDate());
            alarmCountList.add(i, oildto.getAlarmCount() + oiloutDTO.getAlarmCount());
            confirmedCountList.add(i, oildto.getConfirmedAlarmCount() + oiloutDTO.getConfirmedAlarmCount());
        }
        Map<String, List> result = new HashMap<>();
        result.put("dateArr", dateList);
        result.put("alarmCountArr", alarmCountList);
        result.put("confirmedCountArr", confirmedCountList);
        return result;
    }
 
    @Override
    public List<AlarmStatDTO> regionAlarmStat(String today, String orgCode, Integer limit) {
        List<AlarmStatDTO> oilList = oilEventMapper.regionAlarmStat(today, orgCode, limit);
        return oilList;
    }
 
    @Override
    public Map<String, List> alarmTypeStat(Integer freq, String orgCode) {
        List<AlarmTypeStatDTO> list;
        List<String> algorithmNameList = new ArrayList();
        List<Integer> alarmCountList = new ArrayList();
        List<Double> alarmProportionList = new ArrayList();
        Map<String, List> result = new HashMap<>();
        result.put("algorithmNameArr", algorithmNameList);
        result.put("alarmCountArr", alarmCountList);
        result.put("alarmProportionArr", alarmProportionList);
        if (freq == 0) { //当天
            list = oilEventMapper.alarmTypeStatByDay(orgCode, DateUtil.today());
        } else { //当月
            list = oilEventMapper.alarmTypeStatByMonth(orgCode, DateUtil.format(new Date(), "yyyy-MM"));
        }
        if (list.isEmpty()) {
            return result;
        }
        int alarmSum = list.stream().mapToInt(AlarmTypeStatDTO::getAlarmCount).sum();
        for (int i = 0; i < list.size(); i++) {
            AlarmTypeStatDTO dto = list.get(i);
            algorithmNameList.add(i, dto.getAlgorithmName());
            alarmCountList.add(i, dto.getAlarmCount());
            alarmProportionList.add(i, BigDecimal.valueOf((float) dto.getAlarmCount() / alarmSum).doubleValue());
        }
        // 百分比校准
        result.put("alarmProportionArr", PercentUtil.getPercentValue(alarmProportionList, 1, 0));
        return result;
    }
 
    @Override
    public List<AlarmEventVO> latestAlarm(String date, String orgCode, Integer limit) {
        return oilEventMapper.getLatestAlarm(date, orgCode, limit);
    }
 
    @Override
    public Map<String, List> deviceStatusStat(String orgCode, Integer type) {
        DeviceStatusStatDTO dto;
        if (type == 0) {
            dto = cameraMapper.cameraStatusStat(orgCode);
        } else {
            dto = deviceMapper.cameraStatusStat(orgCode);
        }
 
        List<String> statusList = new ArrayList();
        List<Integer> countList = new ArrayList();
        Map<String, List> result = new HashMap<>();
        result.put("statusArr", statusList);
        result.put("countArr", countList);
        countList.add(0, dto.getOnlineCount());
        statusList.add(0, "在线");
        countList.add(1, dto.getOfflineCount());
        statusList.add(1, "离线");
        return result;
    }
 
    @Override
    public Map<String, List> alarmTrendStat(int intervalDay, String endDay, String orgCode) {
        List<AlarmStatDTO> oilEventAlarmStatList = oilEventMapper.alarmTrendStatRange(intervalDay, endDay, orgCode);
        List<AlarmStatDTO> oiloutEventAlarmStatList = oiloutEventMapper.alarmTrendStatRange(intervalDay, endDay, orgCode);
        Map<String, AlarmStatDTO> oiloutMap = oiloutEventAlarmStatList.stream().collect(Collectors.toMap(AlarmStatDTO::getDate, Function.identity()));
        List<String> dateList = new ArrayList();
        List<Integer> oiloutEventAlarmCountList = new ArrayList();
        List<Integer> oilEventAlarmCountList = new ArrayList();
        for (int i = 0; i < oilEventAlarmStatList.size(); i++) {
            AlarmStatDTO oildto = oilEventAlarmStatList.get(i);
            AlarmStatDTO oiloutDTO = oiloutMap.get(oildto.getDate());
            dateList.add(i, oildto.getDate());
            oiloutEventAlarmCountList.add(i, oiloutDTO.getOiloutEventAlarmCount());
            oilEventAlarmCountList.add(i, oildto.getOilEventAlarmCount());
        }
        Map<String, List> result = new HashMap<>();
        result.put("dateArr", dateList);
        result.put("oiloutEventAlarmCountArr", oiloutEventAlarmCountList);
        result.put("oilEventAlarmCountArr", oilEventAlarmCountList);
        return result;
    }
 
    @Override
    public List<OrgAlarmCountVO> alarmRanking(String orgCode, Integer freq, Integer limit) {
        List<OrgAlarmCountVO> result;
        if (freq == 0) { //当天
            result = oilEventMapper.getAlarmRankingByDay(DateUtil.today(), orgCode, limit);
        } else { //当月
            result = oilEventMapper.getAlarmRankingByMonth(DateUtil.format(new Date(), "yyyy-MM"), orgCode, limit);
        }
        return result;
    }
 
    @Override
    public Map<String, PartitionAlarmStatVO> partitionAlarmStat(String date, String orgCode) {
        List<PartitionAlarmStatVO> list = oilEventMapper.getPartirionAlarmStat(date, orgCode);
        Map<String, PartitionAlarmStatVO> collect = list.stream().collect(Collectors.toMap(PartitionAlarmStatVO::getPartitionName, Function.identity()));
        String[] partitionArr = new String[]{"1", "2", "3"};
        for (int i = 0; i < partitionArr.length; i++) {
            PartitionAlarmStatVO vo = collect.get(partitionArr[i]);
            if (vo == null) {
                vo = new PartitionAlarmStatVO(partitionArr[i],0 ,0 , 0.00d);
                collect.put(partitionArr[i], vo);
            } else {
                if (vo.getAlarmCount()==0){
                    vo.setConfirmedProportion(0.00d);
                } else {
                    vo.setConfirmedProportion(BigDecimal.valueOf((float)vo.getConfirmedAlarmCount()/vo.getAlarmCount()).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                }
            }
        }
        return collect;
    }
 
}