feat: 系统参数独立表与迁移
This commit is contained in:
@@ -6,6 +6,7 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using TakeoutSaaS.Domain.Dictionary.Entities;
|
||||
using TakeoutSaaS.Domain.Dictionary.Enums;
|
||||
using TakeoutSaaS.Domain.SystemParameters.Entities;
|
||||
using TakeoutSaaS.Domain.Tenants.Entities;
|
||||
using TakeoutSaaS.Domain.Tenants.Enums;
|
||||
using TakeoutSaaS.Infrastructure.App.Options;
|
||||
@@ -219,8 +220,9 @@ public sealed class AppDataSeeder(
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 确保系统参数以字典形式幂等种子。
|
||||
/// 确保系统参数以独立表形式可重复种子。
|
||||
/// </summary>
|
||||
private async Task EnsureSystemParametersAsync(DictionaryDbContext dbContext, long? defaultTenantId, CancellationToken cancellationToken)
|
||||
{
|
||||
@@ -245,37 +247,64 @@ public sealed class AppDataSeeder(
|
||||
foreach (var group in grouped)
|
||||
{
|
||||
var tenantId = group.Key;
|
||||
var dictionaryGroup = await dbContext.DictionaryGroups
|
||||
var existingParameters = await dbContext.SystemParameters
|
||||
.IgnoreQueryFilters()
|
||||
.FirstOrDefaultAsync(x => x.TenantId == tenantId && x.Code == "system_parameters", cancellationToken);
|
||||
.Where(x => x.TenantId == tenantId)
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
if (dictionaryGroup == null)
|
||||
foreach (var seed in group)
|
||||
{
|
||||
dictionaryGroup = new DictionaryGroup
|
||||
var key = seed.Key.Trim();
|
||||
var existing = existingParameters.FirstOrDefault(x => x.Key.Equals(key, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (existing == null)
|
||||
{
|
||||
Id = 0,
|
||||
TenantId = tenantId,
|
||||
Code = "system_parameters",
|
||||
Name = "系统参数",
|
||||
Scope = tenantId == 0 ? DictionaryScope.System : DictionaryScope.Business,
|
||||
Description = "系统参数配置",
|
||||
IsEnabled = true
|
||||
};
|
||||
var parameter = new SystemParameter
|
||||
{
|
||||
Id = 0,
|
||||
TenantId = tenantId,
|
||||
Key = key,
|
||||
Value = seed.Value.Trim(),
|
||||
Description = seed.Description?.Trim(),
|
||||
SortOrder = seed.SortOrder,
|
||||
IsEnabled = seed.IsEnabled
|
||||
};
|
||||
|
||||
await dbContext.DictionaryGroups.AddAsync(dictionaryGroup, cancellationToken);
|
||||
logger.LogInformation("AppSeed 创建系统参数分组 (Tenant: {TenantId})", tenantId);
|
||||
await dbContext.SystemParameters.AddAsync(parameter, cancellationToken);
|
||||
continue;
|
||||
}
|
||||
|
||||
var updated = false;
|
||||
|
||||
if (!string.Equals(existing.Value, seed.Value, StringComparison.Ordinal))
|
||||
{
|
||||
existing.Value = seed.Value.Trim();
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (!string.Equals(existing.Description, seed.Description, StringComparison.Ordinal))
|
||||
{
|
||||
existing.Description = seed.Description?.Trim();
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (existing.SortOrder != seed.SortOrder)
|
||||
{
|
||||
existing.SortOrder = seed.SortOrder;
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (existing.IsEnabled != seed.IsEnabled)
|
||||
{
|
||||
existing.IsEnabled = seed.IsEnabled;
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (updated)
|
||||
{
|
||||
dbContext.SystemParameters.Update(existing);
|
||||
}
|
||||
}
|
||||
|
||||
var seedItems = group.Select(x => new DictionarySeedItemOptions
|
||||
{
|
||||
Key = x.Key.Trim(),
|
||||
Value = x.Value.Trim(),
|
||||
Description = x.Description?.Trim(),
|
||||
SortOrder = x.SortOrder,
|
||||
IsEnabled = x.IsEnabled
|
||||
});
|
||||
|
||||
await UpsertDictionaryItemsAsync(dbContext, dictionaryGroup, seedItems, tenantId, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user