DailyAverageUpJob.java
42.9 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
package com.skua.modules.quartz;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.skua.common.report.CustomReportUtil;
import com.skua.core.context.SpringContextUtils;
import com.skua.core.service.IFactoryInfoService;
import com.skua.core.util.ConvertUtils;
import com.skua.core.util.DateUtils;
import com.skua.core.util.DoubleOperaUtils;
import com.skua.core.util.chuanglan.HttpSenderMsg;
import com.skua.core.util.push.IPushService;
import com.skua.core.util.push.MessageEntity;
import com.skua.core.util.push.PushMessageFactory;
import com.skua.modules.alarmtmp.entity.*;
import com.skua.modules.alarmtmp.service.*;
import com.skua.modules.alarmtmp.vo.AlarmRuleConfigAndLevelVO;
import com.skua.modules.flow.business.service.FlowBusinessService;
import com.skua.modules.flow.business.service.IFlowService;
import com.skua.modules.flow.core.constant.ProcessConstant;
import com.skua.modules.flow.core.entity.BladeFlow;
import com.skua.modules.flow.core.utils.FlowUtil;
import com.skua.modules.flow.support.Kv;
import com.skua.modules.flow.utils.Func;
import com.skua.modules.system.entity.SysDepart;
import com.skua.modules.system.entity.SysUser;
import com.skua.modules.system.service.ISysDepartService;
import com.skua.modules.system.service.ISysUserService;
import com.skua.modules.util.SysMessagePushService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngines;
import org.flowable.engine.TaskService;
import org.flowable.identitylink.api.IdentityLink;
import org.flowable.identitylink.api.IdentityLinkType;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.math.MathContext;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
* <pre>
* 日均值上升趋势报警定时任务
* </pre>
* @author Li Yuanyuan
* @version V0.1, 2024年1月22日 下午5:25:41
*/
@Slf4j
@Component
public class DailyAverageUpJob implements Job {
@Autowired
private IFactoryInfoService factoryInfoService;
@Autowired
private AlarmRecordHistoryService alarmRecordHistoryService;
@Autowired
private AlarmMessageHistoryService alarmMessageHistoryService;
@Autowired
private AlarmRuleLevelConfigService alarmRuleLevelConfigService;
@Autowired
private IFlowService flowService;
@Autowired
private FlowBusinessService flowBusinessService;
@Autowired
private ISysUserService sysUserService;
@Autowired
private TaskService taskService;
@Autowired
private ISysDepartService sysDepartService;
@Autowired
private IAlarmRuleConfigPublicTabService alarmRuleConfigPublicTabService;
@Autowired
private IAlarmCustomRuleConfigService alarmCustomRuleConfigService;
private static final String prefix = "【金控数矿】在";
private static final String departNameTemplete = "${departName}";
private static final String suffix = ",请注意!";
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
log.info("日均值上升趋势报警定时任务开始");
JdbcTemplate masterDB = (JdbcTemplate) SpringContextUtils.getBean("master");
//查询机构和ID对应关系的Map
Map<String,String> departNameMap = new HashMap<>();
List<SysDepart> sysDepartList = sysDepartService.list();
for (SysDepart sysDepart : sysDepartList) {
departNameMap.put(sysDepart.getId(),sysDepart.getDepartName());
}
//获取报警类别,已报警类别id为key,构造map对象
Map<String,AlarmRuleConfigPublicTab> alarmTypeDictMap = new HashMap<String,AlarmRuleConfigPublicTab>();
List<AlarmRuleConfigPublicTab> alarmRuleConfigPublicTabList = alarmRuleConfigPublicTabService.list();
alarmRuleConfigPublicTabList.forEach(v->{
alarmTypeDictMap.put(v.getId(), v);
});
//预警规则获取
Map<String,Map<String,Map<String,Object>>> waterQualityStandardMap = DailyAverageJobUtil.getWaterQualityStandard();
//获取当前时间处于什么季节
String season = "other";//默认非冬季
int month = DateUtils.getCalendar().get(Calendar.MONTH)+1;
if((month>=11&&month<=12)||(month>=1&&month<=3)){
season = "winter";
}
//根据当前季节获取阈值报出水水质标准
Map<String,Map<String, Object>> standardMap = waterQualityStandardMap.get(season);
//获取要报警指标
QueryWrapper<AlarmCustomRuleConfig> alarmCustomRuleConfigQueryWrapper = new QueryWrapper<>();
alarmCustomRuleConfigQueryWrapper.eq("alarm_rule_repository_id","1749361020540551169");
List<AlarmCustomRuleConfig> alarmCustomRuleConfigList = alarmCustomRuleConfigService.list(alarmCustomRuleConfigQueryWrapper);
//报警级别接收对象
AlarmRuleLevelConfig alarmRuleLevelConfig = null;
List<String> monitorDictCodeList = new ArrayList<>();
String qrDay = DateUtils.formatAddTime(DateUtils.getYesterday(),"yyyy-MM-dd",Calendar.DATE,-1);
//报警异常数据接收对象
List<AlarmRuleConfigAndLevelVO> excessList = new ArrayList<AlarmRuleConfigAndLevelVO>();
if(alarmCustomRuleConfigList!=null&&alarmCustomRuleConfigList.size()>0) {
for (AlarmCustomRuleConfig alarmCustomRuleConfig : alarmCustomRuleConfigList) {
String alarmRuleJson = alarmCustomRuleConfig.getAlarmRuleJson();
String departId = alarmCustomRuleConfig.getDepartId();
String alarmIndexJson = alarmCustomRuleConfig.getAlarmIndexJson();
String alarmRuleId = alarmCustomRuleConfig.getId();
String alarmRuleType = alarmCustomRuleConfig.getAlarmRuleType();
String alarmRecommend = alarmCustomRuleConfig.getAlarmRecommend();
String alarmRuleRepositoryTreepath = alarmCustomRuleConfig.getAlarmRuleRepositoryTreepath();
//报警规则参数
int dayCountParam = 0;
int weekCountParam = 0;
double standardScale = 0;
//获取报警规则id对应的报警级别
QueryWrapper<AlarmRuleLevelConfig> queryWrapper = new QueryWrapper<AlarmRuleLevelConfig>();
queryWrapper.eq("alarm_rule_id", alarmRuleId);
alarmRuleLevelConfig = alarmRuleLevelConfigService.getOne(queryWrapper);
List<Map> alarmIndexList = JSON.parseArray(alarmIndexJson, Map.class);
alarmIndexList.forEach(alarmIndexMap ->{
monitorDictCodeList.add(ConvertUtils.getString(alarmIndexMap.get("id")));
});
List<String> configParamList = CustomReportUtil.getDataKey(alarmRuleJson);
standardScale = ConvertUtils.getDouble(configParamList.get(0), 0.0);
dayCountParam = ConvertUtils.getInt(configParamList.get(1));
weekCountParam = ConvertUtils.getInt(configParamList.get(2));
for(String monitorDictCode : monitorDictCodeList) {
AlarmRuleConfigAndLevelVO alarmRuleConfigAndLevelVO = new AlarmRuleConfigAndLevelVO();
StringBuffer msgBuffer = new StringBuffer();
//基于指标标签,获取真实采集点位
//String monitorDictCodeSqlPart = "'"+String.join("','", monitorDictCodeList)+"'";
String monitorDictCodeSqlPart = "'"+monitorDictCode+"'";
List<Map<String,Object>> monitorMetricInfoList = masterDB.queryForList("select id,metric_name,metric_uid_tag,metric_unit from sys_monitor_metric_info where metric_uid_tag in("+monitorDictCodeSqlPart+")");
//默认设定一个标签对应一个指标,如果后期需求有变化,注意修改这里的逻辑
Map<String,Object> monitorMetricInfoMap = new HashMap<String,Object>();
if(monitorMetricInfoList!=null&&monitorMetricInfoList.size()>0) {
monitorMetricInfoMap = monitorMetricInfoList.get(0);
}
//遍历点位集合,构造真实点位和采集指标对应关系
Map<String, Map<String,Object>> realMonitorMapping = new HashMap<String,Map<String,Object>>();
for(Map<String,Object> monitorMetricInfo : monitorMetricInfoList) {
realMonitorMapping.put(ConvertUtils.getString(monitorMetricInfo.get("id")), monitorMetricInfo);
}
//遍历点位集合,构造要查询的点位字符串
String realMonitorMetricInfoCodes = monitorMetricInfoList.stream().map(monitorMetricInfo -> Objects.toString(monitorMetricInfo.get("id"), "")).collect(Collectors.joining(","));
String[] realMonitorMetricInfoCodeArray = realMonitorMetricInfoCodes.split(",");
//获取前日小时数据
List<Map<String, Object>> qrMonitorDataList = factoryInfoService.queryFactoryInfos(departId,realMonitorMetricInfoCodes,qrDay+" 00:00:00",qrDay+" 23:59:59","1");
//按日粒度聚合数据
List<Map<String, Object>> qrHandleMonitorDataList =factoryInfoService.handleResult(qrMonitorDataList,"d",realMonitorMetricInfoCodes);
//接收不符合规则一的指标
List<String> firstRuleFailCodes = new ArrayList<>();
//规则一,如果前日均值>=标准值的80%,进入报警处理
for(Map<String,Object> qrHandleMonitorDataMap : qrHandleMonitorDataList){
for (String key : realMonitorMetricInfoCodeArray) {
String metricTag = ConvertUtils.getString(realMonitorMapping.get(key).get("metric_uid_tag"));
String metricName = ConvertUtils.getString(realMonitorMapping.get(key).get("metric_name"));
String metricUnit = ConvertUtils.getString(realMonitorMapping.get(key).get("metric_unit"));
Double val = ConvertUtils.getDouble(qrHandleMonitorDataMap.get(key),0.0);
Double val_tag = ConvertUtils.getDouble(qrHandleMonitorDataMap.get(key+"_tag"),0);
if(val_tag>0){
//计算日均值
String avg_day = handleDoubleValue(String.valueOf(DoubleOperaUtils.bigDecimalRound(val/val_tag,4)));
Map<String, Object> tagStandardMap = standardMap.get(metricTag);
if(tagStandardMap!=null){
//获取指标标准值
double standardValue = ConvertUtils.getDouble(tagStandardMap.get("value"),0);
double scaleStandardValue = DoubleOperaUtils.bigDecimalRound(standardValue*standardScale/100,4);
if(Double.valueOf(avg_day)>scaleStandardValue){
//条件成立,触发报警
msgBuffer.append(metricName+"前日均值【"+avg_day+metricUnit+"】>=标准值【"+standardValue+metricUnit+"】的"+standardScale+"%【"+scaleStandardValue+metricUnit+"】,触发数据报警,请及时处理!");
System.out.println(metricName+"前日均值【"+avg_day+metricUnit+"】>=标准值【"+standardValue+metricUnit+"】的"+standardScale+"%【"+scaleStandardValue+metricUnit+"】,触发数据报警,请及时处理!");
}else{
//条件不成立,记录指标,进入下一层预警规则
firstRuleFailCodes.add(key);
}
}else{
log.info(metricName+"采集指标未获取到标准值,无法进行监测预警");
}
}else{
log.info(metricName+"采集数据断线请核查具体原因");
}
}
}
//规则二、规则三
if(firstRuleFailCodes!=null&&firstRuleFailCodes.size()>0){
//对不满足规则一的数据应用规则二和规则三
realMonitorMetricInfoCodes = firstRuleFailCodes.stream().collect(Collectors.joining(","));
String threeEndDay = DateUtils.getYesterday();
String threeStartDay = "";
try {
threeStartDay = DateUtils.formatAddTime(threeEndDay,"yyyy-MM-dd",Calendar.DATE,-(dayCountParam-1));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<Map<String, Object>> threeDayMonitorDataList = factoryInfoService.queryFactoryInfos(departId,realMonitorMetricInfoCodes,threeStartDay+" 00:00:00",threeEndDay+" 23:59:59","1");
List<Map<String, Object>> threeHandleMonitorDataList =factoryInfoService.handleResult(threeDayMonitorDataList,"d",realMonitorMetricInfoCodes);
//获取三天连续数据
List<String> dayList = DateUtils.sliceUpDateRange(threeStartDay,threeEndDay,"month");
Map<String,Map<String,Object>> threeMonitorDataDayGroupMap = new HashMap<String,Map<String,Object>>();
for(String realMonitorMetricInfoCode : firstRuleFailCodes){
Map<String,Object> tempMap = new HashMap<>();
threeMonitorDataDayGroupMap.put(realMonitorMetricInfoCode,tempMap);
for(Map<String,Object> monitorDataMap : threeHandleMonitorDataList){
String time = DateUtils.formatDate(DateUtils.getDate(Long.valueOf(ConvertUtils.getString(monitorDataMap.get("time"))+"000")), "yyyy-MM-dd");
double val = ConvertUtils.getDouble(monitorDataMap.get(realMonitorMetricInfoCode),0.0);
int val_tag = ConvertUtils.getInt(monitorDataMap.get(realMonitorMetricInfoCode+"_tag"),0);
//String avg_day = DoubleOperaUtils.bigDecimalRound(val/val_tag,4);
String avg_day = handleDoubleValue(String.valueOf(DoubleOperaUtils.bigDecimalRound(val/val_tag,4)));
tempMap.put(time,avg_day);
}
}
//获取近3周连续数据,连续三天判定逻辑为从昨天往前推21天
String weekEndDay = DateUtils.getYesterday();
String weekStartDay = "";
try {
weekStartDay = DateUtils.formatAddTime(threeEndDay,"yyyy-MM-dd",Calendar.DATE,-(7*weekCountParam-1));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<Map<String, Object>> weekMonitorDataList = factoryInfoService.queryFactoryInfos(departId,realMonitorMetricInfoCodes,weekStartDay+" 00:00:00",weekEndDay+" 23:59:59","1");
List<Map<String, Object>> weekHandleMonitorDataList =factoryInfoService.handleResult(weekMonitorDataList,"d",realMonitorMetricInfoCodes);
Map<String,Map<String,Object>> weekMonitorDataDayGroupMap = new HashMap<String,Map<String,Object>>();
for(String realMonitorMetricInfoCode : firstRuleFailCodes){
Map<String,Object> weekTempMap = new HashMap<>();
weekMonitorDataDayGroupMap.put(realMonitorMetricInfoCode,weekTempMap);
for(Map<String,Object> monitorDataMap : weekHandleMonitorDataList){
//String time = ConvertUtils.getString(monitorDataMap.get("time"));
String time = DateUtils.formatDate(DateUtils.getDate(Long.valueOf(ConvertUtils.getString(monitorDataMap.get("time"))+"000")), "yyyy-MM-dd");
double val = ConvertUtils.getDouble(monitorDataMap.get(realMonitorMetricInfoCode),0.0);
int val_tag = ConvertUtils.getInt(monitorDataMap.get(realMonitorMetricInfoCode+"_tag"),0);
String avg_day = handleDoubleValue(String.valueOf(DoubleOperaUtils.bigDecimalRound(val/val_tag,4)));
//double avg_day = DoubleOperaUtils.bigDecimalRound(val/val_tag,4);
weekTempMap.put(time,avg_day);
}
//做周数据平均
for(int i=0;i<weekCountParam;i++){
//第一周
double sum_week = 0.0;
double tag_week = 0;
for(int j=0;j<7;j++){
String tempWeekStartDay="";
try {
tempWeekStartDay = DateUtils.formatAddTime(weekStartDay,"yyyy-MM-dd",Calendar.DATE,j+(i*7));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sum_week = sum_week + ConvertUtils.getDouble(weekTempMap.get(tempWeekStartDay),0.0);
tag_week++;
}
weekTempMap.put(String.valueOf(i+1),DoubleOperaUtils.bigDecimalRound(sum_week/tag_week,4));
}
}
for(String realMonitorMetricInfoCode : firstRuleFailCodes){
String metricName = ConvertUtils.getString(realMonitorMapping.get(realMonitorMetricInfoCode).get("metric_name"));
String metricUnit = ConvertUtils.getString(realMonitorMapping.get(realMonitorMetricInfoCode).get("metric_unit"));
//近三日均值
Map<String,Object> threeMap = threeMonitorDataDayGroupMap.get(realMonitorMetricInfoCode);
String dayTempMiddleValue="0.0";
//默认连续递增
boolean dayCheckResultTag = true;
String dayCheckResultMsg = "";
for(int i=0;i<dayList.size();i++){
if(i>0){
double value = ConvertUtils.getDouble(threeMap.get(dayList.get(i)),0);
if(value<Double.valueOf(dayTempMiddleValue)){
dayCheckResultTag = false;
}
}
dayTempMiddleValue = ConvertUtils.getString(threeMap.get(dayList.get(i)));
dayCheckResultMsg = dayCheckResultMsg+","+dayList.get(i)+"日均值:"+dayTempMiddleValue+" "+metricUnit;
}
if(dayCheckResultTag){
msgBuffer.append(metricName+"近"+dayCountParam+"天每日均值连续上升【"+dayCountParam+"日均值分别是:"+dayCheckResultMsg.substring(1)+"】,触发数据报警,请及时处理!");
//System.out.println(metricName+"近三天每日均值连续上升【三日均值分别是:"+dayCheckResultMsg.substring(1)+"】,触发数据预警,请及时处理!");
}
//近三周均值
Map<String,Object> weekMap = weekMonitorDataDayGroupMap.get(realMonitorMetricInfoCode);
String weekTempMiddleValue="0.0";
//默认连续递增
boolean weekCheckResultTag = true;
String weekCheckResultMsg = "";
for(int i=0;i<weekCountParam;i++){
if(i>0){
double value = ConvertUtils.getDouble(weekMap.get(String.valueOf(i+1)),0);
if(value<Double.valueOf(weekTempMiddleValue)){
weekCheckResultTag = false;
}
}
weekTempMiddleValue = ConvertUtils.getString(weekMap.get(String.valueOf(i+1)));
weekCheckResultMsg = weekCheckResultMsg+",第"+(i+1)+"周均值:"+weekTempMiddleValue+" "+metricUnit;
}
if(weekCheckResultTag){
msgBuffer.append(metricName+"近"+weekCountParam+"周每周均值连续上升【由远及近"+weekCountParam+"周均值分别是:"+weekCheckResultMsg.substring(1)+"】,触发数据报警,请及时处理!");
//System.out.println(metricName+"近三周每周均值连续上升【由远及近三周均值分别是:"+weekCheckResultMsg.substring(1)+"】,触发数据预警,请及时处理!");
}
}
//判断是否连续三日上升
}
//预警数据处理
//List<AlarmRuleConfigAndLevelVO> alarmRuleAndAlarmRuleLevelVOList = alarmRuleConfigService.getAlarmRuleAndAlarmRuleLeveList();
//查询pg数据超标警报
//Map<String, Object> excessAlarmPgList = checkPgExcessAlarm(alarmRuleAndAlarmRuleLevelVOList);
if(StringUtils.isNotBlank(msgBuffer.toString())) {
//存在监测报警数据
alarmRuleConfigAndLevelVO.setDepartId(departId);
alarmRuleConfigAndLevelVO.setAlarmRuleLevelConfig(alarmRuleLevelConfig);
alarmRuleConfigAndLevelVO.setAlarmParamCode(monitorDictCode);
alarmRuleConfigAndLevelVO.setAlarmParamName(ConvertUtils.getString(monitorMetricInfoMap.get("metric_name")));
alarmRuleConfigAndLevelVO.setAlarmProgramCode(ConvertUtils.getString(monitorMetricInfoMap.get("id")));
alarmRuleConfigAndLevelVO.setAlarmParamUnit(ConvertUtils.getString(monitorMetricInfoMap.get("metric_unit")));
alarmRuleConfigAndLevelVO.setDepartName(departNameMap.get(departId));
alarmRuleConfigAndLevelVO.setAlarmMsg(msgBuffer.toString());
alarmRuleConfigAndLevelVO.setAlarmRecommend(alarmRecommend);
//设置报警类型
alarmRuleConfigAndLevelVO.setAlarmRuleType(alarmRuleType);
AlarmRuleConfigPublicTab alarmRuleConfigPublicTab = alarmTypeDictMap.get(alarmRuleType);
if(alarmRuleConfigPublicTab!=null) {
String alarmTypeTreePath = alarmRuleConfigPublicTab.getTreePath();
String[] treeArray = alarmTypeTreePath.split(",");
alarmRuleConfigAndLevelVO.setAlarmRuleTopType(treeArray[0]);
alarmRuleConfigAndLevelVO.setAlarmRuleSecondType(treeArray[1]);
alarmRuleConfigAndLevelVO.setAlarmRuleTypeTreepath(alarmTypeTreePath);
}
alarmRuleConfigAndLevelVO.setAlarmCustomRuleId(alarmRuleId);
String[] treeArray = alarmRuleRepositoryTreepath.split(",");
alarmRuleConfigAndLevelVO.setAlarmCustomRuleTopType(treeArray[0]);
//alarmRuleConfigAndLevelVO.setAlarmCustomRuleSecondType(treeArray[1]);
alarmRuleConfigAndLevelVO.setAlarmCustomRuleType(treeArray[treeArray.length-1]);
alarmRuleConfigAndLevelVO.setAlarmCustomRuleTypeTreepath(alarmRuleRepositoryTreepath);
excessList.add(alarmRuleConfigAndLevelVO);
}
}
}
}else {
log.info("报警规则初始参数未配置,请检查报警规则库,阈值报警规则");
return;
}
sendMessage(excessList);
log.info("在线监测预警(阈值预警)定时任务结束");
} catch (Exception e) {
e.printStackTrace();
log.error("在线监测预警(阈值预警)定时任务异常" + e);
}
}
public static String handleDoubleValue(String doubleValue) {
if(doubleValue.contains("E")) {
MathContext mc = new MathContext(32);
BigDecimal bigNumber = new BigDecimal(doubleValue, mc);
BigDecimal setScale = bigNumber.setScale(4,BigDecimal.ROUND_HALF_UP);
return setScale.toString();
}else {
return ConvertUtils.getString(ConvertUtils.getDouble(doubleValue,0.0,4));
}
}
/**
* 判断是否需要发送短信和通知
*
* @param resultList
*/
public void sendMessage(List<AlarmRuleConfigAndLevelVO> excessList) {
//超标报警
if (excessList != null && excessList.size() > 0) {
excessList.forEach(alarmRuleAndAlarmRuleLevelVO -> {
dealData(alarmRuleAndAlarmRuleLevelVO);
});
}
}
private void dealData(AlarmRuleConfigAndLevelVO alarmRuleAndAlarmRuleLevelVO) {
String date = DateUtils.getDate("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//查询质量报警配置
String departId = alarmRuleAndAlarmRuleLevelVO.getDepartId();
AlarmRuleLevelConfig alarmRuleLevelConfig = alarmRuleAndAlarmRuleLevelVO.getAlarmRuleLevelConfig();
Double alarmValue = alarmRuleAndAlarmRuleLevelVO.getValue();
String alarmParamCode = alarmRuleAndAlarmRuleLevelVO.getAlarmProgramCode();
String alarmParamName = alarmRuleAndAlarmRuleLevelVO.getAlarmParamName();
String alarmRuleType = alarmRuleAndAlarmRuleLevelVO.getAlarmRuleType();
String alarmRuleLevelName = alarmRuleLevelConfig.getAlarmRuleLevelName();
String alarmRuleLevelConfigId = alarmRuleLevelConfig.getId();
String content = alarmRuleAndAlarmRuleLevelVO.getAlarmMsg();
String departName = departNameTemplete.replace("${departName}", alarmRuleAndAlarmRuleLevelVO.getDepartName());
String unit = "";
if (alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit() != null) {
unit = alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit();
}
//查询短信上一条报警是否处理,未处理就不新增报警记录
//TODO 先从数据库中获取,后期优化放redis中
QueryWrapper<AlarmRecordHistory> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("depart_id", departId);
queryWrapper.eq("alarm_rule_type", alarmRuleType);
queryWrapper.eq("alarm_param_code", alarmParamCode);
queryWrapper.eq("alarm_rule_level_id", alarmRuleLevelConfigId);
queryWrapper.eq("alarm_data_source", "gyfz_yzjc");
queryWrapper.orderByDesc("record_time").last("limit 1");
AlarmRecordHistory alarmRecordHistoryOne = alarmRecordHistoryService.getOne(queryWrapper);
//获取上次信息发送的时间,如果满足则发送
//TODO 先从数据库中获取,后期优化方redis中
QueryWrapper<AlarmMessageHistory> alarmMessageHistoryQueryWrapper = new QueryWrapper<>();
alarmMessageHistoryQueryWrapper.eq("alarm_param_code", alarmParamCode);
alarmMessageHistoryQueryWrapper.eq("alarm_send_type", "1");
alarmMessageHistoryQueryWrapper.eq("alarm_rule_type", alarmRuleType);
alarmMessageHistoryQueryWrapper.eq("alarm_rule_level_id", alarmRuleLevelConfigId);
alarmMessageHistoryQueryWrapper.orderByDesc("alarm_send_time").last("limit 1");
AlarmMessageHistory messageHistoryOne = alarmMessageHistoryService.getOne(alarmMessageHistoryQueryWrapper);
Long nowDateLong = null;
try {
nowDateLong = simpleDateFormat1.parse(date).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
//如果没有上条记录或者处理状态(0:未处理,1:已处理)为已处理
if ((alarmRecordHistoryOne == null || alarmRecordHistoryOne.getHandleStatus() == 1)) {
AlarmRecordHistory alarmRecordHistory = new AlarmRecordHistory();
//手动创建UUID,用于报警自动送审
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
alarmRecordHistory.setId(uuid);
alarmRecordHistory.setAlarmParamCode(alarmParamCode);
alarmRecordHistory.setAlarmParamName(alarmParamName);
alarmRecordHistory.setAlarmWorkName(alarmParamName+"报警");
alarmRecordHistory.setAlarmRuleLevelName(alarmRuleLevelName);
alarmRecordHistory.setAlarmRuleType(alarmRuleType);
alarmRecordHistory.setAlarmRuleLevelId(alarmRuleLevelConfigId);
try {
alarmRecordHistory.setRecordTime(simpleDateFormat1.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
alarmRecordHistory.setAlarmParamUnit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit());
alarmRecordHistory.setDepartId(departId);
//alarmRecordHistory.setActualAvgValue(alarmValue.toString());
alarmRecordHistory.setHandleStatus(0);
alarmRecordHistory.setDelFlag(1);
alarmRecordHistory.setAlarmDataSource("gyfz_yzjc");
alarmRecordHistory.setHandleMethon(alarmRuleAndAlarmRuleLevelVO.getAlarmRecommend());
//${start}采集数据${conent}已超标,请注意!
alarmRecordHistory.setAlarmContent(content + suffix);
alarmRecordHistory.setCreateBy("system");//标记为系统创建
alarmRecordHistory.setHandleUser(alarmRuleLevelConfig.getAlarmWorkRecipient());
//流程添加定义ID
String processDefinitionId = flowBusinessService.getProcessDefinitionLastVersionId("productAlarmProcess");
alarmRecordHistory.setProcessDefinitionId(processDefinitionId);
//添加报警类型相关数据
alarmRecordHistory.setAlarmRuleType(alarmRuleType);
alarmRecordHistory.setAlarmRuleTopType(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleTopType());
alarmRecordHistory.setAlarmRuleSecondType(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleSecondType());
alarmRecordHistory.setAlarmRuleTypeTreepath(alarmRuleAndAlarmRuleLevelVO.getAlarmRuleTypeTreepath());
//添加报警分类相关数据
alarmRecordHistory.setAlarmCustomRuleId(alarmRuleAndAlarmRuleLevelVO.getAlarmCustomRuleId());
alarmRecordHistory.setAlarmCustomRuleTopType(alarmRuleAndAlarmRuleLevelVO.getAlarmCustomRuleTopType());
alarmRecordHistory.setAlarmCustomRuleSecondType(alarmRuleAndAlarmRuleLevelVO.getAlarmCustomRuleSecondType());
alarmRecordHistory.setAlarmCustomRuleType(alarmRuleAndAlarmRuleLevelVO.getAlarmCustomRuleType());
alarmRecordHistory.setAlarmCustomRuleTypeTreepath(alarmRuleAndAlarmRuleLevelVO.getAlarmCustomRuleTypeTreepath());
alarmRecordHistory.setAlarmCustomIndexJsonData(alarmRuleAndAlarmRuleLevelVO.getAlarmCustomIndexJsonData());
alarmRecordHistoryService.save(alarmRecordHistory);
//判断是否需要发送系统通知 (内容跟报警记录信息相同)
if ("1".equals(alarmRuleLevelConfig.getIsSendNotice())) {
if (StringUtils.isNotBlank(alarmRuleLevelConfig.getAlarmNoticeRecipient())) {
SysMessagePushService sysMessagePushService = (SysMessagePushService) SpringContextUtils.getBean("sysMessagePushService");
sysMessagePushService.pushAlarmRecordMsgNoProcess(alarmRecordHistory);
}
}
//判断是否需要自动发起工作流
if("1".equals(alarmRuleLevelConfig.getIsFlowWork())) {
if(StringUtils.isNotBlank(alarmRuleLevelConfig.getAlarmWorkRecipient())) {
//TODO 暂时注释掉流程发起逻辑
startProcessFlow("alarm_record_history","productAlarmProcess",Arrays.asList(alarmRuleLevelConfig.getAlarmWorkRecipient().split(",")),uuid,alarmRecordHistory);
}
}
}
//判断是否需要发送短信,1表示发送
if (alarmRuleLevelConfig.getIsSendSms().equals("1")) {
//判读是否在发送时间内
Date allowStartDate = null;
Date allowEndDate = null;
Date nowDate = null;
try {
allowEndDate = simpleDateFormat.parse(alarmRuleLevelConfig.getSmsAllowSendTime().split("-")[1]);
allowStartDate = simpleDateFormat.parse(alarmRuleLevelConfig.getSmsAllowSendTime().split("-")[0]);
nowDate = simpleDateFormat.parse(date.substring(11, 16));
} catch (ParseException e) {
e.printStackTrace();
}
//如果在发送时间内,则发送
if (isEffectiveDate(nowDate, allowStartDate, allowEndDate)) {
Date lastTime1 = null;
//如果数据库没有上次时间,则设置一个默认时间
if (messageHistoryOne != null) {
lastTime1 = messageHistoryOne.getAlarmSendTime();
} else {
try {
lastTime1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-01-01 00:00:00");
} catch (ParseException e) {
e.printStackTrace();
}
}
//判断是否满足报警频率,将两个时间转为毫秒
Long lastTimeLong1 = lastTime1.getTime();
if (nowDateLong - lastTimeLong1 > Double.parseDouble(alarmRuleLevelConfig.getSmsSendFrequency()) * 60 * 60 * 1000) {
List<String> userList = new ArrayList<>();
String alarmSmsRecipient = alarmRuleLevelConfig.getAlarmSmsRecipient();
//获取需要发送短信的手机号
if (StringUtils.isNotBlank(alarmSmsRecipient)) {
String[] userArr = alarmSmsRecipient.split(",");
for (int i = 0; i < userArr.length; i++) {
userList.add(userArr[i]);
}
QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
sysUserQueryWrapper.in("id", userList);
List<SysUser> sysUserList = sysUserService.list(sysUserQueryWrapper);
//获取报警接收人的手机号
StringBuilder phoneStringBuilder = new StringBuilder();
for (int i = 0; i < sysUserList.size(); i++) {
phoneStringBuilder.append(",");
String phone1 = sysUserList.get(i).getPhone();
if (StringUtils.isNotBlank(phone1)) {
phoneStringBuilder.append(phone1);
//发送短信
HttpSenderMsg.sendMsg(prefix + date + "时" + departName + "采集数据" + content + suffix, phone1);
log.info("短信发送成功手机号:" + phone1 + ",内容:" + content);
}
}
String phone = phoneStringBuilder.toString().replaceFirst(",", "");
if (StringUtils.isNotBlank(phone)) {
//将发送的短信保存到数据库
AlarmMessageHistory alarmMessageHistory = new AlarmMessageHistory();
alarmMessageHistory.setAlarmSendUser(alarmSmsRecipient);
alarmMessageHistory.setAlarmSendPhone(phone);
try {
alarmMessageHistory.setAlarmSendTime(simpleDateFormat1.parse(date));
} catch (ParseException e) {
e.printStackTrace();
}
alarmMessageHistory.setAlarmSendContent(prefix + date + "时" + departName + "采集数据" + content + suffix);
alarmMessageHistory.setAlarmSendType("1");
alarmMessageHistory.setAlarmParamCode(alarmParamCode);
alarmMessageHistory.setAlarmParamName(alarmParamName);
alarmMessageHistory.setAlarmRuleLevelId(alarmRuleLevelConfigId);
alarmMessageHistory.setAlarmRuleType(alarmRuleType);
alarmMessageHistory.setAlarmParamUnit(alarmRuleAndAlarmRuleLevelVO.getAlarmParamUnit());
alarmMessageHistory.setDepartId(departId);
alarmMessageHistory.setActualAvgValue(alarmValue.toString());
alarmMessageHistoryService.save(alarmMessageHistory);
}
}
}
}
}
}
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
public void startProcessFlow(String businessTable,String flowKey,List<String> candidateUserList,String bussinessId,AlarmRecordHistory alarmRecordHistory) {
// 启动流程
Kv variables = Kv.create().set(ProcessConstant.TASK_VARIABLE_CREATE_USER, "");
String processDefinitionId = flowBusinessService.getProcessDefinitionLastVersionId(flowKey);
alarmRecordHistory.setProcessDefinitionId(processDefinitionId);
BladeFlow flow = flowService.startProcessInstanceById(processDefinitionId, FlowUtil.getBusinessKey(businessTable, String.valueOf(bussinessId)), variables);
if (Func.isNotEmpty(flow)) {
log.debug("流程已启动,流程ID: " + flow.getProcessInstanceId());
//动态添加下一节点候选用户
if(candidateUserList!=null&&candidateUserList.size()>0) {
//获取当前活动任务
TaskEntity taskEntity=(TaskEntity)taskService.createTaskQuery().processInstanceId(flow.getProcessInstanceId()).singleResult();
String taskId = taskEntity.getId();
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//获取当前结点审批人 考虑到有多个审批人的情况 需要先查询出所有的审批人 所以涉及到identityLink
List<String> userIds = processEngine.getTaskService().getIdentityLinksForTask(taskId).stream().map(IdentityLink::getUserId).distinct().collect(Collectors.toList());
//删除identityLink中的用户关系
for (String userId : userIds) {
taskService.deleteUserIdentityLink(taskId, userId, IdentityLinkType.CANDIDATE);
}
//清空task中的assignee
taskService.unclaim(taskId);
//当前节点审核人列表 清空申请人
userIds = new ArrayList<>();
//当前节点审核人列表 追加流转人
userIds.addAll(candidateUserList);
//给当前任务节点分配审核人
for (String userId : userIds) {
taskService.addCandidateUser(taskId, "taskUser_"+userId);
}
//工单消息推送
if (userIds != null && userIds.size() > 0) {
List<String> userCidList = new ArrayList<>();
userCidList = sysUserService.getCidByUserIds(String.join(",", userIds));
//获取用户列表后做消息推送
MessageEntity messageEntity = new MessageEntity();
messageEntity.setMessageTitle("流程审批消息");
messageEntity.setMessageBody("您有一条" + alarmRecordHistory.getAlarmWorkName() + "待审批,请及时处理");
messageEntity.setReceiveUser(userIds);
messageEntity.setReceiveUserCid(userCidList);
messageEntity.setSendUser("系统");
messageEntity.setForwardTag("todo");
IPushService appPushService = PushMessageFactory.getPushService("MOB");
IPushService webPushService = PushMessageFactory.getPushService("WEB");
appPushService.pushMessage(messageEntity);
webPushService.pushMessage(messageEntity);
}
}
//返回流程id写入leave
alarmRecordHistory.setProcessInstanceId(flow.getProcessInstanceId());
alarmRecordHistoryService.saveOrUpdate(alarmRecordHistory);
log.debug("流程发起成功");
} else {
log.debug("流程发起失败");
}
}
}