package thyyxxk.webserver.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import thyyxxk.webserver.entity.login.VueMenu;

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

/**
 * @description: 层级菜单工具
 * @author: DingJie
 * @create: 2021/8/1016:09
 */
@Slf4j
public class TreeUtil {

    public static List<VueMenu> getVueMenuTree(List<VueMenu> tempList) {
        List<VueMenu> resultList = new ArrayList<>();
        Map<Integer, VueMenu> treeMap = new HashMap<>(tempList.size());
        for (VueMenu item : tempList) {
            JSONObject meta = new JSONObject();
            if (StringUtil.notBlank(item.getMetaIcon())) {
                meta.put("icon", item.getMetaIcon());
            }
            meta.put("title", item.getMetaTitle());
            if (null != item.getHideTabs()) {
                meta.put("hideTabs", item.getHideTabs());
            }
            item.setMeta(meta);
            treeMap.put(item.getId(), item);
            if (0 == item.getParent()) {
                resultList.add(item);
            }
        }
//        for (Map.Entry<Integer, VueMenu> entry : treeMap.entrySet()) {
//            VueMenu menu = entry.getValue();
//            if (!resultList.contains(menu)) {
//                menu.setCompleteRoute(treeMap.get(menu.getParent()).getCompleteRoute() + '/' + menu.getCompleteRoute());
//            }
//        }
        for (VueMenu item : tempList) {
            VueMenu template = treeMap.get(item.getParent());
            if (null != template) {
                if (null == template.getChildren()) {
                    template.setChildren(new ArrayList<>());
                }
                template.getChildren().add(item);
            }
        }
        return resultList;
    }
}
