fix: 修复公告模块核心问题并完善功能

主要修复内容:
1. 修复 RowVersion 并发控制
   - 配置 EF Core RowVersion 映射为 bytea 类型
   - 添加 PostgreSQL 触发器自动生成 RowVersion
   - 在更新/发布/撤销操作中添加 RowVersion 校验
   - 移除 Application 层对 EF Core 的直接依赖

2. 修复 API 路由和校验问题
   - 添加平台公告列表路由的版本化别名
   - 租户公告接口添加 X-Tenant-Id 必填校验,返回 400
   - 生效时间校验返回 422 而非 500
   - 修复 FluentValidation 异常命名冲突

3. 实现关键词搜索功能
   - 在查询参数中添加 keyword 字段
   - 使用 PostgreSQL ILIKE 实现大小写不敏感搜索
   - 支持标题和内容字段的模糊匹配

4. 数据库迁移
   - 新增 RowVersion 触发器迁移文件
   - 回填现有公告记录的 RowVersion
This commit is contained in:
2025-12-26 09:08:30 +08:00
parent 857f776447
commit 755b61a044
15 changed files with 178 additions and 9 deletions

View File

@@ -807,10 +807,13 @@ public sealed class TakeoutAppDbContext(
builder.Property(x => x.TenantId).IsRequired();
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.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.Status)
.HasConversion<int>();
builder.Property(x => x.PublishedAt);
builder.Property(x => x.RevokedAt);
builder.Property(x => x.ScheduledPublishAt);
@@ -819,7 +822,9 @@ public sealed class TakeoutAppDbContext(
builder.Property(x => x.Priority).IsRequired();
builder.Property(x => x.IsActive).IsRequired();
builder.Property(x => x.RowVersion)
.IsConcurrencyToken();
.IsRowVersion()
.IsConcurrencyToken()
.HasColumnType("bytea");
ConfigureAuditableEntity(builder);
ConfigureSoftDeleteEntity(builder);
builder.HasIndex(x => new { x.TenantId, x.AnnouncementType, x.IsActive });

View File

@@ -14,6 +14,7 @@ public sealed class EfTenantAnnouncementRepository(TakeoutAppDbContext context)
/// <inheritdoc />
public async Task<IReadOnlyList<TenantAnnouncement>> SearchAsync(
long tenantId,
string? keyword,
AnnouncementStatus? status,
TenantAnnouncementType? type,
bool? isActive,
@@ -29,6 +30,14 @@ public sealed class EfTenantAnnouncementRepository(TakeoutAppDbContext context)
.IgnoreQueryFilters()
.Where(x => tenantIds.Contains(x.TenantId));
if (!string.IsNullOrWhiteSpace(keyword))
{
var normalized = keyword.Trim();
query = query.Where(x =>
EF.Functions.ILike(x.Title, $"%{normalized}%")
|| EF.Functions.ILike(x.Content, $"%{normalized}%"));
}
if (status.HasValue)
{
query = query.Where(x => x.Status == status.Value);

View File

@@ -0,0 +1,50 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace TakeoutSaaS.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddTenantAnnouncementRowVersionTrigger : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(
"""
CREATE OR REPLACE FUNCTION public.set_tenant_announcement_row_version()
RETURNS trigger AS $$
BEGIN
NEW."RowVersion" = decode(md5(random()::text || clock_timestamp()::text), 'hex');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
""");
migrationBuilder.Sql(
"""
DROP TRIGGER IF EXISTS trg_tenant_announcements_row_version ON tenant_announcements;
CREATE TRIGGER trg_tenant_announcements_row_version
BEFORE INSERT OR UPDATE ON tenant_announcements
FOR EACH ROW EXECUTE FUNCTION public.set_tenant_announcement_row_version();
""");
migrationBuilder.Sql(
"""
UPDATE tenant_announcements
SET "RowVersion" = decode(md5(random()::text || clock_timestamp()::text), 'hex')
WHERE "RowVersion" IS NULL OR octet_length("RowVersion") = 0;
""");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(
"""
DROP TRIGGER IF EXISTS trg_tenant_announcements_row_version ON tenant_announcements;
DROP FUNCTION IF EXISTS public.set_tenant_announcement_row_version();
""");
}
}
}