feat: 配送单查询支持tenantId可选过滤
This commit is contained in:
@@ -41,6 +41,7 @@ public sealed class DeliveriesController(IMediator mediator) : BaseApiController
|
||||
/// <summary>
|
||||
/// 查询配送单列表。
|
||||
/// </summary>
|
||||
/// <param name="tenantId">租户 ID(可选;为空表示跨租户查询)。</param>
|
||||
/// <param name="orderId">订单 ID。</param>
|
||||
/// <param name="status">配送状态。</param>
|
||||
/// <param name="page">页码。</param>
|
||||
@@ -53,6 +54,7 @@ public sealed class DeliveriesController(IMediator mediator) : BaseApiController
|
||||
[PermissionAuthorize("delivery:read")]
|
||||
[ProducesResponseType(typeof(ApiResponse<PagedResult<DeliveryOrderDto>>), StatusCodes.Status200OK)]
|
||||
public async Task<ApiResponse<PagedResult<DeliveryOrderDto>>> List(
|
||||
[FromQuery] long? tenantId,
|
||||
[FromQuery] long? orderId,
|
||||
[FromQuery] DeliveryStatus? status,
|
||||
[FromQuery] int page = 1,
|
||||
@@ -64,6 +66,7 @@ public sealed class DeliveriesController(IMediator mediator) : BaseApiController
|
||||
// 1. 组装查询参数
|
||||
var result = await mediator.Send(new SearchDeliveryOrdersQuery
|
||||
{
|
||||
TenantId = tenantId,
|
||||
OrderId = orderId,
|
||||
Status = status,
|
||||
Page = page,
|
||||
|
||||
@@ -4,21 +4,35 @@ using TakeoutSaaS.Application.App.Deliveries.Commands;
|
||||
using TakeoutSaaS.Application.App.Deliveries.Dto;
|
||||
using TakeoutSaaS.Domain.Deliveries.Entities;
|
||||
using TakeoutSaaS.Domain.Deliveries.Repositories;
|
||||
using TakeoutSaaS.Domain.Orders.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
|
||||
/// <summary>
|
||||
/// 创建配送单命令处理器。
|
||||
/// </summary>
|
||||
public sealed class CreateDeliveryOrderCommandHandler(IDeliveryRepository deliveryRepository, ILogger<CreateDeliveryOrderCommandHandler> logger)
|
||||
public sealed class CreateDeliveryOrderCommandHandler(
|
||||
IDeliveryRepository deliveryRepository,
|
||||
IOrderRepository orderRepository,
|
||||
ILogger<CreateDeliveryOrderCommandHandler> logger)
|
||||
: IRequestHandler<CreateDeliveryOrderCommand, DeliveryOrderDto>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<DeliveryOrderDto> Handle(CreateDeliveryOrderCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 构建配送单实体
|
||||
// 1. 查询订单以确定租户
|
||||
var order = await orderRepository.FindByIdAsync(request.OrderId, cancellationToken);
|
||||
if (order is null)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "订单不存在");
|
||||
}
|
||||
|
||||
// 2. (空行后) 构建配送单实体并写入租户
|
||||
var deliveryOrder = new DeliveryOrder
|
||||
{
|
||||
TenantId = order.TenantId,
|
||||
OrderId = request.OrderId,
|
||||
Provider = request.Provider,
|
||||
ProviderOrderId = request.ProviderOrderId?.Trim(),
|
||||
@@ -32,14 +46,14 @@ public sealed class CreateDeliveryOrderCommandHandler(IDeliveryRepository delive
|
||||
FailureReason = request.FailureReason?.Trim()
|
||||
};
|
||||
|
||||
// 2. 持久化配送单
|
||||
// 3. (空行后) 持久化配送单
|
||||
await deliveryRepository.AddDeliveryOrderAsync(deliveryOrder, cancellationToken);
|
||||
await deliveryRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 3. 记录日志
|
||||
// 4. (空行后) 记录日志
|
||||
logger.LogInformation("创建配送单 {DeliveryOrderId} 对应订单 {OrderId}", deliveryOrder.Id, deliveryOrder.OrderId);
|
||||
|
||||
// 4. 映射 DTO 返回
|
||||
// 5. (空行后) 映射 DTO 返回
|
||||
return MapToDto(deliveryOrder, []);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ using MediatR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using TakeoutSaaS.Application.App.Deliveries.Commands;
|
||||
using TakeoutSaaS.Domain.Deliveries.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
|
||||
@@ -11,26 +10,24 @@ namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
/// </summary>
|
||||
public sealed class DeleteDeliveryOrderCommandHandler(
|
||||
IDeliveryRepository deliveryRepository,
|
||||
ITenantProvider tenantProvider,
|
||||
ILogger<DeleteDeliveryOrderCommandHandler> logger)
|
||||
: IRequestHandler<DeleteDeliveryOrderCommand, bool>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<bool> Handle(DeleteDeliveryOrderCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 获取租户并定位配送单
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var existing = await deliveryRepository.FindByIdAsync(request.DeliveryOrderId, tenantId, cancellationToken);
|
||||
// 1. 定位配送单(跨租户)
|
||||
var existing = await deliveryRepository.FindByIdAsync(request.DeliveryOrderId, cancellationToken);
|
||||
if (existing == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// 2. 删除并保存
|
||||
await deliveryRepository.DeleteDeliveryOrderAsync(request.DeliveryOrderId, tenantId, cancellationToken);
|
||||
// 2. (空行后) 删除并保存
|
||||
await deliveryRepository.DeleteDeliveryOrderAsync(request.DeliveryOrderId, existing.TenantId, cancellationToken);
|
||||
await deliveryRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 3. 记录删除日志
|
||||
// 3. (空行后) 记录删除日志
|
||||
logger.LogInformation("删除配送单 {DeliveryOrderId}", request.DeliveryOrderId);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -3,7 +3,6 @@ using TakeoutSaaS.Application.App.Deliveries.Dto;
|
||||
using TakeoutSaaS.Application.App.Deliveries.Queries;
|
||||
using TakeoutSaaS.Domain.Deliveries.Entities;
|
||||
using TakeoutSaaS.Domain.Deliveries.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
|
||||
@@ -11,27 +10,23 @@ namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
/// 配送单详情查询处理器。
|
||||
/// </summary>
|
||||
public sealed class GetDeliveryOrderByIdQueryHandler(
|
||||
IDeliveryRepository deliveryRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
IDeliveryRepository deliveryRepository)
|
||||
: IRequestHandler<GetDeliveryOrderByIdQuery, DeliveryOrderDto?>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<DeliveryOrderDto?> Handle(GetDeliveryOrderByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 读取当前租户标识
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
|
||||
// 2. 查询配送单主体
|
||||
var order = await deliveryRepository.FindByIdAsync(request.DeliveryOrderId, tenantId, cancellationToken);
|
||||
// 1. 查询配送单主体(跨租户)
|
||||
var order = await deliveryRepository.FindByIdAsync(request.DeliveryOrderId, cancellationToken);
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3. 查询配送事件明细
|
||||
var events = await deliveryRepository.GetEventsAsync(order.Id, tenantId, cancellationToken);
|
||||
// 2. (空行后) 查询配送事件明细
|
||||
var events = await deliveryRepository.GetEventsAsync(order.Id, order.TenantId, cancellationToken);
|
||||
|
||||
// 4. 映射为 DTO 返回
|
||||
// 3. (空行后) 映射为 DTO 返回
|
||||
return MapToDto(order, events);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ using TakeoutSaaS.Application.App.Deliveries.Dto;
|
||||
using TakeoutSaaS.Application.App.Deliveries.Queries;
|
||||
using TakeoutSaaS.Domain.Deliveries.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
|
||||
@@ -11,29 +10,25 @@ namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
/// 配送单列表查询处理器。
|
||||
/// </summary>
|
||||
public sealed class SearchDeliveryOrdersQueryHandler(
|
||||
IDeliveryRepository deliveryRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
IDeliveryRepository deliveryRepository)
|
||||
: IRequestHandler<SearchDeliveryOrdersQuery, PagedResult<DeliveryOrderDto>>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<PagedResult<DeliveryOrderDto>> Handle(SearchDeliveryOrdersQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 获取当前租户标识
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
// 1. 查询配送单列表(可选租户过滤)
|
||||
var orders = await deliveryRepository.SearchAsync(request.TenantId, request.Status, request.OrderId, cancellationToken);
|
||||
|
||||
// 2. 查询配送单列表(租户隔离)
|
||||
var orders = await deliveryRepository.SearchAsync(tenantId, request.Status, request.OrderId, cancellationToken);
|
||||
|
||||
// 3. 本地排序
|
||||
// 2. (空行后) 本地排序
|
||||
var sorted = ApplySorting(orders, request.SortBy, request.SortDescending);
|
||||
|
||||
// 4. 本地分页
|
||||
// 3. (空行后) 本地分页
|
||||
var paged = sorted
|
||||
.Skip((request.Page - 1) * request.PageSize)
|
||||
.Take(request.PageSize)
|
||||
.ToList();
|
||||
|
||||
// 5. 映射 DTO
|
||||
// 4. (空行后) 映射 DTO
|
||||
var items = paged.Select(order => new DeliveryOrderDto
|
||||
{
|
||||
Id = order.Id,
|
||||
@@ -52,7 +47,7 @@ public sealed class SearchDeliveryOrdersQueryHandler(
|
||||
CreatedAt = order.CreatedAt
|
||||
}).ToList();
|
||||
|
||||
// 6. 返回分页结果
|
||||
// 5. (空行后) 返回分页结果
|
||||
return new PagedResult<DeliveryOrderDto>(items, request.Page, request.PageSize, orders.Count);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,9 @@ using TakeoutSaaS.Application.App.Deliveries.Commands;
|
||||
using TakeoutSaaS.Application.App.Deliveries.Dto;
|
||||
using TakeoutSaaS.Domain.Deliveries.Entities;
|
||||
using TakeoutSaaS.Domain.Deliveries.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
using TakeoutSaaS.Domain.Orders.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Constants;
|
||||
using TakeoutSaaS.Shared.Abstractions.Exceptions;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
|
||||
@@ -13,24 +15,29 @@ namespace TakeoutSaaS.Application.App.Deliveries.Handlers;
|
||||
/// </summary>
|
||||
public sealed class UpdateDeliveryOrderCommandHandler(
|
||||
IDeliveryRepository deliveryRepository,
|
||||
ITenantProvider tenantProvider,
|
||||
IOrderRepository orderRepository,
|
||||
ILogger<UpdateDeliveryOrderCommandHandler> logger)
|
||||
: IRequestHandler<UpdateDeliveryOrderCommand, DeliveryOrderDto?>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<DeliveryOrderDto?> Handle(UpdateDeliveryOrderCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 获取当前租户标识
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
|
||||
// 2. 查询目标配送单
|
||||
var existing = await deliveryRepository.FindByIdAsync(request.DeliveryOrderId, tenantId, cancellationToken);
|
||||
// 1. 查询目标配送单(跨租户)
|
||||
var existing = await deliveryRepository.FindByIdAsync(request.DeliveryOrderId, cancellationToken);
|
||||
if (existing == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3. 更新字段
|
||||
// 2. (空行后) 查询订单以确定租户
|
||||
var order = await orderRepository.FindByIdAsync(request.OrderId, cancellationToken);
|
||||
if (order is null)
|
||||
{
|
||||
throw new BusinessException(ErrorCodes.NotFound, "订单不存在");
|
||||
}
|
||||
|
||||
// 3. (空行后) 更新字段并写入租户
|
||||
existing.TenantId = order.TenantId;
|
||||
existing.OrderId = request.OrderId;
|
||||
existing.Provider = request.Provider;
|
||||
existing.ProviderOrderId = request.ProviderOrderId?.Trim();
|
||||
@@ -43,15 +50,15 @@ public sealed class UpdateDeliveryOrderCommandHandler(
|
||||
existing.DeliveredAt = request.DeliveredAt;
|
||||
existing.FailureReason = request.FailureReason?.Trim();
|
||||
|
||||
// 4. 持久化变更
|
||||
// 4. (空行后) 持久化变更
|
||||
await deliveryRepository.UpdateDeliveryOrderAsync(existing, cancellationToken);
|
||||
await deliveryRepository.SaveChangesAsync(cancellationToken);
|
||||
|
||||
// 5. 记录更新日志
|
||||
// 5. (空行后) 记录更新日志
|
||||
logger.LogInformation("更新配送单 {DeliveryOrderId}", existing.Id);
|
||||
|
||||
// 6. 查询事件并返回映射结果
|
||||
var events = await deliveryRepository.GetEventsAsync(existing.Id, tenantId, cancellationToken);
|
||||
// 6. (空行后) 查询事件并返回映射结果
|
||||
var events = await deliveryRepository.GetEventsAsync(existing.Id, existing.TenantId, cancellationToken);
|
||||
return MapToDto(existing, events);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,11 @@ namespace TakeoutSaaS.Application.App.Deliveries.Queries;
|
||||
/// </summary>
|
||||
public sealed class SearchDeliveryOrdersQuery : IRequest<PagedResult<DeliveryOrderDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 租户 ID(可选;为空表示跨租户查询)。
|
||||
/// </summary>
|
||||
public long? TenantId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 订单 ID(可选)。
|
||||
/// </summary>
|
||||
|
||||
@@ -17,6 +17,14 @@ public interface IDeliveryRepository
|
||||
/// <returns>配送单实体或 null。</returns>
|
||||
Task<DeliveryOrder?> FindByIdAsync(long deliveryOrderId, long tenantId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 依据标识获取配送单(跨租户)。
|
||||
/// </summary>
|
||||
/// <param name="deliveryOrderId">配送单 ID。</param>
|
||||
/// <param name="cancellationToken">取消标记。</param>
|
||||
/// <returns>配送单实体或 null。</returns>
|
||||
Task<DeliveryOrder?> FindByIdAsync(long deliveryOrderId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 依据订单标识获取配送单。
|
||||
/// </summary>
|
||||
@@ -68,6 +76,16 @@ public interface IDeliveryRepository
|
||||
/// <returns>配送单列表。</returns>
|
||||
Task<IReadOnlyList<DeliveryOrder>> SearchAsync(long tenantId, DeliveryStatus? status, long? orderId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 按状态查询配送单(可选租户过滤)。
|
||||
/// </summary>
|
||||
/// <param name="tenantId">租户 ID(为空则不做租户过滤)。</param>
|
||||
/// <param name="status">配送状态。</param>
|
||||
/// <param name="orderId">订单 ID。</param>
|
||||
/// <param name="cancellationToken">取消标记。</param>
|
||||
/// <returns>配送单列表。</returns>
|
||||
Task<IReadOnlyList<DeliveryOrder>> SearchAsync(long? tenantId, DeliveryStatus? status, long? orderId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// 更新配送单。
|
||||
/// </summary>
|
||||
|
||||
@@ -23,6 +23,16 @@ public sealed class EfDeliveryRepository(TakeoutAdminDbContext context) : IDeliv
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<DeliveryOrder?> FindByIdAsync(long deliveryOrderId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 按主键查询(跨租户)
|
||||
return context.DeliveryOrders
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == deliveryOrderId)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<DeliveryOrder?> FindByOrderIdAsync(long orderId, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -84,6 +94,34 @@ public sealed class EfDeliveryRepository(TakeoutAdminDbContext context) : IDeliv
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<DeliveryOrder>> SearchAsync(long? tenantId, DeliveryStatus? status, long? orderId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 构建查询(可选租户过滤)
|
||||
var query = context.DeliveryOrders.AsNoTracking();
|
||||
if (tenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.TenantId == tenantId.Value);
|
||||
}
|
||||
|
||||
// 2. (空行后) 可选过滤:配送状态
|
||||
if (status.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.Status == status.Value);
|
||||
}
|
||||
|
||||
// 3. (空行后) 可选过滤:订单标识
|
||||
if (orderId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.OrderId == orderId.Value);
|
||||
}
|
||||
|
||||
// 4. (空行后) 排序并返回
|
||||
return await query
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task UpdateDeliveryOrderAsync(DeliveryOrder deliveryOrder, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user