feat: 完成saas多租户测试

This commit is contained in:
chenchun
2024-07-28 00:25:00 +08:00
parent df1a4d7149
commit 8b890a9271
2 changed files with 33 additions and 32 deletions

View File

@@ -57,12 +57,10 @@ namespace Yi.Framework.SqlSugarCore
connectionCreator.DataExecuted = DataExecuted; connectionCreator.DataExecuted = DataExecuted;
connectionCreator.OnLogExecuting = OnLogExecuting; connectionCreator.OnLogExecuting = OnLogExecuting;
connectionCreator.OnLogExecuted = OnLogExecuted; connectionCreator.OnLogExecuted = OnLogExecuted;
var currentConnection = GetCurrentConnectionString();
var currentDbType = GetCurrentDbType();
SqlSugarClient = new SqlSugarClient(connectionCreator.Build(action: options => SqlSugarClient = new SqlSugarClient(connectionCreator.Build(action: options =>
{ {
options.ConnectionString = currentConnection; options.ConnectionString = GetCurrentConnectionString();
options.DbType = GetCurrentDbType();
})); }));
connectionCreator.SetDbAop(SqlSugarClient); connectionCreator.SetDbAop(SqlSugarClient);
} }

View File

@@ -18,11 +18,15 @@ namespace Yi.Framework.TenantManagement.Application
/// <summary> /// <summary>
/// 租户管理 /// 租户管理
/// </summary> /// </summary>
public class TenantService : YiCrudAppService<TenantAggregateRoot, TenantGetOutputDto, TenantGetListOutputDto, Guid, TenantGetListInput, TenantCreateInput, TenantUpdateInput>, ITenantService public class TenantService :
YiCrudAppService<TenantAggregateRoot, TenantGetOutputDto, TenantGetListOutputDto, Guid, TenantGetListInput,
TenantCreateInput, TenantUpdateInput>, ITenantService
{ {
private ISqlSugarRepository<TenantAggregateRoot, Guid> _repository; private ISqlSugarRepository<TenantAggregateRoot, Guid> _repository;
private IDataSeeder _dataSeeder; private IDataSeeder _dataSeeder;
public TenantService(ISqlSugarRepository<TenantAggregateRoot, Guid> repository, IDataSeeder dataSeeder) : base(repository)
public TenantService(ISqlSugarRepository<TenantAggregateRoot, Guid> repository, IDataSeeder dataSeeder) :
base(repository)
{ {
_repository = repository; _repository = repository;
_dataSeeder = dataSeeder; _dataSeeder = dataSeeder;
@@ -47,9 +51,11 @@ namespace Yi.Framework.TenantManagement.Application
{ {
RefAsync<int> total = 0; RefAsync<int> total = 0;
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name!)) var entities = await _repository._DbQueryable
.WhereIF(input.StartTime is not null && input.EndTime is not null, x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime) .WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name!))
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); .WhereIF(input.StartTime is not null && input.EndTime is not null,
x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
return new PagedResultDto<TenantGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities)); return new PagedResultDto<TenantGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
} }
@@ -75,6 +81,7 @@ namespace Yi.Framework.TenantManagement.Application
{ {
throw new UserFriendlyException("创建失败,当前租户已存在"); throw new UserFriendlyException("创建失败,当前租户已存在");
} }
return await base.CreateAsync(input); return await base.CreateAsync(input);
} }
@@ -118,9 +125,8 @@ namespace Yi.Framework.TenantManagement.Application
using (CurrentTenant.Change(id)) using (CurrentTenant.Change(id))
{ {
await CodeFirst(this.LazyServiceProvider); await CodeFirst(this.LazyServiceProvider);
await _dataSeeder.SeedAsync(id); await _dataSeeder.SeedAsync(id);
} }
} }
private async Task CodeFirst(IServiceProvider service) private async Task CodeFirst(IServiceProvider service)
@@ -134,27 +140,24 @@ namespace Yi.Framework.TenantManagement.Application
db = await _repository.GetDbContextAsync(); db = await _repository.GetDbContextAsync();
//尝试创建数据库 //尝试创建数据库
db.DbMaintenance.CreateDatabase(); db.DbMaintenance.CreateDatabase();
await uow.CompleteAsync();
List<Type> types = new List<Type>();
foreach (var module in moduleContainer.Modules)
{
types.AddRange(module.Assembly.GetTypes()
.Where(x => x.GetCustomAttribute<IgnoreCodeFirstAttribute>() == null)
.Where(x => x.GetCustomAttribute<SugarTable>() != null)
.Where(x => x.GetCustomAttribute<DefaultTenantTableAttribute>() is null)
.Where(x => x.GetCustomAttribute<SplitTableAttribute>() is null));
}
if (types.Count > 0)
{
db.CodeFirst.InitTables(types.ToArray());
}
await uow.CompleteAsync();
} }
List<Type> types = new List<Type>();
foreach (var module in moduleContainer.Modules)
{
types.AddRange(module.Assembly.GetTypes()
.Where(x => x.GetCustomAttribute<IgnoreCodeFirstAttribute>() == null)
.Where(x => x.GetCustomAttribute<SugarTable>() != null)
.Where(x=>x.GetCustomAttribute<DefaultTenantTableAttribute>() is null)
.Where(x => x.GetCustomAttribute<SplitTableAttribute>() is null));
}
if (types.Count > 0)
{
db.CopyNew().CodeFirst.InitTables(types.ToArray());
}
} }
} }
} }