#
luxiaotao1123
2024-12-23 766bcd63a19f7acaff2f5ed2cd1cb016894978ea
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
97
98
99
100
101
102
103
104
package com.zy.acs.manager.core.utils;
 
import com.zy.acs.framework.common.Cools;
import com.zy.acs.manager.core.service.astart.CodeNodeType;
import com.zy.acs.manager.core.service.astart.MapDataDispatcher;
import com.zy.acs.manager.manager.entity.Code;
import com.zy.acs.manager.manager.service.CodeGapService;
import com.zy.acs.manager.manager.service.CodeService;
import com.zy.acs.manager.manager.service.RouteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by vincent on 12/9/2024
 */
@Slf4j
@Component
public class RouteGenerator {
 
    @Autowired
    private MapDataDispatcher mapDataDispatcher;
    @Autowired
    private CodeService codeService;
    @Autowired
    private RouteService routeService;
    @Autowired
    private CodeGapService codeGapService;
 
    public static String generateRouteKey(String codeData0, String codeData1) {
        if (Cools.isEmpty(codeData0, codeData1)) {
            return null;
        }
        if (codeData0.compareTo(codeData1) < 0) {
            return codeData0 + "-" + codeData1;
        } else {
            return codeData1 + "-" + codeData0;
        }
    }
 
    public static String generateRouteCdaKey(int[] code0Cda, int[] code1Cda) {
        if (Cools.isEmpty(code0Cda, code1Cda)) {
            return null;
        }
        if (code0Cda[0] < code1Cda[0] || (code0Cda[0] == code1Cda[0] && code0Cda[1] < code1Cda[1])) {
            return code0Cda[0] + "," + code0Cda[1] + "-" + code1Cda[0] + "," + code1Cda[1];
        } else {
            return code1Cda[0] + "," + code1Cda[1] + "-" + code0Cda[0] + "," + code0Cda[1];
        }
    }
 
    public List<String> generateRoutes(String[][] codeMatrix) {
        List<String> list = new ArrayList<>();
 
        int rows = codeMatrix.length;
        int cols = codeMatrix[0].length;
 
        // 遍历二维数组,生成相邻条码之间的 Route
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                String currentCodeData = codeMatrix[i][j];
 
                // 跳过 NONE 条码
                if (currentCodeData == null || CodeNodeType.NONE.val.equals(currentCodeData)) {
                    continue;
                }
                Code currCode = codeService.selectByData(currentCodeData);
 
                // 检查右边相邻条码
                if (j + 1 < cols) {
                    String rightCodeData = codeMatrix[i][j + 1];
                    if (!Cools.isEmpty(rightCodeData) && !CodeNodeType.NONE.val.equals(rightCodeData)) {
                        Code rightCode = codeService.selectByData(rightCodeData);
                        list.add(currCode.getData() + "-" + rightCode.getData());
                        routeService.createRouteByCode(currCode, rightCode, 0, null);
                        codeGapService.createCodeGapByCode(currCode, rightCode, null);
 
                        log.info(currCode.getData() + "-" + rightCode.getData() + " [finished]");
                    }
                }
 
                // 检查下边相邻条码
                if (i + 1 < rows) {
                    String bottomCodeData = codeMatrix[i + 1][j];
                    if (!Cools.isEmpty(bottomCodeData) && !CodeNodeType.NONE.val.equals(bottomCodeData)) {
                        Code bottomCode = codeService.selectByData(bottomCodeData);
                        list.add(currCode.getData() + "-" + bottomCode.getData());
                        routeService.createRouteByCode(currCode, bottomCode, 0, null);
                        codeGapService.createCodeGapByCode(currCode, bottomCode, null);
 
                        log.info(currCode.getData() + "-" + bottomCode.getData() + " [finished]");
                    }
                }
            }
        }
 
        return list;
    }
 
 
}