fix: 统一逐租户上下文执行

This commit is contained in:
root
2026-01-30 01:04:51 +00:00
parent 41cfd2e2e8
commit cf697b3889
29 changed files with 1037 additions and 490 deletions

View File

@@ -3,6 +3,7 @@ using TakeoutSaaS.Application.App.Billings.Commands;
using TakeoutSaaS.Domain.Tenants.Repositories;
using TakeoutSaaS.Shared.Abstractions.Constants;
using TakeoutSaaS.Shared.Abstractions.Exceptions;
using TakeoutSaaS.Shared.Abstractions.Tenancy;
namespace TakeoutSaaS.Application.App.Billings.Handlers;
@@ -10,7 +11,8 @@ namespace TakeoutSaaS.Application.App.Billings.Handlers;
/// 批量更新账单状态处理器。
/// </summary>
public sealed class BatchUpdateStatusCommandHandler(
ITenantBillingRepository billingRepository)
ITenantBillingRepository billingRepository,
ITenantContextAccessor tenantContextAccessor)
: IRequestHandler<BatchUpdateStatusCommand, int>
{
/// <summary>
@@ -34,33 +36,53 @@ public sealed class BatchUpdateStatusCommandHandler(
throw new BusinessException(ErrorCodes.NotFound, "未找到任何匹配的账单");
}
// 3. 批量更新状态
// 3. 批量更新状态(逐租户上下文执行,避免跨租户写入)
var now = DateTime.UtcNow;
var updatedCount = 0;
foreach (var billing in billings)
var currentTenantId = tenantContextAccessor.Current?.TenantId ?? 0;
if (currentTenantId != 0 && billings.Any(x => x.TenantId != currentTenantId))
{
// 业务规则检查:某些状态转换可能不允许
if (CanTransitionStatus(billing.Status, request.NewStatus))
{
billing.Status = request.NewStatus;
billing.UpdatedAt = now;
throw new BusinessException(ErrorCodes.Forbidden, "禁止跨租户批量更新账单状态");
}
if (!string.IsNullOrWhiteSpace(request.Notes))
var updatedTotal = 0;
var grouped = billings.GroupBy(x => x.TenantId).ToList();
foreach (var group in grouped)
{
using (currentTenantId == 0 ? tenantContextAccessor.EnterTenantScope(group.Key, "billing:batch-update") : null)
{
var updatedCount = 0;
foreach (var billing in group)
{
billing.Notes = string.IsNullOrWhiteSpace(billing.Notes)
? $"[批量操作] {request.Notes}"
: $"{billing.Notes}\n[批量操作] {request.Notes}";
// 业务规则检查:某些状态转换可能不允许
if (!CanTransitionStatus(billing.Status, request.NewStatus))
{
continue;
}
billing.Status = request.NewStatus;
billing.UpdatedAt = now;
if (!string.IsNullOrWhiteSpace(request.Notes))
{
billing.Notes = string.IsNullOrWhiteSpace(billing.Notes)
? $"[批量操作] {request.Notes}"
: $"{billing.Notes}\n[批量操作] {request.Notes}";
}
await billingRepository.UpdateAsync(billing, cancellationToken);
updatedCount++;
}
await billingRepository.UpdateAsync(billing, cancellationToken);
updatedCount++;
if (updatedCount > 0)
{
await billingRepository.SaveChangesAsync(cancellationToken);
}
updatedTotal += updatedCount;
}
}
// 4. 持久化变更
await billingRepository.SaveChangesAsync(cancellationToken);
return updatedCount;
return updatedTotal;
}
/// <summary>