package com.philisense.www.utils;

import com.philisense.www.core.text.Convert;

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

/**
 * 当前用户Map
 *
 * @author
 */
public class ActiveUserUtils {

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

    public static Map<String, Object> getActiveUserMap() {
        return activeUserMap.get();
    }

    public static void removeUser(){
        activeUserMap.remove();
    }

    public static void setActiveUserMap(Map<String, Object> activeUserMap) {
        ActiveUserUtils.activeUserMap.set(StringUtils.isNotNull(activeUserMap) ? activeUserMap : new HashMap<>());
    }

    public static String getUserId() {
        Map<String, Object> userMap = getActiveUserMap();
        return userMap == null ? "" : Convert.toStr(userMap.get("user_id"), "");
    }

    public static String getUserName() {
        Map<String, Object> userMap = getActiveUserMap();
        return userMap == null ? "" : Convert.toStr(userMap.get("user_name"), "");
    }

    public static String getUserNameCn() {
        Map<String, Object> userMap = getActiveUserMap();
        return userMap == null ? "" : Convert.toStr(userMap.get("user_name_cn"), "");
    }

    public static Integer getTenantId() {
        Map<String, Object> userMap = getActiveUserMap();
        return userMap == null ? 0 : Convert.toInt(userMap.get("tenant_id"), 0);
    }

    public static String getTenantName() {
        Map<String, Object> userMap = getActiveUserMap();
        return userMap == null ? "" : Convert.toStr(userMap.get("tenant_name"), "");
    }

    /*public static String getDeptId() {
        return Convert.toStr(activeUserMap.get("dept_id"), "");
    }

    public static String getDeptName() {
        return Convert.toStr(activeUserMap.get("dept_name"), "");
    }*/

    public static String getUserValueByKey(String key) {
        Map<String, Object> userMap = getActiveUserMap();
        return StringUtils.isEmpty(key) || userMap == null ?  "" : Convert.toStr(userMap.get(key), "");
    }
}
