import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import com.xiaoyu.test.model.Group;
import com.ecfund.system.utils.TreeUtils;
/**
* @Title:
* @ClassName: Test
* @Description: 递归构建树形节后(分级算法)
* 分级指,每一次级别都比下一级别多3位数值,如下:
* 第一级:001
* 第二级:001001
* 第三级:001001001
* @author: liukx
* @date: 2019年7月19日 下午2:50:53
* @since JDK1.8
* @history 2019年7月19日 新建
*/
public class Test {
/**
* 简单组织机构树,异步加载组信息为树
* @Title: tree
* @Description: TODO
* @param @param entity
* @param @return
* @param @throws Exception
* @return String
* @throws
*/
public String tree(Group entity) throws Exception {
List<Group> GroupList = GroupDAO.find(entity); //数据库查询列表
//List<Group> treeList = getTree(managerUserGroupList);
List<Group> parentList = new ArrayList<Group>();
for(Group perm: GroupList){
if(perm.getGroupid().length() == 3){
parentList.add(perm);
}
}
for(Group per:parentList){
per.setChildren(getChild(per.getGroupid(),GroupList));
}
String [] param = {"guid","groupname","groupid","createdate","operatorid","lastupdate"};
TreeUtils<Group> utils = new TreeUtils<Group>("guid", "nodeName", "leaf", null, "children",param);
utils.setLeafType("是");
return utils.convert(parentList);
}
/**
*
* @Title: getChild
* @Description: 获得子菜单
* @param @param parentID 父节点ID
* @param @param perList 全量list
* @param @return
* @return List<Group>
* @throws
*/
public List<Group> getChild(String parentID,List<Group> perList){
List<Group> childList = new ArrayList<Group>();
/**
* 子节点判定条件
* 1、子节点Groupid必须以父Groupid为开始
* 2、子节点Groupid不能等于父节点Groupid
* 3、子节点Groupid必须并且比父节点Groupid长度多三位
*/
for(Group per: perList){
if(StringUtils.startsWith(per.getGroupid(), parentID)
&& !StringUtils.equals(per.getGroupid(), parentID)
&& (per.getGroupid().length() - parentID.length()) == 3){
//getChild(per.getGroupid(),perList);
//per.setChildren(childList);
childList.add(per);
}
}
for(Group child: childList){
child.setChildren(getChild(child.getGroupid(),perList));
}
if(childList.size() == 0){
return null;
}
return childList;
}
}