package com.ibeetl.admin.core.util;

import com.ibeetl.admin.core.entity.DouYinChannelEntity;
import com.ibeetl.admin.core.rbac.tree.ChannelItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建一个渠道树，用于前端选择
 * @author xiao
 *
 */
public class ChannelBuildUtil {

	private  ChannelBuildUtil(){
		
	}
	public static  ChannelItem buildChannelTree(List<DouYinChannelEntity> list){
		DouYinChannelEntity root = new DouYinChannelEntity();
		root.setId(0L);
		ChannelItem rootOrg = new  ChannelItem(root);
		buildTreeNode(rootOrg,list);
		return rootOrg;
	}
	
	private static void buildTreeNode(ChannelItem parent,List<DouYinChannelEntity> list){
		
		long id = parent.getId();
		List<DouYinChannelEntity> dels = new ArrayList<>();
		for(DouYinChannelEntity SysFunction:list){
			if(SysFunction.getParentid()!=null && SysFunction.getParentid()==id){
				ChannelItem item = new ChannelItem(SysFunction);
				item.setParent(parent);
				dels.add(SysFunction);
			}
		}
		list.removeAll(dels);
		if(list.isEmpty()){
			return ;
		}
		for(ChannelItem child:parent.getChildren()){
			buildTreeNode(child,list);
		}
		
	}
}
