//格式化树形数据
export function getTrees(list) {
  //@wxcwater重写方法 适应多个树
  let highLevelLength = 99; //预设最大节点id长度，目前最大应该为12，所以设置为99保证正常
  list.forEach((item, index) => {
    item['pidLength'] = item.pid && item.pid != '' ? item.pid.length : 0;
    if (highLevelLength > item.pidLength) {
      highLevelLength = item.pidLength;
    }
  });
  const heightLevelNode = [];
  list.forEach((item, index) => {
    if (highLevelLength >= item.pidLength) {
      heightLevelNode.push(item);
    }
  });
  list.forEach((item, index) => {
    heightLevelNode.forEach((pItem) => {
      addTreeNode(pItem, item);
    });
  });
  return heightLevelNode;
}
function addTreeNode(treeNode, leafNode) {
  if (!Object.hasOwn(treeNode, 'children')) {
    treeNode['children'] = [];
  }
  if (leafNode.pid == treeNode.id) {
    treeNode.children.push(leafNode);
    return;
  }
  treeNode.children.forEach((item) => {
    addTreeNode(item, leafNode);
  });
}
