From 74e3485e6bae63e73f5d3be8b36c671e30aa389d Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 20 十一月 2024 10:31:21 +0800
Subject: [PATCH] 1

---
 src/main/java/com/yssh/service/WarningAnalyseService.java |  308 ++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 232 insertions(+), 76 deletions(-)

diff --git a/src/main/java/com/yssh/service/WarningAnalyseService.java b/src/main/java/com/yssh/service/WarningAnalyseService.java
index f04b5d4..0a0aece 100644
--- a/src/main/java/com/yssh/service/WarningAnalyseService.java
+++ b/src/main/java/com/yssh/service/WarningAnalyseService.java
@@ -1,6 +1,5 @@
 package com.yssh.service;
 
-import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.concurrent.CountDownLatch;
 
@@ -9,6 +8,7 @@
 import com.yssh.mapper.QxshMapper;
 import com.yssh.mapper.SuYuanMapper;
 import com.yssh.mapper.WarningDetailMapper;
+import com.yssh.utils.CacheUtils;
 import com.yssh.utils.CalculateUtils;
 import com.yssh.utils.DateUtils;
 import com.yssh.utils.StringUtils;
@@ -18,7 +18,6 @@
 import org.springframework.stereotype.Service;
 
 import com.google.common.collect.Lists;
-import com.yssh.entity.WarningVo;
 
 import javax.annotation.Resource;
 
@@ -27,27 +26,30 @@
     protected final Logger logger = LoggerFactory.getLogger(this.getClass());
 
     @Resource
-    private WarningDetailMapper warningDetailMapper;
+    WarningDetailMapper warningDetailMapper;
 
     @Resource
-    private CommonService commonService;
+    CommonService commonService;
 
     @Resource
-    private SuYuanMapper suYuanMapper;
+    SuYuanMapper suYuanMapper;
 
     @Resource
-    private DictRecordMapper dictRecordMapper;
+    DictRecordMapper dictRecordMapper;
 
     @Resource
-    private AsyncService asyncService;
+    AsyncService asyncService;
 
     @Resource
-    private QxshMapper qxshMapper;
+    QxshMapper qxshMapper;
 
     @Resource
-    private VocValsService vocValsService;
+    VocValsService vocValsService;
 
-    private SimpleDateFormat ym = new SimpleDateFormat("yyyyMM%");
+    @Resource
+    SuYuanService suYuanService;
+
+    private static HashMap<String, String> filterMap = new HashMap<>();
 
     @Async("threadPoolTaskExecutor")
     public void insertWarningDetails(List<WarningDetail> warning) throws Exception {
@@ -63,71 +65,33 @@
     /**
      * 鑾峰彇瀹炴椂鎶ヨ
      */
-    public List<Qxsh> getRunTimeAlarmAnalyse(Date endDate) {
-        Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate));
-        Date startDate = DateUtils.getAPeriodOfTime(endDate, -5, Calendar.HOUR_OF_DAY);
-        Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
+    public List<Qxsh> getRunTimeAlarmAnalyse(Integer start, Integer end) {
+        //List<Qxsh> list = qxshMapper.selectAlarmByBeginAndEnd(start, end);
+        String key = "qxshMapper.selectAlarmByBeginAndEnd." + start + "." + end;
+        List<Qxsh> list = CacheUtils.getListByKey(key);
+        if (null == list) {
+            list = qxshMapper.selectAlarmByBeginAndEnd(start, end);
+            copeHasSuYuan(list);
+            CacheUtils.putListByKey(key, list);
+        }
 
-        return qxshMapper.selectAlarmByBeginAndEnd(start, end);
+        return list;
     }
 
     /**
      * 鑾峰彇瀹炴椂棰勮
      */
