chore: 提交现有修改
This commit is contained in:
@@ -16,42 +16,34 @@ namespace TakeoutSaaS.Infrastructure.App.Persistence;
|
||||
/// <summary>
|
||||
/// 业务数据种子,确保默认租户与基础字典可重复执行。
|
||||
/// </summary>
|
||||
public sealed class AppDataSeeder : IHostedService
|
||||
/// <remarks>
|
||||
/// 初始化种子服务。
|
||||
/// </remarks>
|
||||
public sealed class AppDataSeeder(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<AppDataSeeder> logger,
|
||||
IOptions<AppSeedOptions> options) : IHostedService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ILogger<AppDataSeeder> _logger;
|
||||
private readonly AppSeedOptions _options;
|
||||
private readonly AppSeedOptions _options = options.Value;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化种子服务。
|
||||
/// </summary>
|
||||
public AppDataSeeder(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<AppDataSeeder> logger,
|
||||
IOptions<AppSeedOptions> options)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_logger = logger;
|
||||
_options = options.Value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (!_options.Enabled)
|
||||
{
|
||||
_logger.LogInformation("AppSeed 未启用,跳过业务数据初始化");
|
||||
logger.LogInformation("AppSeed 未启用,跳过业务数据初始化");
|
||||
return;
|
||||
}
|
||||
|
||||
using var scope = _serviceProvider.CreateScope();
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var appDbContext = scope.ServiceProvider.GetRequiredService<TakeoutAppDbContext>();
|
||||
var dictionaryDbContext = scope.ServiceProvider.GetRequiredService<DictionaryDbContext>();
|
||||
|
||||
var defaultTenantId = await EnsureDefaultTenantAsync(appDbContext, cancellationToken);
|
||||
await EnsureDictionarySeedsAsync(dictionaryDbContext, defaultTenantId, cancellationToken);
|
||||
|
||||
_logger.LogInformation("AppSeed 完成业务数据初始化");
|
||||
logger.LogInformation("AppSeed 完成业务数据初始化");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -65,7 +57,7 @@ public sealed class AppDataSeeder : IHostedService
|
||||
var tenantOptions = _options.DefaultTenant;
|
||||
if (tenantOptions == null || string.IsNullOrWhiteSpace(tenantOptions.Code) || string.IsNullOrWhiteSpace(tenantOptions.Name))
|
||||
{
|
||||
_logger.LogInformation("AppSeed 未配置默认租户,跳过租户种子");
|
||||
logger.LogInformation("AppSeed 未配置默认租户,跳过租户种子");
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -89,7 +81,7 @@ public sealed class AppDataSeeder : IHostedService
|
||||
|
||||
await dbContext.Tenants.AddAsync(tenant, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
_logger.LogInformation("AppSeed 已创建默认租户 {TenantCode}", code);
|
||||
logger.LogInformation("AppSeed 已创建默认租户 {TenantCode}", code);
|
||||
return tenant.Id;
|
||||
}
|
||||
|
||||
@@ -129,11 +121,11 @@ public sealed class AppDataSeeder : IHostedService
|
||||
{
|
||||
dbContext.Tenants.Update(existingTenant);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
_logger.LogInformation("AppSeed 已更新默认租户 {TenantCode}", code);
|
||||
logger.LogInformation("AppSeed 已更新默认租户 {TenantCode}", code);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("AppSeed 默认租户 {TenantCode} 已存在且无需更新", code);
|
||||
logger.LogInformation("AppSeed 默认租户 {TenantCode} 已存在且无需更新", code);
|
||||
}
|
||||
|
||||
return existingTenant.Id;
|
||||
@@ -149,7 +141,7 @@ public sealed class AppDataSeeder : IHostedService
|
||||
|
||||
if (!hasDictionaryGroups)
|
||||
{
|
||||
_logger.LogInformation("AppSeed 未配置基础字典,跳过字典种子");
|
||||
logger.LogInformation("AppSeed 未配置基础字典,跳过字典种子");
|
||||
}
|
||||
|
||||
if (hasDictionaryGroups)
|
||||
@@ -158,7 +150,7 @@ public sealed class AppDataSeeder : IHostedService
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(groupOptions.Code) || string.IsNullOrWhiteSpace(groupOptions.Name))
|
||||
{
|
||||
_logger.LogWarning("AppSeed 跳过字典分组,Code 或 Name 为空");
|
||||
logger.LogWarning("AppSeed 跳过字典分组,Code 或 Name 为空");
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -183,7 +175,7 @@ public sealed class AppDataSeeder : IHostedService
|
||||
};
|
||||
|
||||
await dbContext.DictionaryGroups.AddAsync(group, cancellationToken);
|
||||
_logger.LogInformation("AppSeed 创建字典分组 {GroupCode} (Tenant: {TenantId})", code, tenantId);
|
||||
logger.LogInformation("AppSeed 创建字典分组 {GroupCode} (Tenant: {TenantId})", code, tenantId);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -236,7 +228,7 @@ public sealed class AppDataSeeder : IHostedService
|
||||
|
||||
if (systemParameters.Count == 0)
|
||||
{
|
||||
_logger.LogInformation("AppSeed 未配置系统参数,跳过系统参数种子");
|
||||
logger.LogInformation("AppSeed 未配置系统参数,跳过系统参数种子");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -246,7 +238,7 @@ public sealed class AppDataSeeder : IHostedService
|
||||
|
||||
if (!grouped.Any())
|
||||
{
|
||||
_logger.LogInformation("AppSeed 系统参数配置为空,跳过系统参数种子");
|
||||
logger.LogInformation("AppSeed 系统参数配置为空,跳过系统参数种子");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -271,7 +263,7 @@ public sealed class AppDataSeeder : IHostedService
|
||||
};
|
||||
|
||||
await dbContext.DictionaryGroups.AddAsync(dictionaryGroup, cancellationToken);
|
||||
_logger.LogInformation("AppSeed 创建系统参数分组 (Tenant: {TenantId})", tenantId);
|
||||
logger.LogInformation("AppSeed 创建系统参数分组 (Tenant: {TenantId})", tenantId);
|
||||
}
|
||||
|
||||
var seedItems = group.Select(x => new DictionarySeedItemOptions
|
||||
|
||||
Reference in New Issue
Block a user