zzw
昨天 1332cc511936d7dbe804921bc733c83d5e4d1400
emergency-repair-server/src/main/java/com/emergencyrepair/system/service/SystemService.java
@@ -108,11 +108,20 @@
            menu.setMenuType(menu.getId() == null ? 1 : menu.getMenuType());
        }
        boolean isRoot = menu.getPid() == null || menu.getPid() == 0L;
        if (menu.getMenuType() == 2) {
            if (isRoot) {
                throw new BusinessException("按钮类型只能建在菜单类型下级");
        if (menu.getId() != null && !isRoot && menu.getId().equals(menu.getPid())) {
            throw new BusinessException("上级菜单不能选择当前菜单");
        }
        SysMenu parent = null;
        if (!isRoot) {
            parent = menuMapper.selectById(menu.getPid());
            if (parent == null) {
                throw new BusinessException("上级菜单不存在");
            }
            SysMenu parent = menuMapper.selectById(menu.getPid());
            if (menu.getId() != null && isMenuDescendant(menu.getPid(), menu.getId())) {
                throw new BusinessException("上级菜单不能选择当前菜单的下级");
            }
        }
        if (menu.getMenuType() == 2) {
            if (parent == null || parent.getMenuType() == null || parent.getMenuType() != 1) {
                throw new BusinessException("按钮类型只能建在菜单类型下级");
            }
@@ -121,11 +130,8 @@
            }
            menu.setUrl(null);
            menu.setIcon(null);
        } else if (!isRoot) {
            SysMenu parent = menuMapper.selectById(menu.getPid());
            if (parent != null && parent.getMenuType() != null && parent.getMenuType() == 2) {
                throw new BusinessException("按钮类型下级不能再建菜单");
            }
        } else if (parent != null && parent.getMenuType() != null && parent.getMenuType() == 2) {
            throw new BusinessException("按钮类型下级不能再建菜单");
        }
        LocalDateTime now = LocalDateTime.now();
        menu.setUpdateTime(now);
@@ -144,6 +150,19 @@
        return menuMapper.selectById(menu.getId());
    }
    private boolean isMenuDescendant(Long candidateId, Long ancestorId) {
        Long cursor = candidateId;
        int guard = 0;
        while (cursor != null && cursor != 0L && guard++ < 64) {
            if (cursor.equals(ancestorId)) {
                return true;
            }
            SysMenu current = menuMapper.selectById(cursor);
            cursor = current == null ? null : current.getPid();
        }
        return false;
    }
    @Transactional
    public void deleteMenu(Long id) {
        long children = menuMapper.selectCount(Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getPid, id));