From d9714be7130e14e063e6499637e1cc5241ff9dd3 Mon Sep 17 00:00:00 2001
From: baoshiwei <baoshiwei@shlanbao.cn>
Date: 星期六, 21 六月 2025 10:06:15 +0800
Subject: [PATCH] refactor(engine): 重构全厂总电量统计逻辑

---
 src/main/java/com/zhitan/engine/service/impl/DataCleaningServiceImpl.java |  127 +++++++++++++++++++++++------------------
 1 files changed, 71 insertions(+), 56 deletions(-)

diff --git a/src/main/java/com/zhitan/engine/service/impl/DataCleaningServiceImpl.java b/src/main/java/com/zhitan/engine/service/impl/DataCleaningServiceImpl.java
index 9a7a1c3..4abeb79 100644
--- a/src/main/java/com/zhitan/engine/service/impl/DataCleaningServiceImpl.java
+++ b/src/main/java/com/zhitan/engine/service/impl/DataCleaningServiceImpl.java
@@ -176,12 +176,9 @@
         log.info("寮�濮嬫墽琛屽叏鍘傛�荤數閲忕粺璁★紝鏃堕棿鐐癸細{}", dateTime);
         
         // 瀹氫箟闇�瑕佺粺璁$殑鐢佃〃缃戝叧鍒楄〃
-        List<String> gatewayCodes = new java.util.ArrayList<>(List.of("00601925032100028432", "00601925032100026840", "00601925032100028685", "00601925032100027392", "00601925032100028050", "00601925032100028751"));
-        gatewayCodes.add("00601925032100026275");
-        gatewayCodes.add("00601925032100028117");
-        gatewayCodes.add("00601925032100028814");
-        gatewayCodes.add("00601925032100028990");
-        gatewayCodes.add("00601925032100031307");
+        List<String> oldGatewayList = List.of("00601925032100028432", "00601925032100026840");
+        List<String> sevenGatewayList = List.of("00601925032100028685", "00601925032100027392", "00601925032100028050", "00601925032100028751","00601925032100026275","00601925032100028117","00601925032100028814","00601925032100028990");
+        List<String> outGatewayList = List.of("00601925032100031307");
 
 
         // 瀹氫箟闇�瑕佺粺璁$殑鍙傛暟绫诲瀷鍙婂叾缁熻鏂瑰紡
@@ -196,13 +193,7 @@
                 , "VoltageUAB", "AVG" // 鎬荤數鍘� - 骞冲潎鍊�
                 , "VoltageUBC", "AVG" // 鎬荤數鍘� - 骞冲潎鍊�
                 , "VoltageUCA", "AVG" // 鎬荤數鍘� - 骞冲潎鍊�
-//            ,"CurrentA", "SUM" // 鎬荤數娴� - 姹傚拰
-//            ,"CurrentB", "SUM" // 鎬荤數娴� - 姹傚拰
-//            ,"CurrentC", "SUM" // 鎬荤數娴� - 姹傚拰
-//            ,"Io", "SUM" // 鎬荤數娴� - 姹傚拰
-//            ,"VoltageA", "AVG" // 鎬荤數鍘� - 骞冲潎鍊�
-//            ,"VoltageB", "AVG" // 鎬荤數鍘� - 骞冲潎鍊�
-//            ,"VoltageC", "AVG"
+
         ));
         parameterTypes.put("CurrentA", "SUM");
         parameterTypes.put("CurrentB", "SUM");
@@ -211,57 +202,81 @@
         parameterTypes.put("VoltageA", "AVG");
         parameterTypes.put("VoltageB", "AVG");
         parameterTypes.put("VoltageC", "AVG");
