feat: 订单商品查询支持tenantId可选过滤
This commit is contained in:
@@ -24,6 +24,16 @@ public sealed class EfOrderRepository(TakeoutAdminDbContext context) : IOrderRep
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<Order?> FindByIdAsync(long orderId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 按主键查询(跨租户)
|
||||
return context.Orders
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == orderId)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<Order?> FindByOrderNoAsync(string orderNo, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -57,6 +67,35 @@ public sealed class EfOrderRepository(TakeoutAdminDbContext context) : IOrderRep
|
||||
return orders;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<Order>> SearchAsync(long? tenantId, OrderStatus? status, PaymentStatus? paymentStatus, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 构建查询(可选租户过滤)
|
||||
var query = context.Orders.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 (paymentStatus.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.PaymentStatus == paymentStatus.Value);
|
||||
}
|
||||
|
||||
// 4. (空行后) 排序并返回
|
||||
var orders = await query
|
||||
.OrderByDescending(x => x.CreatedAt)
|
||||
.ToListAsync(cancellationToken);
|
||||
return orders;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<OrderItem>> GetItemsAsync(long orderId, long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
@@ -25,6 +25,16 @@ public sealed class EfProductRepository(TakeoutAdminDbContext context) : IProduc
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<Product?> FindByIdAsync(long productId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// 1. 按主键查询(跨租户)
|
||||
return context.Products
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == productId)
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<Product>> SearchAsync(long tenantId, long? storeId, long? categoryId, ProductStatus? status, CancellationToken cancellationToken = default, DateTime? updatedAfter = null)
|
||||
{
|
||||
@@ -59,6 +69,47 @@ public sealed class EfProductRepository(TakeoutAdminDbContext context) : IProduc
|
||||
return products;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<Product>> SearchAsync(long? tenantId, long? storeId, long? categoryId, ProductStatus? status, CancellationToken cancellationToken = default, DateTime? updatedAfter = null)
|
||||
{
|
||||
// 1. 构建查询(可选租户过滤)
|
||||
var query = context.Products.AsNoTracking();
|
||||
if (tenantId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.TenantId == tenantId.Value);
|
||||
}
|
||||
|
||||
// 2. (空行后) 可选过滤:门店
|
||||
if (storeId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.StoreId == storeId.Value);
|
||||
}
|
||||
|
||||
// 3. (空行后) 可选过滤:分类
|
||||
if (categoryId.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.CategoryId == categoryId.Value);
|
||||
}
|
||||
|
||||
// 4. (空行后) 可选过滤:状态
|
||||
if (status.HasValue)
|
||||
{
|
||||
query = query.Where(x => x.Status == status.Value);
|
||||
}
|
||||
|
||||
// 5. (空行后) 可选过滤:更新时间下限
|
||||
if (updatedAfter.HasValue)
|
||||
{
|
||||
query = query.Where(x => (x.UpdatedAt ?? x.CreatedAt) >= updatedAfter.Value);
|
||||
}
|
||||
|
||||
// 6. (空行后) 排序并返回
|
||||
var products = await query
|
||||
.OrderBy(x => x.Name)
|
||||
.ToListAsync(cancellationToken);
|
||||
return products;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<ProductCategory>> GetCategoriesAsync(long tenantId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user