|
@@ -0,0 +1,157 @@
|
|
|
+package com.gyee.wisdom.alarm.schedule.job;
|
|
|
+
|
|
|
+
|
|
|
+import com.gyee.wisdom.alarm.schedule.cache.DataDictionaryCache;
|
|
|
+import com.gyee.wisdom.alarm.schedule.entity.*;
|
|
|
+import com.gyee.wisdom.alarm.schedule.mapper.WindturbineAlarmIdCountMapper;
|
|
|
+import com.gyee.wisdom.alarm.schedule.mapper.WindturbineAlarmPartsCountMapper;
|
|
|
+import com.gyee.wisdom.alarm.schedule.service.*;
|
|
|
+import com.gyee.wisdom.alarm.schedule.util.SnowflakeGenerator;
|
|
|
+import com.gyee.wisdom.common.utils.DateUtil;
|
|
|
+import com.xxl.job.core.handler.annotation.XxlJob;
|
|
|
+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 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 AlarmCountPartsHandler {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlarmSnapService alarmSnapService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TableNameService tableNameService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WindturbineAlarmIdCountMapper windturbineAlarmIdCountMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WindturbineAlarmPartsCountMapper windturbineAlarmPartsCountMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private Warning2Service warning2Service;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WindturbineService windturbineService;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AlarmHistoryService alarmHistoryService;
|
|
|
+
|
|
|
+ @XxlJob("AlarmCountParts")
|
|
|
+ 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<WindturbineAlarmPartsCount> acList = new ArrayList<>();
|
|
|
+ List<WindturbineAlarmIdCount> 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 = listEntry.getValue();
|
|
|
+ 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();
|
|
|
+ if (value.size()>0) {
|
|
|
+ for (AlarmHistory alarmHistory : value) {
|
|
|
+ AlarmSnap alarmSnap = alarmSnapService.queryById(alarmHistory.getSnapId());
|
|
|
+ Warning2 warning2 = warning2Service.selectByEdnvalue(alarmSnap.getAlertValue());
|
|
|
+ if (warning2 != null){
|
|
|
+ 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.selectByEdnvalue(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()) {
|
|
|
+ //统计某一种规则的报警次数
|
|
|
+ WindturbineAlarmIdCount windturbineAlarmIdCount = new WindturbineAlarmIdCount();
|
|
|
+ windturbineAlarmIdCount.setId(SnowflakeGenerator.generateId());
|
|
|
+ windturbineAlarmIdCount.setAlarmDate(dtStart);
|
|
|
+ windturbineAlarmIdCount.setParts(entry1.getKey());
|
|
|
+ windturbineAlarmIdCount.setRulesId(entryString.getKey());
|
|
|
+ windturbineAlarmIdCount.setCount(entryString.getValue());
|
|
|
+ windturbineAlarmIdCount.setWindturbineId(entry.getKey());
|
|
|
+ windturbineAlarmIdCount.setPartsName(DataDictionaryCache.getPartName(entry1.getKey()));
|
|
|
+ windturbineAlarmIdCount.setWindpowerstationId(windturbineService.selectWindturbineByWindturbineid(entry.getKey()).getWindpowerstationid());
|
|
|
+ alarmRuleCounts.add(windturbineAlarmIdCount);
|
|
|
+ }
|
|
|
+ if (count > 0) {
|
|
|
+ //统计部件分类的部件次数
|
|
|
+ WindturbineAlarmPartsCount windturbineAlarmPartsCount = new WindturbineAlarmPartsCount();
|
|
|
+ windturbineAlarmPartsCount.setId(SnowflakeGenerator.generateId());
|
|
|
+ windturbineAlarmPartsCount.setAlarmDate(dtStart);
|
|
|
+ windturbineAlarmPartsCount.setParts(entry1.getKey());
|
|
|
+ windturbineAlarmPartsCount.setCount(count);
|
|
|
+ windturbineAlarmPartsCount.setPartsName(DataDictionaryCache.getPartName(entry1.getKey()));
|
|
|
+ windturbineAlarmPartsCount.setWindturbineId(entry.getKey());
|
|
|
+ windturbineAlarmPartsCount.setWindpowerstationId(windturbineService.selectWindturbineByWindturbineid(entry.getKey()).getWindpowerstationid());
|
|
|
+ acList.add(windturbineAlarmPartsCount);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (alarmRuleCounts.size() > 0) {
|
|
|
+ windturbineAlarmIdCountMapper.batchInsert(alarmRuleCounts);
|
|
|
+ }
|
|
|
+ if (acList.size() > 0) {
|
|
|
+ windturbineAlarmPartsCountMapper.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;
|
|
|
+ }
|
|
|
+}
|