package com.storlead.framework.util;

import com.storlead.framework.auth.vo.LoginUser;
import com.storlead.framework.common.constant.UserCacheKeyConstants;
import com.storlead.framework.core.context.Context;
import com.storlead.framework.core.tenant.TenantContext;
import lombok.extern.log4j.Log4j2;

/**
 * @author chenkq
 */
@Log4j2
public class LoginUserUtil {
    /**
     * 获取当前用户信息
     * @return
     */
    public static LoginUser getLoginUser(){
        try {
            LoginUser loginUser = Context.getContext().getAttribute(UserCacheKeyConstants.LOGIN_USER_INFO_KEY, LoginUser.class);
            return loginUser;
        } catch (Exception e) {
            return null;
        }
    }

    public static String getLoginToken(){
        try {
            String loginToken = Context.getContext().getAttribute(UserCacheKeyConstants.LOGIN_USER_INFO_TOKEN_KEY, String.class);
            return loginToken;
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 获取当前用户信息
     * @return
     */
    public static Long getCurrentUserId(){
        try {
            Long currentUserId = Context.getContext().getAttribute(UserCacheKeyConstants.LOGIN_USER_INFO_ID_KEY, Long.class);
            return currentUserId;
        } catch (Exception e) {
            log.error("error - getCurrentUserId",e);
            return null;
        }
    }

    /**
     * 当前租户 ID（请求线程内）；未登录或未绑定时为 null。
     */
    public static Long getCurrentTenantId() {
        return TenantContext.getTenantId();
    }

}
