| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using Application.Abstractions.Data;
- using Application.Abstractions.Messaging;
- using Domain.Entities.Donations.ValueObject;
- using Microsoft.EntityFrameworkCore;
- namespace Application.Features.Admin.Donation.Alert.Search;
- public sealed class Handler(IAppDbContext db) : IQueryHandler<Query, Response>
- {
- public async Task<Response> Handle(Query request, CancellationToken ct)
- {
- var query = db.DonationAlert.AsNoTracking().Include(x => x.Sponsor).Include(x => x.Receiver).AsQueryable();
- if (!string.IsNullOrWhiteSpace(request.Keyword))
- {
- query = request.Search switch
- {
- 1 => query.Where(x => x.Sponsor != null && x.Sponsor.ID.ToString().Contains(request.Keyword)),
- 2 => query.Where(x => x.Sponsor != null && x.Sponsor.SID.Contains(request.Keyword)),
- 3 => query.Where(x => x.Sponsor != null && x.Sponsor.Name != null && x.Sponsor.Name.Contains(request.Keyword)),
- 4 => query.Where(x => x.Receiver != null && x.Receiver.ID.ToString().Contains(request.Keyword)),
- 5 => query.Where(x => x.Receiver != null && x.Receiver.SID.Contains(request.Keyword)),
- 6 => query.Where(x => x.Receiver != null && x.Receiver.Name != null && x.Receiver.Name.Contains(request.Keyword)),
- _ => query
- };
- }
- if (!string.IsNullOrWhiteSpace(request.StartAt) && DateTime.TryParse(request.StartAt, out var startAt))
- {
- query = query.Where(x => x.CreatedAt >= startAt);
- }
- if (!string.IsNullOrWhiteSpace(request.EndAt) && DateTime.TryParse(request.EndAt, out var endAt))
- {
- query = query.Where(x => x.CreatedAt <= endAt);
- }
- // 탭별 건수 집계 (탭 필터 적용 전)
- var counts = await query.GroupBy(c => 1).Select(c => new
- {
- Received = c.Count(x => x.Status == AlertStatus.Delivered),
- NotReceived = c.Count(x => x.Status != AlertStatus.Delivered)
- }).FirstOrDefaultAsync(ct);
- var received = counts?.Received ?? 0;
- var notReceived = counts?.NotReceived ?? 0;
- query = request.Tab switch
- {
- 1 => query.Where(x => x.Status == AlertStatus.Delivered),
- 2 => query.Where(x => x.Status != AlertStatus.Delivered),
- _ => query
- };
- var total = await query.CountAsync(ct);
- var skip = (request.Page - 1) * request.PerPage;
- var list = await query
- .OrderByDescending(x => x.ID)
- .Skip(skip)
- .Take(request.PerPage)
- .Select(x => new
- {
- x.ID,
- x.DonationID,
- x.Status,
- x.StartAt,
- x.ArrivedAt,
- x.IpAddress,
- x.CreatedAt,
- SponsorID = x.Sponsor != null ? x.Sponsor.ID : 0,
- SponsorEmail = x.Sponsor != null ? x.Sponsor.Email : string.Empty,
- SponsorName = x.Sponsor != null ? x.Sponsor.Name : null,
- ReceiverID = x.Receiver != null ? x.Receiver.ID : 0,
- ReceiverEmail = x.Receiver != null ? x.Receiver.Email : string.Empty,
- ReceiverName = x.Receiver != null ? x.Receiver.Name : null
- })
- .ToListAsync(ct);
- var rows = list.Select((x, idx) => new Response.Row
- {
- Num = total - skip - idx,
- ID = x.ID,
- DonationID = x.DonationID,
- Sponsor = new Response.MemberInfo
- {
- ID = x.SponsorID,
- Email = x.SponsorEmail,
- Name = x.SponsorName
- },
- Receiver = new Response.MemberInfo
- {
- ID = x.ReceiverID,
- Email = x.ReceiverEmail,
- Name = x.ReceiverName
- },
- Status = x.Status.ToString(),
- IsReceived = x.Status == AlertStatus.Delivered,
- StartAt = x.StartAt,
- ArrivedAt = x.ArrivedAt,
- IpAddress = x.IpAddress,
- CreatedAt = x.CreatedAt
- }).ToList();
- return new Response
- {
- Total = total,
- Received = received,
- NotReceived = notReceived,
- List = rows
- };
- }
- }
|