From 0c336d5c5c0596691c9b33c08643c03486d47d5f Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期一, 27 四月 2026 18:56:39 +0800
Subject: [PATCH] refactor: move station buffer capacity to bas station

---
 src/main/java/com/zy/ai/service/impl/FlowTopologySnapshotServiceImpl.java |   98 ++++++++++++++++++++++++++++++-------------------
 1 files changed, 60 insertions(+), 38 deletions(-)

diff --git a/src/main/java/com/zy/ai/service/impl/FlowTopologySnapshotServiceImpl.java b/src/main/java/com/zy/ai/service/impl/FlowTopologySnapshotServiceImpl.java
index fa044bf..a69ad50 100644
--- a/src/main/java/com/zy/ai/service/impl/FlowTopologySnapshotServiceImpl.java
+++ b/src/main/java/com/zy/ai/service/impl/FlowTopologySnapshotServiceImpl.java
@@ -4,9 +4,12 @@
 import com.zy.ai.domain.autotune.AutoTuneFlowTopologyItem;
 import com.zy.ai.domain.autotune.AutoTuneStationRuntimeItem;
 import com.zy.ai.service.FlowTopologySnapshotService;
-import com.zy.asrs.entity.StationFlowCapacity;
-import com.zy.asrs.service.StationFlowCapacityService;
+import com.zy.asrs.entity.BasDevp;
+import com.zy.asrs.entity.BasStation;
+import com.zy.asrs.service.BasDevpService;
+import com.zy.asrs.service.BasStationService;
 import com.zy.common.utils.NavigateUtils;
