package com.lutao.cloud.common.core.util;

import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;
import lombok.Data;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class TreeTest{

    @Data
    static class Area{


        private Integer id;
        private String name;
        private Integer parentId;
        private Integer sort;
    }

    public static void main(String[] args){
        List<Area> areaList = new ArrayList<>();

        for (int i = 1; i < 11; i++){
            Area area = new Area();
            area.setId(i);
            area.setName("区域" + i);
            if (i == 1){
                area.setParentId(0);
            } else{
                area.setParentId(new Random().nextInt(2));
            }
            area.setSort(new Random().nextInt(5));
            areaList.add(area);
        }

        List<TreeNode<Integer>> collect = areaList.stream()
                .filter(area -> area.getId().intValue() != area.getParentId())
                .sorted(Comparator.comparingInt(Area::getSort)).map(area ->{
                    TreeNode<Integer> treeNode = new TreeNode();
                    treeNode.setId(area.getId());
                    treeNode.setParentId(area.getParentId());
                    treeNode.setName(area.getName());
                    treeNode.setWeight(area.getSort());
                    return treeNode;
                }).collect(Collectors.toList());
        JSONObject jsonObject = new JSONObject();
    }
}
