#
Junjie
9 小时以前 c5d7868e9e5fb8013edb088a70e75fc83d575690
src/main/java/com/zy/asrs/controller/ConsoleController.java
@@ -350,21 +350,126 @@
     */
    @GetMapping("/map/{lev}/auth")
    public R getLocMap(@PathVariable Integer lev) {
        Object object = redisUtil.get(RedisKeyType.LOC_MAP_BASE.key);
        List<List<HashMap<String, Object>>> mapNodeList = null;
        if (object != null) {
            mapNodeList = (List<List<HashMap<String, Object>>>) object;
        List<List<HashMap<String, Object>>> mapNodeList = getLocMapBaseSnapshot();
        if (mapNodeList == null || mapNodeList.isEmpty()) {
            return R.error("请先初始化地图");
        }
        List<LocMast> locMastList = locMastService.selectLocByLev(lev);
        boolean needRefreshBase = false;
        for (LocMast locMast : locMastList) {
            String[] locType = locMast.getLocType().split("-");
            HashMap<String, Object> mapNode = mapNodeList.get(Integer.parseInt(locType[0])).get(Integer.parseInt(locType[1]));
            Integer[] pos = parseLocTypePos(locMast.getLocType());
            if (pos == null || !isValidMapNodeIndex(mapNodeList, pos[0], pos[1])) {
                needRefreshBase = true;
                break;
            }
        }
        if (needRefreshBase) {
            refreshLocMapBaseCache();
            mapNodeList = getLocMapBaseSnapshot();
        }
        for (LocMast locMast : locMastList) {
            Integer[] pos = parseLocTypePos(locMast.getLocType());
            if (pos == null || !isValidMapNodeIndex(mapNodeList, pos[0], pos[1])) {
                log.warn("locMap skip invalid locType, locNo={}, locType={}", locMast.getLocNo(), locMast.getLocType());
                continue;
            }
            HashMap<String, Object> mapNode = mapNodeList.get(pos[0]).get(pos[1]);
            mapNode.put("locSts", locMast.getLocSts());
            mapNode.put("locNo", locMast.getLocNo());
        }
        return R.ok().add(mapNodeList);
    }
    private List<List<HashMap<String, Object>>> getLocMapBaseSnapshot() {
        Object object = redisUtil.get(RedisKeyType.LOC_MAP_BASE.key);
        if (!(object instanceof List)) {
            return buildLocMapBase();
        }
        return cloneMapNodeList((List<List<HashMap<String, Object>>>) object);
    }
    private void refreshLocMapBaseCache() {
        List<List<HashMap<String, Object>>> base = buildLocMapBase();
        if (base != null && !base.isEmpty()) {
            redisUtil.set(RedisKeyType.LOC_MAP_BASE.key, base);
        }
    }
    private List<List<HashMap<String, Object>>> buildLocMapBase() {
        BasMap basMap = basMapService.selectOne(new EntityWrapper<BasMap>().eq("lev", 1));
        if (Cools.isEmpty(basMap) || Cools.isEmpty(basMap.getData())) {
            return null;
        }
        List<List<JSONObject>> dataList = JSON.parseObject(basMap.getData(), List.class);
        List<List<HashMap<String, Object>>> mapNodeList = new ArrayList<>();
        for (int i = 0; i < dataList.size(); i++) {
            List<JSONObject> row = dataList.get(i);
            List<HashMap<String, Object>> mapNodeRow = new ArrayList<>();
            for (int j = 0; j < row.size(); j++) {
                JSONObject map = row.get(j);
                HashMap<String, Object> mapNode = new HashMap<>();
                mapNode.put("id", i + "-" + j);
                String nodeType = map.getString("type");
                mapNode.put("type", nodeType);
                if ("shelf".equals(nodeType)) {
                    mapNode.put("value", MapNodeType.NORMAL_PATH.id);
                } else if ("devp".equals(nodeType)) {
                    mapNode.put("value", MapNodeType.DISABLE.id);
                } else if ("crn".equals(nodeType) || "dualCrn".equals(nodeType) || "rgv".equals(nodeType)) {
                    mapNode.put("value", MapNodeType.MAIN_PATH.id);
                } else {
                    mapNode.put("value", MapNodeType.DISABLE.id);
                }
                mapNodeRow.add(mapNode);
            }
            mapNodeList.add(mapNodeRow);
        }
        return mapNodeList;
    }
    private List<List<HashMap<String, Object>>> cloneMapNodeList(List<List<HashMap<String, Object>>> source) {
        List<List<HashMap<String, Object>>> copy = new ArrayList<>();
        for (List<HashMap<String, Object>> row : source) {
            List<HashMap<String, Object>> rowCopy = new ArrayList<>();
            if (row != null) {
                for (HashMap<String, Object> item : row) {
                    rowCopy.add(item == null ? new HashMap<>() : new HashMap<>(item));
                }
            }
            copy.add(rowCopy);
        }
        return copy;
    }
    private Integer[] parseLocTypePos(String locType) {
        if (Cools.isEmpty(locType)) {
            return null;
        }
        String[] parts = locType.split("-");
        if (parts.length < 2) {
            return null;
        }
        try {
            return new Integer[]{Integer.parseInt(parts[0]), Integer.parseInt(parts[1])};
        } catch (NumberFormatException e) {
            log.warn("parse locType fail, locType={}", locType, e);
            return null;
        }
    }
    private boolean isValidMapNodeIndex(List<List<HashMap<String, Object>>> mapNodeList, Integer rowIdx, Integer colIdx) {
        if (mapNodeList == null || rowIdx == null || colIdx == null || rowIdx < 0 || colIdx < 0 || rowIdx >= mapNodeList.size()) {
            return false;
        }
        List<HashMap<String, Object>> row = mapNodeList.get(rowIdx);
        return row != null && colIdx < row.size();
    }
    @RequestMapping(value = "/map/locList")
    public R mapLocList() {
        Object object = redisUtil.get(RedisKeyType.LOC_MAST_MAP_LIST.key);