Handler.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using Application.Abstractions.Data;
  2. using Application.Abstractions.Messaging;
  3. using Domain.Entities.Store.ValueObject;
  4. using Microsoft.EntityFrameworkCore;
  5. namespace Application.Features.Admin.Store.Settlement.Search;
  6. public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
  7. {
  8. public async Task<Response> Handle(Query request, CancellationToken ct)
  9. {
  10. // navtab counts (필터 무관 전체)
  11. var counts = await db.GameSettlement.AsNoTracking()
  12. .GroupBy(s => s.Status)
  13. .Select(g => new { Status = g.Key, Count = g.Count() })
  14. .ToListAsync(ct);
  15. int CountOf(GameSettlementStatus s) => counts.FirstOrDefault(x => x.Status == s)?.Count ?? 0;
  16. var statusCounts = new Response.StatusCounts(
  17. All: counts.Sum(x => x.Count),
  18. Pending: CountOf(GameSettlementStatus.Pending),
  19. Settled: CountOf(GameSettlementStatus.Settled)
  20. );
  21. // 필터 적용
  22. var query = db.GameSettlement.AsNoTracking()
  23. .Include(s => s.Game)
  24. .AsQueryable();
  25. if (request.GameID is > 0)
  26. {
  27. query = query.Where(s => s.GameID == request.GameID);
  28. }
  29. if (request.Year is > 0)
  30. {
  31. query = query.Where(s => s.Year == request.Year);
  32. }
  33. if (request.Month is > 0)
  34. {
  35. query = query.Where(s => s.Month == request.Month);
  36. }
  37. if (request.Status.HasValue)
  38. {
  39. query = query.Where(s => s.Status == request.Status.Value);
  40. }
  41. var total = await query.CountAsync(ct);
  42. // 합계 (필터 적용 후)
  43. var summary = await query
  44. .GroupBy(s => 1)
  45. .Select(g => new { Sum = g.Sum(s => (long)s.TotalRevenueAmount), Orders = g.Sum(s => s.OrderCount) })
  46. .FirstOrDefaultAsync(ct);
  47. var list = await query
  48. .OrderByDescending(s => s.Year)
  49. .ThenByDescending(s => s.Month)
  50. .ThenBy(s => s.GameID)
  51. .Skip((request.Page - 1) * request.PerPage)
  52. .Take(request.PerPage)
  53. .Select(s => new
  54. {
  55. s.ID,
  56. s.GameID,
  57. GameName = s.Game!.KorName,
  58. GamePublisher = s.Game.Publisher,
  59. s.Year,
  60. s.Month,
  61. s.TotalRevenueAmount,
  62. s.OrderCount,
  63. s.Status,
  64. s.CreatedAt,
  65. s.SettledAt,
  66. s.SettledByAdminUserId,
  67. s.SettledByAdminEmail,
  68. s.AdminMemo
  69. })
  70. .ToListAsync(ct);
  71. var startNum = total - ((request.Page - 1) * request.PerPage);
  72. return new Response(
  73. Total: total,
  74. List: [.. list.Select((c, i) => new Response.Row(
  75. Num: startNum - i,
  76. c.ID,
  77. c.GameID,
  78. c.GameName,
  79. c.GamePublisher,
  80. c.Year,
  81. c.Month,
  82. c.TotalRevenueAmount,
  83. c.OrderCount,
  84. c.Status,
  85. c.CreatedAt,
  86. c.SettledAt,
  87. c.SettledByAdminUserId,
  88. c.SettledByAdminEmail,
  89. c.AdminMemo
  90. ))],
  91. Counts: statusCounts,
  92. Totals: new Response.Summary(
  93. TotalRevenue: summary?.Sum ?? 0,
  94. TotalOrders: summary?.Orders ?? 0
  95. )
  96. );
  97. }
  98. }