java 查询所有部门,然后循环递归部门层级

List  list  = categoryService.findDeptNameByLevel();

//一次性查出所有数据

Integer i=-200;
//定义第一级的树形菜单的数据的父级
List  list1=new ArrayList();
for (DeptsVo deptsVo : list) {
    if (deptsVo.getDeptLevel().intValue()==2){
        list1.add(deptsVo);
        i--;
        Category category=new Category();
        category.setDeptId(deptsVo.getId());
        category.setCategoryId(i);
        category.setCategoryName(deptsVo.getName());
        category.setCategoryType("metadataCustomType");
        category.setParentCategoryId(-1);
        categorys.add(category);
    }
}
//递归子集
setChildren(categorys,list,list1,i);

 

 

public void setChildren( List categorys, List  list, List  list1,Integer i){
    // list1.clear();
    List  list2=new ArrayList();
    for (DeptsVo deptsVo:list1){
        for (DeptsVo deptsVo1:list){
            if (deptsVo.getId().equals(deptsVo1.getPid())){
                list2.add(deptsVo1);
// 循环数组可以去掉已经用过的数据,考虑数据量小,没去
                i--;
                setCategory(categorys,deptsVo1,i);
            }
        }
    }
    if (list2!=null&&list2.size()>0){
        //递归子集
        setChildren(categorys,list,list2,i);
    }

}
public void setCategory( List categorys,DeptsVo deptsVo,Integer i){
    Category category=new Category();
    Integer id =deptsVo.getId();
    String name = deptsVo.getName();
    category.setDeptId(id);
    category.setCategoryId(i);
    category.setCategoryName(name);
    category.setCategoryType("metadataCustomType");
    for (Category category1:categorys ){
        if (category1.getDeptId()!=null&&category1.getDeptId().equals(deptsVo.getPid())){
            category.setParentCategoryId(category1.getCategoryId());
              
            break;
        }
    }
    categorys.add(category);
}

这里是将部门数据放入树形菜单了,CategoryId是树形菜单id,setParentCategoryId树形菜单父id,id用的不是部门id,pid是上级部门id,自己生成的id用来做数形菜单的id,用复数是因为正数都被数据库已有数据使用,是数据库自建目录数据结构加上系统组织架构部门结构做数据目录展现

 

你可能感兴趣的:(java技术)