#
Junjie
5 小时以前 e9b531edd2917b01a80dfa14e917ec21ddad8882
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
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<Integer> path = copyIntegerList(original.getNavigatePath());
        List<Integer> 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<Integer> 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<StationCommand> segmentCommands = new ArrayList<>();
        int buildStartIdx = 0;
        for (Integer endIdx : segmentEndIndices) {
            if (endIdx == null || endIdx < buildStartIdx) {
                continue;
            }
            List<Integer> 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<Integer> copyIntegerList(List<Integer> source) {
        if (source == null) {
            return new ArrayList<>();
        }
        return JSON.parseArray(JSON.toJSONString(source, SerializerFeature.DisableCircularReferenceDetect), Integer.class);
    }
}