From fa565c2d9dd77f4d5ccea1a8fb56feb1b864e2a7 Mon Sep 17 00:00:00 2001
From: luxiaotao1123 <t1341870251@163.com>
Date: 星期四, 09 一月 2025 13:20:17 +0800
Subject: [PATCH] #

---
 zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java   |    8 ++
 zy-acs-manager/src/main/java/com/zy/acs/manager/manager/utils/ActionSorter.java |  135 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 141 insertions(+), 2 deletions(-)

diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java
index b1652e5..6dd57dd 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/MainService.java
@@ -31,6 +31,7 @@
 import com.zy.acs.manager.manager.enums.*;
 import com.zy.acs.manager.manager.service.*;
 import com.zy.acs.manager.manager.service.impl.WebsocketServiceImpl;
+import com.zy.acs.manager.manager.utils.ActionSorter;
 import com.zy.acs.manager.system.service.ConfigService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -93,6 +94,8 @@
     private AgvModelService agvModelService;
     @Autowired
     private LaneService laneService;
+    @Autowired
+    private ActionSorter actionSorter;
 
 
     @SuppressWarnings("all")
@@ -985,11 +988,12 @@
                     now    // 宸ヤ綔鏃堕棿
             ));
 
-            List<Action> newActionList = new ArrayList<>(actionList);
+//            List<Action> newActionList = new ArrayList<>(actionList);
 
 //            List<Action> optimizeList = actionService.optimizeSort(actionList);
 
-//            List<Action> newActionList = new ArrayList<>(optimizeList);
+            List<Action> optimizeList = actionSorter.optimizeSort(actionList);
+            List<Action> newActionList = new ArrayList<>(optimizeList);
 
 
             String groupId = String.valueOf(snowflakeIdWorker.nextId()).substring(3);
diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/utils/ActionSorter.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/utils/ActionSorter.java
new file mode 100644
index 0000000..896dac7
--- /dev/null
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/manager/utils/ActionSorter.java
@@ -0,0 +1,135 @@
+package com.zy.acs.manager.manager.utils;
+
+import com.zy.acs.manager.manager.entity.Action;
+import com.zy.acs.manager.manager.enums.ActionTypeType;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+public class ActionSorter {
+
+    /**
+     * 瀵逛紶鍏ョ殑 actionList 杩涜鎺掑簭鎴栭噸鏂版帓鍒�
+     * 瑙勫垯锛�
+     * 1. 瀵绘壘绗竴涓� ReadyTakeFromAgvSite 鐨勫姩浣滐紙鍙栬揣鍔ㄤ綔锛夈��
+     * 2. 濡傛灉鎵惧埌锛屽垯瀵绘壘鏈�鍚庝竴涓� TurnCorner 鐨勫姩浣溿��
+     *    - 鑻ユ壘鍒� TurnCorner锛屽垯鍦ㄥ叾鍚庢彃鍏ュ彇璐у姩浣滐紙骞跺皢鍙栬揣鍔ㄤ綔鐨� code 鏇挎崲涓� TurnCorner 鍔ㄤ綔鐨� code锛夈��
+     *    - 鑻ユ病鎵惧埌 TurnCorner锛屽垯灏嗗彇璐у姩浣滄斁鍦ㄥ垪琛ㄦ渶鍓嶉潰锛堝悓鏃舵浛鎹负绗竴涓姩浣滅殑 code锛夈��
+     * 3. 濡傛灉娌℃湁 ReadyTakeFromAgvSite锛屽垯鐩存帴杩斿洖鍘熷垪琛ㄣ��
+     *
+     * @param actionList 鍘熷鍔ㄤ綔鍒楄〃
+     * @return 鎺掑簭鍚庣殑鍔ㄤ綔鍒楄〃
+     */
+    public List<Action> optimizeSort(List<Action> actionList) {
+        if (actionList == null || actionList.isEmpty()) {
+            return actionList;
+        }
+
+        // 鑾峰彇鍏抽敭淇℃伅
+        final long readyTakeFromAgvSite  = ActionTypeType.ReadyTakeFromAgvSite.val();
+        final long readyReleaseToAgvSite = ActionTypeType.ReadyReleaseToAgvSite.val();
+        final long turnCorner            = ActionTypeType.TurnCorner.val();
+
+        // 鎵惧埌绗竴涓� readyTakeFromAgvSite 鍔ㄤ綔鐨勪笅鏍�
+        int takeFromIdx = findFirstActionIndex(actionList, readyTakeFromAgvSite);
+        if (takeFromIdx == -1) {
+            // 濡傛灉娌℃湁鍙栬揣鍔ㄤ綔锛岀洿鎺ヨ繑鍥炲師鍒楄〃
+            return actionList;
+        }
+
+        // 鎵惧埌鏈�鍚庝竴涓� readyReleaseToAgvSite 鍔ㄤ綔鐨勪笅鏍�
+        int releaseToAgvSiteIdx = findLastActionIndex(actionList, readyReleaseToAgvSite);
+        // 濡傛灉鍚屾椂瀛樺湪 readyTakeFromAgvSite 鍜� readyReleaseToAgvSite锛岃鏄� 鍙栬揣 - 鏀捐揣 鑲畾鏄湪涓�涓湴闈㈢爜涓婏紝杩欐椂鍊欐病蹇呰鍐嶈皟鏁翠綅缃�
+        if (releaseToAgvSiteIdx != -1) {
+            return actionList;
+        }
+
+        // 鎵惧埌鏈�鍚庝竴涓� turnCorner 鍔ㄤ綔鐨勪笅鏍�
+        int turnCornerIdx = findLastActionIndex(actionList, turnCorner);
+
+        // 鏍规嵁鏄惁鎵惧埌 turnCorner 鏉ラ�夋嫨涓嶅悓鐨勯噸鏂版帓搴忛�昏緫
+        if (turnCornerIdx == -1) {
+            return reorderWithoutTurnCorner(actionList, takeFromIdx);
+        } else {
+            return reorderWithTurnCorner(actionList, takeFromIdx, turnCornerIdx);
+        }
+    }
+
+    /**
+     * 濡傛灉娌℃湁鎵惧埌 TurnCorner锛屽垯灏嗗彇璐у姩浣滄斁鍒板垪琛ㄦ渶鍓嶉潰锛堝苟浣跨敤鍘熸潵绗竴涓姩浣滅殑 code锛�
+     */
+    private List<Action> reorderWithoutTurnCorner(List<Action> actionList, int takeFromIdx) {
+        List<Action> sortedList = new ArrayList<>(actionList.size());
+        Action takeFromAction = actionList.get(takeFromIdx);
+
+        for (int i = 0; i < actionList.size(); i++) {
+            // 鍦ㄥ鐞嗙涓�涓厓绱犱箣鍓嶏紝鍏堟妸鍙栬揣鍔ㄤ綔鍔犺繘鍘�
+            if (i == 0) {
+                // 鐢ㄧ涓�涓姩浣滅殑 code 鏇挎崲鍙栬揣鍔ㄤ綔
+                takeFromAction.setCode(actionList.get(0).getCode());
+                sortedList.add(takeFromAction);
+            }
+            // 璺宠繃鍙栬揣鍔ㄤ綔鏈韩
+            if (i == takeFromIdx) {
+                continue;
+            }
+            // 鍔犲叆鍘熷姩浣�
+            sortedList.add(actionList.get(i));
+        }
+        return sortedList;
+    }
+
+    /**
+     * 濡傛灉鎵惧埌 TurnCorner锛屽垯鍦� turnCorner 鍔ㄤ綔鍚庢彃鍏ュ彇璐у姩浣滐紙骞朵娇鐢� turnCorner 鐨� code锛�
+     */
+    private List<Action> reorderWithTurnCorner(List<Action> actionList, int takeFromIdx, int turnCornerIdx) {
+        // 鎷疯礉涓�浠斤紝鍑嗗閲嶆帓
+        List<Action> sortedList = new ArrayList<>(actionList.size());
+        Action takeFromAction = actionList.get(takeFromIdx);
+
+        for (int i = 0; i < actionList.size(); i++) {
+            // 璺宠繃鍘熷厛鐨勫彇璐у姩浣�
+            if (i == takeFromIdx) {
+                continue;
+            }
+            // 鍏堝皢褰撳墠鍔ㄤ綔鍔犲埌鍒楄〃
+            Action curr = actionList.get(i);
+            sortedList.add(curr);
+
+            // 鑻ュ綋鍓嶅姩浣滄槸 turnCorner 鍔ㄤ綔锛屽垯鏇挎崲鍙栬揣鍔ㄤ綔 code 骞舵彃鍏�
+            if (i == turnCornerIdx) {
+                takeFromAction.setCode(curr.getCode());
+                sortedList.add(takeFromAction);
+            }
+        }
+        return sortedList;
+    }
+
+    /**
+     * 鍦� actionList 涓壘鍒扮涓�涓寚瀹� actionType 鐨勪笅鏍�
+     */
+    private int findFirstActionIndex(List<Action> actionList, long actionType) {
+        for (int i = 0; i < actionList.size(); i++) {
+            if (actionList.get(i).getActionType().equals(actionType)) {
+                return i;
+            }
+        }
+        return -1;
+    }
+
+    /**
+     * 鍦� actionList 涓壘鍒版渶鍚庝竴涓寚瀹� actionType 鐨勪笅鏍�
+     */
+    private int findLastActionIndex(List<Action> actionList, long actionType) {
+        int index = -1;
+        for (int i = 0; i < actionList.size(); i++) {
+            if (actionList.get(i).getActionType().equals(actionType)) {
+                index = i;
+            }
+        }
+        return index;
+    }
+
+}

--
Gitblit v1.9.1