From 160e150009b51a39fa95d9462c3798ba28d51a09 Mon Sep 17 00:00:00 2001
From: lxw <810412026@qq.com>
Date: 星期二, 15 八月 2023 08:49:47 +0800
Subject: [PATCH] 审批页面修改

---
 src/main/java/com/whyc/webSocket/HomeAdminSocket.java |  204 +++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 175 insertions(+), 29 deletions(-)

diff --git a/src/main/java/com/whyc/webSocket/HomeAdminSocket.java b/src/main/java/com/whyc/webSocket/HomeAdminSocket.java
index 05e8964..b2ca6af 100644
--- a/src/main/java/com/whyc/webSocket/HomeAdminSocket.java
+++ b/src/main/java/com/whyc/webSocket/HomeAdminSocket.java
@@ -4,13 +4,16 @@
 import com.whyc.dto.Response;
 import com.whyc.factory.ThreadPoolExecutorFactory;
 import com.whyc.pojo.Battinf;
+import com.whyc.pojo.PowerInf;
+import com.whyc.pojo.StationInf;
+import com.whyc.pojo.UserInf;
 import com.whyc.service.*;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import javax.servlet.http.HttpSession;
 import javax.websocket.*;
 import javax.websocket.server.ServerEndpoint;
-import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -20,7 +23,7 @@
 import java.util.stream.Collectors;
 
 /**
- * 绠$悊鍛橀椤�
+ * 鎬婚椤碉紙棰嗗灞傦級
  */
 @Component
 @ServerEndpoint(value = "/homeAdmin", encoders = WebSocketEncoder.class, configurator = WebSocketConfig.class)
@@ -29,15 +32,26 @@
 
     private Thread thread;
 
+    private volatile boolean runFlag = true;
+
+    private volatile Map<String, Thread> threadMap = new HashMap<>();
+
+    private volatile Map<Long, Boolean> threadFlagMap = new HashMap<>();
+
+    private static final int executeTime = 5000;
+
     private static BattInfService battInfService;
+
+    private static StationInfService stationInfService;
 
     private static BadBattMonService badBattMonService;
 
-    private static Fbs9100StateService fbs9100StateService;
 
     private static PowerInfService powerInfService;
 
     private static BatttestdataInfService batttestdataInfService;
+
+    private HttpSession httpSession;
 
     @Autowired
     public void setBattInfService(BattInfService battInfService) {
@@ -50,11 +64,6 @@
     }
 
     @Autowired
-    public void setFbs9100StateService(Fbs9100StateService fbs9100StateService) {
-        HomeAdminSocket.fbs9100StateService = fbs9100StateService;
-    }
-
-    @Autowired
     public void setPowerInfService(PowerInfService powerInfService) {
         HomeAdminSocket.powerInfService = powerInfService;
     }
@@ -64,13 +73,113 @@
         HomeAdminSocket.batttestdataInfService = batttestdataInfService;
     }
 
