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

import cn.superdesk.uniorder.common.numerical.AnCollectionUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.*;


// SELECT *
// FROM `sys_clique`
// WHERE `enabled` =1
// ORDER BY `sys_clique`.levels ASC ,`sys_clique`.pid ASC

/**
 * 集团
 *
 * @author linjinyu
 * @email linjinyu@avic-s.com
 * @date 2021-04-28 14:08:25
 */
@Slf4j
@Data
@ToString
public class SysCliqueTreeModel implements Serializable {

	@ApiModelProperty("集团id")
	private Long    id;
	@ApiModelProperty("上级id")
	private Long    pid;
	@ApiModelProperty("根节点集团id")
	private Long    higherPid;
	@ApiModelProperty("级别,根节点为0")
	private Integer levels;
	@ApiModelProperty("集团名称")
	private String  name;
	@ApiModelProperty("集团编码")
	private String  code;
	@ApiModelProperty("是否有效")
	private Integer enabled;


	private List<SysCliqueTreeModel> children;


	public void addChild(SysCliqueTreeModel child) {
		getChildren().add(child);
	}

	public void addAllChild(List<SysCliqueTreeModel> child) {
		getChildren().addAll(child);
	}


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

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

	public static SysCliqueTreeModel buildRoot(List<SysCliqueTreeModel> models) {

//		log.info("{}", JsonUtils.toJson(models));

		SysCliqueTreeModel modelRoot = new SysCliqueTreeModel();

		modelRoot.setId(0L);
		modelRoot.setPid(0L);
		modelRoot.setHigherPid(0L);
		modelRoot.setLevels(0);
		modelRoot.setName("全部");
		modelRoot.setCode("ROOT");
		modelRoot.setEnabled(1);


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

		Map<Long, SysCliqueTreeModel> modelMap = new HashMap<>();

		// 这个Id不可靠 因为是三个表里边的东西
		for (SysCliqueTreeModel model : models) {
//			if (model.getLevels().compareTo(0) == 0) {
//				modelMap.put(model.getId(), model);
//			}

			if (null == modelMap.get(model.getPid()) && model.getPid().equals(model.getId())) {
				modelMap.put(model.getPid(), model);

				modelRoot.addChild(model);
			} else if (null != modelMap.get(model.getPid()) && !model.getPid().equals(model.getId())) {
				modelMap.get(model.getPid()).getChildren().add(model);
			}
		}

//		log.info("{}", JsonUtils.toJson(modelMap));

//		for (SysCliqueTreeModel model : models) {
//			// 集团 物征 id = pid && levels == 0
//			if (model.getPid().equals(model.getId()) && model.getLevels().compareTo(0) == 0))
//			) {
//				root.addChild(model);
//			} else {
//
//	|| !modelMap.containsKey(model.getPid()
////				modelMap.get(model.getPid()).addChild(model);
//			}
//		}

		return modelRoot;
	}

	public static Map<Long, List<SysCliqueTreeModel>> buildChildren(List<SysCliqueTreeModel> models) {

		// key 为上级的id value 为本级List
		Map<Long, List<SysCliqueTreeModel>> modelMap = new HashMap<>();

		for (SysCliqueTreeModel model : models) {

			if (null == modelMap.get(model.getPid())) {
				List<SysCliqueTreeModel> list = new ArrayList<>();
				list.add(model);
				modelMap.put(model.getPid(), list);

			} else {
				modelMap.get(model.getPid()).add(model);
			}
		}

		return modelMap;
	}

	/**
	 * 递归遍历
	 *
	 * @param modelRoot
	 */
	public static void recursiveTraversal(SysCliqueTreeModel modelRoot, List<SysCliqueTreeModel> linear) {

//		log.info("树杆节点 -> 到此一游 {}", JsonUtils.toJson(modelRoot));
//		log.info("到此一游 {}", JsonUtils.toJson(modelRoot));

		linear.add(modelRoot);

		if (AnCollectionUtils.isNotEmpty(modelRoot.getChildren())) {

			modelRoot.getChildren().forEach(model -> {
				recursiveTraversal(model, linear);
			});
		} else {
//			log.info("叶子节点 -> 到此一游 {}", JsonUtils.toJson(modelRoot));
			return;
		}

	}

}