From aa86cb57b09c5300db3c33d75d91d8b51a4b636f Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 16 六月 2023 09:27:45 +0800
Subject: [PATCH] 1

---
 src/main/java/com/yssh/service/impl/WarningAnalyseServiceImpl.java |  189 ++++++++++++++++++++++++++++-------------------
 1 files changed, 112 insertions(+), 77 deletions(-)

diff --git a/src/main/java/com/yssh/service/impl/WarningAnalyseServiceImpl.java b/src/main/java/com/yssh/service/impl/WarningAnalyseServiceImpl.java
index dd31379..3fef537 100644
--- a/src/main/java/com/yssh/service/impl/WarningAnalyseServiceImpl.java
+++ b/src/main/java/com/yssh/service/impl/WarningAnalyseServiceImpl.java
@@ -10,22 +10,14 @@
 import java.util.Map;
 import java.util.concurrent.CountDownLatch;
 
+import com.yssh.dao.*;
+import com.yssh.entity.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 
 import com.google.common.collect.Lists;
-import com.yssh.dao.AlertConfigMapper;
-import com.yssh.dao.DictRecordMapper;
-import com.yssh.dao.SuYuanMapper;
-import com.yssh.dao.WarningDetailMapper;
-import com.yssh.entity.DictRecord;
-import com.yssh.entity.MonitorPointPosition;
-import com.yssh.entity.SuYuan2d;
-import com.yssh.entity.SuYuanMonitorData;
-import com.yssh.entity.WarningDetail;
 import com.yssh.entity.vo.WarningVo;
 import com.yssh.service.IAsyncService;
 import com.yssh.service.ICommonService;
@@ -38,14 +30,10 @@
 
 @Service
 public class WarningAnalyseServiceImpl implements IWarningAnalyseService {
-
     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
     @Resource
     private WarningDetailMapper warningDetailMapper;
-
-    @Resource
-    private AlertConfigMapper alertConfigMapper;
 
     @Resource
     private ICommonService commonService;
@@ -58,6 +46,11 @@
 
     @Resource
     private IAsyncService asyncService;
+
+    @Resource
+    private QxshMapper qxshMapper;
+
+    private SimpleDateFormat ym = new SimpleDateFormat("yyyyMM%");
 
     private SimpleDateFormat ymdh = new SimpleDateFormat("yyyyMMddHH");
 
@@ -88,15 +81,16 @@
 
         List<WarningDetail> list = new ArrayList<>();
         Calendar calendar = getCalendar(nowDate);
-        for (int i = 0; i < 3; i++) {
+        for (int i = 0; i < 6; i++) {
             calendar.add(Calendar.HOUR, -1);
-            List<WarningDetail> rs = alarmAnalyseOperation(calendar.getTime());
+            List<WarningDetail> rs = getAlarmWarnAnalyse(calendar.getTime(), false);
             if (null != rs && rs.size() > 0) {
                 list.addAll(rs);
             }
         }
         list.forEach(s -> {
-            result.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue()));
+            String time = ymdh.format(s.getCreateTime());
+            result.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue(), time));
         });
         return result;
     }
@@ -122,88 +116,63 @@
 
         List<WarningDetail> list = new ArrayList<>();
         Calendar calendar = getCalendar(nowDate);
-        for (int i = 0; i < 3; i++) {
+        for (int i = 0; i < 6; i++) {
             calendar.add(Calendar.HOUR, -1);
-            List<WarningDetail> rs = warningAnalyseOperation(calendar.getTime());
+            List<WarningDetail> rs = getAlarmWarnAnalyse(calendar.getTime(), true);
             if (null != rs && rs.size() > 0) {
                 list.addAll(rs);
             }
         }
         list.forEach(s -> {
-            result.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue()));
+            String time = ymdh.format(s.getCreateTime());
+            result.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue(), time));
         });
         return result;
     }
 
