Junjie
2 天以前 63b01db83d9aad8a15276b4236a9a22e4aeef065
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