using Domain.Entities.Store.ValueObject; using SharedKernel.Extensions; using SharedKernel.Helpers; using Application.Abstractions.Messaging; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace Admin.Pages.Store.Shipment; public class IndexModel(IMediator mediator) : PageModel { [BindProperty(SupportsGet = true)] public QueryParams Query { get; set; } = new(); public sealed class QueryParams { [Range(1, int.MaxValue)] public int PageNum { get; set; } = 1; [Range(1, 100)] public ushort PerPage { get; set; } = 20; [DisplayName("검색 조건")] [Range(1, 3, ErrorMessage = "{0}이(가) 올바르지 않습니다.")] public int? Search { get; set; } [DisplayName("검색어")] [MaxLength(100)] public string? Keyword { get; set; } [DisplayName("시작일")] public string? StartAt { get; set; } [DisplayName("종료일")] public string? EndAt { get; set; } [DisplayName("배송 상태")] public ShipmentStatus? Status { get; set; } } public int Total { get; set; } public SearchShipments.Response.StatusCounts? Counts { get; set; } public List<( int Num, int ID, int OrderID, string OrderNumber, string BuyerEmail, ShipmentStatus Status, string StatusText, string? Carrier, string? TrackingNumber, string CreatedAt, string? ShippedAt, string? DeliveredAt, string DetailURL )> List { get; set; } = []; public Pagination? Pagination { get; set; } public async Task OnGetAsync(CancellationToken ct) { if (!ModelState.IsValid) { return; } var result = await mediator.Send(new SearchShipments.Query( Query.Search, Query.Keyword, Query.StartAt, Query.EndAt, Query.Status, Query.PageNum, Query.PerPage ), ct); Total = result.Total; Counts = result.Counts; var qs = Request.QueryString.ToString(); List = [.. result.List.Select(c => ( c.Num, c.ID, c.OrderID, c.OrderNumber, c.BuyerEmail, c.Status, StatusText: StatusToText(c.Status), c.Carrier, c.TrackingNumber, c.CreatedAt.GetDateAt(), c.ShippedAt?.ToString("yyyy-MM-dd HH:mm"), c.DeliveredAt?.ToString("yyyy-MM-dd HH:mm"), DetailURL: $"/Store/Shipment/Detail/{c.ID}{qs}" ))]; Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage); } public static string StatusToText(ShipmentStatus s) => s switch { ShipmentStatus.Preparing => "준비중", ShipmentStatus.Pickup => "집화", ShipmentStatus.InTransit => "배송중", ShipmentStatus.Delivered => "배송완료", ShipmentStatus.Failed => "실패", _ => "?" }; }