燕山石化溯源三维电子沙盘-【后端】-服务
1
13693261870
2023-07-21 58cb215b67a5bf50fba0d63969f40711fc23135c
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package com.yssh.service;
 
import java.text.SimpleDateFormat;
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 com.yssh.entity.WarningVo;
 
import javax.annotation.Resource;
 
@Service
public class WarningAnalyseService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Resource
    private WarningDetailMapper warningDetailMapper;
 
    @Resource
    private CommonService commonService;
 
    @Resource
    private SuYuanMapper suYuanMapper;
 
    @Resource
    private DictRecordMapper dictRecordMapper;
 
    @Resource
    private AsyncService asyncService;
 
    @Resource
    private QxshMapper qxshMapper;
 
    @Resource
    private VocValsService vocValsService;
 
    private SimpleDateFormat ym = new SimpleDateFormat("yyyyMM%");
 
    @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<WarningVo> getRunTimeAlarmAnalyse() {
        List<WarningVo> result = new ArrayList<>();
        Date nowDate = DateUtils.getNowDate();
        //String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, nowDate);
        //DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
        //if (StringUtils.isNull(dictRecord)) {
        //    nowDate = DateUtils.getAPeriodOfTime(nowDate, -1, Calendar.HOUR_OF_DAY);
        //}
        //List<WarningDetail> list = alarmAnalyseOperation(nowDate);
 
        List<WarningDetail> list = new ArrayList<>();
        Calendar calendar = getCalendar(nowDate);
        for (int i = 0; i < 6; i++) {
            calendar.add(Calendar.HOUR, -1);
            List<WarningDetail> rs = getAlarmWarnAnalyse(calendar.getTime(), false);
            if (null != rs && rs.size() > 0) {
                list.addAll(rs);
            }
        }
        list.forEach(s -> {
            String time = DateUtils.getYyyyMmDdHh(s.getCreateTime());
            result.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue(), time));
        });
        return result;
    }
 
    private Calendar getCalendar(Date nowDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.HOUR, 1);
 
        return calendar;
    }
 
    public List<WarningVo> getRunTimeWarningAnalyse() {
        List<WarningVo> result = new ArrayList<>();
        Date nowDate = DateUtils.getNowDate();
        //String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, nowDate);
        //DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
        //if (StringUtils.isNull(dictRecord)) {
        //    nowDate = DateUtils.getAPeriodOfTime(nowDate, -1, Calendar.HOUR_OF_DAY);
        //}
        //List<WarningDetail> list = warningAnalyseOperation(nowDate);
 
        List<WarningDetail> list = new ArrayList<>();
        Calendar calendar = getCalendar(nowDate);
        for (int i = 0; i < 6; i++) {
            calendar.add(Calendar.HOUR, -1);
            List<WarningDetail> rs = getAlarmWarnAnalyse(calendar.getTime(), true);
            if (null != rs && rs.size() > 0) {
                list.addAll(rs);
            }
        }
        list.forEach(s -> {
            String time = DateUtils.getYyyyMmDdHh(s.getCreateTime());
            result.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue(), time));
        });
        return result;
    }
 
    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;
    }
 
    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();
            }
        }
    }
 
    public Map<String, Integer> countThisMonthAlarmAndWarning() {
        Map<String, Integer> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("startTime", DateUtils.getYyyyMmDdHhMmSs(DateUtils.getMonthStart()));
        param.put("endTime", DateUtils.getYyyyMmDdHhMmSs(DateUtils.getMonthEnd()));
 
        /*param.put("type", 0);
        List<WarningVo> alarms = warningDetailMapper.selectWarningDetailByMap(param);
        if (StringUtils.isNull(alarms)) {
            alarms = new ArrayList<>();
        }
        result.put("alarmNumber", alarms.size());*/
        Integer alarmNum = warningDetailMapper.countAlarmByMap(param);
        result.put("alarmNumber", null == alarmNum ? 0 : alarmNum);
 
        param.put("type", 1);
        /*List<WarningVo> warnings = warningDetailMapper.selectWarningDetailByMap(param);
        if (StringUtils.isNull(warnings)) {
            warnings = new ArrayList<>();
        }
        result.put("warningNumber", warnings.size());*/
        Integer warnNum = warningDetailMapper.countWarnByMap(param);
        result.put("warningNumber", null == warnNum ? 0 : warnNum);
 
        return result;
    }
 
    public Map<String, List<Map<String, Object>>> countEverydayAlarmAndWarning() {
        Map<String, List<Map<String, Object>>> result = new HashMap<>();
 
        Map<String, Object> param = new HashMap<String, Object>();
        Date now = new Date();
        Date last = DateUtils.getAPeriodOfTime(now, -7, Calendar.DATE); // 上周的今天
        param.put("startTime", DateUtils.getYyyyMmDdHhMmSs(last));
        param.put("endTime", DateUtils.getYyyyMmDdHhMmSs(now));
 
        param.put("type", 0);
        List<Map<String, Object>> alarmDayCount = warningDetailMapper.selectWarningDayCountByMap(param);
        result.put("alarmDayCount", alarmDayCount);
 
        param.put("type", 1);
        List<Map<String, Object>> warningDayCount = warningDetailMapper.selectWarningDayCountByMap(param);
        result.put("warningDayCount", warningDayCount);
 
        return result;
    }
 
    public Map<String, List<Double>> selectEachLocationDataChange() {
        /*Map<String, List<Double>> result = new LinkedHashMap<>();
        List<MonitorPointPosition> checkPoints = commonService.getCheckPoints3d();
        Date nowDate = DateUtils.getNowDate();
        Long endTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, nowDate));
        Long startTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getAPeriodOfTime(nowDate, -3, Calendar.HOUR_OF_DAY)));
        List<DictRecord> recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime);
        checkPoints.forEach(c -> {
            List<String> tableNames = new ArrayList<>();
            recordList.forEach(r -> {
                tableNames.add(r.getTableName());
            });
            List<Double> values = new ArrayList<>();
            if (StringUtils.isNotNull(tableNames)) {
                List<SuYuanMonitorData> data = suYuanMapper.getMonitorData(tableNames, c.getId());
                data.forEach(v -> {
                    values.add(v.getValue());
                });
                result.put(c.getName(), values);
            }
        });
        return result;*/
 
        Map<String, List<Double>> result = new LinkedHashMap<>();
        List<MonitorPointPosition> checkPoints = commonService.getCheckPoints3d();
        Date nowDate = DateUtils.getNowDate();
        Long endTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, nowDate));
        Long startTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getAPeriodOfTime(nowDate, -3, Calendar.HOUR_OF_DAY)));
        List<DictRecord> recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime);
 
        List<String> tableNames = new ArrayList<>();
        for (DictRecord dr : recordList) {
            if (suYuanMapper.isTableExists(dr.getTableName()) > 0) {
                tableNames.add(dr.getTableName());
            }
        }
        if (tableNames.isEmpty()) {
            return result;
        }
 
        for (MonitorPointPosition c : checkPoints) {
            List<Double> values = new ArrayList<>();
            List<SuYuanMonitorData> data = suYuanMapper.getMonitorData(tableNames, c.getId());
            if (null != data && data.size() > 0) {
                for (SuYuanMonitorData v : data) {
                    values.add(v.getValue());
                }
            }
            result.put(c.getName(), values);
        }
        return result;
    }
 
    public List<Map<String, Object>> selectThisMonthLocationValueDataTop10() {
        /*List<Map<String, Object>> result = new ArrayList<>();
        List<MonitorPointPosition> checkPoints = commonService.getCheckPoints3d();
        Long startTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getMonthStart()));
        Long endTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getMonthEnd()));
        List<DictRecord> recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime);
        checkPoints.forEach(c -> {
            List<String> tableNames = new ArrayList<>();
            recordList.forEach(r -> {
                tableNames.add(r.getTableName());
            });
            Map<String, Object> map = suYuanMapper.getMonthValueDataMax(tableNames, c.getId());
            if (StringUtils.isNotNull(map) && StringUtils.isNotEmpty(map)) {
                String suYuanId = map.get("id").toString();
                map.put("name", commonService.select3dCheckPointById(suYuanId).getName());
                map.remove("id");
            }
            result.add(map);
        });
        return CalculateUtils.sort(result, "value", true).subList(0, 10);*/
 
        List<Map<String, Object>> result = new ArrayList<>();
        List<MonitorPointPosition> checkPoints = commonService.getCheckPoints3d();
        Long startTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getMonthStart()));
        Long endTime = Long.parseLong(DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, DateUtils.getMonthEnd()));
        //startTime = 2023040100L;
        //endTime = 2023043023L;
 
        List<DictRecord> recordList = dictRecordMapper.selectByTimeDictRecordList(startTime, endTime);
 
        List<String> tableNames = new ArrayList<>();
        for (DictRecord dr : recordList) {
            if (suYuanMapper.isTableExists(dr.getTableName()) > 0) {
                tableNames.add(dr.getTableName());
            }
        }
        if (tableNames.isEmpty()) {
            return result;
        }
 
        /*List<String> idList = new ArrayList<>();
        for (MonitorPointPosition c : checkPoints) {
            idList.add("'" + c.getId() + "'");
        }
        String ids = String.join(",", idList);*/
 
        for (MonitorPointPosition c : checkPoints) {
            Map<String, Object> map = suYuanMapper.getMonthValueDataMax(tableNames, c.getId());
            if (null != map && map.size() > 0) {
                //String suYuanId = map.get("id").toString();
                //map.put("name", commonService.select3dCheckPointById(suYuanId).getName());
                //map.remove("id");
                map.put("name", c.getName());
                result.add(map);
            }
        }
 
        return CalculateUtils.sort(result, "value", true).subList(0, 10);
    }
 
    public List<Qxsh> selectMonthTop10() {
        String time = DateUtils.getYyyyMm(new Date());
 
        List<Qxsh> list = qxshMapper.selectMonthTop10(time + "%");
 
        return list;
    }
 
    public Map<String, List<Double>> select3Hours() {
        Map<String, List<Double>> map = new LinkedHashMap<>();
 
        List<String> times = DateUtils.get3Hours();
        List<Qxsh> list = qxshMapper.select3Hours(times);
        if (null == list || list.isEmpty()) {
            return map;
        }
 
        for (Qxsh qxsh : list) {
            String name = qxsh.getName();
            List<Double> values = map.computeIfAbsent(name, k -> new ArrayList<>());
            values.add(qxsh.getValue());
        }
 
        return map;
    }
 
    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.selectByBeginAndEnd(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);
            suMax.setAddr(getAddr(suMax.getId()));
 
            Report report = Report.calcReport(wd, suYuan, suMax);
            report.setLastVal(lastVal);
 
            list.add(report);
        }
 
        return list;
    }
 
    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;
    }
 
    public List<Report> getAlarmAndWarnByTime2(Date begin, Date end) {
        String startTime = DateUtils.getYyyyMmDdHhMmSs(begin);
        String endTime = DateUtils.getYyyyMmDdHhMmSs(end);
 
        List<WarningDetail> rs = new ArrayList<>();
        List<WarningDetail> rsYj = warningDetailMapper.selectByTimeForYj(startTime, endTime);
        if (null != rsYj && rsYj.size() > 0) rs.addAll(rsYj);
 
        List<WarningDetail> rsBj = warningDetailMapper.selectByTimeForBj(startTime, endTime);
        if (null != rsBj && rsBj.size() > 0) rs.addAll(rsBj);
 
        Hashtable<String, Boolean> dict = new Hashtable<>();
        List<Report> list = new ArrayList<>();
        for (WarningDetail wd : rs) {
            /*DistanceSuYuan suYuan = suYuanMapper.getSuYuanById(wd.getTableName(), wd.getSuYuanId());
            Double lastVal = warningDetailMapper.getLastYearVal(wd.getId());
 
            int rows = suYuanMapper.isTableExists(wd.getTableName());
            DistanceSuYuan suMax = null;
            if (rows > 0) {
                MonitorPointPosition point = commonService.select3dCheckPointByName(wd.getLocationName());
                List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(point, 50);
                suMax = suYuanMapper.getSuYuan500Max(wd.getTableName(), ids3d);
            } else {
                suMax = suYuan;
            }*/
            if (dict.containsKey(wd.getTableName())) {
                if (!dict.get(wd.getTableName())) continue;
            } else {
                Boolean b = suYuanMapper.isTableExists(wd.getTableName()) > 0;
                dict.put(wd.getTableName(), b);
                if (!b) continue;
            }
 
            DistanceSuYuan suYuan = suYuanMapper.getSuYuanById(wd.getTableName(), wd.getSuYuanId());
            Double lastVal = warningDetailMapper.getLastYearVal(wd.getId());
 
            MonitorPointPosition point = commonService.select3dCheckPointByName(wd.getLocationName());
            List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(point, 50);
            DistanceSuYuan suMax = suYuanMapper.getSuYuan500Max(wd.getTableName(), ids3d);
            suMax.setAddr(getAddr(suMax.getId()));
 
            Report report = Report.calcReport(wd, suYuan, suMax);
            report.setLastVal(lastVal);
 
            list.add(report);
        }
 
        return list;
    }
 
    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;
    }
}