-    public List<Qxsh> getRunTimeWarningAnalyse(Date endDate) {
-        Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate));
-        Date startDate = DateUtils.getAPeriodOfTime(endDate, -5, Calendar.HOUR_OF_DAY);
-        Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
-
-        return qxshMapper.selectWarnByBeginAndEnd(start, end);
-    }
-
-    // 宸插簾寮� *
-    public void warningOperationStorage(Date date) {
-        List<WarningDetail> allData = new ArrayList<>();
-        List<WarningDetail> alarms = this.getAlarmWarnAnalyse(date, false);
-        if (StringUtils.isNotEmpty(alarms)) {
-            allData.addAll(alarms);
+    public List<Qxsh> getRunTimeWarningAnalyse(Integer start, Integer end) {
+        //List<Qxsh> list = qxshMapper.selectWarnByBeginAndEnd(start, end);
+        String key = "qxshMapper.selectWarnByBeginAndEnd." + start + "." + end;
+        List<Qxsh> list = CacheUtils.getListByKey(key);
+        if (null == list) {
+            list = qxshMapper.selectWarnByBeginAndEnd(start, end);
+            copeHasSuYuan(list);
+            CacheUtils.putListByKey(key, list);
         }
 
-        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();
-            }
-        }
-    }
-
-    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;
+        return list;
     }
 
     /**
@@ -139,12 +103,20 @@
         Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
         Integer end = Integer.parseInt(DateUtils.getYyyyMmDdHh(endDate));
 
+        String key = "WAS.monthCount." + start + "." + end;
+        Object obj = CacheUtils.get(key);
+        if (obj instanceof Map<?, ?>) {
+            return (Map<String, Integer>) obj;
+        }
+
         Map<String, Integer> result = new HashMap<>();
         Integer warnNum = qxshMapper.countMonthForWarn(start, end);
         result.put("warningNumber", null == warnNum ? 0 : warnNum);
 
         Integer alarmNum = qxshMapper.countMonthForAlarm(start, end);
         result.put("alarmNumber", null == alarmNum ? 0 : alarmNum);
+
+        CacheUtils.put(key, result);
 
         return result;
     }
@@ -158,10 +130,22 @@
         Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
 
         Map<String, List<Map<String, Object>>> result = new HashMap<>();
-        List<Map<String, Object>> warnList = qxshMapper.count7DayForWarn(start, end);
+        //List<Map<String, Object>> warnList = qxshMapper.count7DayForWarn(start, end);
+        String key = "qxshMapper.count7DayForWarn." + start + "." + end;
+        List<Map<String, Object>> warnList = CacheUtils.getListByKey(key);
+        if (null == warnList) {
+            warnList = qxshMapper.count7DayForWarn(start, end);
+            CacheUtils.putListByKey(key, warnList);
+        }
         result.put("warningDayCount", warnList);
 
-        List<Map<String, Object>> alarmList = qxshMapper.count7DayForAlarm(start, end);
+        //List<Map<String, Object>> alarmList = qxshMapper.count7DayForAlarm(start, end);
+        String key2 = "qxshMapper.count7DayForAlarm." + start + "." + end;
+        List<Map<String, Object>> alarmList = CacheUtils.getListByKey(key2);
+        if (null == alarmList) {
+            alarmList = qxshMapper.count7DayForAlarm(start, end);
+            CacheUtils.putListByKey(key2, alarmList);
+        }
         result.put("alarmDayCount", alarmList);
 
         return result;
@@ -176,9 +160,16 @@
         Integer start = Integer.parseInt(DateUtils.getYyyyMmDdHh(startDate));
 
         //  List<String> times = DateUtils.get3Hours();
-        List<Qxsh> list = qxshMapper.select3Hours(start, end);
-        if (null == list || list.isEmpty()) {
-            return null;
+        //List<Qxsh> list = qxshMapper.select3Hours(start, end);
+        //if (null == list || list.isEmpty()) {
+        //    return null;
+        //}
+
+        String key = "qxshMapper.select3Hours." + start + "." + end;
+        List<Qxsh> list = CacheUtils.getListByKey(key);
+        if (null == list) {
+            list = qxshMapper.select3Hours(start, end);
+            CacheUtils.putListByKey(key, list);
         }
 
         /* Map<String, List<Double>> map = new LinkedHashMap<>();
@@ -192,11 +183,59 @@
     }
 
     /**
-     * 鑾峰彇鏈湀鐩戞祴澶ф暟鎹珯鐐规渶澶у�糡OP10
+     * 鑾峰彇鏈湀鐩戞祴绔欑偣鏈�澶у�糡OP10
      */
     public List<Qxsh> selectMonthTop10() {
         String time = DateUtils.getYyyyMm(new Date());
-        List<Qxsh> list = qxshMapper.selectMonthTop10(time + "%");
+        //List<Qxsh> list = qxshMapper.selectMonthTop10(time + "%");
+
+        String key = "qxshMapper.selectMonthTop10." + time;
+        List<Qxsh> list = CacheUtils.getListByKey(key);
+        if (null == list) {
+            list = qxshMapper.selectMonthTop10(time + "%");
+            CacheUtils.putListByKey(key, list);
+        }
+
+        return list;
+    }
+
+    /**
+     * 鑾峰彇鏈懆鐩戞祴绔欑偣鏈�澶у�糡OP10
+     */
+    public List<Qxsh> selectWeekTop10() {
+        Date now = new Date();
+        int weekOfYear = DateUtils.getWeekOfYear(now);
+        int year = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYY, now));
+        Date lastSun = DateUtils.getWeekOfYearForSun(year, weekOfYear);
+        Date mon = DateUtils.getAPeriodOfTime(lastSun, 1, Calendar.DATE);
+        Date sun = DateUtils.getAPeriodOfTime(lastSun, 7, Calendar.DATE);
+        Integer intMon = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, mon) + "00"); // 2023080700
+        Integer intSun = Integer.parseInt(DateUtils.parseDateToStr(DateUtils.YYYYMMDD, sun) + "23"); // 2023081323
+
+        //List<Qxsh> list = qxshMapper.selectWeekTop10(intMon, intSun);
+        String key = "qxshMapper.selectWeekTop10." + intMon + "." + intSun;
+        List<Qxsh> list = CacheUtils.getListByKey(key);
+        if (null == list) {
+            list = qxshMapper.selectWeekTop10(intMon, intSun);
+            CacheUtils.putListByKey(key, list);
+        }
+
+        return list;
+    }
+
+    /**
+     * 鑾峰彇鏈棩鐩戞祴绔欑偣鏈�澶у�糡OP10
+     */
+    public List<Qxsh> selectDayTop10() {
+        String time = DateUtils.getYyyyMmDd(new Date());
+        //List<Qxsh> list = qxshMapper.selectDayTop10(time + "%");
+
+        String key = "qxshMapper.selectDayTop10." + time;
+        List<Qxsh> list = CacheUtils.getListByKey(key);
+        if (null == list) {
+            list = qxshMapper.selectDayTop10(time + "%");
+            CacheUtils.putListByKey(key, list);
+        }
 
         return list;
     }
