package com.zy.core.thread.impl.station; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.zy.core.model.command.StationCommand; import java.util.ArrayList; import java.util.List; import java.util.Objects; public class StationSegmentPlanner { public StationSegmentExecutionPlan buildPlan(StationCommand original) { StationSegmentExecutionPlan plan = new StationSegmentExecutionPlan(); if (original == null) { return plan; } List path = copyIntegerList(original.getNavigatePath()); List liftTransferPath = copyIntegerList(original.getLiftTransferPath()); Integer startStationId = original.getStationId(); Integer targetStationId = original.getTargetStaNo(); if ((path == null || path.isEmpty()) && Objects.equals(startStationId, targetStationId) && startStationId != null) { path = new ArrayList<>(); path.add(startStationId); } if (path == null || path.isEmpty()) { return plan; } plan.setFullPathStationIds(copyIntegerList(path)); int total = path.size(); List segmentEndIndices = new ArrayList<>(); if (liftTransferPath != null) { int searchStartIdx = 0; for (Integer liftTransferStationId : liftTransferPath) { int endIndex = findNextStationIndex(path, liftTransferStationId, searchStartIdx); if (endIndex >= 0) { searchStartIdx = endIndex + 1; } if (endIndex <= 0) { continue; } if (segmentEndIndices.isEmpty() || endIndex > segmentEndIndices.get(segmentEndIndices.size() - 1)) { segmentEndIndices.add(endIndex); } } } if (segmentEndIndices.isEmpty() || !Objects.equals(segmentEndIndices.get(segmentEndIndices.size() - 1), total - 1)) { segmentEndIndices.add(total - 1); } List segmentCommands = new ArrayList<>(); int buildStartIdx = 0; for (Integer endIdx : segmentEndIndices) { if (endIdx == null || endIdx < buildStartIdx) { continue; } List segmentPath = new ArrayList<>(path.subList(buildStartIdx, endIdx + 1)); if (segmentPath.isEmpty()) { buildStartIdx = endIdx + 1; continue; } StationCommand segmentCommand = new StationCommand(); segmentCommand.setTaskNo(original.getTaskNo()); segmentCommand.setCommandType(original.getCommandType()); segmentCommand.setPalletSize(original.getPalletSize()); segmentCommand.setBarcode(original.getBarcode()); segmentCommand.setOriginalNavigatePath(copyIntegerList(path)); segmentCommand.setNavigatePath(segmentPath); segmentCommand.setStationId(segmentPath.get(0)); segmentCommand.setTargetStaNo(segmentPath.get(segmentPath.size() - 1)); segmentCommand.setSegmentStartIndex(buildStartIdx); segmentCommand.setSegmentEndIndex(endIdx); segmentCommand.setRouteVersion(original.getRouteVersion()); segmentCommands.add(segmentCommand); buildStartIdx = endIdx; } int segmentCount = segmentCommands.size(); for (int i = 0; i < segmentCommands.size(); i++) { StationCommand segmentCommand = segmentCommands.get(i); segmentCommand.setSegmentNo(i + 1); segmentCommand.setSegmentCount(segmentCount); } plan.setSegmentCommands(segmentCommands); return plan; } private int findNextStationIndex(List path, Integer stationId, int fromIndex) { if (path == null || path.isEmpty() || stationId == null) { return -1; } int startIdx = Math.max(fromIndex, 0); for (int i = startIdx; i < path.size(); i++) { if (stationId.equals(path.get(i))) { return i; } } return -1; } private List copyIntegerList(List source) { if (source == null) { return new ArrayList<>(); } return JSON.parseArray(JSON.toJSONString(source, SerializerFeature.DisableCircularReferenceDetect), Integer.class); } }