using Fengling.Backend.Infrastructure; namespace Fengling.Backend.Web.Application.Queries.MarketingCodes; /// /// 营销码DTO /// public record MarketingCodeDto { public Guid Id { get; init; } public string Code { get; init; } = string.Empty; public string BatchNo { get; init; } = string.Empty; public Guid ProductId { get; init; } public string ProductName { get; init; } = string.Empty; public Guid? CategoryId { get; init; } public string? CategoryName { get; init; } public bool IsUsed { get; init; } public Guid? UsedByMemberId { get; init; } public DateTime? UsedAt { get; init; } public DateTime? ExpiryDate { get; init; } public DateTime CreatedAt { get; init; } } /// /// 批次信息DTO /// public record MarketingCodeBatchDto { public string BatchNo { get; init; } = string.Empty; public Guid ProductId { get; init; } public string ProductName { get; init; } = string.Empty; public Guid? CategoryId { get; init; } public string? CategoryName { get; init; } public int TotalCount { get; init; } public int UsedCount { get; init; } public DateTime CreatedAt { get; init; } public DateTime? ExpiryDate { get; init; } } /// /// 查询营销码列表 /// public record GetMarketingCodesQuery( string? BatchNo = null, Guid? ProductId = null, bool? IsUsed = null, DateTime? StartDate = null, DateTime? EndDate = null, int Page = 1, int PageSize = 20) : IQuery>; public class GetMarketingCodesQueryHandler(ApplicationDbContext dbContext) : IQueryHandler> { public async Task> Handle(GetMarketingCodesQuery request, CancellationToken cancellationToken) { var query = dbContext.MarketingCodes.AsQueryable(); // 按批次号筛选 if (!string.IsNullOrWhiteSpace(request.BatchNo)) { query = query.Where(x => x.BatchNo == request.BatchNo); } // 按产品ID筛选 if (request.ProductId.HasValue) { query = query.Where(x => x.ProductInfo.ProductId == request.ProductId.Value); } // 按使用状态筛选 if (request.IsUsed.HasValue) { query = query.Where(x => x.IsUsed == request.IsUsed.Value); } // 按创建时间范围筛选 if (request.StartDate.HasValue) { query = query.Where(x => x.CreatedAt >= request.StartDate.Value); } if (request.EndDate.HasValue) { // EndDate包含当天整天 var endOfDay = request.EndDate.Value.Date.AddDays(1); query = query.Where(x => x.CreatedAt < endOfDay); } // 获取总数 var totalCount = await query.CountAsync(cancellationToken); // 分页查询 var marketingCodes = await query .OrderByDescending(x => x.CreatedAt) .Skip((request.Page - 1) * request.PageSize) .Take(request.PageSize) .Select(x => new MarketingCodeDto { Id = x.Id.Id, Code = x.Code, BatchNo = x.BatchNo, ProductId = x.ProductInfo.ProductId, ProductName = x.ProductInfo.ProductName, CategoryId = x.ProductInfo.CategoryId, CategoryName = x.ProductInfo.CategoryName, IsUsed = x.IsUsed, UsedByMemberId = x.UsedByMemberId, UsedAt = x.UsedAt, ExpiryDate = x.ExpiryDate, CreatedAt = x.CreatedAt }) .ToListAsync(cancellationToken); return new PagedResult( marketingCodes, totalCount, request.Page, request.PageSize); } } /// /// 查询所有批次列表 /// public record GetMarketingCodeBatchesQuery : IQuery>; public class GetMarketingCodeBatchesQueryHandler(ApplicationDbContext dbContext) : IQueryHandler> { public async Task> Handle(GetMarketingCodeBatchesQuery request, CancellationToken cancellationToken) { var batches = await dbContext.MarketingCodes .GroupBy(x => new { x.BatchNo, x.ProductInfo.ProductId, x.ProductInfo.ProductName, x.ProductInfo.CategoryId, x.ProductInfo.CategoryName, x.ExpiryDate }) .Select(g => new MarketingCodeBatchDto { BatchNo = g.Key.BatchNo, ProductId = g.Key.ProductId, ProductName = g.Key.ProductName, CategoryId = g.Key.CategoryId, CategoryName = g.Key.CategoryName, TotalCount = g.Count(), UsedCount = g.Count(x => x.IsUsed), CreatedAt = g.Min(x => x.CreatedAt), ExpiryDate = g.Key.ExpiryDate }) .OrderByDescending(x => x.CreatedAt) .ToListAsync(cancellationToken); return batches; } }