fix: 修复门店租户写入与跨租户查看
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
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.Merchants.Repositories;
|
||||
using TakeoutSaaS.Domain.Stores.Entities;
|
||||
using TakeoutSaaS.Domain.Stores.Enums;
|
||||
using TakeoutSaaS.Domain.Stores.Repositories;
|
||||
@@ -17,17 +19,30 @@ namespace TakeoutSaaS.Application.App.Stores.Handlers;
|
||||
/// </summary>
|
||||
public sealed class CreateStoreCommandHandler(
|
||||
IStoreRepository storeRepository,
|
||||
IMerchantRepository merchantRepository,
|
||||
ITenantProvider tenantProvider,
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
ILogger<CreateStoreCommandHandler> logger)
|
||||
: IRequestHandler<CreateStoreCommand, StoreDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<StoreDto> Handle(CreateStoreCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 校验门店坐标唯一性(100 米内禁止重复)
|
||||
// 1. 校验商户存在并解析租户
|
||||
var currentTenantId = tenantProvider.GetCurrentTenantId();
|
||||
var allowCrossTenant = StoreTenantAccess.ShouldIgnoreTenantFilter(httpContextAccessor);
|
||||
var merchant = allowCrossTenant
|
||||
? await merchantRepository.FindByIdAsync(request.MerchantId, cancellationToken)
|
||||
: await merchantRepository.FindByIdAsync(request.MerchantId, currentTenantId, cancellationToken);
|
||||
if (merchant == null)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "商户不存在");
|
||||
}
|
||||
var tenantId = merchant.TenantId;
|
||||
|
||||
// 2. (空行后) 校验门店坐标唯一性(100 米内禁止重复)
|
||||
if (request.Longitude.HasValue && request.Latitude.HasValue)
|
||||
{
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var isDuplicate = await storeRepository.ExistsStoreWithinDistanceAsync(
|
||||
request.MerchantId,
|
||||
tenantId,
|
||||
@@ -41,16 +56,17 @@ public sealed class CreateStoreCommandHandler(
|
||||
}
|
||||
}
|
||||
|
||||
// 2. (空行后) 计算审核与经营状态
|
||||
// 3. (空行后) 计算审核与经营状态
|
||||
var now = DateTime.UtcNow;
|
||||
var isSameEntity = request.OwnershipType == StoreOwnershipType.SameEntity;
|
||||
var auditStatus = isSameEntity ? StoreAuditStatus.Activated : StoreAuditStatus.Draft;
|
||||
var businessStatus = StoreBusinessStatus.Resting;
|
||||
DateTime? activatedAt = isSameEntity ? now : null;
|
||||
|
||||
// 3. (空行后) 构建实体
|
||||
// 4. (空行后) 构建实体
|
||||
var store = new Store
|
||||
{
|
||||
TenantId = tenantId,
|
||||
MerchantId = request.MerchantId,
|
||||
Code = request.Code.Trim(),
|
||||
Name = request.Name.Trim(),
|
||||
@@ -79,10 +95,14 @@ public sealed class CreateStoreCommandHandler(
|
||||
SupportsQueueing = request.SupportsQueueing
|
||||
};
|
||||
|
||||
// 4. (空行后) 持久化并初始化费用配置
|
||||
// 5. (空行后) 持久化门店以获取标识
|
||||
await storeRepository.AddStoreAsync(store, cancellationToken);
|
||||
await storeRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 6. (空行后) 初始化费用配置
|
||||
await storeRepository.AddStoreFeeAsync(new StoreFee
|
||||
{
|
||||
TenantId = tenantId,
|
||||
StoreId = store.Id,
|
||||
MinimumOrderAmount = 0m,
|
||||
BaseDeliveryFee = 0m,
|
||||
@@ -92,7 +112,7 @@ public sealed class CreateStoreCommandHandler(
|
||||
await storeRepository.SaveChangesAsync(cancellationToken);
|
||||
logger.LogInformation("创建门店 {StoreId} - {StoreName}", store.Id, store.Name);
|
||||
|
||||
// 5. (空行后) 返回 DTO
|
||||
// 7. (空行后) 返回 DTO
|
||||
return StoreMapping.ToDto(store);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user