From 429bc159b230ed31092aed3b8c4893c20e52f5c6 Mon Sep 17 00:00:00 2001
From: Junjie <xjj@123>
Date: 星期六, 16 三月 2024 15:47:02 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/common/utils/NavigateMapData.java |  131 ++++++++++++++++++++++++-------------------
 1 files changed, 74 insertions(+), 57 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/NavigateMapData.java b/src/main/java/com/zy/common/utils/NavigateMapData.java
index 786fc6b..f0065b4 100644
--- a/src/main/java/com/zy/common/utils/NavigateMapData.java
+++ b/src/main/java/com/zy/common/utils/NavigateMapData.java
@@ -9,7 +9,9 @@
 import com.zy.common.model.MapNode;
 import com.zy.common.model.NavigateNode;
 import com.zy.common.model.enums.NavigationMapType;
+import com.zy.core.enums.RedisKeyType;
 import com.zy.core.enums.ShuttleTaskModeType;
+import org.springframework.core.io.ClassPathResource;
 import org.springframework.stereotype.Component;
 
 import java.io.*;
@@ -40,9 +42,15 @@
     public int[][] getData(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
         try {
             String mapFilename = "map_" + lev + ".json";
+            ClassPathResource classPathResource = new ClassPathResource(mapFilename);
+            InputStream inputStream = classPathResource.getInputStream();
+            byte[] buffer = new byte[inputStream.available()];
+            inputStream.read(buffer);
+            File file = File.createTempFile("prefix","suffix");
+            try (OutputStream outStream = new FileOutputStream(file)) {
+                outStream.write(buffer);
+            }
 
-            String fileName = this.getClass().getClassLoader().getResource(mapFilename).getPath();//鑾峰彇鏂囦欢璺緞
-            File file = new File(fileName);
             StringBuffer stringBuffer = new StringBuffer();
             if (file.isFile() && file.exists()) {
                 InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
@@ -84,7 +92,7 @@
      */
     public int[][] getDataFromRedis(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
         RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
-        Object o = redisUtil.get("realtimeBasMap_" + lev);
+        Object o = redisUtil.get(RedisKeyType.MAP.key + lev);
         if (o == null) {
             return null;
         }
@@ -119,9 +127,52 @@
     public List<List<MapNode>> getJsonData(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
         try {
             String mapFilename = "map_" + lev + ".json";
+            ClassPathResource classPathResource = new ClassPathResource(mapFilename);
+            InputStream inputStream = classPathResource.getInputStream();
+            byte[] buffer = new byte[inputStream.available()];
+            inputStream.read(buffer);
+            File file = File.createTempFile("prefix","suffix");
+            try (OutputStream outStream = new FileOutputStream(file)) {
+                outStream.write(buffer);
+            }
 
-            String fileName = this.getClass().getClassLoader().getResource(mapFilename).getPath();//鑾峰彇鏂囦欢璺緞
-            File file = new File(fileName);
+            StringBuffer stringBuffer = new StringBuffer();
+            if (file.isFile() && file.exists()) {
+                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
+                BufferedReader br = new BufferedReader(isr);
+                String lineTxt = null;
+                while ((lineTxt = br.readLine()) != null) {
+                    stringBuffer.append(lineTxt);
+                }
+                br.close();
+
+                //瑙f瀽json鍦板浘鏁版嵁
+                ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class);
+                List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁
+
+                return lists;
+            } else {
+                System.out.println("鏂囦欢涓嶅瓨鍦�!");
+            }
+        } catch (IOException ioException) {
+            ioException.printStackTrace();
+        }
+        return null;
+    }
+
+    //鑾峰彇JSON鏍煎紡鏁版嵁
+    public List<List<MapNode>> getJsonData(Integer lev, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
+        try {
+            String mapFilename = "map_" + lev + ".json";
+            ClassPathResource classPathResource = new ClassPathResource(mapFilename);
+            InputStream inputStream = classPathResource.getInputStream();
+            byte[] buffer = new byte[inputStream.available()];
+            inputStream.read(buffer);
+            File file = File.createTempFile("prefix","suffix");
+            try (OutputStream outStream = new FileOutputStream(file)) {
+                outStream.write(buffer);
+            }
+
             StringBuffer stringBuffer = new StringBuffer();
             if (file.isFile() && file.exists()) {
                 InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
@@ -232,61 +283,27 @@
             }
         }
 
-        return lists;
-    }
+        //鍔犺浇鐧藉悕鍗曡妭鐐�
+        if (whitePoints != null) {
+            List<List<MapNode>> realMap = getJsonData(lev, -1, null, null);//鑾峰彇瀹屾暣鍦板浘
+            for (int[] points : whitePoints) {
+                //鑾峰彇鍘熷鑺傜偣鏁版嵁
+                int x = points[0];
+                int y = points[1];
+                List<MapNode> rows = realMap.get(x);
+                MapNode col = rows.get(y);
 
-    /**
-     * 鍐欏叆璺緞鑺傜偣鏁版嵁鍒皉edis鍦板浘涓�
-     * lock涓簍rue 绂佺敤搴撲綅锛宭ock涓篺alse鎭㈠搴撲綅
-     */
-    public boolean writeNavigateNodeToRedisMap(List<NavigateNode> nodes, boolean lock) {
-        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
-        try {
-            if (!redisUtil.tryLock("realtimeBasMap_" + lev)) {
-                return false;//鍔犻攣澶辫触
+                List<MapNode> list = lists.get(x);
+                MapNode mapNode = list.get(y);
+                mapNode.setValue(col.getValue());//鎭㈠鍘熷鑺傜偣
+
+                //鏇存柊list
+                list.set(y, mapNode);
+                lists.set(x, list);
             }
-
-            Object o = redisUtil.get("realtimeBasMap_" + lev);
-            if (o == null) {
-                return false;
-            }
-
-            BasMap basMap = JSON.parseObject(o.toString(), BasMap.class);
-            ArrayList arrayList = JSON.parseObject(basMap.getData(), ArrayList.class);
-            List<List<MapNode>> lists = filterMap(NavigationMapType.NONE.id, arrayList, lev, null, null);//鑾峰彇鍏ㄩ儴鍦板浘鏁版嵁
-
-            NavigateMapData mapData = new NavigateMapData(nodes.get(0).getZ());
-            List<List<MapNode>> realMap = mapData.getJsonData(-1, null, null);//鑾峰彇瀹屾暣鍦板浘(鍖呮嫭鍏ュ簱鍑哄簱)
-            for (NavigateNode node : nodes) {
-                if (node.getZ() != lev) {
-                    continue;
-                }
-
-                List<MapNode> listX = lists.get(node.getX());
-                MapNode mapNode = listX.get(node.getY());
-                if (lock) {
-                    mapNode.setValue(-999);//绂佺敤搴撲綅
-                } else {
-                    //鑾峰彇鍘熷鑺傜偣鏁版嵁
-                    List<MapNode> rows = realMap.get(node.getX());
-                    MapNode col = rows.get(node.getY());
-                    mapNode.setValue(col.getValue());//鎭㈠搴撲綅
-                }
-
-                listX.set(node.getY(), mapNode);
-                lists.set(node.getX(), listX);
-            }
-            basMap.setData(JSON.toJSONString(lists));
-            basMap.setUpdateTime(new Date());
-            //灏嗘暟鎹簱鍦板浘鏁版嵁瀛樺叆redis
-            redisUtil.set("realtimeBasMap_" + lev, JSON.toJSONString(basMap));
-        } catch (Exception e) {
-            e.printStackTrace();
-        }finally {
-            //瑙i攣
-            redisUtil.unlock("realtimeBasMap_" + lev);
         }
-        return true;
+
+        return lists;
     }
 
 }

--
Gitblit v1.9.1