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

113 lines
3.3 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>
/// 租户配置包装器
2025-02-21 18:00:06 +08:00
/// </summary>
public class TenantConfigurationWrapper : ITransientDependency
{
2025-02-22 15:26:00 +08:00
private readonly IAbpLazyServiceProvider _serviceProvider;
private ICurrentTenant CurrentTenantService =>
_serviceProvider.LazyGetRequiredService<ICurrentTenant>();
private ITenantStore TenantStoreService =>
_serviceProvider.LazyGetRequiredService<ITenantStore>();
private DbConnOptions DbConnectionOptions =>
_serviceProvider.LazyGetRequiredService<IOptions<DbConnOptions>>().Value;
2025-02-21 18:00:06 +08:00
/// <summary>
/// 构造函数
/// </summary>
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-21 18:00:06 +08:00
/// </summary>
public async Task<TenantConfiguration?> GetAsync()
{
if (!DbConnectionOptions.EnabledSaasMultiTenancy)
2025-02-21 18:00:06 +08:00
{
return await TenantStoreService.FindAsync(ConnectionStrings.DefaultConnectionStringName);
2025-02-21 18:00:06 +08:00
}
return await GetTenantConfigurationByCurrentTenant();
}
private async Task<TenantConfiguration?> GetTenantConfigurationByCurrentTenant()
{
// 通过租户ID查找
if (CurrentTenantService.Id.HasValue)
2025-02-21 18:00:06 +08:00
{
var config = await TenantStoreService.FindAsync(CurrentTenantService.Id.Value);
if (config == null)
2025-02-22 15:26:00 +08:00
{
throw new ApplicationException($"未找到租户信息,租户Id:{CurrentTenantService.Id}");
2025-02-22 15:26:00 +08:00
}
return config;
2025-02-21 18:00:06 +08:00
}
// 通过租户名称查找
if (!string.IsNullOrEmpty(CurrentTenantService.Name))
2025-02-21 18:00:06 +08:00
{
var config = await TenantStoreService.FindAsync(CurrentTenantService.Name);
if (config == null)
2025-02-22 15:26:00 +08:00
{
throw new ApplicationException($"未找到租户信息,租户名称:{CurrentTenantService.Name}");
2025-02-22 15:26:00 +08:00
}
return config;
2025-02-21 18:00:06 +08:00
}
// 返回默认配置
return await TenantStoreService.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;
}
}