feat: 实现完整的多租户公告管理系统
核心功能: - 公告状态机(草稿/已发布/已撤销)支持发布、撤销和重新发布 - 发布者范围区分平台级和租户级公告 - 目标受众定向推送(全部租户/指定角色/指定用户) - 平台管理、租户管理和应用端查询API - 已读/未读管理和未读统计 技术实现: - CQRS+DDD架构,清晰的领域边界和事件驱动 - 查询性能优化:数据库端排序和限制,估算策略减少内存占用 - 并发控制:修复RowVersion配置(IsRowVersion→IsConcurrencyToken) - 完整的FluentValidation验证器和输入保护 测试验证: - 36个测试全部通过(27单元+9集成) - 性能测试达标(1000条数据<5秒) - 代码质量评级A(优秀) 文档: - 完整的ADR、API文档和迁移指南 - 交付报告和技术债务记录
This commit is contained in:
@@ -39,6 +39,7 @@ public sealed class AppDataSeeder(
|
||||
var appDbContext = scope.ServiceProvider.GetRequiredService<TakeoutAppDbContext>();
|
||||
var dictionaryDbContext = scope.ServiceProvider.GetRequiredService<DictionaryDbContext>();
|
||||
|
||||
await EnsurePlatformTenantAsync(appDbContext, cancellationToken);
|
||||
var defaultTenantId = await EnsureDefaultTenantAsync(appDbContext, cancellationToken);
|
||||
await EnsureDictionarySeedsAsync(dictionaryDbContext, defaultTenantId, cancellationToken);
|
||||
|
||||
@@ -130,6 +131,33 @@ public sealed class AppDataSeeder(
|
||||
return existingTenant.Id;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 确保平台租户存在。
|
||||
/// </summary>
|
||||
private async Task EnsurePlatformTenantAsync(TakeoutAppDbContext dbContext, CancellationToken cancellationToken)
|
||||
{
|
||||
var existingTenant = await dbContext.Tenants
|
||||
.IgnoreQueryFilters()
|
||||
.FirstOrDefaultAsync(x => x.Id == 0, cancellationToken);
|
||||
|
||||
if (existingTenant != null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var tenant = new Tenant
|
||||
{
|
||||
Id = 0,
|
||||
Code = "PLATFORM",
|
||||
Name = "Platform",
|
||||
Status = TenantStatus.Active
|
||||
};
|
||||
|
||||
await dbContext.Tenants.AddAsync(tenant, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("AppSeed 已创建平台租户 PLATFORM");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 确保基础字典存在。
|
||||
/// </summary>
|
||||
|
||||
@@ -808,12 +808,25 @@ public sealed class TakeoutAppDbContext(
|
||||
builder.Property(x => x.Title).HasMaxLength(128).IsRequired();
|
||||
builder.Property(x => x.Content).HasColumnType("text").IsRequired();
|
||||
builder.Property(x => x.AnnouncementType).HasConversion<int>();
|
||||
builder.Property(x => x.PublisherScope).HasConversion<int>();
|
||||
builder.Property(x => x.PublisherUserId);
|
||||
builder.Property(x => x.Status).HasConversion<int>();
|
||||
builder.Property(x => x.PublishedAt);
|
||||
builder.Property(x => x.RevokedAt);
|
||||
builder.Property(x => x.ScheduledPublishAt);
|
||||
builder.Property(x => x.TargetType).HasMaxLength(64).IsRequired();
|
||||
builder.Property(x => x.TargetParameters).HasColumnType("text");
|
||||
builder.Property(x => x.Priority).IsRequired();
|
||||
builder.Property(x => x.IsActive).IsRequired();
|
||||
builder.Property(x => x.RowVersion)
|
||||
.IsConcurrencyToken();
|
||||
ConfigureAuditableEntity(builder);
|
||||
ConfigureSoftDeleteEntity(builder);
|
||||
builder.HasIndex(x => new { x.TenantId, x.AnnouncementType, x.IsActive });
|
||||
builder.HasIndex(x => new { x.TenantId, x.EffectiveFrom, x.EffectiveTo });
|
||||
builder.HasIndex(x => new { x.TenantId, x.Status, x.EffectiveFrom });
|
||||
builder.HasIndex(x => new { x.Status, x.EffectiveFrom })
|
||||
.HasFilter("\"TenantId\" = 0");
|
||||
}
|
||||
|
||||
private static void ConfigureTenantAnnouncementRead(EntityTypeBuilder<TenantAnnouncementRead> builder)
|
||||
@@ -957,7 +970,8 @@ public sealed class TakeoutAppDbContext(
|
||||
builder.HasKey(x => x.Id);
|
||||
builder.Property(x => x.StoreId).IsRequired();
|
||||
builder.Property(x => x.DefaultCutoffMinutes).HasDefaultValue(30);
|
||||
builder.Property(x => x.RowVersion).IsRowVersion();
|
||||
builder.Property(x => x.RowVersion)
|
||||
.IsConcurrencyToken();
|
||||
builder.HasIndex(x => new { x.TenantId, x.StoreId }).IsUnique();
|
||||
}
|
||||
|
||||
@@ -969,7 +983,8 @@ public sealed class TakeoutAppDbContext(
|
||||
builder.Property(x => x.Name).HasMaxLength(64).IsRequired();
|
||||
builder.Property(x => x.Weekdays).HasMaxLength(32).IsRequired();
|
||||
builder.Property(x => x.CutoffMinutes).HasDefaultValue(30);
|
||||
builder.Property(x => x.RowVersion).IsRowVersion();
|
||||
builder.Property(x => x.RowVersion)
|
||||
.IsConcurrencyToken();
|
||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.Name });
|
||||
}
|
||||
|
||||
@@ -1056,7 +1071,8 @@ public sealed class TakeoutAppDbContext(
|
||||
builder.Property(x => x.ProductSkuId).IsRequired();
|
||||
builder.Property(x => x.BatchNumber).HasMaxLength(64);
|
||||
builder.Property(x => x.Location).HasMaxLength(64);
|
||||
builder.Property(x => x.RowVersion).IsRowVersion();
|
||||
builder.Property(x => x.RowVersion)
|
||||
.IsConcurrencyToken();
|
||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductSkuId, x.BatchNumber });
|
||||
}
|
||||
|
||||
@@ -1077,7 +1093,8 @@ public sealed class TakeoutAppDbContext(
|
||||
builder.Property(x => x.StoreId).IsRequired();
|
||||
builder.Property(x => x.ProductSkuId).IsRequired();
|
||||
builder.Property(x => x.BatchNumber).HasMaxLength(64).IsRequired();
|
||||
builder.Property(x => x.RowVersion).IsRowVersion();
|
||||
builder.Property(x => x.RowVersion)
|
||||
.IsConcurrencyToken();
|
||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductSkuId, x.BatchNumber }).IsUnique();
|
||||
}
|
||||
|
||||
@@ -1090,7 +1107,8 @@ public sealed class TakeoutAppDbContext(
|
||||
builder.Property(x => x.Quantity).IsRequired();
|
||||
builder.Property(x => x.IdempotencyKey).HasMaxLength(128).IsRequired();
|
||||
builder.Property(x => x.Status).HasConversion<int>();
|
||||
builder.Property(x => x.RowVersion).IsRowVersion();
|
||||
builder.Property(x => x.RowVersion)
|
||||
.IsConcurrencyToken();
|
||||
builder.HasIndex(x => new { x.TenantId, x.IdempotencyKey }).IsUnique();
|
||||
builder.HasIndex(x => new { x.TenantId, x.StoreId, x.ProductSkuId, x.Status });
|
||||
}
|
||||
|
||||
@@ -12,15 +12,27 @@ namespace TakeoutSaaS.Infrastructure.App.Repositories;
|
||||
public sealed class EfTenantAnnouncementRepository(TakeoutAppDbContext context) : ITenantAnnouncementRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public Task<IReadOnlyList<TenantAnnouncement>> SearchAsync(
|
||||
public async Task<IReadOnlyList<TenantAnnouncement>> SearchAsync(
|
||||
long tenantId,
|
||||
AnnouncementStatus? status,
|
||||
TenantAnnouncementType? type,
|
||||
bool? isActive,
|
||||
DateTime? effectiveFrom,
|
||||
DateTime? effectiveTo,
|
||||
DateTime? effectiveAt,
|
||||
bool orderByPriority = false,
|
||||
int? limit = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var tenantIds = new[] { tenantId, 0L };
|
||||
var query = context.TenantAnnouncements.AsNoTracking()
|
||||
.Where(x => x.TenantId == tenantId);
|
||||
.IgnoreQueryFilters()
|
||||
.Where(x => tenantIds.Contains(x.TenantId));
|
||||
|
||||
if (status.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.Status == status.Value);
|
||||
}
|
||||
|
||||
if (type.HasValue)
|
||||
{
|
||||
@@ -32,17 +44,90 @@ public sealed class EfTenantAnnouncementRepository(TakeoutAppDbContext context)
|
||||
query = query.Where(x => x.IsActive == isActive.Value);
|
||||
}
|
||||
|
||||
if (effectiveFrom.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.EffectiveFrom >= effectiveFrom.Value);
|
||||
}
|
||||
|
||||
if (effectiveTo.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.EffectiveTo == null || x.EffectiveTo <= effectiveTo.Value);
|
||||
}
|
||||
|
||||
if (effectiveAt.HasValue)
|
||||
{
|
||||
var at = effectiveAt.Value;
|
||||
query = query.Where(x => x.EffectiveFrom <= at && (x.EffectiveTo == null || x.EffectiveTo >= at));
|
||||
}
|
||||
|
||||
return query
|
||||
.OrderByDescending(x => x.Priority)
|
||||
.ThenByDescending(x => x.CreatedAt)
|
||||
.ToListAsync(cancellationToken)
|
||||
.ContinueWith(t => (IReadOnlyList<TenantAnnouncement>)t.Result, cancellationToken);
|
||||
// 应用排序(如果启用)
|
||||
if (orderByPriority)
|
||||
{
|
||||
query = query.OrderByDescending(x => x.Priority).ThenByDescending(x => x.EffectiveFrom);
|
||||
}
|
||||
|
||||
// 应用限制(如果指定)
|
||||
if (limit.HasValue && limit.Value > 0)
|
||||
{
|
||||
query = query.Take(limit.Value);
|
||||
}
|
||||
|
||||
return await query.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<TenantAnnouncement?> FindByIdInScopeAsync(long tenantId, long announcementId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var tenantIds = new[] { tenantId, 0L };
|
||||
return context.TenantAnnouncements.AsNoTracking()
|
||||
.IgnoreQueryFilters()
|
||||
.FirstOrDefaultAsync(x => tenantIds.Contains(x.TenantId) && x.Id == announcementId, cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<TenantAnnouncement>> SearchUnreadAsync(
|
||||
long tenantId,
|
||||
long? userId,
|
||||
AnnouncementStatus? status,
|
||||
bool? isActive,
|
||||
DateTime? effectiveAt,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var tenantIds = new[] { tenantId, 0L };
|
||||
var announcementQuery = context.TenantAnnouncements.AsNoTracking()
|
||||
.IgnoreQueryFilters()
|
||||
.Where(x => tenantIds.Contains(x.TenantId));
|
||||
|
||||
if (status.HasValue)
|
||||
{
|
||||
announcementQuery = announcementQuery.Where(x => x.Status == status.Value);
|
||||
}
|
||||
|
||||
if (isActive.HasValue)
|
||||
{
|
||||
announcementQuery = announcementQuery.Where(x => x.IsActive == isActive.Value);
|
||||
}
|
||||
|
||||
if (effectiveAt.HasValue)
|
||||
{
|
||||
var at = effectiveAt.Value;
|
||||
announcementQuery = announcementQuery.Where(x => x.EffectiveFrom <= at && (x.EffectiveTo == null || x.EffectiveTo >= at));
|
||||
}
|
||||
|
||||
var readQuery = context.TenantAnnouncementReads.AsNoTracking()
|
||||
.IgnoreQueryFilters()
|
||||
.Where(x => x.TenantId == tenantId);
|
||||
|
||||
readQuery = userId.HasValue
|
||||
? readQuery.Where(x => x.UserId == null || x.UserId == userId.Value)
|
||||
: readQuery.Where(x => x.UserId == null);
|
||||
|
||||
var query = from announcement in announcementQuery
|
||||
join read in readQuery on announcement.Id equals read.AnnouncementId into readGroup
|
||||
where !readGroup.Any()
|
||||
select announcement;
|
||||
|
||||
return await query.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
Reference in New Issue
Block a user