From 6cbb420754e6e29fa134a6afca4514b8dfd62918 Mon Sep 17 00:00:00 2001
From: vincentlu <t1341870251@gmail.com>
Date: 星期二, 13 一月 2026 16:14:49 +0800
Subject: [PATCH] #

---
 zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java |   98 ++++++++++++++++++++++++++-----------------------
 1 files changed, 52 insertions(+), 46 deletions(-)

diff --git a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java
index f2c6663..2918e16 100644
--- a/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java
+++ b/zy-acs-manager/src/main/java/com/zy/acs/manager/core/service/AllocateService.java
@@ -4,19 +4,17 @@
 import com.zy.acs.framework.common.Cools;
 import com.zy.acs.manager.common.utils.CommonUtil;
 import com.zy.acs.manager.core.domain.AgvCntDto;
-import com.zy.acs.manager.core.domain.Lane;
 import com.zy.acs.manager.core.domain.FilterLaneDto;
+import com.zy.acs.manager.core.domain.LaneDto;
 import com.zy.acs.manager.core.domain.TaskPosDto;
 import com.zy.acs.manager.manager.entity.*;
-import com.zy.acs.manager.manager.enums.StaTypeType;
-import com.zy.acs.manager.manager.enums.StatusType;
-import com.zy.acs.manager.manager.enums.TaskStsType;
-import com.zy.acs.manager.manager.enums.TaskTypeType;
+import com.zy.acs.manager.manager.enums.*;
 import com.zy.acs.manager.manager.service.*;
 import com.zy.acs.manager.system.service.ConfigService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 import java.util.*;
 import java.util.stream.Collectors;
@@ -47,7 +45,7 @@
     @Autowired
     private LocService locService;
     @Autowired
-    private LaneService laneService;
+    private LaneBuilder laneBuilder;
     @Autowired
     private AgvAreaDispatcher agvAreaDispatcher;
     @Autowired
@@ -91,12 +89,13 @@
         return result;
     }
 
+    @Transactional(rollbackFor = Exception.class)
     public synchronized String execute(Task task, AllocateSupport inbound, AllocateSupport normal) {
         // inbound roller station
         Sta rollerOriSta = getInboundRollerSta(task);
         String inboundAgv = tryAllocateForRoller(task, rollerOriSta, true);
         if (!Cools.isEmpty(inboundAgv)) {
-            inbound.success(task, inboundAgv);
+            inbound.success(task, inboundAgv, rollerOriSta);
             return inboundAgv;
         }
 
@@ -104,13 +103,13 @@
         Sta rollerDestSta = getOutboundRollerSta(task);
         String outboundAgv = tryAllocateForRoller(task, rollerDestSta, false);
         if (!Cools.isEmpty(outboundAgv)) {
-            normal.success(task, outboundAgv);
+            normal.success(task, outboundAgv, rollerDestSta);
             return outboundAgv;
         }
 
         String normalAgv = this.normalExecute(task);
         if (!Cools.isEmpty(normalAgv)) {
-            normal.success(task, normalAgv);
+            normal.success(task, normalAgv, null);
             return normalAgv;
         }
 
@@ -136,11 +135,11 @@
         }
 
         // record lane hash for later dispatch/traffic-control logic
