Files
Yi.Admin/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/TenantConfigurationWrapper.cs

101 lines
3.1 KiB
C#
Raw Normal View History

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