From 2ab23232212bdea3435240031b41f3a6a0d56352 Mon Sep 17 00:00:00 2001
From: lsh <lsh@163.com>
Date: 星期三, 23 十月 2024 10:07:52 +0800
Subject: [PATCH] *

---
 src/main/java/com/zy/core/thread/RgvThread.java                   |    1 
 src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java       |   68 +++++++++++
 src/main/java/com/zy/core/MainProcess.java                        |    2 
 src/main/java/com/zy/asrs/utils/SortTheExecutionOfTheCarUtil.java |  288 ++++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 338 insertions(+), 21 deletions(-)

diff --git a/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java b/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
index 1f5b0d3..221f89c 100644
--- a/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
+++ b/src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -49,6 +49,74 @@
     /**
      * 绔欑偣浠诲姟妫�娴�  鏇存柊灏忚溅浣嶇疆淇℃伅
      */
+    public synchronized void updateStePositionNearby() {
+        try{
+            List<WrkMast> wrkMasts = wrkMastService.selectList(new EntityWrapper<WrkMast>().eq("wrk_sts", 1L));
+            if (wrkMasts.isEmpty()){
+                return;
+            }
+            List<BasDevpPosition> basDevpPositions = basDevpPositionService.selectList(new EntityWrapper<BasDevpPosition>().orderBy("plc_position",false));
+            if (basDevpPositions.isEmpty()){
+                log.error("鑾峰彇鎵�鏈夌珯鐐逛俊鎭紓甯�");
+                return;
+            }
+            List<Integer> siteList = SortTheExecutionOfTheCarUtil.WrkMastExtractSites(wrkMasts);
+            List<Integer> devpList = SortTheExecutionOfTheCarUtil.BasDevpPositionExtractSites(basDevpPositions);
+            Integer siteNo = SortTheExecutionOfTheCarUtil.ObtainDenseAreasFirstPlace(siteList, devpList);
+            if (siteNo == null){
+                log.error("灏辫繎瀵嗛泦鍘熷垯鑾峰彇绔欑偣澶辫触");
+                return;
+            }
+            Long sitePosition = 0L;
+            for (BasDevpPosition basDevpPosition : basDevpPositions){
+                if (basDevpPosition.getDevNo().equals(siteNo)){
+                    sitePosition = basDevpPosition.getPlcPosition();
+                }
+            }
+            List<List<Long>> rgvPositionList = new ArrayList<>();
+            for (RgvSlave rgvSlave:slaveProperties.getRgv()) {
+                RgvThread rgvThread = (RgvThread) SlaveConnection.get(SlaveType.Rgv, rgvSlave.getId());
+                RgvProtocol rgvProtocol = rgvThread.getRgvProtocol();
+                if (rgvProtocol == null) {
+                    continue;
+                }
+                List<Long> rgvPosition = new ArrayList<>();
+                rgvPosition.add(rgvProtocol.getRgvNo().longValue());
+                rgvPosition.add(rgvProtocol.getRgvPos());
+                rgvPositionList.add(rgvPosition);
+            }
+            Integer rgvNo = SortTheExecutionOfTheCarUtil.LatelyAndGreaterThan(rgvPositionList, sitePosition);
+
+            List<BasCircularShuttle> basCircularShuttleList = basCircularShuttleService.selectList(new EntityWrapper<BasCircularShuttle>().orderBy("rgv_id", true));
+
+            int[][] ints = new int[basCircularShuttleList.size()][2];
+            for (BasCircularShuttle basCircularShuttle : basCircularShuttleList){
+                ints[basCircularShuttle.getRgvNo()-1] = new int[]{basCircularShuttle.getRgvNo(),basCircularShuttle.getRgvId()};
+            }
+            String[] oldList = new String[ints.length];
+            for (int[] rgv : ints){
+                oldList[rgv[0]-1] = Arrays.toString(rgv);
+            }
+            int[][] rgvList = SortTheExecutionOfTheCarUtil.ReorderSteId(ints, rgvNo);
+            String[] newList = new String[rgvList.length];
+            for (int[] rgv : rgvList){
+                newList[rgv[0]-1] = Arrays.toString(rgv);
+            }
+
+            log.info("鏇存柊灏忚溅鎺掑簭淇℃伅锛氬師濮嬪皬杞﹀彿rgvNo={},灏忚溅閲嶆柊鎺掑簭淇℃伅={},灏忚溅鍘熷鎺掑簭淇℃伅={}",rgvNo,Arrays.toString(newList),Arrays.toString(oldList));
+            for (BasCircularShuttle basCircularShuttle : basCircularShuttleList){
+                int[] rgv = rgvList[basCircularShuttle.getRgvNo() - 1];
+                basCircularShuttle.setRgvId(rgv[1]);
+                basCircularShuttleService.updateById(basCircularShuttle);
+            }
+
+        } catch (Exception e) {
+            log.error("鑷姩鏇存柊灏忚溅鎺掑簭淇℃伅澶辫触锛屽紓甯革細"+e);
+        }
+    }
+    /**
+     * 绔欑偣浠诲姟妫�娴�  鏇存柊灏忚溅浣嶇疆淇℃伅
+     */
     public synchronized void updateStePosition() {
         try{
             boolean sign = false;
diff --git a/src/main/java/com/zy/asrs/utils/SortTheExecutionOfTheCarUtil.java b/src/main/java/com/zy/asrs/utils/SortTheExecutionOfTheCarUtil.java
index 6a3dbf6..63ee644 100644
--- a/src/main/java/com/zy/asrs/utils/SortTheExecutionOfTheCarUtil.java
+++ b/src/main/java/com/zy/asrs/utils/SortTheExecutionOfTheCarUtil.java
@@ -1,10 +1,10 @@
 package com.zy.asrs.utils;
 
 import com.zy.asrs.entity.BasDevpPosition;
+import com.zy.asrs.entity.WrkMast;
 import com.zy.core.enums.RouteCollectCountType;
 
-import java.util.Arrays;
-import java.util.List;
+import java.util.*;
 
 public class SortTheExecutionOfTheCarUtil {
     //鎺掑簭
@@ -49,20 +49,41 @@
         }
         return result;
     }
-    //鑾峰彇鏈�杩戝苟鍦ㄥ綋鍓嶄綅缃墠杈圭殑浣嶇疆
-    public static Long LatelyAndLessThan(long[] devpPosition,long nowPosition){
-        long result = 0L;
+//    //鑾峰彇鏈�杩戝苟鍦ㄥ綋鍓嶄綅缃墠杈圭殑浣嶇疆
+//    public static Long LatelyAndLessThan(long[] devpPosition,long nowPosition){
+//        long result = 0L;
+//        long Difference = 1737000L;
+//        for (long position : devpPosition){
+//            if (position<=nowPosition){
+//                if ((nowPosition-position) < Difference){
+//                    Difference = nowPosition-position;
+//                    result = position;
+//                }
+//            } else {
+//                if ((nowPosition-(position - 1737000L)) < Difference){
+//                    Difference = nowPosition-(position - 1737000L);
+//                    result = position;
+//                }
+//            }
+//        }
+//        return result;
+//    }
+
+    //鑾峰彇鏈�杩戝苟鍦ㄥ綋鍓嶄綅缃悗杈圭殑浣嶇疆
+    public static Integer LatelyAndGreaterThan(List<List<Long>> sitePosition, long nowPosition){
+        int result = 0;
         long Difference = 1737000L;
-        for (long position : devpPosition){
-            if (position<=nowPosition){
-                if ((nowPosition-position) < Difference){
-                    Difference = nowPosition-position;
-                    result = position;
+        for (List<Long> rgvPositions: sitePosition){
+            Long position = rgvPositions.get(1);
+            if (position>nowPosition){
+                if ((position - nowPosition) < Difference){
+                    Difference = position - nowPosition;
+                    result = rgvPositions.get(0).intValue();
                 }
             } else {
-                if ((nowPosition-(position - 1737000L)) < Difference){
-                    Difference = nowPosition-(position - 1737000L);
-                    result = position;
+                if ((1737000L - (nowPosition - position)) < Difference){
+                    Difference = 1737000L - (nowPosition - position);
+                    result = rgvPositions.get(0).intValue();
                 }
             }
         }
@@ -128,16 +149,241 @@
         return sou>end;
     }
 
-    public static void main(String[] args) {
-        int[][] ints = new int[][]{{1,1},{3,3},{2,2},{4,4},{5,5},{6,6},{7,7},{8,8},{9,9},{10,10}};
+    //鎻愬彇绔欑偣闆嗗悎
+    public static List<Integer> WrkMastExtractSites(List<WrkMast> wrkMastList){
+        List<Integer> siteList = new ArrayList<>();
+        for (WrkMast wrkMast : wrkMastList){
+            if (!siteList.contains(wrkMast.getSourceStaNo())){
+                siteList.add(wrkMast.getSourceStaNo());
+            }
+        }
+        return siteList;
+    }
+    //鎻愬彇绔欑偣闆嗗悎
+    public static List<Integer> BasDevpPositionExtractSites(List<BasDevpPosition> basDevpPositions){
+        List<Integer> siteList = new ArrayList<>();
+        for (BasDevpPosition basDevpPosition : basDevpPositions){
+            if (!siteList.contains(basDevpPosition.getDevNo())){
+                siteList.add(basDevpPosition.getDevNo());
+            }
+        }
+        return siteList;
+    }
 
-        int[][] reorderSteId = ReorderSteId(ints, 2);
-        for (int[] ste : reorderSteId){
-            System.out.println(Arrays.toString(ste));
+    //鎻愬彇绔欑偣闆嗗悎X2
+    public static List<Integer> ExtractSitesComplex(List<Integer> siteList){
+        siteList.addAll(siteList);
+        return siteList;
+    }
+
+    //闆嗗悎杞暟缁�
+    public static int[] SetToArrayConversion(List<Integer> siteList){
+        int[] nums = new int[siteList.size()];
+        for (int i = 0;i<siteList.size();i++){
+            nums[i] = siteList.get(i);
+        }
+        return nums;
+    }
+
+    //鑾峰彇浠诲姟瀵嗛泦鍖�
+    public static List<Integer> ObtainDenseAreasSite(List<Integer> siteList,List<Integer> devpList){
+
+        List<Integer> siteLists = ExtractSitesComplex(devpList);
+        int[] nums = SetToArrayConversion(siteLists);
+
+        List<Integer> champion = new ArrayList<>();
+        for (int rangeArea = 1; rangeArea<=devpList.size();rangeArea++){
+            List<List<Integer>> subsets = getConsecutiveSubsets(nums, rangeArea);
+            if (subsets != null){
+                int temporaryMaxCount = 0;
+                List<Integer> championTemporary = new ArrayList<>();
+                int championCount = 0;
+
+                for (List<Integer> one : subsets){
+                    int intersectionSize = getIntersectionSize(siteList, one);
+                    if (intersectionSize == temporaryMaxCount){
+                        championCount++;
+                        championTemporary = siteLists;
+                    } else if (intersectionSize > temporaryMaxCount){
+                        championCount = 1 ;
+                        temporaryMaxCount = intersectionSize;
+                        championTemporary = siteLists;
+                    }
+                }
+                if (championCount == 1){
+                    champion = championTemporary;
+                    break;
+                }
+                continue;
+            }
+            break;
         }
 
-        long[] longs = new long[]{1L,1000L,200000L,1100000L,1600000L};
-        long l = LatelyAndLessThan(longs, 1100000L);
-        System.out.println("l:"+l);
+        return champion;
     }
+
+    public static Integer ObtainDenseAreasFirstPlace(List<Integer> siteList,List<Integer> devpList){
+        List<Integer> integers = ObtainDenseAreasSite(siteList, devpList);
+        if (integers==null){
+            return null;
+        }
+        return integers.get(0);
+    }
+
+//    public static void main(String[] args) {
+////        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+//        List<Integer> siteList = new ArrayList<Integer>() {{
+//            add(1);add(2);add(3);add(4);add(5);add(6);add(7);add(8);add(9);add(10);
+//        }};
+//        List<Integer> siteLists = ExtractSitesComplex(siteList);
+//        int[] nums = SetToArrayConversion(siteLists);
+//        int subsetSize = 5;  // 鍙互閫氳繃鐢ㄦ埛杈撳叆鎴栬�呭叾浠栨柟寮忔帶鍒�
+//        List<List<Integer>> subsets = getConsecutiveSubsets(nums, subsetSize);
+//        if (subsets != null){
+//            for (List<Integer> subset : subsets) {
+//                System.out.println(subset);
+//            }
+//        } else {
+//            System.out.println("瀛愰泦澶у皬蹇呴』澶т簬0涓斿皬浜庣瓑浜庢暟缁勯暱搴�");
+//        }
+//    }
+
+    public static List<List<Integer>> getConsecutiveSubsets(int[] nums, int size) {
+        List<List<Integer>> result = new ArrayList<>();
+
+        // 纭繚瀛愰泦鐨勫ぇ灏忓悎娉�
+        if (size > nums.length || size <= 0) {
+//            throw new IllegalArgumentException("瀛愰泦澶у皬蹇呴』澶т簬0涓斿皬浜庣瓑浜庢暟缁勯暱搴�");
+            return null;
+        }
+
+        // 閫氳繃婊戝姩绐楀彛娉曠敓鎴愮浉閭荤殑瀛愰泦
+        for (int i = 0; i <= nums.length - size; i++) {
+            List<Integer> subset = new ArrayList<>();
+            for (int j = 0; j < size; j++) {
+                subset.add(nums[i + j]);
+            }
+            if (!result.contains(subset)){
+                result.add(subset);
+            }
+        }
+
+        return result;
+    }
+
+    public static int getIntersectionSize(List<Integer> listA, List<Integer> listB) {
+        // 浣跨敤闆嗗悎鏉ュ瓨鍌ㄤ氦闆嗗厓绱狅紝閬垮厤閲嶅
+        Set<Integer> intersection = new HashSet<>(listA);
+
+        // 淇濈暀闆嗗悎A鍜岄泦鍚圔涓殑鍏卞悓鍏冪礌
+        intersection.retainAll(listB);
+
+        // 杩斿洖浜ら泦鐨勫ぇ灏�
+        return intersection.size();
+    }
+
+//    public static void main(String[] args) {
+//        int[][] ints = new int[][]{{1,1},{3,3},{2,2},{4,4},{5,5},{6,6},{7,7},{8,8},{9,9},{10,10}};
+//
+//        int[][] reorderSteId = ReorderSteId(ints, 2);
+//        for (int[] ste : reorderSteId){
+//            System.out.println(Arrays.toString(ste));
+//        }
+//
+//        long[] longs = new long[]{1L,1000L,200000L,1100000L,1600000L};
+//        long l = LatelyAndLessThan(longs, 1100000L);
+//        System.out.println("l:"+l);
+//    }
+
+
+//    public static void main(String[] args) {
+//        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+//        List<List<Integer>> subsets = getSubsets(nums);
+//
+//        for (List<Integer> subset : subsets) {
+//            System.out.println(subset);
+//        }
+//    }
+//
+//    public static List<List<Integer>> getSubsets(int[] nums) {
+//        List<List<Integer>> result = new ArrayList<>();
+//        generateSubsets(0, nums, new ArrayList<>(), result);
+//        return result;
+//    }
+//
+//    private static void generateSubsets(int index, int[] nums, List<Integer> current, List<List<Integer>> result) {
+//        if (index == nums.length) {
+//            result.add(new ArrayList<>(current));
+//            return;
+//        }
+//
+//        // 涓嶅寘鍚綋鍓嶅厓绱犵殑瀛愰泦
+//        generateSubsets(index + 1, nums, current, result);
+//
+//        // 鍖呭惈褰撳墠鍏冪礌鐨勫瓙闆�
+//        current.add(nums[index]);
+//        generateSubsets(index + 1, nums, current, result);
+//
+//        // 鍥炴函
+//        current.remove(current.size() - 1);
+//    }
+
+    /*  濂戒笢瑗匡紝鍋氳褰�
+    public static void main(String[] args) {
+        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+        int subsetSize = 3;  // 鍙互閫氳繃鐢ㄦ埛杈撳叆鎴栬�呭叾浠栨柟寮忔帶鍒�
+        List<List<Integer>> subsets = getConsecutiveSubsets(nums, subsetSize);
+
+        for (List<Integer> subset : subsets) {
+            System.out.println(subset);
+        }
+    }
+
+    public static List<List<Integer>> getConsecutiveSubsets(int[] nums, int size) {
+        List<List<Integer>> result = new ArrayList<>();
+
+        // 纭繚瀛愰泦鐨勫ぇ灏忓悎娉�
+        if (size > nums.length || size <= 0) {
+            throw new IllegalArgumentException("瀛愰泦澶у皬蹇呴』澶т簬0涓斿皬浜庣瓑浜庢暟缁勯暱搴�");
+        }
+
+        // 閫氳繃婊戝姩绐楀彛娉曠敓鎴愮浉閭荤殑瀛愰泦
+        for (int i = 0; i <= nums.length - size; i++) {
+            List<Integer> subset = new ArrayList<>();
+            for (int j = 0; j < size; j++) {
+                subset.add(nums[i + j]);
+            }
+            result.add(subset);
+        }
+
+        return result;
+    }
+    * */
+
+
+//    public static void main(String[] args) {
+//        // 绀轰緥鍒楄〃A鍜屽垪琛˙
+//        List<Integer> listA = new ArrayList<>();
+//        List<Integer> listB = new ArrayList<>();
+//
+//        // 鍒濆鍖栧垪琛ˋ
+//        listA.add(1);
+//        listA.add(2);
+//        listA.add(3);
+//        listA.add(4);
+//        listA.add(5);
+//
+//        // 鍒濆鍖栧垪琛˙
+//        listB.add(3);
+//        listB.add(4);
+//        listB.add(5);
+//        listB.add(6);
+//        listB.add(7);
+//
+//        // 璁$畻浜ら泦鏁伴噺
+//        int intersectionSize = getIntersectionSize(listA, listB);
+//        System.out.println("浜ら泦鐨勬暟閲忔槸: " + intersectionSize);
+//    }
+
+
 }
diff --git a/src/main/java/com/zy/core/MainProcess.java b/src/main/java/com/zy/core/MainProcess.java
index 7158282..1041b7d 100644
--- a/src/main/java/com/zy/core/MainProcess.java
+++ b/src/main/java/com/zy/core/MainProcess.java
@@ -55,6 +55,8 @@
                         continue;
                     }
                     //鏇存柊浣嶇疆淇℃伅
+                    mainService.updateStePositionNearby();
+
                     mainService.updateStePosition();
 
                     // 浠诲姟涓嬪彂
diff --git a/src/main/java/com/zy/core/thread/RgvThread.java b/src/main/java/com/zy/core/thread/RgvThread.java
index f384975..014bacc 100644
--- a/src/main/java/com/zy/core/thread/RgvThread.java
+++ b/src/main/java/com/zy/core/thread/RgvThread.java
@@ -152,6 +152,7 @@
                     rgvProtocol = new RgvProtocol();
                     rgvProtocol.setRgvNo(slave.getId());
                 }
+                rgvProtocol.setRgvNo(slave.getId());
                 rgvProtocol.setMode(siemensNet.getByteTransform().TransInt16(result.Content, 2));
                 rgvProtocol.setStartSta(siemensNet.getByteTransform().TransInt16(result.Content, 4));
                 rgvProtocol.setEndSta(siemensNet.getByteTransform().TransInt16(result.Content, 6));

--
Gitblit v1.9.1