TenantContext.java 968 字节
package com.aigeo.common.context;

/**
 * 租户上下文 - 多租户架构的核心组件
 *
 * @author AIGEO Team
 * @since 1.0.0
 */
public class TenantContext {
    
    private static final ThreadLocal<Integer> CURRENT_TENANT = new ThreadLocal<>();
    
    /**
     * 设置当前租户ID
     */
    public static void setCurrentTenant(Integer tenantId) {
        CURRENT_TENANT.set(tenantId);
    }
    
    /**
     * 获取当前租户ID
     */
    public static Integer getCurrentTenant() {
        return CURRENT_TENANT.get();
    }
    
    /**
     * 获取当前租户ID(别名方法)
     */
    public static Integer getCurrentTenantId() {
        return getCurrentTenant();
    }
    
    /**
     * 检查是否有设置租户
     */
    public static boolean hasTenant() {
        return getCurrentTenant() != null;
    }
    
    /**
     * 清除当前租户
     */
    public static void clear() {
        CURRENT_TENANT.remove();
    }
}