fix: 门店时段与临时调整跨租户处理

This commit is contained in:
2026-01-20 20:38:05 +08:00
parent 32f5bbbd43
commit 8bde1a6440
6 changed files with 100 additions and 20 deletions

View File

@@ -1,5 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using TakeoutSaaS.Application.App.Stores;
using TakeoutSaaS.Application.App.Stores.Commands;
using TakeoutSaaS.Application.App.Stores.Dto;
using TakeoutSaaS.Application.App.Stores.Validators;
@@ -17,6 +19,7 @@ namespace TakeoutSaaS.Application.App.Stores.Handlers;
public sealed class BatchUpdateBusinessHoursCommandHandler(
IStoreRepository storeRepository,
ITenantProvider tenantProvider,
IHttpContextAccessor httpContextAccessor,
ILogger<BatchUpdateBusinessHoursCommandHandler> logger)
: IRequestHandler<BatchUpdateBusinessHoursCommand, IReadOnlyList<StoreBusinessHourDto>>
{
@@ -24,12 +27,14 @@ public sealed class BatchUpdateBusinessHoursCommandHandler(
public async Task<IReadOnlyList<StoreBusinessHourDto>> Handle(BatchUpdateBusinessHoursCommand request, CancellationToken cancellationToken)
{
// 1. 校验门店存在
var tenantId = tenantProvider.GetCurrentTenantId();
var ignoreTenantFilter = StoreTenantAccess.ShouldIgnoreTenantFilter(httpContextAccessor);
var tenantId = ignoreTenantFilter ? 0 : tenantProvider.GetCurrentTenantId();
var store = await storeRepository.FindByIdAsync(request.StoreId, tenantId, cancellationToken);
if (store is null)
{
throw new BusinessException(ErrorCodes.NotFound, "门店不存在");
}
var storeTenantId = store.TenantId;
// 2. (空行后) 校验时段重叠
var overlapError = BusinessHourValidators.ValidateOverlap(request.Items);
@@ -39,10 +44,10 @@ public sealed class BatchUpdateBusinessHoursCommandHandler(
}
// 3. (空行后) 删除旧时段
var existingHours = await storeRepository.GetBusinessHoursAsync(request.StoreId, tenantId, cancellationToken);
var existingHours = await storeRepository.GetBusinessHoursAsync(request.StoreId, storeTenantId, cancellationToken);
foreach (var hour in existingHours)
{
await storeRepository.DeleteBusinessHourAsync(hour.Id, tenantId, cancellationToken);
await storeRepository.DeleteBusinessHourAsync(hour.Id, storeTenantId, cancellationToken);
}
// 4. (空行后) 新增时段配置
@@ -50,6 +55,7 @@ public sealed class BatchUpdateBusinessHoursCommandHandler(
{
var hours = request.Items.Select(item => new StoreBusinessHour
{
TenantId = storeTenantId,
StoreId = request.StoreId,
DayOfWeek = item.DayOfWeek,
HourType = item.HourType,
@@ -66,7 +72,7 @@ public sealed class BatchUpdateBusinessHoursCommandHandler(
await storeRepository.SaveChangesAsync(cancellationToken);
logger.LogInformation("批量更新门店营业时段 {StoreId}", request.StoreId);
var refreshed = await storeRepository.GetBusinessHoursAsync(request.StoreId, tenantId, cancellationToken);
var refreshed = await storeRepository.GetBusinessHoursAsync(request.StoreId, storeTenantId, cancellationToken);
return refreshed.Select(StoreMapping.ToDto).ToList();
}
}

View File

@@ -1,5 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using TakeoutSaaS.Application.App.Stores;
using TakeoutSaaS.Application.App.Stores.Commands;
using TakeoutSaaS.Application.App.Stores.Dto;
using TakeoutSaaS.Domain.Stores.Entities;
@@ -17,30 +19,35 @@ namespace TakeoutSaaS.Application.App.Stores.Handlers;
public sealed class CreateStoreHolidayCommandHandler(
IStoreRepository storeRepository,
ITenantProvider tenantProvider,
IHttpContextAccessor httpContextAccessor,
ILogger<CreateStoreHolidayCommandHandler> logger)
: IRequestHandler<CreateStoreHolidayCommand, StoreHolidayDto>
{
private readonly IStoreRepository _storeRepository = storeRepository;
private readonly ITenantProvider _tenantProvider = tenantProvider;
private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;
private readonly ILogger<CreateStoreHolidayCommandHandler> _logger = logger;
/// <inheritdoc />
public async Task<StoreHolidayDto> Handle(CreateStoreHolidayCommand request, CancellationToken cancellationToken)
{
// 1. 校验门店存在
var tenantId = _tenantProvider.GetCurrentTenantId();
var ignoreTenantFilter = StoreTenantAccess.ShouldIgnoreTenantFilter(_httpContextAccessor);
var tenantId = ignoreTenantFilter ? 0 : _tenantProvider.GetCurrentTenantId();
var store = await _storeRepository.FindByIdAsync(request.StoreId, tenantId, cancellationToken);
if (store is null)
{
throw new BusinessException(ErrorCodes.NotFound, "门店不存在");
}
var storeTenantId = store.TenantId;
// 2. 构建实体
var holiday = new StoreHoliday
{
TenantId = storeTenantId,
StoreId = request.StoreId,
Date = request.Date,
EndDate = request.EndDate,
Date = NormalizeToUtc(request.Date),
EndDate = request.EndDate.HasValue ? NormalizeToUtc(request.EndDate.Value) : null,
IsAllDay = request.IsAllDay,
StartTime = request.StartTime,
EndTime = request.EndTime,
@@ -57,4 +64,14 @@ public sealed class CreateStoreHolidayCommandHandler(
// 4. 返回 DTO
return StoreMapping.ToDto(holiday);
}
private static DateTime NormalizeToUtc(DateTime value)
{
return value.Kind switch
{
DateTimeKind.Utc => value,
DateTimeKind.Local => value.ToUniversalTime(),
_ => DateTime.SpecifyKind(value, DateTimeKind.Utc)
};
}
}

View File

@@ -1,5 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using TakeoutSaaS.Application.App.Stores;
using TakeoutSaaS.Application.App.Stores.Commands;
using TakeoutSaaS.Domain.Stores.Repositories;
using TakeoutSaaS.Shared.Abstractions.Tenancy;
@@ -12,18 +14,21 @@ namespace TakeoutSaaS.Application.App.Stores.Handlers;
public sealed class DeleteStoreHolidayCommandHandler(
IStoreRepository storeRepository,
ITenantProvider tenantProvider,
IHttpContextAccessor httpContextAccessor,
ILogger<DeleteStoreHolidayCommandHandler> logger)
: IRequestHandler<DeleteStoreHolidayCommand, bool>
{
private readonly IStoreRepository _storeRepository = storeRepository;
private readonly ITenantProvider _tenantProvider = tenantProvider;
private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;
private readonly ILogger<DeleteStoreHolidayCommandHandler> _logger = logger;
/// <inheritdoc />
public async Task<bool> Handle(DeleteStoreHolidayCommand request, CancellationToken cancellationToken)
{
// 1. 读取配置
var tenantId = _tenantProvider.GetCurrentTenantId();
var ignoreTenantFilter = StoreTenantAccess.ShouldIgnoreTenantFilter(_httpContextAccessor);
var tenantId = ignoreTenantFilter ? 0 : _tenantProvider.GetCurrentTenantId();
var existing = await _storeRepository.FindHolidayByIdAsync(request.HolidayId, tenantId, cancellationToken);
if (existing is null)
{

View File

@@ -1,5 +1,7 @@
using System.Linq;
using MediatR;
using Microsoft.AspNetCore.Http;
using TakeoutSaaS.Application.App.Stores;
using TakeoutSaaS.Application.App.Stores.Dto;
using TakeoutSaaS.Application.App.Stores.Queries;
using TakeoutSaaS.Domain.Stores.Repositories;
@@ -12,17 +14,20 @@ namespace TakeoutSaaS.Application.App.Stores.Handlers;
/// </summary>
public sealed class ListStoreHolidaysQueryHandler(
IStoreRepository storeRepository,
ITenantProvider tenantProvider)
ITenantProvider tenantProvider,
IHttpContextAccessor httpContextAccessor)
: IRequestHandler<ListStoreHolidaysQuery, IReadOnlyList<StoreHolidayDto>>
{
private readonly IStoreRepository _storeRepository = storeRepository;
private readonly ITenantProvider _tenantProvider = tenantProvider;
private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;
/// <inheritdoc />
public async Task<IReadOnlyList<StoreHolidayDto>> Handle(ListStoreHolidaysQuery request, CancellationToken cancellationToken)
{
// 1. 查询节假日
var tenantId = _tenantProvider.GetCurrentTenantId();
var ignoreTenantFilter = StoreTenantAccess.ShouldIgnoreTenantFilter(_httpContextAccessor);
var tenantId = ignoreTenantFilter ? 0 : _tenantProvider.GetCurrentTenantId();
var holidays = await _storeRepository.GetHolidaysAsync(request.StoreId, tenantId, cancellationToken);
// 2. 映射 DTO

View File

@@ -1,5 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using TakeoutSaaS.Application.App.Stores;
using TakeoutSaaS.Application.App.Stores.Commands;
using TakeoutSaaS.Application.App.Stores.Dto;
using TakeoutSaaS.Domain.Stores.Entities;
@@ -17,18 +19,21 @@ namespace TakeoutSaaS.Application.App.Stores.Handlers;
public sealed class UpdateStoreHolidayCommandHandler(
IStoreRepository storeRepository,
ITenantProvider tenantProvider,
IHttpContextAccessor httpContextAccessor,
ILogger<UpdateStoreHolidayCommandHandler> logger)
: IRequestHandler<UpdateStoreHolidayCommand, StoreHolidayDto?>
{
private readonly IStoreRepository _storeRepository = storeRepository;
private readonly ITenantProvider _tenantProvider = tenantProvider;
private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;
private readonly ILogger<UpdateStoreHolidayCommandHandler> _logger = logger;
/// <inheritdoc />
public async Task<StoreHolidayDto?> Handle(UpdateStoreHolidayCommand request, CancellationToken cancellationToken)
{
// 1. 读取配置
var tenantId = _tenantProvider.GetCurrentTenantId();
var ignoreTenantFilter = StoreTenantAccess.ShouldIgnoreTenantFilter(_httpContextAccessor);
var tenantId = ignoreTenantFilter ? 0 : _tenantProvider.GetCurrentTenantId();
var existing = await _storeRepository.FindHolidayByIdAsync(request.HolidayId, tenantId, cancellationToken);
if (existing is null)
{
@@ -42,8 +47,8 @@ public sealed class UpdateStoreHolidayCommandHandler(
}
// 3. 更新字段
existing.Date = request.Date;
existing.EndDate = request.EndDate;
existing.Date = NormalizeToUtc(request.Date);
existing.EndDate = request.EndDate.HasValue ? NormalizeToUtc(request.EndDate.Value) : null;
existing.IsAllDay = request.IsAllDay;
existing.StartTime = request.StartTime;
existing.EndTime = request.EndTime;
@@ -59,4 +64,14 @@ public sealed class UpdateStoreHolidayCommandHandler(
// 5. 返回 DTO
return StoreMapping.ToDto(existing);
}
private static DateTime NormalizeToUtc(DateTime value)
{
return value.Kind switch
{
DateTimeKind.Utc => value,
DateTimeKind.Local => value.ToUniversalTime(),
_ => DateTime.SpecifyKind(value, DateTimeKind.Utc)
};
}
}