package com.usoftchina.bi.core.base;

import org.springframework.util.ObjectUtils;

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

/**
 * @Author chenwei
 * @Date 2019-05-07
 */
public class BaseContextHolder {

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

    public static void set(String key, Object value) {
        Map<String, Object> map = contextHolder.get();
        if (null == map) {
            map = new HashMap<String, Object>(1);
            contextHolder.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = contextHolder.get();
        if (null == map) {
            return null;
        }
        return map.get(key);
    }

    public static int getUserId() {
        Object value = get("userId");
        return Integer.valueOf(String.valueOf(value));
    }

    public static void setUserId(int userId) {
        set("userId", userId);
    }

    public static String getUserName() {
        Object value = get("userName");
        return String.valueOf(value);
    }

    public static void setUserName(String userName) {
        set("userName", userName);
    }

    public static String getRole(){
        Object value = get("role");
        return String.valueOf(value);
    }

    public static void setRole(String role){
        set("role", role);
    }
}
