|  |  | 
 |  |  | package com.zy.acs.manager.core.service.floyd; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.alibaba.fastjson.TypeReference; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
 |  |  | import com.zy.acs.common.constant.RedisConstant; | 
 |  |  | import com.zy.acs.common.utils.RedisSupport; | 
 |  |  | import com.zy.acs.framework.common.Cools; | 
 |  |  | import com.zy.acs.manager.common.exception.BusinessException; | 
 |  |  | import com.zy.acs.manager.core.service.astart.MapDataDispatcher; | 
 |  |  | import com.zy.acs.manager.manager.entity.Code; | 
 |  |  | import com.zy.acs.manager.manager.entity.CodeGap; | 
 |  |  | import com.zy.acs.manager.manager.entity.Route; | 
 |  |  | 
 |  |  | public class FloydNavigateService { | 
 |  |  |  | 
 |  |  |     public static final double INF = 999999.0; | 
 |  |  |  | 
 |  |  |     private final RedisSupport redis = RedisSupport.defaultRedisSupport; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private CodeService codeService; | 
 |  |  | 
 |  |  |         return this.matrixHeader.get(idx); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @PostConstruct | 
 |  |  |     @SuppressWarnings("all") | 
 |  |  |     @PostConstruct | 
 |  |  |     public void generateMatrix() { | 
 |  |  |         log.info("【FLOYD】正在计算矩阵数据......"); | 
 |  |  |         List<Code> codeList = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, 1).eq(Code::getDeleted, false)); | 
 |  |  |         Integer lev = MapDataDispatcher.MAP_DEFAULT_LEV; | 
 |  |  |  | 
 |  |  |         int size = codeList.size(); | 
 |  |  |         String floydHeaderMatrixStr = redis.getValue(RedisConstant.MAP_FLOYD_MATRIX_HEADER_FLAG, String.valueOf(lev)); | 
 |  |  |         String floydPathMatrixStr = redis.getValue(RedisConstant.MAP_FLOYD_MATRIX_PATH_FLAG, String.valueOf(lev)); | 
 |  |  |         String floydMatrixStr = redis.getValue(RedisConstant.MAP_FLOYD_MATRIX_FLAG, String.valueOf(lev)); | 
 |  |  |  | 
 |  |  |         matrixHeader = new ArrayList<>(size); | 
 |  |  |         adjacencyMatrix = new Double[size][size]; | 
 |  |  |         pathMatrix = new int[size][size]; | 
 |  |  |         if (!Cools.isEmpty(floydMatrixStr) && !Cools.isEmpty(floydPathMatrixStr) && !Cools.isEmpty(floydHeaderMatrixStr)) { | 
 |  |  |             this.matrixHeader = JSON.parseObject(floydHeaderMatrixStr, new TypeReference<ArrayList<Long>>() {}); | 
 |  |  |             this.pathMatrix = JSON.parseObject(floydPathMatrixStr, int[][].class); | 
 |  |  |             this.floydMatrix = JSON.parseObject(floydMatrixStr, Double[][].class); | 
 |  |  |         } else { | 
 |  |  |  | 
 |  |  |         for (int i = 0; i < size; i++) { | 
 |  |  |             Code startCode = codeList.get(i); | 
 |  |  |             matrixHeader.add(startCode.getId()); | 
 |  |  |             for (int j = 0; j < size; j++) { | 
 |  |  |                 Code endCode = codeList.get(j); | 
 |  |  |                 Route route = routeService.findByCodeOfBoth(startCode.getId(), endCode.getId()); | 
 |  |  |                 CodeGap codeGap = codeGapService.findByCodeOfBoth(startCode.getId(), endCode.getId()); | 
 |  |  |             log.info("【FLOYD】正在计算矩阵数据......"); | 
 |  |  |             List<Code> codeList = codeService.list(new LambdaQueryWrapper<Code>().eq(Code::getStatus, 1).eq(Code::getDeleted, false)); | 
 |  |  |  | 
 |  |  |                 double distance = INF; | 
 |  |  |                 int path = -1; | 
 |  |  |             int size = codeList.size(); | 
 |  |  |  | 
 |  |  |                 if (null != route && null != codeGap && codeGap.getDistance() > 0) { | 
 |  |  |                     if (i != j) { | 
 |  |  |                         distance = codeGap.getDistance(); | 
 |  |  |             matrixHeader = new ArrayList<>(size); | 
 |  |  |             adjacencyMatrix = new Double[size][size]; | 
 |  |  |             pathMatrix = new int[size][size]; | 
 |  |  |  | 
 |  |  |             for (int i = 0; i < size; i++) { | 
 |  |  |                 Code startCode = codeList.get(i); | 
 |  |  |                 matrixHeader.add(startCode.getId()); | 
 |  |  |                 for (int j = 0; j < size; j++) { | 
 |  |  |                     Code endCode = codeList.get(j); | 
 |  |  |                     Route route = routeService.findByCodeOfBoth(startCode.getId(), endCode.getId()); | 
 |  |  |                     CodeGap codeGap = codeGapService.findByCodeOfBoth(startCode.getId(), endCode.getId()); | 
 |  |  |  | 
 |  |  |                     double distance = INF; | 
 |  |  |                     int path = -1; | 
 |  |  |  | 
 |  |  |                     if (null != route && null != codeGap && codeGap.getDistance() > 0) { | 
 |  |  |                         if (i != j) { | 
 |  |  |                             distance = codeGap.getDistance(); | 
 |  |  |                         } | 
 |  |  |                         path = i; | 
 |  |  |                     } | 
 |  |  |                     path = i; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |                     adjacencyMatrix[i][j] = distance; | 
 |  |  |  | 
 |  |  |                     pathMatrix[i][j] = path; | 
 |  |  |                 } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |                 adjacencyMatrix[i][j] = distance; | 
 |  |  |  | 
 |  |  |                 pathMatrix[i][j] = path; | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         floydMatrix = this.floydAlgorithm(adjacencyMatrix); | 
 |  |  |             floydMatrix = this.floydAlgorithm(adjacencyMatrix); | 
 |  |  |  | 
 |  |  |             redis.setValue(RedisConstant.MAP_FLOYD_MATRIX_HEADER_FLAG, String.valueOf(lev), JSON.toJSONString(matrixHeader)); | 
 |  |  |             redis.setValue(RedisConstant.MAP_FLOYD_MATRIX_PATH_FLAG, String.valueOf(lev), JSON.toJSONString(pathMatrix)); | 
 |  |  |             redis.setValue(RedisConstant.MAP_FLOYD_MATRIX_FLAG, String.valueOf(lev), JSON.toJSONString(floydMatrix)); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  |