refactor: 禁止 TenantId=0 并改为系统租户

This commit is contained in:
root
2026-01-29 13:16:15 +00:00
parent 77836e270f
commit bc1c4cc41b
4 changed files with 48 additions and 14 deletions

View File

@@ -39,7 +39,7 @@ public sealed class AppDataSeeder(
var appDbContext = scope.ServiceProvider.GetRequiredService<TakeoutAppDbContext>();
var dictionaryDbContext = scope.ServiceProvider.GetRequiredService<DictionaryDbContext>();
await EnsurePlatformTenantAsync(appDbContext, cancellationToken);
await EnsureSystemTenantAsync(appDbContext, cancellationToken);
var defaultTenantId = await EnsureDefaultTenantAsync(appDbContext, cancellationToken);
await EnsureDictionarySeedsAsync(dictionaryDbContext, defaultTenantId, cancellationToken);
@@ -132,9 +132,9 @@ public sealed class AppDataSeeder(
}
/// <summary>
/// 确保平台租户存在。
/// 确保系统租户存在TenantId=0用于系统级数据归属
/// </summary>
private async Task EnsurePlatformTenantAsync(TakeoutAppDbContext dbContext, CancellationToken cancellationToken)
private async Task EnsureSystemTenantAsync(TakeoutAppDbContext dbContext, CancellationToken cancellationToken)
{
var existingTenant = await dbContext.Tenants
.IgnoreQueryFilters()
@@ -142,20 +142,47 @@ public sealed class AppDataSeeder(
if (existingTenant != null)
{
// 1. (空行后) 若历史数据仍为 PLATFORM则自动修正为 SYSTEM
var updated = false;
if (!string.Equals(existingTenant.Code, "SYSTEM", StringComparison.Ordinal))
{
existingTenant.Code = "SYSTEM";
updated = true;
}
if (!string.Equals(existingTenant.Name, "System", StringComparison.Ordinal))
{
existingTenant.Name = "System";
updated = true;
}
if (existingTenant.Status != TenantStatus.Active)
{
existingTenant.Status = TenantStatus.Active;
updated = true;
}
if (updated)
{
dbContext.Tenants.Update(existingTenant);
await dbContext.SaveChangesAsync(cancellationToken);
logger.LogInformation("AppSeed 已更新系统租户 SYSTEM");
}
return;
}
var tenant = new Tenant
{
Id = 0,
Code = "PLATFORM",
Name = "Platform",
Code = "SYSTEM",
Name = "System",
Status = TenantStatus.Active
};
await dbContext.Tenants.AddAsync(tenant, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
logger.LogInformation("AppSeed 已创建平台租户 PLATFORM");
logger.LogInformation("AppSeed 已创建系统租户 SYSTEM");
}
/// <summary>