燕山石化溯源三维电子沙盘-【后端】-服务
13693261870
2023-10-31 a9f99d3f074472e1e16ec6109e5d819ca8dcf4d0
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.yssh.service;
 
import java.util.*;
import java.util.concurrent.CountDownLatch;
 
import com.yssh.entity.*;
import com.yssh.mapper.DictRecordMapper;
import com.yssh.mapper.QxshMapper;
import com.yssh.mapper.SuYuanMapper;
import com.yssh.mapper.WarningDetailMapper;
import com.yssh.utils.CalculateUtils;
import com.yssh.utils.DateUtils;
import com.yssh.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
 
import com.google.common.collect.Lists;
 
import javax.annotation.Resource;
 
@Service
public class WarningAnalyseService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Resource
    WarningDetailMapper warningDetailMapper;
 
    @Resource
    CommonService commonService;
 
    @Resource
    SuYuanMapper suYuanMapper;
 
    @Resource
    DictRecordMapper dictRecordMapper;
 
    @Resource
    AsyncService asyncService;
 
    @Resource
    QxshMapper qxshMapper;
 
    @Resource
    VocValsService vocValsService;
 
    @Resource
    SuYuanService suYuanService;
 
    private static HashMap<String, String> filterMap = new HashMap<>();
 
    @Async("threadPoolTaskExecutor")
    public void insertWarningDetails(List<WarningDetail> warning) throws Exception {
        // 插入数据
        List<List<WarningDetail>> list = Lists.partition(warning, AsyncService.BATCH_INSERT_NUMBER);
        CountDownLatch countDownLatch = new CountDownLatch(list.size());
        for (List<WarningDetail> corpReserveList : list) {
            asyncService.executeAsync("", corpReserveList, warningDetailMapper, countDownLatch);
        }
        countDownLatch.await();
    }
 
    /**
     * 获取实时报警
     */
    public List<Qxsh> getRunTimeAlarmAnalyse(Integer start, Integer end) {
        List<Qxsh> list = qxshMapper.selectAlarmByBeginAndEnd(start, end);
 
        return copeHasSuYuan(list);
    }
 
    /**
     * 获取实时预警
     */
    public List<Qxsh> getRunTimeWarningAnalyse(Integer start, Integer end) {
        List<Qxsh> list = qxshMapper.selectWarnByBeginAndEnd(start, end);
 
        return copeHasSuYuan(list);
    }
 
    /**
     * 本月预警报警统计
     */
    public Map<String, Integer> monthCount(Date date) {
        Date startDate = DateUtils.getMonthStart(date);
        Date endDate = DateUtils.getMonthEnd(date);
        Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
        Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate));
 
        Map<String, Integer> result = new HashMap<>();
        Integer warnNum = qxshMapper.countMonthForWarn(start, end);
        result.put("warningNumber", null == warnNum ? 0 : warnNum);
 
        Integer alarmNum = qxshMapper.countMonthForAlarm(start, end);
        result.put("alarmNumber", null == alarmNum ? 0 : alarmNum);
 
        return result;
    }
 
    /**
     * 每日预警、报警数量变化趋势
     */
    public Map<String, List<Map<String, Object>>> everydayCount(Date endDate) {
        Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate));
        Date startDate = DateUtils.getAPeriodOfTime(endDate, -6, Calendar.DATE);
        Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
 
        Map<String, List<Map<String, Object>>> result = new HashMap<>();
        List<Map<String, Object>> warnList = qxshMapper.count7DayForWarn(start, end);
        result.put("warningDayCount", warnList);
 
        List<Map<String, Object>> alarmList = qxshMapper.count7DayForAlarm(start, end);
        result.put("alarmDayCount", alarmList);
 
        return result;
    }
 
    /**
     * 三小时监测站点数据变化趋势
     */
    public Object select3Hours(Date endDate) {
        Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate));
        Date startDate = DateUtils.getAPeriodOfTime(endDate, -2, Calendar.HOUR_OF_DAY);
        Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
 
        //  List<String> times = DateUtils.get3Hours();
        List<Qxsh> list = qxshMapper.select3Hours(start, end);
        if (null == list || list.isEmpty()) {
            return null;
        }
 
        /* Map<String, List<Double>> map = new LinkedHashMap<>();
        for (Qxsh qxsh : list) {
            String name = qxsh.getName();
            List<Double> values = map.computeIfAbsent(name, k -> new ArrayList<>());
            values.add(qxsh.getValue());
        }*/
 
        return list;
    }
 
    /**
     * 获取本月监测站点最大值TOP10
     */
    public List<Qxsh> selectMonthTop10() {
        String time = DateUtils.getYyyyMm(new Date());
        List<Qxsh> list = qxshMapper.selectMonthTop10(time + "%");
 
        return list;
    }
 
    /**
     * 获取本周监测站点最大值TOP10
     */
    public List<Qxsh> selectWeekTop10() {
        Date now = new Date();
        int weekOfYear = DateUtils.getWeekOfYear(now);
        int year = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYY, now));
        Date lastSun = DateUtils.getWeekOfYearForSun(year, weekOfYear);
        Date mon = DateUtils.getAPeriodOfTime(lastSun, 1, Calendar.DATE);
        Date sun = DateUtils.getAPeriodOfTime(lastSun, 7, Calendar.DATE);
        Integer intMon = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, mon) + "00"); // 2023080700
        Integer intSun = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, sun) + "23"); // 2023081323
 
        List<Qxsh> list = qxshMapper.selectWeekTop10(intMon, intSun);
 
        return list;
    }
 
    /**
     * 获取本日监测站点最大值TOP10
     */
    public List<Qxsh> selectDayTop10() {
        String time = DateUtils.getYyyyMmDd(new Date());
        List<Qxsh> list = qxshMapper.selectDayTop10(time + "%");
 
        return list;
    }
 
    /**
     * 根据时间获取报警和预警信息
     */
    public List<Report> getAlarmAndWarnByTime(Date beginDate, Date endDate) {
        String strStart = DateUtils.getYyyyMmDdHh(beginDate);
        String strEnd = DateUtils.getYyyyMmDdHh(endDate);
        Integer start = Integer.parseInt(strStart);
        Integer end = Integer.parseInt(strEnd);
 
        List<Qxsh> rsList = qxshMapper.selectForReport(start, end);
        if (null == rsList || rsList.isEmpty()) return null;
 
        Hashtable<String, Boolean> dict = getExistTabDict(rsList);
        List<Report> list = new ArrayList<>();
        for (Qxsh qxsh : rsList) {
            String tab = "su_yuan_" + qxsh.getTime();
            if (!dict.get(tab)) continue;
 
            MonitorPointPosition point = commonService.select3dCheckPointByName(qxsh.getName());
            String suYuanId = point.getId().substring(0, point.getId().lastIndexOf("_") + 1) + "0";
            Double lastVal = qxshMapper.selectLastYearVal(Integer.parseInt(qxsh.getTime()) - 1000000, qxsh.getName());
 
            WarningDetail wd = new WarningDetail(0L, tab, suYuanId, qxsh.getName(), null, 0, qxsh.getValue());
            DistanceSuYuan suYuan = suYuanMapper.getSuYuanById(tab, suYuanId);
            //List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(point, 50);
            //DistanceSuYuan suMax = suYuanMapper.getSuYuan500Max(tab, ids3d);
 
            //String filter = CalculateUtils.getFilterByExtend(point, 50);
            String filter = getFilterByPoint(point, 50);
            DistanceSuYuan suMax = suYuanMapper.getSuYuan500MaxByFilter(tab, filter);
            suMax.setAddr(getAddr(suMax.getId()));
 
            Report report = Report.calcReport(wd, suYuan, suMax);
            report.setLastVal(lastVal);
 
            SuYuan700 su = suYuanMapper.selectSuYuan46ById(suYuanId, qxsh.getTime().substring(0, 4) + "-" + qxsh.getTime().substring(4, 6) + "-" + qxsh.getTime().substring(6, 8) + " " + qxsh.getTime().substring(8, 10) + ":00:00");
            report.setSu(su);
 
            list.add(report);
        }
 
        return list;
    }
 
    public static String getFilterByPoint(MonitorPointPosition point, int range) {
        String key = String.format("%d_%d_%d", point.getX(), point.getY(), range);
        if (filterMap.containsKey(key)) {
            return filterMap.get(key);
        }
 
        List<String> ids = CalculateUtils.aloneCrosswiseExtend(point, range);
        for (int i = 0, c = ids.size(); i < c; i++) {
            ids.set(i, "'" + ids.get(i) + "'");
        }
 
        String filter = "id in (" + StringUtils.join(ids, ",") + ")";
        if (!filterMap.containsKey(key)) {
            filterMap.put(key, filter);
        }
 
        return filter;
    }
 
    private Hashtable<String, Boolean> getExistTabDict(List<Qxsh> rsList) {
        Hashtable<String, Boolean> dict = new Hashtable<>();
        for (Qxsh qxsh : rsList) {
            String tab = "su_yuan_" + qxsh.getTime();
            if (dict.containsKey(tab)) {
                continue;
            }
 
            Boolean b = suYuanMapper.isTableExists(tab) > 0;
            dict.put(tab, b);
        }
 
        return dict;
    }
 
    private String getAddr(String id) {
        String[] strs = id.split("_");
        int x = Integer.parseInt(strs[0]) / 10;
        int y = Integer.parseInt(strs[1]) / 10;
 
        List<VocCoords> list = vocValsService.selectCoords(x, y);
        for (VocCoords vc : list) {
            if (null != vc.getAddr() && vc.getAddr().length() > 0) {
                return vc.getAddr();
            }
        }
 
        return null;
    }
 
    /**
     * 存储告警/预警信息
     */
    public void warningOperationStorage(Date date) {
        List<WarningDetail> allData = new ArrayList<>();
        List<WarningDetail> alarms = this.getAlarmWarnAnalyse(date, false);
        if (StringUtils.isNotEmpty(alarms)) {
            allData.addAll(alarms);
        }
 
        List<WarningDetail> warnings = this.getAlarmWarnAnalyse(date, true);
        if (StringUtils.isNotEmpty(warnings)) {
            allData.addAll(warnings);
        }
 
        if (StringUtils.isNotEmpty(allData)) {
            try {
                insertWarningDetails(allData);
            } catch (Exception e) {
                logger.error("批量插入告警数据出现异常!!!", e);
                e.printStackTrace();
            }
        }
    }
 
    /**
     * 获取告警/预警分析
     */
    private List<WarningDetail> getAlarmWarnAnalyse(Date date, boolean isWarn) {
        List<String> ids = CalculateUtils.assembleId(commonService.getCheckPoints3d());
 
        String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
        DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
        if (null == dictRecord) return null;
 
        String tableName = dictRecord.getTableName();
        if (suYuanMapper.isTableExists(tableName) == 0) return null;
 
        List<SuYuan2d> list = isWarn ? suYuanMapper.getWarningAnalyse(tableName, ids) : suYuanMapper.getAlarmsAnalyse(tableName, ids);
        if (null == list || list.isEmpty()) {
            return null;
        }
 
        int type = isWarn ? 1 : 0;
        List<WarningDetail> result = new ArrayList<>();
        for (SuYuan2d s : list) {
            String locationName = commonService.select3dCheckPointById(s.getId()).getName();
            result.add(new WarningDetail(0L, tableName, s.getId(), locationName, date, type, s.getValue()));
        }
 
        return result;
    }
 
    /**
     * 处理是否含有溯源信息
     */
    private List<Qxsh> copeHasSuYuan(List<Qxsh> list) {
        if (null == list || list.isEmpty()) {
            return null;
        }
 
        Map<String, Integer> tabs = new HashMap<>();
        for (Qxsh qxsh : list) {
            String tab = "su_yuan_" + qxsh.getTime();
            if (!tabs.containsKey(tab)) {
                tabs.put(tab, suYuanMapper.isTableExists(tab));
            }
 
            int rows = tabs.get(tab);
            if (0 == rows) {
                continue;
            }
 
            int isSuYuan = hasSuYuan(qxsh);
            qxsh.setIsSuYuan(isSuYuan);
        }
 
        return list;
    }
 
    /**
     * 查询溯源
     */
    private Integer hasSuYuan(Qxsh qxsh) {
        String suYuanId = suYuanService.selectSuYuanIdByName(qxsh.getName());
        String createTime = qxsh.getTime().substring(0, 4) + "-" + qxsh.getTime().substring(4, 6) + "-" + qxsh.getTime().substring(6, 8) + " " + qxsh.getTime().substring(8, 10) + ":00:00";
 
        return qxshMapper.hasSuYuan(suYuanId, createTime);
    }
}