package cn.superdesk.uniorder.dto.vo._function.tree;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;


/**
 * @Description: 这个是有标识为叶子的情况下
 * @Author: linjinyu
 * @CreateDate: 2021/5/10 下午5:52
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021/5/10 下午5:52
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public class TreeModel001 {

	private Integer            id;
	private String             name;
	private String             value;
	private String             icon;
	private Integer            pid;
	private String             pName;
	private boolean            leaf;
	private boolean            spread;
	private String             extraAttr;
	private Object             originData;
	private List<TreeModel001> children;


	public TreeModel001() {
	}


	public TreeModel001(Integer id, String name, String value, String icon, Integer pid, boolean leaf) {
		this.id = id;
		this.name = name;
		this.value = value;
		this.icon = icon;
		this.pid = pid;
		this.leaf = leaf;
	}


	public Integer getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}


	public String getValue() {
		return value;
	}


	public void setValue(String value) {
		this.value = value;
	}


	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public Integer getPid() {
		return pid == null ? 0 : pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public String getpName() {
		return pName;
	}

	public void setpName(String pName) {
		this.pName = pName;
	}


	public boolean isLeaf() {
		return leaf;
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public boolean isSpread() {
		return spread;
	}

	public void setSpread(boolean spread) {
		this.spread = spread;
	}

	public String getExtraAttr() {
		return extraAttr;
	}

	public void setExtraAttr(String extraAttr) {
		this.extraAttr = extraAttr;
	}

	public Object getOriginData() {
		return originData;
	}

	public void setOriginData(Object originData) {
		this.originData = originData;
	}

	public void addChild(TreeModel001 child) {
		getChildren().add(child);
		child.setpName(name);
	}

	public List<TreeModel001> getChildren() {
		return children == null ? (children = new ArrayList<>()) : children;
	}

	public void setChildren(List<TreeModel001> children) {
		this.children = children;
	}


	@Override
	public int hashCode() {
		final int prime  = 31;
		int       result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TreeModel001 other = (TreeModel001) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.JSON_STYLE);
	}


	public static TreeModel001 build(List<TreeModel001> models) {
		TreeModel001 root = new TreeModel001();

		// 按pid排序
		Collections.sort(models, new Comparator<TreeModel001>() {
			@Override
			public int compare(TreeModel001 o1, TreeModel001 o2) {
				return o1.getPid().compareTo(o2.getPid());
			}
		});

		Map<Integer, TreeModel001> modelMap = new HashMap<>();

		for (TreeModel001 model : models) {
			if (!model.isLeaf()) {
				modelMap.put(model.getId(), model);
			}
		}

		for (TreeModel001 model : models) {
			if ((model.getPid() == 0 && !model.isLeaf()) || !modelMap.containsKey(model.getPid())) {
				root.addChild(model);
			} else {
				modelMap.get(model.getPid()).addChild(model);
			}
		}

		return root;
	}

}
