package cn.qinys.platform.upms.api.service.impl;

import cn.qinys.platform.entity.upms.UpmsMenuBase;
import cn.qinys.platform.upms.api.resp.MenuPageResp;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author tanlie
 * Date: 2024/2/6 15:42
 */
public abstract class MenuBaseService {



    protected List<MenuPageResp> getPageTree(List<UpmsMenuBase> menuList) {
        List<UpmsMenuBase> firstLevel = new ArrayList<>();
        menuList.forEach(menu -> {
            if (menu.getParentId() == 0) {
                firstLevel.add(menu);
            }
        });
        List<MenuPageResp> resp = new ArrayList<>();
        for (UpmsMenuBase menu : firstLevel) {
            MenuPageResp root = new MenuPageResp();
            root.setId(menu.getId());
            root.setTitle(menu.getTitle());
            root.setHidden(menu.getHidden());
            root.setComponent(menu.getComponent());
            root.setIcon(menu.getIcon());
            root.setOrd(menu.getOrd());
            root.setKeepAlive(menu.getKeepAlive());
            root.setPath(menu.getPath());
            root.setParentId(0);
            root.setChildren(this.getPageChildren(menu.getId(), menuList));
            resp.add(root);
        }
        return resp;
    }

    protected List<MenuPageResp> getPageChildren(Integer parentId, List<UpmsMenuBase> menuList) {
        List<MenuPageResp> children = new ArrayList<>();
        for (UpmsMenuBase menu : menuList) {
            if (parentId.equals(menu.getParentId())) {
                MenuPageResp child = new MenuPageResp();
                child.setId(menu.getId());
                child.setTitle(menu.getTitle());
                child.setHidden(menu.getHidden());
                child.setComponent(menu.getComponent());
                child.setIcon(menu.getIcon());
                child.setOrd(menu.getOrd());
                child.setKeepAlive(menu.getKeepAlive());
                child.setPath(menu.getPath());
                child.setParentId(parentId);
                child.setChildren(this.getPageChildren(child.getId(), menuList));
                children.add(child);
            }
        }
        return children;
    }


}
