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 generateRoutes(String[][] codeMatrix) { List 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.getCacheByData(currentCodeData); // 检查右边相邻条码 if (j + 1 < cols) { String rightCodeData = codeMatrix[i][j + 1]; if (!Cools.isEmpty(rightCodeData) && !CodeNodeType.NONE.val.equals(rightCodeData)) { Code rightCode = codeService.getCacheByData(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.getCacheByData(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; } }