若依 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;
    }
相关推荐
\xin1 小时前
pikachu自编exp,xss反射性get,post,存储型xss,dom,dom-x
前端·javascript·xss
是烟花哈5 小时前
【前端】React框架学习
前端·学习·react.js
qq4356947016 小时前
JavaWeb08
前端
2401_878454537 小时前
html和css的复习(1)
前端·css·html
@PHARAOH7 小时前
WHAT - git worktree 概念
前端·git
IT_陈寒8 小时前
我竟然被JavaScript的隐式类型转换坑了三天!
前端·人工智能·后端
我亚索贼六丶8 小时前
二十六. AI基础概念之如何更好的使用AI
前端
小码哥_常8 小时前
安卓启动页Logo适配秘籍:告别“奇形怪状”的展示
前端
我亚索贼六丶8 小时前
二十五.Electron 初体验与进阶
前端
当时只道寻常8 小时前
像使用 Redis 一样操作 LocalStorage
前端·前端工程化