-    public List<WarningDetail> alarmAnalyseOperation(Date date) {
-        List<WarningDetail> result = new ArrayList<>();
-        String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
+    private List<WarningDetail> getAlarmWarnAnalyse(Date date, boolean isWarn) {
         List<String> ids = CalculateUtils.assembleId(commonService.getCheckPoints3d());
-        if (ids.size() == 0) {
-            return result;
-        }
-        DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
-        if (null == dictRecord) {
-            return result;
-        }
-        String tableName = dictRecord.getTableName();
-        if (suYuanMapper.isTableExists(tableName) == 0) {
-            return result;
-        }
 
-        List<SuYuan2d> list = suYuanMapper.getAlarmsAnalyse(tableName, ids);
-        if (null != list && list.size() > 0) {
-            for (SuYuan2d s : list) {
-                String locationName = commonService.select3dCheckPointById(s.getId()).getName();
-                result.add(new WarningDetail(0L, tableName, s.getId(), locationName, date, 0, s.getValue()));
-            }
-        }
-
-        return result;
-    }
-
-    public List<WarningDetail> warningAnalyseOperation(Date date) {
-        List<WarningDetail> result = new ArrayList<>();
         String time = DateUtils.parseDateToStr(DateUtils.YYYYMMDDHH, date);
-        List<String> ids = CalculateUtils.assembleId(commonService.getCheckPoints3d());
-        if (ids.size() == 0) {
-            return result;
-        }
         DictRecord dictRecord = dictRecordMapper.selectByCreateTime(Long.parseLong(time));
-        if (null == dictRecord) {
-            return result;
-        }
+        if (null == dictRecord) return null;
+
         String tableName = dictRecord.getTableName();
-        if (suYuanMapper.isTableExists(tableName) == 0) {
-            return result;
+        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;
         }
 
-        List<SuYuan2d> list = suYuanMapper.getWarningAnalyse(tableName, ids);
-        if (null != list && list.size() > 0) {
-            for (SuYuan2d s : list) {
-                String locationName = commonService.select3dCheckPointById(s.getId()).getName();
-                result.add(new WarningDetail(0L, tableName, s.getId(), locationName, date, 1, s.getValue()));
-            }
+        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;
     }
 
     @Override
     public void warningOperationStorage(Date date) {
         List<WarningDetail> allData = new ArrayList<>();
-        List<WarningDetail> alarms = this.alarmAnalyseOperation(date);
+        List<WarningDetail> alarms = this.getAlarmWarnAnalyse(date, false);
         if (StringUtils.isNotEmpty(alarms)) {
             allData.addAll(alarms);
         }
-        List<WarningDetail> warnings = this.warningAnalyseOperation(date);
+
+        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("鎵归噺鎻掑叆鍛婅鏁版嵁鍑虹幇寮傚父锛侊紒锛�");
+                logger.error("鎵归噺鎻掑叆鍛婅鏁版嵁鍑虹幇寮傚父锛侊紒锛�", e);
                 e.printStackTrace();
             }
         }
@@ -213,37 +182,44 @@
     public Map<String, Integer> countThisMonthAlarmAndWarning() {
         Map<String, Integer> result = new HashMap<>();
         Map<String, Object> param = new HashMap<>();
+        param.put("startTime", ymdhms.format(DateUtils.getMonthStart()));
+        param.put("endTime", ymdhms.format(DateUtils.getMonthEnd()));
+
         param.put("type", 0);
-        param.put("startTime", DateUtils.getMonthStart());
-        param.put("endTime", DateUtils.getMonthEnd());
         List<WarningVo> alarms = warningDetailMapper.selectWarningDetailByMap(param);
         if (StringUtils.isNull(alarms)) {
             alarms = new ArrayList<>();
         }
         result.put("alarmNumber", alarms.size());
+
         param.put("type", 1);
         List<WarningVo> warnings = warningDetailMapper.selectWarningDetailByMap(param);
         if (StringUtils.isNull(warnings)) {
             warnings = new ArrayList<>();
         }
         result.put("warningNumber", warnings.size());
+
         return result;
     }
 
     @Override
     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", ymdhms.format(last));
+        param.put("endTime", ymdhms.format(now));
+
         param.put("type", 0);
-        Date endTime = DateUtils.getNowDate();
-        //涓婂懆鐨勪粖澶�
-        param.put("startTime", DateUtils.getAPeriodOfTime(endTime, -7, Calendar.DATE));
-        param.put("endTime", endTime);
         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;
     }
 
@@ -327,6 +303,9 @@
         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<>();
@@ -339,21 +318,75 @@
             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");
+                //String suYuanId = map.get("id").toString();
+                //map.put("name", commonService.select3dCheckPointById(suYuanId).getName());
+                //map.remove("id");
+                map.put("name", c.getName());
+                result.add(map);
             }
-            result.add(map);
         }
 
         return CalculateUtils.sort(result, "value", true).subList(0, 10);
     }
 
     @Override
+    public List<Qxsh> selectMonthTop10() {
+        String time = ym.format(new Date());
+
+        List<Qxsh> list = qxshMapper.selectMonthTop10(time);
+
+        return list;
+    }
+
+    @Override
+    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;
+    }
+
+    @Override
     public List<WarningVo> getAlarmAndWarnByTime(Date begin, Date end) {
+        String startTime = ymdhms.format(begin);
+        String endTime = ymdhms.format(end);
+
+        List<WarningDetail> rs = warningDetailMapper.selectByTime(startTime, endTime);
+        if (null == rs || rs.isEmpty()) {
+            return null;
+        }
+
+        List<WarningVo> list = new ArrayList<>();
+        for (WarningDetail wd : rs) {
+            String time = ymdh.format(wd.getCreateTime());
+            list.add(new WarningVo(wd.getLocationName(), wd.getSuYuanId(), 0.0, 0.0, wd.getValue(), time));
+        }
+
+        return list;
+    }
+
+    public List<WarningVo> getAlarmAndWarnByTime_old(Date begin, Date end) {
         List<WarningVo> list = new ArrayList<>();
         List<String> ids = CalculateUtils.assembleId(commonService.getCheckPoints3d());
         if (ids.size() == 0) {
@@ -376,13 +409,15 @@
         for (String tab : tables) {
             List<WarningDetail> alarmList = getAlarmByTime(tab, ids);
             for (WarningDetail s : alarmList) {
-                list.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue()));
+                String time = ymdh.format(s.getCreateTime());
+                list.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue(), time));
             }
         }
         for (String tab : tables) {
             List<WarningDetail> warnList = getWarnByTime(tab, ids);
             for (WarningDetail s : warnList) {
-                list.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue()));
+                String time = ymdh.format(s.getCreateTime());
+                list.add(new WarningVo(s.getLocationName(), s.getSuYuanId(), 0.0, 0.0, s.getValue(), time));
             }
         }
 

--
Gitblit v1.9.3