|
@@ -0,0 +1,144 @@
|
|
|
+package com.gyee.wisdom.alarm.sharding.schedule;
|
|
|
+
|
|
|
+import com.gyee.wisdom.alarm.sharding.entity.*;
|
|
|
+import com.gyee.wisdom.alarm.sharding.mapper.AlarmPartsCountMapper;
|
|
|
+import com.gyee.wisdom.alarm.sharding.mapper.AlarmRuleCountMapper;
|
|
|
+import com.gyee.wisdom.alarm.sharding.service.AlarmHistoryService;
|
|
|
+import com.gyee.wisdom.alarm.sharding.service.AlarmSnapService;
|
|
|
+import com.gyee.wisdom.alarm.sharding.service.TableNameService;
|
|
|
+import com.gyee.wisdom.alarm.sharding.service.Warning2Service;
|
|
|
+import com.gyee.wisdom.alarm.sharding.util.SnowflakeGenerator;
|
|
|
+import com.gyee.wisdom.common.utils.DateUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+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.w3c.dom.ls.LSInput;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author :xugp
|
|
|
+ * @date :Created in 2022/9/8 10:15
|
|
|
+ * @description:报警信息统计,根据部件、规则统计发生报警次数
|
|
|
+ * @modified By:
|
|
|
+ * @version: $
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+@EnableScheduling
|
|
|
+public class AlarmCountPartsScheduled {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlarmSnapService alarmSnapService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TableNameService tableNameService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlarmPartsCountMapper alarmPartsCountMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlarmRuleCountMapper alarmRuleCountMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private Warning2Service warning2Service;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlarmHistoryService alarmHistoryService;
|
|
|
+ @Scheduled(cron = "* 46 17 ? * 5")
|
|
|
+ public boolean doTask() {
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<AlarmSnap> snapList = alarmSnapService.queryAll(null, null, "windturbine", null, null, null, null, null, null, null);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+ String strDate = sdf.format(DateUtil.addDays(new Date(),-1));
|
|
|
+ Date dtStart = sdf.parse(strDate);
|
|
|
+ Date dtEnd = DateUtil.addDays(dtStart, 1);
|
|
|
+ Date dtNow = new Date();
|
|
|
+ while (dtEnd.getTime() < dtNow.getTime()) {
|
|
|
+ List<AlarmPartsCount> acList = new ArrayList<>();
|
|
|
+ List<AlarmRuleCount> alarmRuleCounts = new ArrayList<>();
|
|
|
+ Map<String,List<AlarmHistory>> map = new HashMap<>();
|
|
|
+ //根据windturbineid分组
|
|
|
+ Map<String,List<AlarmSnap>> maps = snapList.stream().collect(Collectors.groupingBy(AlarmSnap::getWindturbineId));
|
|
|
+ for (Map.Entry<String,List<AlarmSnap>> listEntry : maps.entrySet()) {
|
|
|
+ List<AlarmSnap> alarmSnap = alarmSnapService.getAlarmSnap(listEntry.getKey());
|
|
|
+ List<AlarmHistory> alarmHistories = new ArrayList<>();
|
|
|
+ for(AlarmSnap snap : alarmSnap){
|
|
|
+ String tbName = tableNameService.getAlarmHistoryTableName(snap.getStationId(), dtStart);
|
|
|
+ List<AlarmHistory> alarmHistories1 = alarmHistoryService.selectAlarmHistory(tbName, snap.getId(), dtStart, dtEnd);
|
|
|
+ alarmHistories.addAll(alarmHistories1);
|
|
|
+ }
|
|
|
+ map.put(listEntry.getKey(),alarmHistories);
|
|
|
+ }
|
|
|
+ //遍历map(key:windturbined,value:history)
|
|
|
+ for (Map.Entry<String,List<AlarmHistory>> entry : map.entrySet()){
|
|
|
+ Map<String,String> map1 = new HashMap<>();
|
|
|
+ List<AlarmHistory> value = entry.getValue();
|
|
|
+ for (AlarmHistory alarmHistory : value){
|
|
|
+ AlarmSnap alarmSnap = alarmSnapService.queryById(alarmHistory.getSnapId());
|
|
|
+ Warning2 warning2 = warning2Service.selectByModelidAndEdnvalue(alarmSnap.getModelId(),alarmSnap.getAlertValue());
|
|
|
+ String id = alarmHistory.getId() + "_" + alarmHistory.getSnapId();
|
|
|
+ map1.put(id, warning2.getRelatedParts());
|
|
|
+ }
|
|
|
+ //分组转化为key:parts,value:snapId
|
|
|
+ Map<String, List<Map.Entry<String,String>>> result = map1.entrySet().stream().collect(Collectors.groupingBy(c -> c.getValue()));
|
|
|
+ for (Map.Entry<String, List<Map.Entry<String,String>>> entry1 : result.entrySet()) {
|
|
|
+ List<Map.Entry<String, String>> listValue = entry1.getValue();
|
|
|
+ int count = 0;
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
+ for (Map.Entry<String, String> map2 : listValue) {
|
|
|
+ String[] split = map2.getKey().split("_");
|
|
|
+ long l = Long.parseLong(split[1]);
|
|
|
+ AlarmSnap alarmSnap = alarmSnapService.queryById(l);
|
|
|
+ Warning2 warning2 = warning2Service.selectByModelidAndEdnvalue(alarmSnap.getModelId(), alarmSnap.getAlertValue());
|
|
|
+ String tbName = tableNameService.getAlarmHistoryTableName(alarmSnap.getStationId(), dtStart);
|
|
|
+ count = count + alarmHistoryService.selectAlarmSnapCount(tbName,Long.parseLong(split[0]));
|
|
|
+ list.add(warning2.getId());
|
|
|
+ }
|
|
|
+ Map<String, Integer> mapCount = list.stream().collect(Collectors.toMap(key -> key, values -> 1, (value1,value2) ->value1 + value2));
|
|
|
+ for(Map.Entry<String,Integer> entryString : mapCount.entrySet()){
|
|
|
+ //统计某一种规则的报警次数
|
|
|
+ AlarmRuleCount alarmRuleCount = new AlarmRuleCount();
|
|
|
+ alarmRuleCount.setId(SnowflakeGenerator.generateId());
|
|
|
+ alarmRuleCount.setAlarmDate(dtStart);
|
|
|
+ alarmRuleCount.setParts(entry1.getKey());
|
|
|
+ alarmRuleCount.setRulesId(entryString.getKey());
|
|
|
+ alarmRuleCount.setCount(entryString.getValue());
|
|
|
+ alarmRuleCount.setWindturbineId(entry.getKey());
|
|
|
+ alarmRuleCounts.add(alarmRuleCount);
|
|
|
+ }
|
|
|
+ if (count > 0) {
|
|
|
+ //统计部件分类的部件次数
|
|
|
+ AlarmPartsCount alarmPartsCount = new AlarmPartsCount();
|
|
|
+ alarmPartsCount.setId(SnowflakeGenerator.generateId());
|
|
|
+ alarmPartsCount.setAlarmDate(dtStart);
|
|
|
+ alarmPartsCount.setParts(entry1.getKey());
|
|
|
+ alarmPartsCount.setCount(count);
|
|
|
+ alarmPartsCount.setWindturbineId(entry.getKey());
|
|
|
+ acList.add(alarmPartsCount);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (alarmRuleCounts.size() > 0)
|
|
|
+ alarmRuleCountMapper.batchInsert(alarmRuleCounts);
|
|
|
+ if (acList.size() > 0)
|
|
|
+ alarmPartsCountMapper.batchInsert(acList);
|
|
|
+ dtStart = dtEnd;
|
|
|
+ dtEnd = DateUtil.addDays(dtStart, 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch(Exception ex){
|
|
|
+ log.error(ex.getMessage());
|
|
|
+ log.error(ex.getStackTrace().toString());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+}
|