Junjie
2026-04-27 2cc61346cc0a3e5338aa8957f79f4c99c204f339
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.zy.ai.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.common.utils.NavigateUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
 
@Service("flowTopologySnapshotService")
public class FlowTopologySnapshotServiceImpl implements FlowTopologySnapshotService {
 
    @Autowired
    private StationFlowCapacityService stationFlowCapacityService;
 
    @Autowired
    private NavigateUtils navigateUtils;
 
    @Override
    public List<AutoTuneFlowTopologyItem> buildSnapshot(List<AutoTuneStationRuntimeItem> stationRuntimeSnapshot) {
        List<StationFlowCapacity> capacities = loadCapacityList();
        Map<Integer, List<Integer>> adjacencyMap = loadSortedAdjacencyMap();
        List<AutoTuneFlowTopologyItem> itemList = new ArrayList<>();
        for (StationFlowCapacity capacity : capacities) {
            if (capacity == null || capacity.getStationId() == null) {
                continue;
            }
            itemList.add(buildTopologyItem(capacity, adjacencyMap, stationRuntimeSnapshot));
        }
        return itemList;
    }
 
    public AutoTuneFlowTopologyItem buildTopologyItem(StationFlowCapacity capacity,
                                                     Map<Integer, List<Integer>> adjacencyMap,
                                                     List<AutoTuneStationRuntimeItem> stationRuntimeSnapshot) {
        Integer targetStationId = capacity.getStationId();
        Integer bufferCapacity = defaultInt(capacity.getBufferCapacity());
        List<Integer> adjacentStationIds = sortedList(adjacencyMap == null ? null : adjacencyMap.get(targetStationId));
        List<Integer> flowStationIds = buildFlowStationIds(targetStationId, adjacentStationIds);
        List<AutoTuneStationRuntimeItem> flowRuntimeItems = filterRuntimeByStationIds(stationRuntimeSnapshot, flowStationIds);
        RuntimeCount runtimeCount = calculateRuntimeCount(flowRuntimeItems);
 
        AutoTuneFlowTopologyItem item = new AutoTuneFlowTopologyItem();
        item.setTargetStationId(targetStationId);
        item.setDirection(capacity.getDirectionCode());
        item.setAdjacentStationIds(adjacentStationIds);
        // 当前只存在无向站点邻接图,不能把邻接事实冒充为上下游方向事实。
        item.setUpstreamStationIds(Collections.emptyList());
        item.setDownstreamStationIds(Collections.emptyList());
        item.setFlowStationIds(flowStationIds);
        item.setBufferCapacity(bufferCapacity);
        item.setOccupiedCount(runtimeCount.getOccupiedCount());
        item.setFreeCount(Math.max(0, bufferCapacity - runtimeCount.getOccupiedCount()));
        item.setNonAutoingCount(runtimeCount.getNonAutoingCount());
        item.setLoadingCount(runtimeCount.getLoadingCount());
        item.setTaskHoldingCount(runtimeCount.getTaskHoldingCount());
        return item;
    }
 
    public RuntimeCount calculateRuntimeCount(List<AutoTuneStationRuntimeItem> runtimeItems) {
        RuntimeCount count = new RuntimeCount();
        if (runtimeItems == null || runtimeItems.isEmpty()) {
            return count;
        }
        for (AutoTuneStationRuntimeItem runtimeItem : runtimeItems) {
            if (runtimeItem == null) {
                continue;
            }
            boolean autoing = Objects.equals(runtimeItem.getAutoing(), 1);
            boolean loading = Objects.equals(runtimeItem.getLoading(), 1);
            boolean taskHolding = runtimeItem.getTaskNo() != null && runtimeItem.getTaskNo() > 0;
            if (!autoing) {
                count.nonAutoingCount++;
            }
            if (loading) {
                count.loadingCount++;
            }
            if (taskHolding) {
                count.taskHoldingCount++;
            }
            if (loading || taskHolding) {
                count.occupiedCount++;
            }
        }
        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) {
            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;
    }
 
    private Map<Integer, List<Integer>> loadSortedAdjacencyMap() {
        if (navigateUtils == null) {
            return Collections.emptyMap();
        }
        Map<Integer, Set<Integer>> graph = navigateUtils.loadUndirectedStationGraphSnapshot();
        if (graph == null || graph.isEmpty()) {
            return Collections.emptyMap();
        }
        return graph.entrySet().stream()
                .filter(entry -> entry.getKey() != null)
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        entry -> sortedList(entry.getValue())
                ));
    }
 
    private List<Integer> buildFlowStationIds(Integer targetStationId,
                                             List<Integer> adjacentStationIds) {
        LinkedHashSet<Integer> stationIds = new LinkedHashSet<>();
        if (targetStationId != null) {
            stationIds.add(targetStationId);
        }
        stationIds.addAll(sortedList(adjacentStationIds));
        return new ArrayList<>(stationIds);
    }
 
    private List<AutoTuneStationRuntimeItem> filterRuntimeByStationIds(List<AutoTuneStationRuntimeItem> runtimeItems,
                                                                       List<Integer> stationIds) {
        if (runtimeItems == null || runtimeItems.isEmpty() || stationIds == null || stationIds.isEmpty()) {
            return Collections.emptyList();
        }
        Set<Integer> stationIdSet = new LinkedHashSet<>(stationIds);
        return runtimeItems.stream()
                .filter(item -> item != null && stationIdSet.contains(item.getStationId()))
                .collect(Collectors.toList());
    }
 
    private static List<Integer> sortedList(Iterable<Integer> values) {
        if (values == null) {
            return new ArrayList<>();
        }
        List<Integer> result = new ArrayList<>();
        for (Integer value : values) {
            if (value != null) {
                result.add(value);
            }
        }
        result.sort(Comparator.naturalOrder());
        return result;
    }
 
    private int defaultInt(Integer value) {
        return value == null ? 0 : value;
    }
 
    @Data
    public static class RuntimeCount {
        private int occupiedCount;
        private int nonAutoingCount;
        private int loadingCount;
        private int taskHoldingCount;
    }
}