0
点赞
收藏
分享

微信扫一扫

Axios网络请求

微言记 1天前 阅读 3

迁移部门表,没有ancestors字段,若依部门权限没办法做,写了一段代码维护ancestors字段。
调用的若依框架组成树的方法,拼接ancestors

    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);
    }
    
    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());
            }
        }
    }
    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;
    }

举报

相关推荐

0 条评论