若依 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;
    }
相关推荐
掘金安东尼36 分钟前
人工智能AI
前端
kyriewen43 分钟前
AI 面试官筛前端简历,先出局的不是写代码慢的
前端·面试·程序员
时间的情敌1 小时前
Three.js 后处理辉光 Bloom、告警发光效果、管线流光、着色器流动管线(ShaderMaterial)
开发语言·javascript·three.js·着色器
BigTopOne1 小时前
10 引用与借用:& 和 &mut
前端
粥里有勺糖1 小时前
视野修炼-技术周刊第139期 | AI 不会让时间变多
前端·github·ai编程
ZJPRENO3 小时前
WorkBuddy 从零到一|混元4 Flash 实战手记-提醒小工具开发
前端
用户302822530683 小时前
AI 视频图片运镜为什么会抖?帧率拉高也救不了多次取整
前端·人工智能·typescript
前端码农人生3 小时前
前端转 Agent:真正要重写的不是接口层,是交互层
前端
AI2中文网3 小时前
App Inventor 做的表单怎么导出 PDF?WebViewer + JavaScript 在本地生成并保存 PDF 的完整示例
android·开发语言·javascript·pdf·鸿蒙·app inventor
孟陬4 小时前
Foobar 技术周刊 #003:黄仁勋的三段吃瓜和回归,抛开事实不谈(嘿嘿)让全人类 AGI 进程得以继续
前端·后端·程序员