package com.xueyi.nlt.nlt.context;

import com.xueyi.common.core.utils.core.ConvertUtil;
import com.xueyi.common.core.utils.core.StrUtil;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TerminalSecurityContextHolder {
    private static final ThreadLocal<Map<String,Object>> THREAD_LOCAL = new ThreadLocal<>();


    public static Long getOperatorId() {
        return ConvertUtil.toLong(get("operatorId"), -1L);
    }

    public static void setOperatorId(String operatorId) {
        set("operatorId", operatorId);
    }

    public static void set(String key, Object value) {
        Map<String, Object> map = getLocalMap();
        map.put(key, value == null ? StrUtil.EMPTY : value);
    }

    public static String get(String key) {
        Map<String, Object> map = getLocalMap();
        return ConvertUtil.toStr(map.getOrDefault(key, StrUtil.EMPTY));
    }
    public static <T> T get(String key, Class<T> clazz) {
        Map<String, Object> map = getLocalMap();
        return ConvertUtil.convert(clazz, map.getOrDefault(key, null));
    }
    public static Map<String, Object> getLocalMap() {
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map == null) {
            map = new ConcurrentHashMap<>();
            THREAD_LOCAL.set(map);
        }
        return map;
    }
}
