package com.zy.core.thread.impl.v5; 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 StationV5SegmentPlanner { public StationV5SegmentExecutionPlan buildPlan(StationCommand original) { StationV5SegmentExecutionPlan plan = new StationV5SegmentExecutionPlan(); 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) { for (Integer liftTransferStationId : liftTransferPath) { int endIndex = path.indexOf(liftTransferStationId); if (endIndex <= 0) { continue; } if (segmentEndIndices.isEmpty() || endIndex > segmentEndIndices.get(segmentEndIndices.size() - 1)) { segmentEndIndices.add(endIndex); } } } if (segmentEndIndices.isEmpty() || 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); 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 List copyIntegerList(List source) { if (source == null) { return new ArrayList<>(); } return JSON.parseArray(JSON.toJSONString(source, SerializerFeature.DisableCircularReferenceDetect), Integer.class); } }