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

@@ -195,7 +195,7 @@ namespace Yi.Framework.SqlSugarCore
return dbTypeFromTenantName!.Value;
}
//根据租户name进行匹配db类型: Test_Sqlite[来自AI]
//根据租户name进行匹配db类型: Test@Sqlite[form:AI]
private DbType? GetDbTypeFromTenantName(string name)
{
if (string.IsNullOrWhiteSpace(name))
@@ -203,25 +203,26 @@ namespace Yi.Framework.SqlSugarCore
return null;
}
// 查找下划线的位置
int underscoreIndex = name.LastIndexOf('_');
// 查找@符号的位置
int atIndex = name.LastIndexOf('@');
if (underscoreIndex == -1 || underscoreIndex == name.Length - 1)
if (atIndex == -1 || atIndex == name.Length - 1)
{
return null;
}
// 提取 枚举 部分
string enumString = name.Substring(underscoreIndex + 1);
string enumString = name.Substring(atIndex + 1);
// 尝试将 尾缀 转换为枚举
if (Enum.TryParse<DbType>(enumString, out DbType result))
{
return result;
}
// 条件不满足时返回 null
return null;
else
{
throw new ArgumentException($"数据库{name}db类型错误或不支持无法匹配{enumString}数据库类型");
}
}
public virtual void BackupDataBase()

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>

View File

@@ -53,7 +53,7 @@ namespace Yi.Framework.SqlSugarCore
options.DefaultSequentialGuidType = guidType;
});
service.TryAddScoped<ISqlSugarDbContext, SqlSugarDbContextFactory>();
service.TryAddTransient<ISqlSugarDbContext, SqlSugarDbContextFactory>();
//不开放sqlsugarClient
//service.AddTransient<ISqlSugarClient>(x => x.GetRequiredService<ISqlsugarDbContext>().SqlSugarClient);
@@ -71,15 +71,25 @@ namespace Yi.Framework.SqlSugarCore
var dbConfig = section.Get<DbConnOptions>();
//将默认db传递给abp连接字符串模块
Configure<AbpDbConnectionOptions>(x => { x.ConnectionStrings.Default = dbConfig.Url; });
//配置abp默认租户
Configure<AbpDefaultTenantStoreOptions>(x => { x.Tenants.AddFirst(new TenantConfiguration
{
Id = Guid.Empty,
Name =$"{ConnectionStrings.DefaultConnectionStringName}",
NormalizedName = ConnectionStrings.DefaultConnectionStringName,
ConnectionStrings = new ConnectionStrings(){{ConnectionStrings.DefaultConnectionStringName,dbConfig.Url}},
IsActive = true
});});
//配置abp默认租户对接abp模块
Configure<AbpDefaultTenantStoreOptions>(x => {
var tenantList = x.Tenants.ToList();
foreach(var tenant in tenantList)
{
tenant.NormalizedName = tenant.Name.Contains("@") ?
tenant.Name.Substring(0, tenant.Name.LastIndexOf("@")) :
tenant.Name;
}
tenantList.Insert(0, new TenantConfiguration
{
Id = Guid.Empty,
Name = $"{ConnectionStrings.DefaultConnectionStringName}",
NormalizedName = ConnectionStrings.DefaultConnectionStringName,
ConnectionStrings = new ConnectionStrings() { { ConnectionStrings.DefaultConnectionStringName, dbConfig.Url } },
IsActive = true
});
x.Tenants = tenantList.ToArray();
});
context.Services.AddYiDbContext<DefaultSqlSugarDbContext>();
return Task.CompletedTask;
}