递归构建级联关系 -- 分级处理

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

}

 

转载于:https://www.cnblogs.com/xiaoyu1994/p/11213297.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值