using Fengling.Backend.Domain.AggregatesModel.RedemptionOrderAggregate; using Fengling.Backend.Infrastructure; namespace Fengling.Backend.Web.Application.Queries.RedemptionOrders; /// /// 兑换订单列表查询 /// public record GetRedemptionOrdersQuery(Guid? MemberId = null, int? Status = null) : IQuery>; public record RedemptionOrderDto { public Guid Id { get; init; } public string OrderNo { get; init; } = string.Empty; public Guid MemberId { get; init; } public Guid GiftId { get; init; } public string GiftName { get; init; } = string.Empty; public int GiftType { get; init; } public int Quantity { get; init; } public int ConsumedPoints { get; init; } public RedemptionOrderAddressDto? ShippingAddress { get; init; } public string? TrackingNo { get; init; } public int Status { get; init; } public string? CancelReason { get; init; } public DateTime CreatedAt { get; init; } public DateTime UpdatedAt { get; init; } } public record RedemptionOrderAddressDto { public string ReceiverName { get; init; } = string.Empty; public string Phone { get; init; } = string.Empty; public string Province { get; init; } = string.Empty; public string City { get; init; } = string.Empty; public string District { get; init; } = string.Empty; public string DetailAddress { get; init; } = string.Empty; } public class GetRedemptionOrdersQueryHandler(ApplicationDbContext dbContext) : IQueryHandler> { public async Task> Handle(GetRedemptionOrdersQuery request, CancellationToken cancellationToken) { var query = dbContext.RedemptionOrders.AsQueryable(); if (request.MemberId.HasValue) { query = query.Where(x => x.MemberId == request.MemberId.Value); } if (request.Status.HasValue) { query = query.Where(x => (int)x.Status == request.Status.Value); } var orders = await query .OrderByDescending(x => x.CreatedAt) .Select(x => new RedemptionOrderDto { Id = x.Id.Id, OrderNo = x.OrderNo, MemberId = x.MemberId, GiftId = x.GiftId, GiftName = x.GiftName, GiftType = x.GiftType, Quantity = x.Quantity, ConsumedPoints = x.ConsumedPoints, ShippingAddress = x.ShippingAddress == null ? null : new RedemptionOrderAddressDto { ReceiverName = x.ShippingAddress.ReceiverName, Phone = x.ShippingAddress.Phone, Province = x.ShippingAddress.Province, City = x.ShippingAddress.City, District = x.ShippingAddress.District, DetailAddress = x.ShippingAddress.DetailAddress }, TrackingNo = x.TrackingNo, Status = (int)x.Status, CancelReason = x.CancelReason, CreatedAt = x.CreatedAt, UpdatedAt = x.UpdatedAt }) .ToListAsync(cancellationToken); return orders; } } /// /// 订单详情查询 /// public record GetRedemptionOrderByIdQuery(Guid OrderId) : IQuery; public class GetRedemptionOrderByIdQueryHandler(ApplicationDbContext dbContext) : IQueryHandler { public async Task Handle(GetRedemptionOrderByIdQuery request, CancellationToken cancellationToken) { var order = await dbContext.RedemptionOrders .Where(x => x.Id == new RedemptionOrderId(request.OrderId)) .Select(x => new RedemptionOrderDto { Id = x.Id.Id, OrderNo = x.OrderNo, MemberId = x.MemberId, GiftId = x.GiftId, GiftName = x.GiftName, GiftType = x.GiftType, Quantity = x.Quantity, ConsumedPoints = x.ConsumedPoints, ShippingAddress = x.ShippingAddress == null ? null : new RedemptionOrderAddressDto { ReceiverName = x.ShippingAddress.ReceiverName, Phone = x.ShippingAddress.Phone, Province = x.ShippingAddress.Province, City = x.ShippingAddress.City, District = x.ShippingAddress.District, DetailAddress = x.ShippingAddress.DetailAddress }, TrackingNo = x.TrackingNo, Status = (int)x.Status, CancelReason = x.CancelReason, CreatedAt = x.CreatedAt, UpdatedAt = x.UpdatedAt }) .FirstOrDefaultAsync(cancellationToken); return order; } }