From d13a01d1769f29b336e5b9f8612c6d41aaabbbb1 Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期五, 22 十一月 2024 20:42:20 +0800
Subject: [PATCH] #读取地图文件优化

---
 src/main/java/com/zy/common/utils/NavigateMapData.java |  383 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 342 insertions(+), 41 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/NavigateMapData.java b/src/main/java/com/zy/common/utils/NavigateMapData.java
index 7ffae0e..77a8562 100644
--- a/src/main/java/com/zy/common/utils/NavigateMapData.java
+++ b/src/main/java/com/zy/common/utils/NavigateMapData.java
@@ -2,65 +2,366 @@
 
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.core.common.SpringUtils;
+import com.zy.asrs.entity.BasMap;
+import com.zy.asrs.entity.LocMast;
+import com.zy.asrs.service.BasShuttleService;
+import com.zy.asrs.service.LocMastService;
+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.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.stereotype.Component;
 
 import java.io.*;
 import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
 
 /**
  * A*绠楁硶鍦板浘鑾峰彇绫�
  */
+@Component
 public class NavigateMapData {
 
-    public int[][] getData() {
-        return getData("in");
+    private Integer lev;//鍦板浘妤煎眰
+
+    public NavigateMapData() {
+        this.lev = 1;
     }
 
-    public int[][] getData(String mapType) {
+    public NavigateMapData(Integer lev) {
+        this.lev = lev;
+    }
+
+    public int[][] getData() {
+        return getData(NavigationMapType.NONE.id, null, null);//榛樿璇诲彇鏃犺繃婊ょ殑鍏ㄩ儴鍦板浘鏁版嵁
+    }
+
+    public int[][] getData(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
         try {
-            String mapFilename = "";
-            if (mapType.equals("in")) {
-                mapFilename = "mapIn.json";
-            }else {
-                mapFilename = "mapOut.json";
+            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
+            Object object = redisUtil.get(RedisKeyType.BASIC_MAP.key + lev);
+            if (object == null) {
+                return null;
             }
 
-            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);
+            //瑙f瀽json鍦板浘鏁版嵁
+            ArrayList arrayList = JSON.parseObject(object.toString(), ArrayList.class);
+            List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁
+            int[][] map = new int[lists.size()][];
+            int j = 0;
+            for (List<MapNode> list : lists) {
+                int[] tmp = new int[list.size()];
+                int i = 0;
+                for (MapNode mapNode : list) {
+                    //灏嗘暟鎹坊鍔犺繘浜岀淮鏁扮粍
+                    tmp[i++] = mapNode.getValue();
                 }
-                br.close();
-
-                //瑙f瀽json鍦板浘鏁版嵁
-                ArrayList arrayList = JSON.parseObject(stringBuffer.toString(), ArrayList.class);
-                int[][] map = new int[arrayList.size()][];
-                int j = 0;
-                for (Object obj : arrayList) {
-                    ArrayList list = JSON.parseObject(obj.toString(), ArrayList.class);
-                    int[] tmp = new int[list.size()];
-                    int i = 0;
-                    for (Object o : list) {
-                        JSONObject jsonObject = JSON.parseObject(o.toString());
-                        //灏嗘暟鎹坊鍔犺繘浜岀淮鏁扮粍
-                        tmp[i++] = Integer.parseInt(jsonObject.get("value").toString());
-                    }
-                    //鏁版嵁娣诲姞杩涗竴缁存暟缁�
-                    map[j++] = tmp;
-                }
-
-                return map;
-            } else {
-                System.out.println("鏂囦欢涓嶅瓨鍦�!");
+                //鏁版嵁娣诲姞杩涗竴缁存暟缁�
+                map[j++] = tmp;
             }
-        } catch (IOException ioException) {
-            ioException.printStackTrace();
+
+//            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");
+//                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);//杩囨护鍦板浘鏁版嵁
+//                int[][] map = new int[lists.size()][];
+//                int j = 0;
+//                for (List<MapNode> list : lists) {
+//                    int[] tmp = new int[list.size()];
+//                    int i = 0;
+//                    for (MapNode mapNode : list) {
+//                        //灏嗘暟鎹坊鍔犺繘浜岀淮鏁扮粍
+//                        tmp[i++] = mapNode.getValue();
+//                    }
+//                    //鏁版嵁娣诲姞杩涗竴缁存暟缁�
+//                    map[j++] = tmp;
+//                }
+//
+//                if (!file.delete()) {
+//                    System.out.println("涓存椂鏂囦欢鍒犻櫎澶辫触");
+//                }
+//                return map;
+//            } else {
+//                System.out.println("鏂囦欢涓嶅瓨鍦�!");
+//            }
+        } catch (Exception e) {
+            e.printStackTrace();
         }
         return null;
     }
 
+    /**
+     * 灏濊瘯浠巖edis鑾峰彇鏁版嵁
+     */
+    public int[][] getDataFromRedis(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
+        RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
+        Object o = redisUtil.get(RedisKeyType.MAP.key + lev);
+        if (o == null) {
+            return null;
+        }
+
+        BasMap basMap = JSON.parseObject(o.toString(), BasMap.class);
+        return this.getDataFormString(basMap.getData(), mapType, whitePoints, shuttlePoints);
+    }
+
+    /**
+     * 浠嶭ist鏁版嵁涓幏鍙栧湴鍥�
+     */
+    public int[][] getDataFormString(String data, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
+        ArrayList arrayList = JSON.parseObject(data, ArrayList.class);
+        List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁
+        int[][] map = new int[lists.size()][];
+        int j = 0;
+        for (List<MapNode> list : lists) {
+            int[] tmp = new int[list.size()];
+            int i = 0;
+            for (MapNode mapNode : list) {
+                //灏嗘暟鎹坊鍔犺繘浜岀淮鏁扮粍
+                tmp[i++] = mapNode.getValue();
+            }
+            //鏁版嵁娣诲姞杩涗竴缁存暟缁�
+            map[j++] = tmp;
+        }
+
+        return map;
+    }
+
+    //鑾峰彇JSON鏍煎紡鏁版嵁
+    public List<List<MapNode>> getJsonData(Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
+        try {
+            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
+            Object object = redisUtil.get(RedisKeyType.BASIC_MAP.key + lev);
+            if (object == null) {
+                return null;
+            }
+
+            //瑙f瀽json鍦板浘鏁版嵁
+            ArrayList arrayList = JSON.parseObject(object.toString(), ArrayList.class);
+            List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁
+            return lists;
+
+
+//            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");
+//                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);//杩囨护鍦板浘鏁版嵁
+//
+//                if (!file.delete()) {
+//                    System.out.println("涓存椂鏂囦欢鍒犻櫎澶辫触");
+//                }
+//
+//                return lists;
+//            } else {
+//                System.out.println("鏂囦欢涓嶅瓨鍦�!");
+//            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    //鑾峰彇JSON鏍煎紡鏁版嵁
+    public List<List<MapNode>> getJsonData(Integer lev, Integer mapType, List<int[]> whitePoints, List<int[]> shuttlePoints) {
+        try {
+            RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);
+            Object object = redisUtil.get(RedisKeyType.BASIC_MAP.key + lev);
+            if (object == null) {
+                return null;
+            }
+
+            //瑙f瀽json鍦板浘鏁版嵁
+            ArrayList arrayList = JSON.parseObject(object.toString(), ArrayList.class);
+            List<List<MapNode>> lists = filterMap(mapType, arrayList, lev, whitePoints, shuttlePoints);//杩囨护鍦板浘鏁版嵁
+            return lists;
+
+//            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");
+//                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);//杩囨护鍦板浘鏁版嵁
+//
+//                if (!file.delete()) {
+//                    System.out.println("涓存椂鏂囦欢鍒犻櫎澶辫触");
+//                }
+//
+//                return lists;
+//            } else {
+//                System.out.println("鏂囦欢涓嶅瓨鍦�!");
+//            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    /**
+     * 杩囨护鍦板浘鏁版嵁
+     * mapType -1=>鏃犺繃婊わ紝1=銆嬭繃婊ゅ簱浣嶇姸鎬丏FX锛�2=銆嬭繃婊ゅ簱浣嶇姸鎬乆
+     *
+     * @param whitePoints 鐧藉悕鍗曡妭鐐癸紝涓嶉渶瑕佽杩囨护
+     * @param shuttlePoints 绌挎杞﹁妭鐐癸紝闇�瑕佸姞杞借繘鍦板浘
+     */
+    public List<List<MapNode>> filterMap(Integer mapType, List arrayList, Integer lev, List<int[]> whitePoints, List<int[]> shuttlePoints) {
+        List<List<MapNode>> lists = new ArrayList<>();
+
+        //閲嶅缓鏁版嵁鏍煎紡
+        for (int i = 0; i < arrayList.size(); i++) {
+            Object obj = arrayList.get(i);
+            List<MapNode> list = JSON.parseArray(obj.toString(), MapNode.class);
+            for (int j = 0; j < list.size(); j++) {
+                MapNode mapNode = list.get(j);
+                list.set(j, mapNode);
+            }
+            lists.add(list);
+        }
+
+        //杩囨护鏁版嵁
+        LocMastService locMastService = SpringUtils.getBean(LocMastService.class);
+        //鑾峰彇褰撳墠妤煎眰搴撲綅鏁版嵁
+        List<LocMast> locMasts = locMastService.selectLocByLev(lev);
+        for (LocMast locMast : locMasts) {
+            Integer row = locMast.getRow1();
+            Integer bay = locMast.getBay1();
+
+            boolean whiteFlag = false;//榛樿涓嶅瓨鍦ㄧ櫧鍚嶅崟
+            if (whitePoints != null) {
+                for (int[] whitePoint : whitePoints) {
+                    if (whitePoint[0] == row && whitePoint[1] == bay) {
+                        //瀛樺湪鐧藉悕鍗�
+                        whiteFlag = true;
+                        break;
+                    }
+                }
+            }
+            if (whiteFlag) {
+                continue;//瀛樺湪鐧藉悕鍗曪紝涓嶆墽琛屼笅鍒楄繃婊ゆ柟妗�
+            }
+
+
+            List<MapNode> list = lists.get(row);
+            MapNode mapNode = list.get(bay);
+
+            if (mapType == NavigationMapType.NONE.id) {
+                //涓嶈繃婊や换浣曟暟鎹�
+            } else if (mapType == NavigationMapType.DFX.id) {
+                //杞﹁締鏈夎揣
+                //璇诲彇瀵瑰簲搴撲綅鏁版嵁锛屽皢DFX搴撲綅鐘舵�佺殑鑺傜偣缃负-1(闅滅鐗�)
+                if (locMast.getLocSts().equals("F")
+                        || locMast.getLocSts().equals("D")
+                        || locMast.getLocSts().equals("X")
+                        || locMast.getLocSts().equals("R")
+                        || locMast.getLocSts().equals("P")
+                ) {
+                    mapNode.setValue(-1);//绂佺敤鑺傜偣
+                }
+            } else if (mapType == NavigationMapType.NORMAL.id) {
+                //杩囨护搴撲綅鐘舵�乆
+                if (locMast.getLocSts().equals("X")) {
+                    mapNode.setValue(-1);//绂佺敤鑺傜偣
+                }
+            }
+
+            //鏇存柊list
+            list.set(bay, mapNode);
+            lists.set(row, list);
+        }
+
+        //鍔犺浇杞﹁締鍧愭爣鍒板湴鍥句腑
+        if (shuttlePoints != null) {
+            for (int[] points : shuttlePoints) {
+                int x = points[0];
+                int y = points[1];
+                List<MapNode> list = lists.get(x);
+                MapNode mapNode = list.get(y);
+                mapNode.setValue(66);//璁剧疆涓鸿溅杈嗕唬鐮�66
+                //鏇存柊list
+                list.set(y, mapNode);
+                lists.set(x, list);
+            }
+        }
+
+        //鍔犺浇鐧藉悕鍗曡妭鐐�
+        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);
+
+                List<MapNode> list = lists.get(x);
+                MapNode mapNode = list.get(y);
+                mapNode.setValue(col.getValue());//鎭㈠鍘熷鑺傜偣
+
+                //鏇存柊list
+                list.set(y, mapNode);
+                lists.set(x, list);
+            }
+        }
+
+        return lists;
+    }
+
 }

--
Gitblit v1.9.1