package cc.smtweb.system.bpm.web.sys.user.menuPlan;

import cc.smtweb.framework.core.util.CommUtil;
import cc.smtweb.system.bpm.web.sys.user.menu.Menu;
import cc.smtweb.system.bpm.web.sys.user.menu.MenuCache;

import java.util.*;

/**
 * @Author: tanghp
 * @Date: 2022-09-26 11:12
 * @Desc: 菜单明细加载类
 */
public class MenuPlanItemProvider {
    private Map<Long, MenuPlanItem2> map = new HashMap<>();
    private MenuPlanItemProvider() {
    }

    public static MenuPlanItemProvider getInstance(){
        return ProviderHolder.provider;
    }

    // 获取菜单方案明细ID
    public MenuPlanItem2 getMenuPlanItem(long menuId){
        MenuPlanItem2  menuPlanItem = map.get(menuId);
        if(menuPlanItem!=null)return menuPlanItem;
        Collection<MenuPlan> plans =  MenuPlanCache.getInstance().getAll();
        if(CommUtil.isEmpty(plans))return null;
        for(MenuPlan menuPlan: plans){
           MenuPlanItem item = MenuPlanCache.getInstance().getById(menuPlan.getEntityId(),menuId);
            if(item!=null){
                menuPlanItem = new MenuPlanItem2();
                menuPlanItem.getData().putAll(item.getData());
                menuPlanItem.setMpId(menuPlan.getEntityId());
                map.put(menuId,menuPlanItem);
                return menuPlanItem;
            }
        }
        return null;
    }

    private static final class ProviderHolder {
        static final MenuPlanItemProvider provider = new MenuPlanItemProvider();
    }

    public static class MenuPlanItem2 extends MenuPlanItem{
        // 获取菜单方案ID
        public long getMpId(){
            return getLong("mp_id");
        }
        public void setMpId(long mp_id){
            put("mp_id",mp_id);
        }
        // 获取页面ID
        public long getPageId(){
            if(getMenu()>0L){
                Menu menu = MenuCache.getInstance().get(getMenu());
                if(menu!=null){
                    return Long.parseLong(menu.getPageId());
                }
            }
            return 0L;
        }
        // 获取路径的参数name
        public Set<String> getParameterNames(){
            Set<String> rtSet = new HashSet<>();
            if(getMenu()>0L){
                Menu menu = MenuCache.getInstance().get(getMenu());
                if(menu!=null){
                  String urlPath = menu.getPath();
                    rtSet = getNamesByPath(urlPath);
                }
            }
            return rtSet;
        }
        private Set<String> getNamesByPath(String url){
            MenuPlanHelper.UrlPath urlPath = new MenuPlanHelper.UrlPath(url);
            return urlPath.getParamNames();
        }
    }
}