@@ -225,8 +264,20 @@
 
             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);
+            if (null == suYuan) {
+                String[] strs = suYuanId.split("_");
+                suYuan = new DistanceSuYuan(null, null, 0.0, suYuanId, Integer.parseInt(strs[0]), Integer.parseInt(strs[1]), 0, 0, null, 0.0, 0.0, 0.0, 0.0);
+            }
+            //List<String> ids3d = CalculateUtils.aloneCrosswiseExtend(point, 50);
+            //DistanceSuYuan suMax = suYuanMapper.getSuYuan500Max(tab, ids3d);
+
+            //String filter = CalculateUtils.getFilterByExtend(point, 50);
+            String filter = getFilterByPoint(point, 50);
+            DistanceSuYuan suMax = suYuanMapper.getSuYuan500MaxByFilter(tab, filter);
+            if (null == suMax) {
+                String[] strs = suYuanId.split("_");
+                suMax = new DistanceSuYuan(null, null, 0.0, suYuanId, Integer.parseInt(strs[0]), Integer.parseInt(strs[1]), 0, 0, null, 0.0, 0.0, 0.0, 0.0);
+            }
             suMax.setAddr(getAddr(suMax.getId()));
 
             Report report = Report.calcReport(wd, suYuan, suMax);
@@ -239,6 +290,25 @@
         }
 
         return list;
+    }
+
+    public static String getFilterByPoint(MonitorPointPosition point, int range) {
+        String key = String.format("%d_%d_%d", point.getX(), point.getY(), range);
+        if (filterMap.containsKey(key)) {
+            return filterMap.get(key);
+        }
+
+        List<String> ids = CalculateUtils.aloneCrosswiseExtend(point, range);
+        for (int i = 0, c = ids.size(); i < c; i++) {
+            ids.set(i, "'" + ids.get(i) + "'");
+        }
+
+        String filter = "id in (" + StringUtils.join(ids, ",") + ")";
+        if (!filterMap.containsKey(key)) {
+            filterMap.put(key, filter);
+        }
+
+        return filter;
     }
 
     private Hashtable<String, Boolean> getExistTabDict(List<Qxsh> rsList) {
@@ -270,4 +340,90 @@
 
         return null;
     }
+
+    /**
+     * 瀛樺偍鍛婅/棰勮淇℃伅
+     */
+    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();
+            }
+        }
+    }
+
+    /**
+     * 鑾峰彇鍛婅/棰勮鍒嗘瀽
+     */
+    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;
+    }
+
+    /**
+     * 澶勭悊鏄惁鍚湁婧簮淇℃伅
+     */
+    private List<Qxsh> copeHasSuYuan(List<Qxsh> list) {
+        if (null == list || list.isEmpty()) {
+            return null;
+        }
+
+        Map<String, Integer> tabs = new HashMap<>();
+        for (Qxsh qxsh : list) {
+            String tab = "su_yuan_" + qxsh.getTime();
+            if (!tabs.containsKey(tab)) {
+                tabs.put(tab, suYuanMapper.isTableExists(tab));
+            }
+
+            int rows = tabs.get(tab);
+            if (0 == rows) {
+                continue;
+            }
+
+            String suYuanId = suYuanService.selectSuYuanIdByName(qxsh.getName());
+            String createTime = qxsh.getTime().substring(0, 4) + "-" + qxsh.getTime().substring(4, 6) + "-" + qxsh.getTime().substring(6, 8) + " " + qxsh.getTime().substring(8, 10) + ":00:00";
+
+            Integer isSuYuan = qxshMapper.hasSuYuan(suYuanId, createTime);
+            qxsh.setIsSuYuan(isSuYuan);
+
+            Integer isFast = qxshMapper.countFastSuYuan(suYuanId, createTime);
+            qxsh.setIsFast(isFast);
+        }
+
+        return list;
+    }
 }

--
Gitblit v1.9.3