From 16a91a31a42336c9a4be5256bfaedfd16c638d3b Mon Sep 17 00:00:00 2001
From: whycxzp <glperry@163.com>
Date: 星期五, 13 六月 2025 16:54:51 +0800
Subject: [PATCH] 蓄电池核容测试信息统计

---
 src/main/java/com/whyc/service/LeaderHomeService.java |  226 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 217 insertions(+), 9 deletions(-)

diff --git a/src/main/java/com/whyc/service/LeaderHomeService.java b/src/main/java/com/whyc/service/LeaderHomeService.java
index d16a59d..a941000 100644
--- a/src/main/java/com/whyc/service/LeaderHomeService.java
+++ b/src/main/java/com/whyc/service/LeaderHomeService.java
@@ -1,17 +1,30 @@
 package com.whyc.service;
 
+import com.whyc.dto.NameValueDto;
 import com.whyc.dto.Response;
 import com.whyc.factory.ThreadPoolExecutorFactory;
+import com.whyc.pojo.db_batt_testdata.BatttestdataInf;
+import com.whyc.pojo.db_station.BattInf;
+import com.whyc.pojo.db_station.PowerInf;
 import com.whyc.pojo.db_station.StationInf;
+import com.whyc.util.DateUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.stream.Collectors;
+
+import java.time.LocalDateTime;
+import java.time.LocalDate;
+import java.time.ZoneId;
+import java.time.temporal.TemporalAdjusters;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static com.whyc.util.DateUtil.convertToLocalDateTime;
+
 
 @Service
 public class LeaderHomeService {
@@ -26,23 +39,218 @@
     private StationInfService stationInfService;
 
     @Autowired
-    private BatttestdataInfService bdTestDataInfService;
+    private BatttestdataInfService battTestDataInfService;
 
-    public Response getAll() {
+    public Response getAll(Integer userId) {
         Response response = new Response();
         Map<String,Object> map = new HashMap<>();
 
-        CountDownLatch latch = new CountDownLatch(1);
+        CountDownLatch latch = new CountDownLatch(9);
         ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
 
-        //绔欑偣淇℃伅缁熻,鎸夌珯鐐圭被鍨嬪拰绔欑偣鐢靛帇 TODO 寰呮潕鍐涙槑纭珯鐐圭被鍨嬫槸浠�涔堝瓧娈�,鐩墠娌℃湁
+        //绔欑偣淇℃伅缁熻,
         poolExecutor.execute(() -> {
-            List<StationInf> stationInfList =stationInfService.getAllWithFields("station_type");
-            Map<String, List<StationInf>> typeMap = stationInfList.stream().collect(Collectors.groupingBy(StationInf::getStationType));
+            stationInfoStatistics(userId, map);
+            latch.countDown();
+        });
 
+        //鐢垫簮淇℃伅(鍝佺墝鍙婄數鍘�)缁熻
+        poolExecutor.execute(() -> {
+            powerInfoStatistics(userId, map);
+            latch.countDown();
+        });
+        //钃勭數姹犱俊鎭�(鍝佺墝鍙婄數鍘�)缁熻
+        poolExecutor.execute(() -> {
+            batteryInfoStatistics(userId, map);
+            latch.countDown();
+        });
+        //娴嬭瘯鏁版嵁淇℃伅缁熻
+        poolExecutor.execute(() -> {
+            testDataInfoStatistics(userId, map);
+            latch.countDown();
+        });
+        //TODO 璁惧鏁伴噺淇℃伅缁熻
+        poolExecutor.execute(() -> {
+            //deviceInfoStatistics(userId, map);
+            latch.countDown();
+        });
+        //TODO 瀹炴椂鍛婅淇℃伅缁熻
+        poolExecutor.execute(() -> {
+            //alarmRealtimeStatistics(userId, map);
+            latch.countDown();
+        });
+        //TODO 涓嶅悓鏃堕棿鍚屼竴鍝佺墝钃勭數姹犳�ц兘缁熻
+        poolExecutor.execute(() -> {
+            //performanceInfoStatistics(userId, map);
+            latch.countDown();
+        });
+        //TODO 鍚屼竴鏃堕棿涓嶅悓鍝佺墝钃勭數姹犳�ц兘缁熻
+        poolExecutor.execute(() -> {
+            //performanceInfoStatistics2(userId, map);
+            latch.countDown();
+        });
+        //TODO 鍚屼竴鏃堕棿鍚屼竴鍝佺墝钃勭數姹犳�ц兘缁熻
+        poolExecutor.execute(() -> {
+            //performanceInfoStatistics3(userId, map);
             latch.countDown();
         });
 
         return response;
     }
+
+    private void testDataInfoStatistics(Integer userId, Map<String, Object> map) {
+        List<BatttestdataInf> testInfList = battTestDataInfService.getListByUserId(userId);
+        //杩囨护鍑烘湰鏈堢殑
+        LocalDateTime startOfMonth = DateUtil.getStartOfMonth();
+        List<BatttestdataInf> testInfListOfMonth = testInfList.stream()
+                .filter(testData -> {
+                    LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime());
+                    return !startTime.isBefore(startOfMonth);
+                })
+                .collect(Collectors.toList());
+
+        //缁熻鏈湀鍚勭鍏呮斁鐢电被鍨嬪強鏁伴噺
+        List<NameValueDto> testInfOfMonth = getChargeAndDischargeStatistics(testInfListOfMonth);
+        map.put("testInf_month",testInfOfMonth);
+
+        //杩囨护鍑烘湰瀛e害鐨�
+        LocalDateTime startOfQuarter = DateUtil.getStartOfQuarter();
+        List<BatttestdataInf> testInfListOfQuarter = testInfList.stream()
+                .filter(testData -> {
+                    LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime());
+                    return !startTime.isBefore(startOfQuarter);
+                })
+                .collect(Collectors.toList());
+
+        //缁熻鏈搴﹀悇绉嶅厖鏀剧數绫诲瀷鍙婃暟閲�
+        List<NameValueDto> testInfOfQuarter = getChargeAndDischargeStatistics(testInfListOfQuarter);
+        map.put("testInf_quarter",testInfOfQuarter);
+
+        //杩囨护鍑烘湰骞寸殑
+        LocalDateTime startOfYear = DateUtil.getStartOfYear();
+        List<BatttestdataInf> testInfListOfYear = testInfList.stream()
+                .filter(testData -> {
+                    LocalDateTime startTime = convertToLocalDateTime(testData.getTestStarttime());
+                    return !startTime.isBefore(startOfYear);
+                })
+                .collect(Collectors.toList());
+
+        //缁熻鏈勾鍚勭鍏呮斁鐢电被鍨嬪強鏁伴噺
+        List<NameValueDto> testInfOfYear = getChargeAndDischargeStatistics(testInfListOfYear);
+        map.put("testInf_year",testInfOfYear);
+    }
+
+    /**
+     * 缁熻鍚勭鍏呮斁鐢电被鍨嬪強鏁伴噺
+     * @param testInfListOfMonth 琚粺璁$殑鍒楄〃
+     * @return
+     */
+    private List<NameValueDto> getChargeAndDischargeStatistics(List<BatttestdataInf> testInfListOfMonth) {
+        List<NameValueDto> testInfOfMonth = new ArrayList<>();
+        int chargeHr = 0;
+        int chargeJc = 0;
+        int dischargeHr = 0;
+        int dischargeJc = 0;
+        for (int i = 0; i < testInfListOfMonth.size(); i++) {
+            BatttestdataInf testData = testInfListOfMonth.get(i);
+            if (testData.getTestType() == 3) {  // 娴嬭瘯绫诲瀷涓烘斁鐢�
+                if (testData.getTestStarttype() == 3) {  //鏍稿鏀剧數
+                    dischargeHr++;
+                } else if(testData.getTestStarttype() != 4){  //鐩戞祴鏀剧數
+                    dischargeJc++;
+                }
+            } else if (testData.getTestType() == 2) {  // 娴嬭瘯绫诲瀷涓哄厖鐢�
+                if (testData.getTestStarttype() == 3) {  //鏍稿鍏呯數
+                    chargeHr++;
+                } else {  //鐩戞祴鍏呯數
+                    chargeJc++;
+                }
+            }
+        }
+        //鏍稿鏀剧數,鏍稿鍏呯數,鐩戞祴鏀剧數,妫�娴嬪厖鐢�
+        testInfOfMonth.add(new NameValueDto("鏍稿鏀剧數",String.valueOf(dischargeHr)));
+        testInfOfMonth.add(new NameValueDto("鏍稿鍏呯數",String.valueOf(chargeHr)));
+        testInfOfMonth.add(new NameValueDto("鐩戞祴鏀剧數",String.valueOf(dischargeJc)));
+        testInfOfMonth.add(new NameValueDto("鐩戞祴鍏呯數",String.valueOf(chargeJc)));
+        return testInfOfMonth;
+    }
+
+    private void batteryInfoStatistics(Integer userId, Map<String, Object> map) {
+        List<BattInf> battInfList = battInfService.getListByUserId(userId);
+        Map<String, List<BattInf>> brandMap = battInfList.stream().collect(Collectors.groupingBy(BattInf::getProduct));
+        Map<Float, List<BattInf>> volMap = battInfList.stream().collect(Collectors.groupingBy(BattInf::getMonvolstd));
+
+        List<NameValueDto> brandList = new ArrayList<>();
+        brandMap.forEach((k,v)->{
+            NameValueDto brandDto = new NameValueDto(k, String.valueOf(v.size()));
+            brandList.add(brandDto);
+        });
+        map.put("battGroupInfo_brand",brandList);
+
+        List<NameValueDto> volList = new ArrayList<>();
+        volMap.forEach((k,v)->{
+            NameValueDto volDto = new NameValueDto(String.valueOf(k), String.valueOf(v.size()));
+            volList.add(volDto);
+        });
+        map.put("battGroupInfo_vol",volList);
+    }
+
+    private void powerInfoStatistics(Integer userId, Map<String, Object> map) {
+        List<PowerInf> powerInfList = powerInfService.getListByUserId(userId);
+        Map<String, List<PowerInf>> brandMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getCompany));
+        Map<Integer, List<PowerInf>> typeMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getPowerType));
+
+        List<NameValueDto> brandList = new ArrayList<>();
+        brandMap.forEach((k,v)->{
+            NameValueDto brandDto = new NameValueDto(k, String.valueOf(v.size()));
+            brandList.add(brandDto);
+        });
+        map.put("powerInfo_brand",brandList);
+
+        List<NameValueDto> typeList = new ArrayList<>();
+        //鐢垫簮绫诲瀷:1:鐩存祦锛�2锛氶�氳锛�3:閰嶇綉
+        typeMap.forEach((k,v)->{
+            NameValueDto typeDto = new NameValueDto();
+            switch (k){
+                case 1:
+                    typeDto.setName("鐩存祦");
+                    break;
+                case 2:
+                    typeDto.setName("閫氳");
+                    break;
+                case 3:
+                    typeDto.setName("閰嶇綉");
+                    break;
+            }
+            typeDto.setValue(String.valueOf(v.size()));
+            typeList.add(typeDto);
+        });
+        map.put("powerInfo_type",typeList);
+
+    }
+
+    /**
+     * 鎸夌珯鐐圭被鍨嬪拰绔欑偣鐢靛帇
+     * 绔欑偣绫诲瀷:鑺傜偣/闈炶妭鐐�
+     */
+    private void stationInfoStatistics(Integer userId, Map<String, Object> map) {
+        List<StationInf> stationInfList =stationInfService.getListByUserId(userId);
+        Map<Integer, List<StationInf>> nodeMap = stationInfList.stream().collect(Collectors.groupingBy(StationInf::getNodeStation));
+        Map<String, List<StationInf>> volMap = stationInfList.stream().collect(Collectors.groupingBy(StationInf::getStationType));
+
+        List<NameValueDto> stationTypeList = new ArrayList<>();
+        List<NameValueDto> stationVolList = new ArrayList<>();
+        nodeMap.forEach((k,v)->{
+            NameValueDto stationTypeDto = new NameValueDto(k == 0 ? "闈炶妭鐐�" : "鑺傜偣", String.valueOf(v.size()));
+            stationTypeList.add(stationTypeDto);
+        });
+        volMap.forEach((k,v)->{
+            NameValueDto stationVolDto = new NameValueDto(k, String.valueOf(v.size()));
+            stationVolList.add(stationVolDto);
+        });
+
+        map.put("stationInfo_stationType",stationTypeList);
+        map.put("stationInfo_stationVol",stationVolList);
+    }
+
 }

--
Gitblit v1.9.1