package com.yiqi.common.core.domain;


import java.util.HashMap;
import java.util.Map;

public class AuthHolder {

    private static final ThreadLocal<Map<String,Object>> authInfoThreadLocal = new ThreadLocal<>();

    public static Long userId(){
        Object value = get("userId");
        if(value == null){
            return null;
        }
        return (Long) value;
    }

    public static String userName(){
        Object value = get("userName");
        if(value == null){
            return "";
        }
        return (String) value;
    }

    public static String nickName(){
        Object value = get("nickName");
        if(value == null){
            return "";
        }
        return (String) value;
    }

    public static String platform(){
        Object value = get("platform");
        if(value == null){
            return "";
        }
        return (String) value;
    }

    public static String openId(){
        Object value = get("openid");
        if(value == null){
            return "";
        }
        return (String) value;
    }

    public static void setInfo(AuthInfoDO authInfoDO){
        Map<String,Object> map = new HashMap<>();
        map.put("userId",authInfoDO.getUserId());
        map.put("userName",authInfoDO.getUserName());
        map.put("nickName",authInfoDO.getNickName());
        map.put("platform",authInfoDO.getPlatform());
        map.put("openid",authInfoDO.getOpenId());
        authInfoThreadLocal.set(map);
    }

    public static Object get(String key){
        return getMap().get(key);
    }

    public static void setMap(Map<String,Object> map){
        authInfoThreadLocal.set(map);
    }

    public static void put(String key,Object value){
        Map<String,Object> map = authInfoThreadLocal.get();
        map.put(key,value);
    }

    private
    static Map<String, Object> getMap(){
        Map<String,Object> map = authInfoThreadLocal.get();
        if(map == null){
            map = new HashMap<>();
        }
        return map;
    }
}