-        if (filterLaneDto.getOriginLane() != null) {
-            task.setOriLaneHash(filterLaneDto.getOriginLane().getHashCode());
+        if (filterLaneDto.getOriginLaneDto() != null) {
+            task.setOriLaneHash(filterLaneDto.getOriginLaneDto().getHashCode());
         }
-        if (filterLaneDto.getDestinationLane() != null) {
-            task.setDestLaneHash(filterLaneDto.getDestinationLane().getHashCode());
+        if (filterLaneDto.getDestinationLaneDto() != null) {
+            task.setDestLaneHash(filterLaneDto.getDestinationLaneDto().getHashCode());
         }
 
         return agvNo;
@@ -168,21 +167,24 @@
         if (null == filterLaneDto) {
             return null;
         }
-        Lane originLane = filterLaneDto.getOriginLane();
-        Lane destinationLane = filterLaneDto.getDestinationLane();
+        LaneDto originLaneDto = filterLaneDto.getOriginLaneDto();
+        LaneDto destinationLaneDto = filterLaneDto.getDestinationLaneDto();
         List<String> actualAvailableAgvNos = filterLaneDto.getActualAvailableAgvNos();
         if (Cools.isEmpty(actualAvailableAgvNos)) {
             return null;
         }
 
         // choose min number of running task
-        actualAvailableAgvNos.sort(Comparator.comparingInt(agvNo -> calcAllocateWeight(agvNo, task)));
+        actualAvailableAgvNos.sort((o1, o2) -> Integer.compare(
+                calcAllocateWeight(o2, task),
+                calcAllocateWeight(o1, task)
+        ));
 
-        if (null != originLane) {
-            task.setOriLaneHash(originLane.getHashCode());
+        if (null != originLaneDto) {
+            task.setOriLaneHash(originLaneDto.getHashCode());
         }
-        if (null != destinationLane) {
-            task.setDestLaneHash(destinationLane.getHashCode());
+        if (null != destinationLaneDto) {
+            task.setDestLaneHash(destinationLaneDto.getHashCode());
         }
 
         return actualAvailableAgvNos.get(0);
@@ -206,24 +208,28 @@
             }
 
             // has running task and within oriSta
-//            List<Segment> currSeg = segmentService.getByAgvAndState(agvId, SegmentStateType.WAITING.toString());
-            int taskCnt = taskService.count(new LambdaQueryWrapper<Task>()
-                    .eq(Task::getAgvId, agvId)
-                    .eq(Task::getOriSta, sta.getId())
-                    .and(wrapper -> wrapper
-                            .eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
-                            .or()
-                            .eq(Task::getTaskSts, TaskStsType.PROGRESS.val())
-                    )
-            );
-            if (taskCnt == 0) {
+//            int taskCnt = taskService.count(new LambdaQueryWrapper<Task>()
+//                    .eq(Task::getAgvId, agvId)
+//                    .eq(Task::getOriSta, sta.getId())
+//                    .and(wrapper -> wrapper
+//                            .eq(Task::getTaskSts, TaskStsType.ASSIGN.val())
+//                            .or()
+//                            .eq(Task::getTaskSts, TaskStsType.PROGRESS.val())
+//                    )
+//            );
+//            if (taskCnt == 0) {
+//                continue;
+//            }
+            // in TransferStationHandler.hasDelayAtSta
+            Segment currSeg = segmentService.getRollerWaiting(agvId, sta.getCode(), TaskPosDto.PosType.ORI_STA);
+            if (null == currSeg) {
                 continue;
             }
 
             // has enough backpack space to load
             Integer backpack = agvService.getBackpack(agvId);
-            int countRemainingBackpack = segmentService.countRemainingBackpack(null, agvId);
-            if (countRemainingBackpack >= backpack) {
+            List<Integer> usedBackpacks = segmentService.selectUsedBackpacks(null, agvId);
+            if (usedBackpacks.size() >= backpack) {
                 continue;
             }
 
@@ -285,13 +291,13 @@
         Integer maxAgvCountInLane = configService.getVal("maxAgvCountInLane", Integer.class);
 
         // checkout lane
-        Lane originLane = taskService.checkoutOriginLane(task);
-        Lane destinationLane = taskService.checkoutDestinationLane(task);
+        LaneDto originLaneDto = taskService.checkoutOriginLane(task);
+        LaneDto destinationLaneDto = taskService.checkoutDestinationLane(task);
 
         // allocate about origin
         List<String> availableAgvNosByOriLane = new ArrayList<>(availableAgvNos);
-        if (null != originLane) {
-            List<String> agvNosByOriLane = findAgvNosByLane(originLane);    // the agv list that had tasks in this lane
+        if (null != originLaneDto) {
+            List<String> agvNosByOriLane = findAgvNosByLane(originLaneDto);    // the agv list that had tasks in this lane
             // if full lane
             if (agvNosByOriLane.size() >= maxAgvCountInLane) {
 
@@ -304,8 +310,8 @@
 
         // allocate about destination
         List<String> availableAgvNosByDestLane = new ArrayList<>(availableAgvNos);
-        if (null != destinationLane) {
-            List<String> agvNosByDestLane = findAgvNosByLane(destinationLane);
+        if (null != destinationLaneDto) {
+            List<String> agvNosByDestLane = findAgvNosByLane(destinationLaneDto);
             if (agvNosByDestLane.size() >= maxAgvCountInLane) {
 
                 availableAgvNosByDestLane = Cools.getIntersection(agvNosByDestLane, availableAgvNos);
@@ -328,14 +334,14 @@
             return null;
         }
 
-        return new FilterLaneDto(originLane, destinationLane, actualAvailableAgvNos);
+        return new FilterLaneDto(originLaneDto, destinationLaneDto, actualAvailableAgvNos);
     }
 
-    public List<String> findAgvNosByLane(Lane lane) {
-        if (null == lane) {
+    public List<String> findAgvNosByLane(LaneDto laneDto) {
+        if (null == laneDto) {
             return new ArrayList<>();
         }
-        List<Task> taskList = taskService.findRunningTasksByLaneHash(lane.getHashCode());
+        List<Task> taskList = taskService.findRunningTasksByLaneHash(laneDto.getHashCode());
         if (Cools.isEmpty(taskList)) {
             return new ArrayList<>();
         }
@@ -406,11 +412,11 @@
     }
 
     public Boolean validCapacityOfLane(String agvNo, Code code) {
-        Lane lane = laneService.search(code.getData());
-        if (null != lane) {
+        LaneDto laneDto = laneBuilder.search(code.getData());
+        if (null != laneDto) {
             Integer maxAgvCountInLane = configService.getVal("maxAgvCountInLane", Integer.class);
 
-            List<String> agvNosByLane = this.findAgvNosByLane(lane);
+            List<String> agvNosByLane = this.findAgvNosByLane(laneDto);
             agvNosByLane.remove(agvNo);
             if (agvNosByLane.size() >= maxAgvCountInLane) {
                 return false;

--
Gitblit v1.9.1