package com.dayou.utils;

import com.dayou.bo.LoginCacheUserBO;
import java.util.Optional;

public class LoginContext {

    private static final ThreadLocal<LoginCacheUserBO> loginThreadLocal = new ThreadLocal<>();

    public static void setLoginCacheUserBO(LoginCacheUserBO loginCacheUserBO) {
        loginThreadLocal.set(loginCacheUserBO);
    }

    public static LoginCacheUserBO getLoginCacheUserBO() {
        return Optional.ofNullable(loginThreadLocal.get()).orElseGet(LoginCacheUserBO::new);
    }


    /**
     * 返回当前登录用户的id
     * @return
     */
    public static Long getCurrentUserId() {
        return getLoginCacheUserBO().getId();

    }


    public static void removeAll() {
        loginThreadLocal.remove();
    }


    public static boolean isLogin() {
        return loginThreadLocal.get() != null;
    }

    public static Long getUserId() {
        return getLoginCacheUserBO().getId();
    }

    public static String getUserName() {
        return getLoginCacheUserBO().getName();
    }
    public static String getAccount() {
        return getLoginCacheUserBO().getAccount();
    }


}
