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

@@ -1,5 +1,6 @@
using FluentValidation;
using MediatR;
using TakeoutSaaS.Shared.Abstractions.Exceptions;
namespace TakeoutSaaS.Application.App.Common.Behaviors;
@@ -26,7 +27,16 @@ public sealed class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidat
if (failures.Count > 0)
{
throw new ValidationException(failures);
var errors = failures
.GroupBy(f => string.IsNullOrWhiteSpace(f.PropertyName) ? "Request" : f.PropertyName)
.ToDictionary(
group => group.Key,
group => group
.Select(f => string.IsNullOrWhiteSpace(f.ErrorMessage) ? "Invalid" : f.ErrorMessage)
.Distinct()
.ToArray());
throw new TakeoutSaaS.Shared.Abstractions.Exceptions.ValidationException(errors);
}
}

View File

@@ -44,6 +44,7 @@ public sealed class GetTenantsAnnouncementsQueryHandler(
// 1. 优化的数据库查询:应用排序和限制
var announcements = await announcementRepository.SearchAsync(
tenantId,
request.Keyword,
request.Status,
request.AnnouncementType,
request.IsActive,

View File

@@ -23,6 +23,11 @@ public sealed class PublishAnnouncementCommandHandler(
/// <inheritdoc />
public async Task<TenantAnnouncementDto?> Handle(PublishAnnouncementCommand request, CancellationToken cancellationToken)
{
if (request.RowVersion == null || request.RowVersion.Length == 0)
{
throw new BusinessException(ErrorCodes.ValidationFailed, "RowVersion 不能为空");
}
// 1. 查询公告
var tenantId = tenantProvider.GetCurrentTenantId();
var announcement = await announcementRepository.FindByIdAsync(tenantId, request.AnnouncementId, cancellationToken);

View File

@@ -23,6 +23,11 @@ public sealed class RevokeAnnouncementCommandHandler(
/// <inheritdoc />
public async Task<TenantAnnouncementDto?> Handle(RevokeAnnouncementCommand request, CancellationToken cancellationToken)
{
if (request.RowVersion == null || request.RowVersion.Length == 0)
{
throw new BusinessException(ErrorCodes.ValidationFailed, "RowVersion 不能为空");
}
// 1. 查询公告
var tenantId = tenantProvider.GetCurrentTenantId();
var announcement = await announcementRepository.FindByIdAsync(tenantId, request.AnnouncementId, cancellationToken);

View File

@@ -22,6 +22,11 @@ public sealed class UpdateTenantAnnouncementCommandHandler(ITenantAnnouncementRe
throw new BusinessException(ErrorCodes.ValidationFailed, "公告标题和内容不能为空");
}
if (request.RowVersion == null || request.RowVersion.Length == 0)
{
throw new BusinessException(ErrorCodes.ValidationFailed, "RowVersion 不能为空");
}
// 2. 查询公告
var announcement = await announcementRepository.FindByIdAsync(request.TenantId, request.AnnouncementId, cancellationToken);
if (announcement == null)

View File

@@ -25,6 +25,11 @@ public sealed record GetTenantsAnnouncementsQuery : IRequest<PagedResult<TenantA
/// </summary>
public AnnouncementStatus? Status { get; init; }
/// <summary>
/// 关键词搜索(标题/内容)。
/// </summary>
public string? Keyword { get; init; }
/// <summary>
/// 是否筛选启用状态。
/// </summary>

View File

@@ -37,6 +37,10 @@ public static class TargetTypeFilter
return normalized switch
{
"ALL" => announcement.TenantId == 0
? ApplyPayloadConstraints(payload, parsed, context, allowEmpty: true)
: announcement.TenantId == context.TenantId
&& ApplyPayloadConstraints(payload, parsed, context, allowEmpty: true),
"ALL_TENANTS" => ApplyPayloadConstraints(payload, parsed, context, allowEmpty: true),
"TENANT_ALL" => announcement.TenantId == context.TenantId
&& ApplyPayloadConstraints(payload, parsed, context, allowEmpty: true),

View File

@@ -28,8 +28,9 @@ public sealed class CreateAnnouncementCommandValidator : AbstractValidator<Creat
.Must(x => x.TenantId != 0 || x.PublisherScope == PublisherScope.Platform)
.WithMessage("TenantId=0 仅允许平台公告");
RuleFor(x => x.EffectiveTo)
.Must((command, effectiveTo) => !effectiveTo.HasValue || command.EffectiveFrom < effectiveTo.Value)
RuleFor(x => x.EffectiveFrom)
.LessThan(x => x.EffectiveTo!.Value)
.When(x => x.EffectiveTo.HasValue)
.WithMessage("生效开始时间必须早于结束时间");
}
}