+import com.zy.core.model.StationObjModel;
 import lombok.Data;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -24,31 +27,36 @@
 @Service("flowTopologySnapshotService")
 public class FlowTopologySnapshotServiceImpl implements FlowTopologySnapshotService {
 
+    private static final String DIRECTION_OUT = "OUT";
+
     @Autowired
-    private StationFlowCapacityService stationFlowCapacityService;
+    private BasDevpService basDevpService;
+
+    @Autowired
+    private BasStationService basStationService;
 
     @Autowired
     private NavigateUtils navigateUtils;
 
     @Override
     public List<AutoTuneFlowTopologyItem> buildSnapshot(List<AutoTuneStationRuntimeItem> stationRuntimeSnapshot) {
-        List<StationFlowCapacity> capacities = loadCapacityList();
+        List<BasStation> outStations = loadOutStationList();
         Map<Integer, List<Integer>> adjacencyMap = loadSortedAdjacencyMap();
         List<AutoTuneFlowTopologyItem> itemList = new ArrayList<>();
-        for (StationFlowCapacity capacity : capacities) {
-            if (capacity == null || capacity.getStationId() == null) {
+        for (BasStation station : outStations) {
+            if (station == null || station.getStationId() == null) {
                 continue;
             }
-            itemList.add(buildTopologyItem(capacity, adjacencyMap, stationRuntimeSnapshot));
+            itemList.add(buildTopologyItem(station, adjacencyMap, stationRuntimeSnapshot));
         }
         return itemList;
     }
 
-    public AutoTuneFlowTopologyItem buildTopologyItem(StationFlowCapacity capacity,
+    public AutoTuneFlowTopologyItem buildTopologyItem(BasStation station,
                                                      Map<Integer, List<Integer>> adjacencyMap,
                                                      List<AutoTuneStationRuntimeItem> stationRuntimeSnapshot) {
-        Integer targetStationId = capacity.getStationId();
-        Integer bufferCapacity = defaultInt(capacity.getBufferCapacity());
+        Integer targetStationId = station.getStationId();
+        Integer bufferCapacity = station.getOutBufferCapacity();
         List<Integer> adjacentStationIds = sortedList(adjacencyMap == null ? null : adjacencyMap.get(targetStationId));
         List<Integer> flowStationIds = buildFlowStationIds(targetStationId, adjacentStationIds);
         List<AutoTuneStationRuntimeItem> flowRuntimeItems = filterRuntimeByStationIds(stationRuntimeSnapshot, flowStationIds);
@@ -56,7 +64,7 @@
 
         AutoTuneFlowTopologyItem item = new AutoTuneFlowTopologyItem();
         item.setTargetStationId(targetStationId);
-        item.setDirection(capacity.getDirectionCode());
+        item.setDirection(DIRECTION_OUT);
         item.setAdjacentStationIds(adjacentStationIds);
         // 褰撳墠鍙瓨鍦ㄦ棤鍚戠珯鐐归偦鎺ュ浘锛屼笉鑳芥妸閭绘帴浜嬪疄鍐掑厖涓轰笂涓嬫父鏂瑰悜浜嬪疄銆�
         item.setUpstreamStationIds(Collections.emptyList());
@@ -64,7 +72,7 @@
         item.setFlowStationIds(flowStationIds);
         item.setBufferCapacity(bufferCapacity);
         item.setOccupiedCount(runtimeCount.getOccupiedCount());
-        item.setFreeCount(Math.max(0, bufferCapacity - runtimeCount.getOccupiedCount()));
+        item.setFreeCount(resolveFreeCount(bufferCapacity, runtimeCount.getOccupiedCount()));
         item.setNonAutoingCount(runtimeCount.getNonAutoingCount());
         item.setLoadingCount(runtimeCount.getLoadingCount());
         item.setTaskHoldingCount(runtimeCount.getTaskHoldingCount());
@@ -99,32 +107,39 @@
         return count;
     }
 
-    public StationFlowCapacity findCapacity(List<StationFlowCapacity> capacities,
-                                            Integer stationId,
-                                            String directionCode) {
-        if (capacities == null || stationId == null || directionCode == null) {
-            return null;
-        }
-        for (StationFlowCapacity capacity : capacities) {
-            if (capacity == null) {
-                continue;
-            }
-            if (Objects.equals(capacity.getStationId(), stationId)
-                    && directionCode.equals(capacity.getDirectionCode())) {
-                return capacity;
-            }
-        }
-        return null;
-    }
-
-    private List<StationFlowCapacity> loadCapacityList() {
-        if (stationFlowCapacityService == null) {
+    private List<BasStation> loadOutStationList() {
+        if (basDevpService == null || basStationService == null) {
             return Collections.emptyList();
         }
-        QueryWrapper<StationFlowCapacity> wrapper = new QueryWrapper<>();
-        wrapper.orderByAsc("station_id", "direction_code");
-        List<StationFlowCapacity> capacityList = stationFlowCapacityService.list(wrapper);
-        return capacityList == null ? Collections.emptyList() : capacityList;
+        Set<Integer> stationIds = loadOutStationIds();
+        if (stationIds.isEmpty()) {
+            return Collections.emptyList();
+        }
+        QueryWrapper<BasStation> wrapper = new QueryWrapper<>();
+        wrapper.in("station_id", stationIds);
+        wrapper.orderByAsc("station_id");
+        List<BasStation> stationList = basStationService.list(wrapper);
+        return stationList == null ? Collections.emptyList() : stationList;
+    }
+
+    private Set<Integer> loadOutStationIds() {
+        LinkedHashSet<Integer> stationIds = new LinkedHashSet<>();
+        if (basDevpService == null) {
+            return stationIds;
+        }
+        QueryWrapper<BasDevp> wrapper = new QueryWrapper<>();
+        wrapper.eq("status", 1);
+        wrapper.orderByAsc("devp_no");
+        List<BasDevp> basDevpList = basDevpService.list(wrapper);
+        for (BasDevp basDevp : safeList(basDevpList)) {
+            List<StationObjModel> outStationList = safeList(basDevp == null ? null : basDevp.getOutStationList$());
+            for (StationObjModel stationObjModel : outStationList) {
+                if (stationObjModel != null && stationObjModel.getStationId() != null) {
+                    stationIds.add(stationObjModel.getStationId());
+                }
+            }
+        }
+        return stationIds;
     }
 
     private Map<Integer, List<Integer>> loadSortedAdjacencyMap() {
@@ -178,8 +193,15 @@
         return result;
     }
 
-    private int defaultInt(Integer value) {
-        return value == null ? 0 : value;
+    private Integer resolveFreeCount(Integer bufferCapacity, int occupiedCount) {
+        if (bufferCapacity == null) {
+            return null;
+        }
+        return Math.max(0, Math.max(0, bufferCapacity) - occupiedCount);
+    }
+
+    private <T> List<T> safeList(List<T> value) {
+        return value == null ? Collections.emptyList() : value;
     }
 
     @Data

--
Gitblit v1.9.1