feat: 订单商品查询支持tenantId可选过滤
This commit is contained in:
@@ -3,7 +3,6 @@ using TakeoutSaaS.Application.App.Orders.Dto;
|
||||
using TakeoutSaaS.Application.App.Orders.Queries;
|
||||
using TakeoutSaaS.Domain.Orders.Entities;
|
||||
using TakeoutSaaS.Domain.Orders.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Orders.Handlers;
|
||||
|
||||
@@ -11,29 +10,26 @@ namespace TakeoutSaaS.Application.App.Orders.Handlers;
|
||||
/// 订单详情查询处理器。
|
||||
/// </summary>
|
||||
public sealed class GetOrderByIdQueryHandler(
|
||||
IOrderRepository orderRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
IOrderRepository orderRepository)
|
||||
: IRequestHandler<GetOrderByIdQuery, OrderDto?>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<OrderDto?> Handle(GetOrderByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 获取当前租户
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
|
||||
// 2. 查询订单主体
|
||||
var order = await orderRepository.FindByIdAsync(request.OrderId, tenantId, cancellationToken);
|
||||
// 1. 查询订单主体(跨租户)
|
||||
var order = await orderRepository.FindByIdAsync(request.OrderId, cancellationToken);
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 3. 查询关联明细
|
||||
// 2. (空行后) 查询关联明细(按订单租户)
|
||||
var tenantId = order.TenantId;
|
||||
var items = await orderRepository.GetItemsAsync(order.Id, tenantId, cancellationToken);
|
||||
var histories = await orderRepository.GetStatusHistoryAsync(order.Id, tenantId, cancellationToken);
|
||||
var refunds = await orderRepository.GetRefundsAsync(order.Id, tenantId, cancellationToken);
|
||||
|
||||
// 4. 映射并返回
|
||||
// 3. (空行后) 映射并返回
|
||||
return MapToDto(order, items, histories, refunds);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ using TakeoutSaaS.Application.App.Orders.Dto;
|
||||
using TakeoutSaaS.Application.App.Orders.Queries;
|
||||
using TakeoutSaaS.Domain.Orders.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Orders.Handlers;
|
||||
|
||||
@@ -11,24 +10,22 @@ namespace TakeoutSaaS.Application.App.Orders.Handlers;
|
||||
/// 订单列表查询处理器。
|
||||
/// </summary>
|
||||
public sealed class SearchOrdersQueryHandler(
|
||||
IOrderRepository orderRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
IOrderRepository orderRepository)
|
||||
: IRequestHandler<SearchOrdersQuery, PagedResult<OrderDto>>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<PagedResult<OrderDto>> Handle(SearchOrdersQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 获取当前租户并查询订单
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var orders = await orderRepository.SearchAsync(tenantId, request.Status, request.PaymentStatus, cancellationToken);
|
||||
// 1. 查询订单(可选租户过滤)
|
||||
var orders = await orderRepository.SearchAsync(request.TenantId, request.Status, request.PaymentStatus, cancellationToken);
|
||||
|
||||
// 2. 可选过滤:门店
|
||||
// 2. (空行后) 可选过滤:门店
|
||||
if (request.StoreId.HasValue)
|
||||
{
|
||||
orders = orders.Where(x => x.StoreId == request.StoreId.Value).ToList();
|
||||
}
|
||||
|
||||
// 3. 可选过滤:订单号模糊
|
||||
// 3. (空行后) 可选过滤:订单号模糊
|
||||
if (!string.IsNullOrWhiteSpace(request.OrderNo))
|
||||
{
|
||||
var orderNo = request.OrderNo.Trim();
|
||||
@@ -37,14 +34,14 @@ public sealed class SearchOrdersQueryHandler(
|
||||
.ToList();
|
||||
}
|
||||
|
||||
// 4. 排序与分页
|
||||
// 4. (空行后) 排序与分页
|
||||
var sorted = ApplySorting(orders, request.SortBy, request.SortDescending);
|
||||
var paged = sorted
|
||||
.Skip((request.Page - 1) * request.PageSize)
|
||||
.Take(request.PageSize)
|
||||
.ToList();
|
||||
|
||||
// 5. 映射 DTO
|
||||
// 5. (空行后) 映射 DTO
|
||||
var items = paged.Select(order => new OrderDto
|
||||
{
|
||||
Id = order.Id,
|
||||
@@ -72,7 +69,7 @@ public sealed class SearchOrdersQueryHandler(
|
||||
CreatedAt = order.CreatedAt
|
||||
}).ToList();
|
||||
|
||||
// 6. 返回分页结果
|
||||
// 6. (空行后) 返回分页结果
|
||||
return new PagedResult<OrderDto>(items, request.Page, request.PageSize, orders.Count);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,11 @@ namespace TakeoutSaaS.Application.App.Orders.Queries;
|
||||
/// </summary>
|
||||
public sealed class SearchOrdersQuery : IRequest<PagedResult<OrderDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 租户 ID(可选,空表示跨租户查询)。
|
||||
/// </summary>
|
||||
public long? TenantId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 门店 ID(可选)。
|
||||
/// </summary>
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Application.App.Products.Queries;
|
||||
using TakeoutSaaS.Domain.Products.Entities;
|
||||
using TakeoutSaaS.Domain.Products.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
|
||||
@@ -11,18 +9,15 @@ namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
/// 商品详情查询处理器。
|
||||
/// </summary>
|
||||
public sealed class GetProductByIdQueryHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
IProductRepository productRepository)
|
||||
: IRequestHandler<GetProductByIdQuery, ProductDto?>
|
||||
{
|
||||
private readonly IProductRepository _productRepository = productRepository;
|
||||
private readonly ITenantProvider _tenantProvider = tenantProvider;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<ProductDto?> Handle(GetProductByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var tenantId = _tenantProvider.GetCurrentTenantId();
|
||||
var product = await _productRepository.FindByIdAsync(request.ProductId, tenantId, cancellationToken);
|
||||
// 1. 查询商品(跨租户)
|
||||
var product = await productRepository.FindByIdAsync(request.ProductId, cancellationToken);
|
||||
// 2. (空行后) 映射并返回
|
||||
return product == null ? null : ProductMapping.ToDto(product);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ using MediatR;
|
||||
using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Application.App.Products.Queries;
|
||||
using TakeoutSaaS.Domain.Products.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
|
||||
@@ -10,22 +9,21 @@ namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
/// 商品全量详情查询处理器。
|
||||
/// </summary>
|
||||
public sealed class GetProductDetailQueryHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
IProductRepository productRepository)
|
||||
: IRequestHandler<GetProductDetailQuery, ProductDetailDto?>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public async Task<ProductDetailDto?> Handle(GetProductDetailQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
// 1. 读取 SPU
|
||||
var tenantId = tenantProvider.GetCurrentTenantId();
|
||||
var product = await productRepository.FindByIdAsync(request.ProductId, tenantId, cancellationToken);
|
||||
// 1. 读取 SPU(跨租户)
|
||||
var product = await productRepository.FindByIdAsync(request.ProductId, cancellationToken);
|
||||
if (product is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// 2. 查询子项
|
||||
// 2. (空行后) 读取租户并查询子项
|
||||
var tenantId = product.TenantId;
|
||||
var skusTask = productRepository.GetSkusAsync(product.Id, tenantId, cancellationToken);
|
||||
var attrGroupsTask = productRepository.GetAttributeGroupsAsync(product.Id, tenantId, cancellationToken);
|
||||
var attrOptionsTask = productRepository.GetAttributeOptionsAsync(product.Id, tenantId, cancellationToken);
|
||||
@@ -36,7 +34,7 @@ public sealed class GetProductDetailQueryHandler(
|
||||
|
||||
await Task.WhenAll(skusTask, attrGroupsTask, attrOptionsTask, addonGroupsTask, addonOptionsTask, mediaTask, pricingTask);
|
||||
|
||||
// 3. 组装 DTO
|
||||
// 3. (空行后) 组装 DTO
|
||||
var skus = await skusTask;
|
||||
var attrGroups = await attrGroupsTask;
|
||||
var attrOptions = (await attrOptionsTask).ToLookup(x => x.AttributeGroupId);
|
||||
|
||||
@@ -3,7 +3,6 @@ using TakeoutSaaS.Application.App.Products.Dto;
|
||||
using TakeoutSaaS.Application.App.Products.Queries;
|
||||
using TakeoutSaaS.Domain.Products.Repositories;
|
||||
using TakeoutSaaS.Shared.Abstractions.Results;
|
||||
using TakeoutSaaS.Shared.Abstractions.Tenancy;
|
||||
|
||||
namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
|
||||
@@ -11,25 +10,28 @@ namespace TakeoutSaaS.Application.App.Products.Handlers;
|
||||
/// 商品列表查询处理器。
|
||||
/// </summary>
|
||||
public sealed class SearchProductsQueryHandler(
|
||||
IProductRepository productRepository,
|
||||
ITenantProvider tenantProvider)
|
||||
IProductRepository productRepository)
|
||||
: IRequestHandler<SearchProductsQuery, PagedResult<ProductDto>>
|
||||
{
|
||||
private readonly IProductRepository _productRepository = productRepository;
|
||||
private readonly ITenantProvider _tenantProvider = tenantProvider;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<PagedResult<ProductDto>> Handle(SearchProductsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var tenantId = _tenantProvider.GetCurrentTenantId();
|
||||
var products = await _productRepository.SearchAsync(tenantId, request.StoreId, request.CategoryId, request.Status, cancellationToken);
|
||||
// 1. 查询商品列表(可选租户过滤)
|
||||
var products = await productRepository.SearchAsync(
|
||||
request.TenantId,
|
||||
request.StoreId,
|
||||
request.CategoryId,
|
||||
request.Status,
|
||||
cancellationToken);
|
||||
|
||||
// 2. (空行后) 排序与分页
|
||||
var sorted = ApplySorting(products, request.SortBy, request.SortDescending);
|
||||
var paged = sorted
|
||||
.Skip((request.Page - 1) * request.PageSize)
|
||||
.Take(request.PageSize)
|
||||
.ToList();
|
||||
|
||||
// 3. (空行后) 映射 DTO 并返回分页结果
|
||||
var items = paged.Select(MapToDto).ToList();
|
||||
return new PagedResult<ProductDto>(items, request.Page, request.PageSize, products.Count);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,11 @@ namespace TakeoutSaaS.Application.App.Products.Queries;
|
||||
/// </summary>
|
||||
public sealed class SearchProductsQuery : IRequest<PagedResult<ProductDto>>
|
||||
{
|
||||
/// <summary>
|
||||
/// 租户 ID(可选,空表示跨租户查询)。
|
||||
/// </summary>
|
||||
public long? TenantId { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// 门店 ID(可选)。
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user