若依 ruoyi-vue 维护Ancestors字段 树转换成List

迁移部门表,没有ancestors字段,若依部门权限没办法做,写了一段代码维护ancestors字段。

调用的若依框架组成树的方法,拼接ancestors

java 复制代码
    public AjaxResult tree() {
        List<SysDept> depts = deptService.selectDeptTree();
        for (SysDept dept : depts) {
            //是顶级节点则设置为
            if (dept.getParentId().equals(0L)) {
                dept.setAncestors("0");
            }
            if (CollectionUtil.isNotEmpty(dept.getChildren())) {
                setAncestors(dept.getAncestors(), dept.getChildren());
            }
        }
        List<SysDept> sysDepts = treeToList(depts);
        for (SysDept sysDept : sysDepts) {
            deptService.updateDept(sysDept);
        }
        return success(depts);
    }
java 复制代码
    
    public void setAncestors(String parentAncestors, List<SysDept> children) {
        for (SysDept child : children) {
            // 将父节点的祖先节点字符串和子部门的父节点ID连接起来,以逗号分隔
            String ancestors = parentAncestors + "," + child.getParentId();
            // 设置当前子部门的祖先节点字符串
            child.setAncestors(ancestors);
            // 如果当前子部门有子部门,则递归调用该方法
            if (CollectionUtil.isNotEmpty(child.getChildren())) {
                setAncestors(ancestors, child.getChildren());
            }
        }
    }
java 复制代码
    public List<SysDept> treeToList(List<SysDept> depts) {
        List<SysDept> deptList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(depts)) {
            for (SysDept dept : depts) {
                // 将当前节点添加到列表中
                deptList.add(dept);
                // 如果当前节点有子节点,则递归将子节点添加到列表中
                if (CollectionUtil.isNotEmpty(dept.getChildren())) {
                    deptList.addAll(treeToList(dept.getChildren()));
                }
            }
        }
        return deptList;
    }
相关推荐
向上的车轮12 分钟前
TypeORM ——TypeScript 生态的主流 ORM对比
javascript·typescript·typeorm
问道飞鱼15 分钟前
【Tauri框架学习】Tauri 与 React 前端集成:通信机制与交互原理详解
前端·学习·react.js·rust·通信
霍理迪19 分钟前
Vue列表过滤与排序
前端·javascript·vue.js
gCode Teacher 格码致知20 分钟前
Javascript提高:Node.js readline 模块 完整使用教程
javascript·node.js
牛十二25 分钟前
智能体框架开发实战
运维·服务器·前端
鹅天帝25 分钟前
20230319网安学习日志——XSS漏洞
前端·学习·web安全·网络安全·xss
floret. 小花25 分钟前
Vue3 + Electron 知识点总结 · 2026-03-21
前端·面试·electron·学习笔记·vue3
蓝黑202027 分钟前
Vue的v-if和v-for放在同一个HTML元素里的坑
前端·javascript·vue.js
进击的雷神32 分钟前
展位号后缀清理、详情页JS数据提取、重试机制控制、地址字段重构——美国NPE展爬虫四大技术难关攻克纪实
javascript·爬虫·python·重构
转角羊儿34 分钟前
精灵图案例
开发语言·前端·javascript