using Microsoft.Extensions.Options;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiTenancy;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.SqlSugarCore;
///
/// 租户配置
///
public class TenantConfigurationWrapper : ITransientDependency
{
private readonly IAbpLazyServiceProvider _serviceProvider;
private ICurrentTenant CurrentTenant => _serviceProvider.LazyGetRequiredService();
private ITenantStore TenantStore => _serviceProvider.LazyGetRequiredService();
private DbConnOptions DbConnOptions => _serviceProvider.LazyGetRequiredService>().Value;
public TenantConfigurationWrapper(IAbpLazyServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
///
/// 获取租户信息
/// [from:ai]
///
///
public async Task GetAsync()
{
//未开启多租户
if (!DbConnOptions.EnabledSaasMultiTenancy)
{
return await TenantStore.FindAsync(ConnectionStrings.DefaultConnectionStringName);
}
TenantConfiguration? tenantConfiguration = null;
if (CurrentTenant.Id is not null)
{
tenantConfiguration = await TenantStore.FindAsync(CurrentTenant.Id.Value);
if (tenantConfiguration == null)
{
throw new ApplicationException($"未找到租户信息,租户Id:{CurrentTenant.Id}");
}
return tenantConfiguration;
}
if (!string.IsNullOrEmpty(CurrentTenant.Name))
{
tenantConfiguration = await TenantStore.FindAsync(CurrentTenant.Name);
if (tenantConfiguration == null)
{
throw new ApplicationException($"未找到租户信息,租户名称:{CurrentTenant.Name}");
}
return tenantConfiguration;
}
return await TenantStore.FindAsync(ConnectionStrings.DefaultConnectionStringName);
}
///
/// 获取当前连接字符串
///
///
public async Task GetCurrentConnectionStringAsync()
{
return (await GetAsync()).ConnectionStrings.Default!;
}
///
/// 获取当前连接名
///
///
public async Task GetCurrentConnectionNameAsync()
{
return (await GetAsync()).Name;
}
}
public static class TenantConfigurationExtensions
{
///
/// 获取当前连接字符串
///
///
public static string GetCurrentConnectionString(this TenantConfiguration tenantConfiguration)
{
return tenantConfiguration.ConnectionStrings.Default!;
}
///
/// 获取当前连接名
///
///
public static string GetCurrentConnectionName(this TenantConfiguration tenantConfiguration)
{
return tenantConfiguration.Name;
}
}