package cn.reghao.tnb.common.auth;

/**
 * @author reghao
 * @date 2023-06-02 10:48:59
 */
public class UserContext implements AutoCloseable {
    static final ThreadLocal<LoginUser> CURRENT = new ThreadLocal<>();

    public UserContext(LoginUser loginUser) {
        CURRENT.set(loginUser);
    }

    public static LoginUser getLoginUser() {
        return CURRENT.get();
    }

    public static long getUser() {
        LoginUser loginUser = CURRENT.get();
        return loginUser != null ? loginUser.getUserId() : -1;
    }

    public static int getUserId() {
        LoginUser loginUser = CURRENT.get();
        return loginUser != null ? (int) loginUser.getUserId() : -1;
    }

    @Override
    public void close() {
        CURRENT.remove();
    }
}