-        
-        // 璁$畻鏃堕棿鑼冨洿锛堝綋鍓嶅垎閽燂級
-        LocalDateTime startTime = dateTime.withSecond(0).withNano(0);
-        LocalDateTime endTime = startTime.plusMinutes(1);
-        
+
+        // 淇濆瓨鎬昏〃鏁版嵁
+        String old = "old_";
+        String seven = "seven_";
+        String out = "out_";
+        String total = "total_";
         // 閬嶅巻鎵�鏈夊弬鏁扮被鍨嬭繘琛岀粺璁�
         parameterTypes.forEach((paramType, calcType) -> {
-            try {
-                double totalValue = 0;
-                int meterCount = 0;
-                
-                // 閬嶅巻鎵�鏈夌綉鍏充笅鐨勭數琛�
-                for (String gatewayCode : gatewayCodes) {
-                    // 閬嶅巻姣忎釜缃戝叧涓嬬殑8涓數琛�
-                    for (int i = 1; i <= 8; i++) {
-                        // 鏋勫缓tag鍚嶇О鏍煎紡锛氱綉鍏崇紪鍙穇鐢佃〃搴忓彿_鍙傛暟绫诲瀷
-                        // 绀轰緥锛�00601925032100028432_1_ActiveZT
-                        String tag = String.format("%s_%d_%s", gatewayCode, i, paramType);
-                        // 鏌ヨ鏈�鍚庝竴娆′繚瀛樻暟鎹�
+            TotalCount totalCount = new TotalCount();
+            statistics(paramType, calcType, oldGatewayList, old, totalCount);
+            statistics(paramType, calcType, sevenGatewayList, seven, totalCount);
+            statistics(paramType, calcType, outGatewayList, out, totalCount);
+            clacAndSave(paramType, calcType,total,  totalCount.totalValue, totalCount.totalCount);
 
-                        Double value = influxdbRepository.getLastPoint(influxDBConfig.getMeasurement(),TAG,tag);
-                        if (value != null) {
-                            totalValue +=  value ;
-                            meterCount++;
-                        }
-                    }
-                }
-                
-                // 璁$畻鏈�缁堝��
-                double finalValue = "SUM".equals(calcType) ? totalValue : (meterCount > 0 ? totalValue / meterCount : 0);
-                
-                // 淇濆瓨鎬昏〃鏁版嵁
-                String totalTag = "total_" + paramType;
-
-                Point point = Point
-                        .measurement(influxDBConfig.getMeasurement())
-                        .addTag(TAG, totalTag)
-                        .addField(FIELD_VALUE, finalValue)
-                        .time(Instant.now(), WritePrecision.S);
-
-                influxdbRepository.writePoint(point);
-                
-                log.info("缁熻瀹屾垚锛歿}={} ({}涓數琛�)", totalTag, finalValue, meterCount);
-            } catch (Exception e) {
-                log.error("缁熻鍙傛暟{}澶辫触锛歿}", paramType, e.getMessage(), e);
-            }
         });
         
         log.info("鍏ㄥ巶鎬荤數閲忕粺璁″畬鎴愶紝鏃堕棿鐐癸細{}", dateTime);
     }
 
+    private static final int METER_COUNT_PER_GATEWAY = 8;
+
+    private static class TotalCount {
+        public int totalCount;
+        public double totalValue;
+        public TotalCount() {
+            totalCount = 0;
+            totalValue = 0;
+        }
+    }
+
+private void statistics(String paramType, String calcType, List<String> gatewayCodes, String area, TotalCount totalCount) {
+    try {
+        double sumValue = 0;
+        int meterCount = 0;
+
+        // 閬嶅巻鑰佹ゼ杞﹂棿鎵�鏈夌綉鍏充笅鐨勭數琛�
+        for (String gatewayCode : gatewayCodes) {
+            // 閬嶅巻姣忎釜缃戝叧涓嬬殑8涓數琛�
+            for (int i = 1; i <= METER_COUNT_PER_GATEWAY; i++) {
+                // 鏋勫缓tag鍚嶇О鏍煎紡锛氱綉鍏崇紪鍙穇鐢佃〃搴忓彿_鍙傛暟绫诲瀷
+                // 绀轰緥锛�00601925032100028432_1_ActiveZT
+                String tag = String.format("%s_%d_%s", gatewayCode, i, paramType);
+                // 鏌ヨ鏈�鍚庝竴娆′繚瀛樻暟鎹�
+                Double value = influxdbRepository.getLastPoint(influxDBConfig.getMeasurement(),TAG, tag);
+                if (value != null) {
+                    sumValue += value;
+                    meterCount++;
+                }
+            }
+        }
+
+        totalCount.totalCount += meterCount;
+        totalCount.totalValue += sumValue;
+        clacAndSave(paramType, calcType, area, sumValue, meterCount);
+
+    } catch (Exception e) {
+        log.error("缁熻鍙傛暟{}澶辫触锛歿}", paramType, e.getMessage(), e);
+    }
+}
+
+
+    private void clacAndSave(String paramType, String calcType, String area, double sumValue, int meterCount) {
+        // 璁$畻鏈�缁堝��
+        double finalValue = "SUM".equals(calcType) ? sumValue : (meterCount > 0 ? sumValue / meterCount : 0);
+        String sumTag = area + paramType;
+        Point point = Point
+                .measurement(influxDBConfig.getMeasurement())
+                .addTag(TAG, sumTag)
+                .addField(FIELD_VALUE, finalValue)
+                .time(Instant.now(), WritePrecision.S);
+
+        influxdbRepository.writePoint(point);
+        log.info("缁熻瀹屾垚锛歿}={} ({}涓數琛�)", sumTag, finalValue, meterCount);
+    }
+
     @Override
     @Transactional
     public void calculatePeakValleyElectricity(String indexId, LocalDateTime dateTime) {

--
Gitblit v1.9.3