package data

import (
	"hongze/hz_crm_api/models/data_manage"
)

func chartClassifyHaveChild(allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems) (childs []*data_manage.ChartClassifyItems, yes bool) {
	for _, v := range allNode {
		if v.ParentId == node.ChartClassifyId {
			childs = append(childs, v)
		}
	}
	if len(childs) > 0 {
		yes = true
	}
	return
}

func ChartClassifyItemsMakeTree(allNode []*data_manage.ChartClassifyItems, node *data_manage.ChartClassifyItems) {
	childs, _ := chartClassifyHaveChild(allNode, node) //判断节点是否有子节点并返回
	if len(childs) > 0 {
		node.Children = append(node.Children, childs[0:]...) //添加子节点
		for _, v := range childs {                           //查询子节点的子节点，并添加到子节点
			_, has := chartClassifyHaveChild(allNode, v)
			if has {
				ChartClassifyItemsMakeTree(allNode, v) //递归添加节点
			} else {
				childrenArr := make([]*data_manage.ChartClassifyItems, 0)
				v.Children = childrenArr
			}
		}
	} else {
		childrenArr := make([]*data_manage.ChartClassifyItems, 0)
		node.Children = childrenArr
	}
}
