| | |
| | | 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; |
| | |
| | | @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); |
| | |
| | | |
| | | AutoTuneFlowTopologyItem item = new AutoTuneFlowTopologyItem(); |
| | | item.setTargetStationId(targetStationId); |
| | | item.setDirection(capacity.getDirectionCode()); |
| | | item.setDirection(DIRECTION_OUT); |
| | | item.setAdjacentStationIds(adjacentStationIds); |
| | | // 当前只存在无向站点邻接图,不能把邻接事实冒充为上下游方向事实。 |
| | | item.setUpstreamStationIds(Collections.emptyList()); |
| | |
| | | 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()); |
| | |
| | | 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() { |
| | |
| | | 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 |