TenantResolveHelper.cs 1.28 KB
using FoodLabeling.Domain.Shared.MultiTenancy;
using Volo.Abp;
using Volo.Abp.MultiTenancy;

namespace FoodLabeling.Th.Application.MultiTenancy;

/// <summary>
/// 平台主库解析租户配置(供泰额登录/Web 登录共用)
/// </summary>
public static class TenantResolveHelper
{
    public static async Task<TenantConfiguration> ResolveTenantAsync(
        ITenantStore tenantStore,
        ICurrentTenant currentTenant,
        Guid tenantId)
    {
        if (tenantId == Guid.Empty)
        {
            throw new UserFriendlyException("租户 Id 不能为空");
        }

        TenantConfiguration? config;
        using (currentTenant.Change(null))
        {
            config = await tenantStore.FindAsync(tenantId);
        }

        if (config is null)
        {
            throw new UserFriendlyException("租户不存在或已停用");
        }

        if (string.IsNullOrWhiteSpace(config.ConnectionStrings?.Default))
        {
            throw new UserFriendlyException("租户未配置业务库连接串");
        }

        return config;
    }

    public static bool IsSeparateDatabaseMode(string? mode) =>
        string.Equals(
            mode?.Trim(),
            FoodLabelingMultiTenancyConsts.SeparateDatabaseMode,
            StringComparison.OrdinalIgnoreCase);
}