diff --git a/src/Api/TakeoutSaaS.AdminApi/Controllers/PlatformAnnouncementsController.cs b/src/Api/TakeoutSaaS.AdminApi/Controllers/PlatformAnnouncementsController.cs index 0c12f90..e2af709 100644 --- a/src/Api/TakeoutSaaS.AdminApi/Controllers/PlatformAnnouncementsController.cs +++ b/src/Api/TakeoutSaaS.AdminApi/Controllers/PlatformAnnouncementsController.cs @@ -94,8 +94,8 @@ public sealed class PlatformAnnouncementsController(IMediator mediator, ITenantC /// /// [HttpGet] - [PermissionAuthorize("platform-announcement:create")] - [SwaggerOperation(Summary = "查询平台公告列表", Description = "需要权限:platform-announcement:create")] + [PermissionAuthorize("platform-announcement:read", "platform-announcement:create")] + [SwaggerOperation(Summary = "查询平台公告列表", Description = "需要权限:platform-announcement:read 或 platform-announcement:create")] [ProducesResponseType(typeof(ApiResponse>), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status403Forbidden)] public async Task>> List([FromQuery] GetTenantsAnnouncementsQuery query, CancellationToken cancellationToken) @@ -126,8 +126,8 @@ public sealed class PlatformAnnouncementsController(IMediator mediator, ITenantC /// /// [HttpGet("{announcementId:long}")] - [PermissionAuthorize("platform-announcement:create")] - [SwaggerOperation(Summary = "获取平台公告详情", Description = "需要权限:platform-announcement:create")] + [PermissionAuthorize("platform-announcement:read", "platform-announcement:create")] + [SwaggerOperation(Summary = "获取平台公告详情", Description = "需要权限:platform-announcement:read 或 platform-announcement:create")] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status403Forbidden)] diff --git a/src/Api/TakeoutSaaS.AdminApi/Controllers/TenantAnnouncementsController.cs b/src/Api/TakeoutSaaS.AdminApi/Controllers/TenantAnnouncementsController.cs index 59f47e7..df6cf1b 100644 --- a/src/Api/TakeoutSaaS.AdminApi/Controllers/TenantAnnouncementsController.cs +++ b/src/Api/TakeoutSaaS.AdminApi/Controllers/TenantAnnouncementsController.cs @@ -8,6 +8,7 @@ using TakeoutSaaS.Application.App.Tenants.Dto; using TakeoutSaaS.Application.App.Tenants.Queries; using TakeoutSaaS.Module.Authorization.Attributes; using TakeoutSaaS.Shared.Abstractions.Results; +using TakeoutSaaS.Shared.Abstractions.Tenancy; using TakeoutSaaS.Shared.Web.Api; namespace TakeoutSaaS.AdminApi.Controllers; @@ -18,7 +19,7 @@ namespace TakeoutSaaS.AdminApi.Controllers; [ApiVersion("1.0")] [Authorize] [Route("api/admin/v{version:apiVersion}/tenants/{tenantId:long}/announcements")] -public sealed class TenantAnnouncementsController(IMediator mediator) : BaseApiController +public sealed class TenantAnnouncementsController(IMediator mediator, ITenantContextAccessor tenantContextAccessor) : BaseApiController { private const string TenantIdHeaderName = "X-Tenant-Id"; @@ -49,6 +50,13 @@ public sealed class TenantAnnouncementsController(IMediator mediator) : BaseApiC [ProducesResponseType(typeof(ApiResponse), StatusCodes.Status403Forbidden)] public async Task>> Search(long tenantId, [FromQuery] GetTenantsAnnouncementsQuery query, CancellationToken cancellationToken) { + if (!Request.Headers.TryGetValue(TenantIdHeaderName, out var tenantHeader) || string.IsNullOrWhiteSpace(tenantHeader)) + { + var request = query with { TenantId = 0 }; + var platformResult = await ExecuteAsPlatformAsync(() => mediator.Send(request, cancellationToken)); + return ApiResponse>.Ok(platformResult); + } + var headerError = EnsureTenantHeader>(); if (headerError != null) { @@ -369,4 +377,18 @@ public sealed class TenantAnnouncementsController(IMediator mediator) : BaseApiC return null; } + + private async Task ExecuteAsPlatformAsync(Func> action) + { + var original = tenantContextAccessor.Current; + tenantContextAccessor.Current = new TenantContext(0, null, "platform"); + try + { + return await action(); + } + finally + { + tenantContextAccessor.Current = original; + } + } } diff --git a/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/CreateTenantAnnouncementCommandHandler.cs b/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/CreateTenantAnnouncementCommandHandler.cs index 9a0385c..83ae3a1 100644 --- a/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/CreateTenantAnnouncementCommandHandler.cs +++ b/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/CreateTenantAnnouncementCommandHandler.cs @@ -37,6 +37,11 @@ public sealed class CreateTenantAnnouncementCommandHandler( throw new BusinessException(ErrorCodes.ValidationFailed, "目标受众类型不能为空"); } + if (request.EffectiveTo.HasValue && request.EffectiveFrom >= request.EffectiveTo.Value) + { + throw new BusinessException(ErrorCodes.ValidationFailed, "生效开始时间必须早于结束时间"); + } + if (request.TenantId == 0 && request.PublisherScope != PublisherScope.Platform) { throw new BusinessException(ErrorCodes.ValidationFailed, "TenantId=0 仅允许平台公告"); diff --git a/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/PublishAnnouncementCommandHandler.cs b/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/PublishAnnouncementCommandHandler.cs index e3b75f3..1162527 100644 --- a/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/PublishAnnouncementCommandHandler.cs +++ b/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/PublishAnnouncementCommandHandler.cs @@ -58,8 +58,15 @@ public sealed class PublishAnnouncementCommandHandler( announcement.RevokedAt = null; announcement.RowVersion = request.RowVersion; - await announcementRepository.UpdateAsync(announcement, cancellationToken); - await announcementRepository.SaveChangesAsync(cancellationToken); + try + { + await announcementRepository.UpdateAsync(announcement, cancellationToken); + await announcementRepository.SaveChangesAsync(cancellationToken); + } + catch (Exception exception) when (exception.GetType().Name == "DbUpdateConcurrencyException") + { + throw new BusinessException(ErrorCodes.Conflict, "公告已被修改,请刷新后重试"); + } // 4. 发布领域事件 await eventPublisher.PublishAsync( diff --git a/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/RevokeAnnouncementCommandHandler.cs b/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/RevokeAnnouncementCommandHandler.cs index 1e4d3bf..01a57d5 100644 --- a/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/RevokeAnnouncementCommandHandler.cs +++ b/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/RevokeAnnouncementCommandHandler.cs @@ -52,8 +52,15 @@ public sealed class RevokeAnnouncementCommandHandler( announcement.RevokedAt = DateTime.UtcNow; announcement.RowVersion = request.RowVersion; - await announcementRepository.UpdateAsync(announcement, cancellationToken); - await announcementRepository.SaveChangesAsync(cancellationToken); + try + { + await announcementRepository.UpdateAsync(announcement, cancellationToken); + await announcementRepository.SaveChangesAsync(cancellationToken); + } + catch (Exception exception) when (exception.GetType().Name == "DbUpdateConcurrencyException") + { + throw new BusinessException(ErrorCodes.Conflict, "公告已被修改,请刷新后重试"); + } // 4. 发布领域事件 await eventPublisher.PublishAsync( diff --git a/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/UpdateTenantAnnouncementCommandHandler.cs b/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/UpdateTenantAnnouncementCommandHandler.cs index 0a661a0..ed19111 100644 --- a/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/UpdateTenantAnnouncementCommandHandler.cs +++ b/src/Application/TakeoutSaaS.Application/App/Tenants/Handlers/UpdateTenantAnnouncementCommandHandler.cs @@ -52,8 +52,15 @@ public sealed class UpdateTenantAnnouncementCommandHandler(ITenantAnnouncementRe announcement.RowVersion = request.RowVersion; // 4. 持久化 - await announcementRepository.UpdateAsync(announcement, cancellationToken); - await announcementRepository.SaveChangesAsync(cancellationToken); + try + { + await announcementRepository.UpdateAsync(announcement, cancellationToken); + await announcementRepository.SaveChangesAsync(cancellationToken); + } + catch (Exception exception) when (exception.GetType().Name == "DbUpdateConcurrencyException") + { + throw new BusinessException(ErrorCodes.Conflict, "公告已被修改,请刷新后重试"); + } // 5. 返回 DTO return announcement.ToDto(false, null); diff --git a/src/Infrastructure/TakeoutSaaS.Infrastructure/Migrations/20251225090000_AddTenantAnnouncementRowVersionTrigger.cs b/src/Infrastructure/TakeoutSaaS.Infrastructure/Migrations/20251225090000_AddTenantAnnouncementRowVersionTrigger.cs index 444d637..8dfaf44 100644 --- a/src/Infrastructure/TakeoutSaaS.Infrastructure/Migrations/20251225090000_AddTenantAnnouncementRowVersionTrigger.cs +++ b/src/Infrastructure/TakeoutSaaS.Infrastructure/Migrations/20251225090000_AddTenantAnnouncementRowVersionTrigger.cs @@ -1,10 +1,14 @@ +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; +using TakeoutSaaS.Infrastructure.App.Persistence; #nullable disable namespace TakeoutSaaS.Infrastructure.Migrations { /// + [DbContext(typeof(TakeoutAppDbContext))] + [Migration("20251225090000_AddTenantAnnouncementRowVersionTrigger")] public partial class AddTenantAnnouncementRowVersionTrigger : Migration { ///