feat: 系统参数独立表与迁移

This commit is contained in:
2025-12-02 12:35:33 +08:00
parent 5d1dd54c80
commit dd15a88ff4
6 changed files with 545 additions and 29 deletions

View File

@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using TakeoutSaaS.Domain.Dictionary.Entities;
using TakeoutSaaS.Domain.SystemParameters.Entities;
using TakeoutSaaS.Infrastructure.Common.Persistence;
using TakeoutSaaS.Shared.Abstractions.Ids;
using TakeoutSaaS.Shared.Abstractions.Security;
@@ -9,7 +10,7 @@ using TakeoutSaaS.Shared.Abstractions.Tenancy;
namespace TakeoutSaaS.Infrastructure.Dictionary.Persistence;
/// <summary>
/// 参数字典 DbContext。
/// 参数字典 DbContext:承载字典与系统参数
/// </summary>
public sealed class DictionaryDbContext(
DbContextOptions<DictionaryDbContext> options,
@@ -19,15 +20,20 @@ public sealed class DictionaryDbContext(
: TenantAwareDbContext(options, tenantProvider, currentUserAccessor, idGenerator)
{
/// <summary>
/// 字典分组集。
/// 字典分组集
/// </summary>
public DbSet<DictionaryGroup> DictionaryGroups => Set<DictionaryGroup>();
/// <summary>
/// 字典项集。
/// 字典项集
/// </summary>
public DbSet<DictionaryItem> DictionaryItems => Set<DictionaryItem>();
/// <summary>
/// 系统参数集合。
/// </summary>
public DbSet<SystemParameter> SystemParameters => Set<SystemParameter>();
/// <summary>
/// 配置实体模型。
/// </summary>
@@ -37,6 +43,7 @@ public sealed class DictionaryDbContext(
base.OnModelCreating(modelBuilder);
ConfigureGroup(modelBuilder.Entity<DictionaryGroup>());
ConfigureItem(modelBuilder.Entity<DictionaryItem>());
ConfigureSystemParameter(modelBuilder.Entity<SystemParameter>());
ApplyTenantQueryFilters(modelBuilder);
}
@@ -87,4 +94,25 @@ public sealed class DictionaryDbContext(
builder.HasIndex(x => x.TenantId);
builder.HasIndex(x => new { x.GroupId, x.Key }).IsUnique();
}
/// <summary>
/// 配置系统参数。
/// </summary>
/// <param name="builder">实体构建器。</param>
private static void ConfigureSystemParameter(EntityTypeBuilder<SystemParameter> builder)
{
builder.ToTable("system_parameters");
builder.HasKey(x => x.Id);
builder.Property(x => x.TenantId).IsRequired();
builder.Property(x => x.Key).HasMaxLength(128).IsRequired();
builder.Property(x => x.Value).HasColumnType("text").IsRequired();
builder.Property(x => x.Description).HasMaxLength(512);
builder.Property(x => x.SortOrder).HasDefaultValue(100);
builder.Property(x => x.IsEnabled).HasDefaultValue(true);
ConfigureAuditableEntity(builder);
ConfigureSoftDeleteEntity(builder);
builder.HasIndex(x => x.TenantId);
builder.HasIndex(x => new { x.TenantId, x.Key }).IsUnique();
}
}