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 |  116 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 115 insertions(+), 1 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 1d41cd3..4abeb79 100644
--- a/src/main/java/com/zhitan/engine/service/impl/DataCleaningServiceImpl.java
+++ b/src/main/java/com/zhitan/engine/service/impl/DataCleaningServiceImpl.java
@@ -1,5 +1,8 @@
 package com.zhitan.engine.service.impl;
 
+import com.influxdb.client.domain.WritePrecision;
+import com.influxdb.client.write.Point;
+import com.zhitan.engine.config.InfluxDBConfig;
 import com.zhitan.engine.influxdb.InfluxdbRepository;
 import com.zhitan.engine.entity.*;
 import com.zhitan.engine.repository.*;
@@ -20,13 +23,18 @@
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
+import static java.util.List.*;
+
 /**
  * 鏁版嵁娓呮礂鏈嶅姟瀹炵幇绫�
  */
 @Slf4j
 @Service
 public class DataCleaningServiceImpl implements DataCleaningService {
-
+    private final String TAG = "tag";
+    private final String FIELD_VALUE = "value";
+    @Autowired
+    private InfluxDBConfig influxDBConfig;
     @Autowired
     private InfluxdbRepository influxdbRepository;
 
@@ -164,6 +172,112 @@
     }
 
     @Override
+    public void calculateTotalElectricity(LocalDateTime dateTime) {
+        log.info("寮�濮嬫墽琛屽叏鍘傛�荤數閲忕粺璁★紝鏃堕棿鐐癸細{}", dateTime);
+        
+        // 瀹氫箟闇�瑕佺粺璁$殑鐢佃〃缃戝叧鍒楄〃
+        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");
+
+
+        // 瀹氫箟闇�瑕佺粺璁$殑鍙傛暟绫诲瀷鍙婂叾缁熻鏂瑰紡
+        Map<String, String> parameterTypes = new java.util.HashMap<>(Map.of(
+                "ActiveZT", "SUM" // 姝e悜鎬绘湁鍔熺數鑳� - 姹傚拰
+                , "ActiveZN", "SUM" // 姝e悜鎬绘棤鍔熺數鑳� - 姹傚拰
+                , "ActiveElectricity", "SUM" // 鏈夊姛鐢佃兘 - 姹傚拰
+                , "NoActiveElectricity", "SUM" // 鏃犲姛鍔熺巼 - 姹傚拰
+                , "ExpZN", "SUM" // 鍙嶅悜鎬绘棤鍔熺數鑳� - 姹傚拰
+                , "Exp", "SUM" // 鍙嶅悜鏈夊姛鐢佃兘 - 姹傚拰
+                , "ActivePow", "SUM" // 姝e悜鏈夊姛鍔熺巼 - 姹傚拰
+                , "VoltageUAB", "AVG" // 鎬荤數鍘� - 骞冲潎鍊�
+                , "VoltageUBC", "AVG" // 鎬荤數鍘� - 骞冲潎鍊�
+                , "VoltageUCA", "AVG" // 鎬荤數鍘� - 骞冲潎鍊�
+
+        ));
+        parameterTypes.put("CurrentA", "SUM");
+        parameterTypes.put("CurrentB", "SUM");
+        parameterTypes.put("CurrentC", "SUM");
+        parameterTypes.put("Io", "SUM");
+        parameterTypes.put("VoltageA", "AVG");
+        parameterTypes.put("VoltageB", "AVG");
+        parameterTypes.put("VoltageC", "AVG");
+
+        // 淇濆瓨鎬昏〃鏁版嵁
+        String old = "old_";
+        String seven = "seven_";
+        String out = "out_";
+        String total = "total_";
+        // 閬嶅巻鎵�鏈夊弬鏁扮被鍨嬭繘琛岀粺璁�
+        parameterTypes.forEach((paramType, calcType) -> {
+            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);
+
+        });
+        
+        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) {
         log.info("寮�濮嬭绠楀皷宄板钩璋风敤鐢甸噺锛岀储寮旾D锛歿}锛屾椂闂寸偣锛歿}", indexId, dateTime);

--
Gitblit v1.9.3