feat: 完成多租户优化改造

This commit is contained in:
橙子
2025-02-22 15:26:00 +08:00
parent 753b5b0a26
commit 45736dfce9
5 changed files with 124 additions and 30 deletions

View File

@@ -1,6 +1,8 @@
using Volo.Abp.Data;
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;
@@ -9,33 +11,52 @@ namespace Yi.Framework.SqlSugarCore;
/// </summary>
public class TenantConfigurationWrapper : ITransientDependency
{
private ICurrentTenant _currentTenant;
private ITenantStore _tenantStore;
private readonly IAbpLazyServiceProvider _serviceProvider;
private ICurrentTenant CurrentTenant => _serviceProvider.LazyGetRequiredService<ICurrentTenant>();
private ITenantStore TenantStore => _serviceProvider.LazyGetRequiredService<ITenantStore>();
private DbConnOptions DbConnOptions => _serviceProvider.LazyGetRequiredService<IOptions<DbConnOptions>>().Value;
public TenantConfigurationWrapper(ICurrentTenant currentTenant, ITenantStore tenantStore)
public TenantConfigurationWrapper(IAbpLazyServiceProvider serviceProvider)
{
_currentTenant = currentTenant;
_tenantStore = tenantStore;
_serviceProvider = serviceProvider;
}
/// <summary>
/// 获取租户信息
/// [from:ai]
/// </summary>
/// <returns></returns>
public async Task<TenantConfiguration?> GetAsync()
{
if (_currentTenant.Id is not null)
//未开启多租户
if (!DbConnOptions.EnabledSaasMultiTenancy)
{
return await _tenantStore.FindAsync(_currentTenant.Id.Value);
return await TenantStore.FindAsync(ConnectionStrings.DefaultConnectionStringName);
}
else if (!string.IsNullOrEmpty(_currentTenant.Name))
TenantConfiguration? tenantConfiguration = null;
if (CurrentTenant.Id is not null)
{
return await _tenantStore.FindAsync(_currentTenant.Name);
tenantConfiguration = await TenantStore.FindAsync(CurrentTenant.Id.Value);
if (tenantConfiguration == null)
{
throw new ApplicationException($"未找到租户信息,租户Id:{CurrentTenant.Id}");
}
return tenantConfiguration;
}
else
if (!string.IsNullOrEmpty(CurrentTenant.Name))
{
return await _tenantStore.FindAsync(ConnectionStrings.DefaultConnectionStringName);
tenantConfiguration = await TenantStore.FindAsync(CurrentTenant.Name);
if (tenantConfiguration == null)
{
throw new ApplicationException($"未找到租户信息,租户名称:{CurrentTenant.Name}");
}
return tenantConfiguration;
}
return await TenantStore.FindAsync(ConnectionStrings.DefaultConnectionStringName);
}
/// <summary>