package com.schisandra.auth.common.context;

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

/**
 * @Classname LoginContextHolder
 * @BelongsProject: qing-yu-club
 * @BelongsPackage: com.landaiqing.subject.application.context
 * @Author: landaiqing
 * @CreateTime: 2024-03-03  18:11
 * @Description: 登录上下文对象
 * @Version: 1.0
 */
public class LoginContextHolder {
   private static final InheritableThreadLocal <Map<String,Object>> THREAD_LOCAL=new InheritableThreadLocal<>();

   public static void set(String key,Object val){
        Map<String,Object> map=getThreadLoacalMap();
        map.put(key, val);
   }
   public static Object get(String key){
       Map<String,Object> threadLoacalMap=getThreadLoacalMap();
       return   threadLoacalMap.get(key);
   }
   public static void remove(){
       THREAD_LOCAL.remove();
   }

   public static String getUserId(){
       return (String) getThreadLoacalMap().get("userId");
   }
   public static Map<String,Object> getThreadLoacalMap(){
       Map<String,Object> map =THREAD_LOCAL.get();
       if (Objects.isNull(map)){
           map=new ConcurrentHashMap<>();
           THREAD_LOCAL.set(map);
       }
       return map;
   }
}
