package cn.pojo.vo;

import com.fasterxml.jackson.annotation.JsonInclude;

import cn.pojo.entity.TSysDept;
import cn.pojo.entity.TSysMenu;
import cn.util.core.IsKit;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Treeselect树结构实体类，el-tree组件的节点所现实的内容 
 */
public class TreeSelectVo implements Serializable
{
	private static final long serialVersionUID = 1L;

	/** 节点ID */
	private Integer id;
	/** 节点名称 */
	private String label;

	/** 子节点 */
	@JsonInclude(JsonInclude.Include.NON_EMPTY)
	private List<TreeSelectVo> children;

	//菜单：2022.7.24.顶级路由出口  格式”system/user/index“特点：都含有index
	//用于控制：按钮授权到指定页面
	private String component;

	public TreeSelectVo()  { }

	public TreeSelectVo(TSysDept dept)
	{
		this.id = dept.getDeptId();
		this.label = dept.getDeptName();
		this.children = dept.getChildren().stream().map(TreeSelectVo::new).collect(Collectors.toList());
	}

	public TreeSelectVo(TSysMenu menu)
	{
		this.id = menu.getMenuId();
		this.label = menu.getMetaTitle();
		this.component =  handlePerm(menu.getComponent());
		this.children = menu.getChildren().stream().map(TreeSelectVo::new).collect(Collectors.toList());
	}

	public Integer getId()
	{
		return id;
	}

	public void setId(Integer id)
	{
		this.id = id;
	}

	public String getLabel()
	{
		return label;
	}

	public void setLabel(String label)
	{
		this.label = label;
	}

	public List<TreeSelectVo> getChildren()
	{
		return children;
	}

	public void setChildren(List<TreeSelectVo> children)
	{
		this.children = children;
	}
	/**
	 * @return the component
	 */
	public String getComponent() {
		return component;
	}

	/**
	 * @param component the component to set
	 */
	public void setComponent(String component) {
		this.component = component;
	}
	/**permStr格式：system/user/index [见t_sys_menu]
	 * 返回格式：A:B
	 */ 
	private String handlePerm(String permStr) 
	{
		if(IsKit.isNullOrEmpty(permStr)) {
			return "";	
		}
		String result = "";
		StringBuilder buf = new StringBuilder();
		if(permStr.contains("index")) {
			String [] arryStr = permStr.split("\\/");
			if(IsKit.isNotNullOrEmpty(arryStr)){
				for(int i=0;i<arryStr.length - 1 ;i++) {
					buf.append(arryStr[i]);
					buf.append(":");
				}
			}
			if(IsKit.isNotNullOrEmpty(buf) && buf.length() > 0){
				result = buf.toString().substring(0,buf.length()-1);
			}
		}
		return result;
	}
}