+    @Autowired
+    public void setStationInfService(StationInfService stationInfService) {
+        HomeAdminSocket.stationInfService = stationInfService;
+    }
+
     @OnOpen
     public void onOpen(Session session, EndpointConfig config) {
         this.session = session;
-         /*HttpSession httpSession = (HttpSession) config.getUserProperties().get("httpSession");
-        UserInf user = (UserInf) httpSession.getAttribute("user");
-        final int userId = user.getUId().intValue();*/
-        final int userId = 1001;
+        this.httpSession = (HttpSession) config.getUserProperties().get("httpSession");
+    }
+
+    //瀹炴椂椤甸潰鎺ㄩ��
+    public Response getAnalysis(int userId) {
+        Map<String, Object> res = new HashMap<>();
+        /*//钃勭數姹犱紭鍔e垎鏋�
+        Response resQualityAnalysis = badBattMonService.getQualityAnalysis(userId);
+        res.put("resQualityAnalysis", resQualityAnalysis);*/
+
+        //钃勭數姹犵粍浼樺姡鍒嗘瀽
+       /* Response resGroupAnalysis = badBattMonService.getGroupAnalysis(userId);
+        res.put("resGroupAnalysis", resGroupAnalysis);*/
+        Response resGroupAnalysis = batttestdataInfService.getGroupAnalysis(userId);
+        res.put("resGroupAnalysis", resGroupAnalysis);
+
+
+        //鍩虹璧勬簮淇℃伅
+        Response resBattInfoAnalysis = battInfService.getBattInfoAnalysis(userId);
+        res.put("resBattInfoAnalysis", resBattInfoAnalysis);
+
+        //绔欑偣淇℃伅
+        Response resStationAnalysis = new Response();
+        try {
+            //List<Battinf> battinfList = battInfService.getSateAnalysis(userId);
+            List<StationInf> sinfList = stationInfService.getSateAnalysis(userId);
+            Map<Integer, List<StationInf>> nodeStationMap = sinfList.stream().collect(Collectors.groupingBy(StationInf::getNodeStation));
+            Map<String, List<StationInf>> stationTypeMap = sinfList.stream().collect(Collectors.groupingBy(StationInf::getStationType));
+            Map<Integer, Object> nodeMap = new HashMap<>();
+            Map<String, Object> stationMap = new HashMap<>();
+            Map<String, Object> battGroupInfoMap = new HashMap<>();
+            int battSize = sinfList.size();
+            for (Integer node : nodeStationMap.keySet()) {
+                nodeMap.put(node, nodeStationMap.get(node).size());
+            }
+            for (String station : stationTypeMap.keySet()) {
+                stationMap.put(station, stationTypeMap.get(station).size());
+            }
+            battGroupInfoMap.put("node", nodeMap);
+            battGroupInfoMap.put("stationType", stationMap);
+            battGroupInfoMap.put("totalNum", battSize);
+            resStationAnalysis.setII(1, true, battGroupInfoMap, null);
+        } catch (Exception e) {
+            resStationAnalysis.set(1, false, "鍙戠敓寮傚父:" + e.getCause());
+        }
+        res.put("resStationAnalysis", resStationAnalysis);
+        //涓嶅悓鍝佺墝鍚屼竴鏃堕棿鍝佽川鍜屽悓鍝佺墝鍚屾椂闂村搧璐�
+        Response resProductQuaAnalysis = badBattMonService.getProductQuaAnalysis(userId);
+        res.put("resProductQuaAnalysis", resProductQuaAnalysis);
+
+        //鐢垫睜淇℃伅
+        Response res_battGroupInfo = new Response();
+        try {
+            List<Battinf> battinfList = battInfService.getBattGroupList(userId);
+            Map<String, List<Battinf>> battProducerMap = battinfList.stream().collect(Collectors.groupingBy(Battinf::getBattProducer));
+            Map<Float, List<Battinf>> battMonVolStdMap = battinfList.stream().collect(Collectors.groupingBy(Battinf::getMonVolStd));
+            Map<String, Object> battGroupProducerMap = new HashMap<>();
+            Map<String, Object> battGroupMonVolMap = new HashMap<>();
+            Map<String, Object> battGroupInfoMap = new HashMap<>();
+            int battSize = battinfList.size();
+            for (String producer : battProducerMap.keySet()) {
+                battGroupProducerMap.put(producer, battProducerMap.get(producer).size());
+            }
+            for (Float monVolStd : battMonVolStdMap.keySet()) {
+                battGroupMonVolMap.put(Float.toString(monVolStd), battMonVolStdMap.get(monVolStd).size());
+            }
+            battGroupInfoMap.put("producer", battGroupProducerMap);
+            battGroupInfoMap.put("monVol", battGroupMonVolMap);
+            battGroupInfoMap.put("totalNum", battSize);
+            res_battGroupInfo.setII(1, true, battGroupInfoMap, null);
+        } catch (Exception e) {
+            res_battGroupInfo.set(1, false, "鍙戠敓寮傚父:" + e.getCause());
+        }
+        res.put("battGroupInfo", res_battGroupInfo);
+        //鐢垫簮淇℃伅
+        Response resPwrdevInfAnalysis = new Response();
+        try {
+            List<PowerInf> powerInfList = powerInfService.getPwrdevInfAnalysis(userId);
+            Map<String, List<PowerInf>> powerProducerMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getPowerProducer));
+            Map<String, Object> AnalysisMap = new HashMap<>();
+            for (String producer : powerProducerMap.keySet()) {
+                AnalysisMap.put(producer, powerProducerMap.get(producer).size());
+            }
+            resPwrdevInfAnalysis.setII(1, true, AnalysisMap, "鐢垫簮淇℃伅");
+        } catch (Exception e) {
+            resPwrdevInfAnalysis.set(1, false, "鍙戠敓寮傚父:" + e.getCause());
+        }
+        res.put("resPwrdevInfAnalysis", resPwrdevInfAnalysis);
+        //娴嬭瘯淇℃伅
+        Response resTestdataInfAnalysis = batttestdataInfService.getTestdataInfAnalysis(userId);
+        res.put("resTestdataInfAnalysis", resTestdataInfAnalysis);
+        return new Response().set(1, res);
+    }
+
+    @OnMessage
+    public void onMessage(Session session, String message) {
+        UserInf user = (UserInf) this.httpSession.getAttribute("user");
+        final int userId = user.getUId().intValue();
+        //final int userId = 1087;
         Thread thread = new Thread() {
             @Override
             public void run() {
@@ -80,9 +189,11 @@
                         ThreadPoolExecutor poolExecutor = ThreadPoolExecutorFactory.getPoolExecutor();
                         CountDownLatch latch = new CountDownLatch(7);
                         poolExecutor.execute(() -> {
-                            //钃勭數姹犱紭鍔e垎鏋�
-                            Response resQualityAnalysis = badBattMonService.getQualityAnalysis(userId);
-                            res.put("resQualityAnalysis", resQualityAnalysis);
+                            //钃勭數姹犵粍浼樺姡鍒嗘瀽(鐢ㄨ搫鐢垫睜缁勭粍鍚庤瘎浼扮殑缁熻)
+                            /*Response resGroupAnalysis = badBattMonService.getGroupAnalysis(userId);
+                            res.put("resGroupAnalysis", resGroupAnalysis);*/
+                            Response resGroupAnalysis = batttestdataInfService.getGroupAnalysis(userId);
+                            res.put("resGroupAnalysis", resGroupAnalysis);
                             latch.countDown();
                         });
                         poolExecutor.execute(() -> {
@@ -93,8 +204,30 @@
                         });
                         poolExecutor.execute(() -> {
                             //绔欑偣淇℃伅
-                            Response resStateAnalysis = fbs9100StateService.getSateAnalysis(userId);
-                            res.put("resStateAnalysis", resStateAnalysis);
+                            Response resStationAnalysis = new Response();
+                            try {
+                                //List<Battinf> battinfList = battInfService.getSateAnalysis(userId);
+                                List<StationInf> sinfList = stationInfService.getSateAnalysis(userId);
+                                Map<Integer, List<StationInf>> nodeStationMap = sinfList.stream().collect(Collectors.groupingBy(StationInf::getNodeStation));
+                                Map<String, List<StationInf>> stationTypeMap = sinfList.stream().collect(Collectors.groupingBy(StationInf::getStationType));
+                                Map<Integer, Object> nodeMap = new HashMap<>();
+                                Map<String, Object> stationMap = new HashMap<>();
+                                Map<String, Object> battGroupInfoMap = new HashMap<>();
+                                int battSize = sinfList.size();
+                                for (Integer node : nodeStationMap.keySet()) {
+                                    nodeMap.put(node, nodeStationMap.get(node).size());
+                                }
+                                for (String station : stationTypeMap.keySet()) {
+                                    stationMap.put(station, stationTypeMap.get(station).size());
+                                }
+                                battGroupInfoMap.put("node", nodeMap);
+                                battGroupInfoMap.put("stationType", stationMap);
+                                battGroupInfoMap.put("totalNum", battSize);
+                                resStationAnalysis.setII(1, true, battGroupInfoMap, null);
+                            } catch (Exception e) {
+                                resStationAnalysis.set(1, false, "鍙戠敓寮傚父:" + e.getCause());
+                            }
+                            res.put("resStationAnalysis", resStationAnalysis);
                             latch.countDown();
                         });
                         poolExecutor.execute(() -> {
@@ -132,19 +265,30 @@
                         });
                         poolExecutor.execute(() -> {
                             //鐢垫簮淇℃伅
-                            Response<Map> resPwrdevInfAnalysis = powerInfService.getPwrdevInfAnalysis(userId);
+                            Response resPwrdevInfAnalysis = new Response();
+                            try {
+                                List<PowerInf> powerInfList = powerInfService.getPwrdevInfAnalysis(userId);
+                                Map<String, List<PowerInf>> powerProducerMap = powerInfList.stream().collect(Collectors.groupingBy(PowerInf::getPowerProducer));
+                                Map<String, Object> AnalysisMap = new HashMap<>();
+                                for (String producer : powerProducerMap.keySet()) {
+                                    AnalysisMap.put(producer, powerProducerMap.get(producer).size());
+                                }
+                                resPwrdevInfAnalysis.setII(1, true, AnalysisMap, "鐢垫簮淇℃伅");
+                            } catch (Exception e) {
+                                resPwrdevInfAnalysis.set(1, false, "鍙戠敓寮傚父:" + e.getCause());
+                            }
                             res.put("resPwrdevInfAnalysis", resPwrdevInfAnalysis);
                             latch.countDown();
                         });
                         poolExecutor.execute(() -> {
                             //娴嬭瘯淇℃伅
-                            Response<Map> resTestdataInfAnalysis = batttestdataInfService.getTestdataInfAnalysis(userId);
+                            Response resTestdataInfAnalysis = batttestdataInfService.getTestdataInfAnalysis(userId);
                             res.put("resTestdataInfAnalysis", resTestdataInfAnalysis);
                             latch.countDown();
                         });
                         latch.await(10, TimeUnit.MINUTES);
                         session.getBasicRemote().sendObject(new Response().set(1, res));
-                        sleep(4000);
+                        sleep(5000);
                     }
                 } catch (Exception e) {
                     this.interrupt();
@@ -156,20 +300,22 @@
     }
 
     @OnClose
-    public void onClose(CloseReason closeReason) throws IOException {
-        //System.err.println("closeReason = " + closeReason);
-        if (session.isOpen()) {
-            session.close();
+    public void onClose(CloseReason closeReason) {
+        System.err.println("closeReason = " + closeReason);
+        runFlag = false;
+        if (thread != null && thread.isAlive()) {
+            thread.interrupt();
         }
+        threadMap.remove(session.getId());
     }
 
     @OnError
-    public void onError(Throwable error) throws IOException {
-        //error.printStackTrace();
-        thread.isInterrupted();
-        if (session.isOpen()) {
-            session.close();
+    public void onError(Throwable error) {
+        error.printStackTrace();
+        if (thread != null && thread.isAlive()) {
+            thread.interrupt();
         }
+        threadMap.remove(session.getId());
     }
 
 }

--
Gitblit v1.9.1