using Application.Abstractions.Data; using Domain.Entities.Paper.ValueObject; using SharedKernel.Helpers; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using System.ComponentModel; using System.ComponentModel.DataAnnotations; namespace Admin.Pages.Paper.Orders; // 모의투자 주문 모니터 (읽기 전용) — d4 §⑤ /Paper/Orders public class IndexModel(IAppDbContext db) : 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("주문 상태")] public PaperOrderStatus? Status { get; set; } } public int Total { get; set; } public List<( int Num, int ID, string SID, string? Nickname, string StockCode, PaperOrderSide Side, PaperFillRule FillRule, int Quantity, decimal ReservedAmount, string TargetDate, PaperOrderStatus Status, string StatusText, string? RejectReason, string CreatedAt )> List { get; set; } = []; public Pagination? Pagination { get; set; } public async Task OnGetAsync(CancellationToken ct) { if (!ModelState.IsValid) { return; } var query = from o in db.PaperOrder.AsNoTracking() join a in db.PaperAccount.AsNoTracking() on o.AccountID equals a.ID join m in db.Member.AsNoTracking() on a.MemberID equals m.ID select new { o.ID, m.SID, Nickname = m.Name, o.StockCode, o.Side, o.FillRule, o.Quantity, o.ReservedAmount, o.TargetDate, o.Status, o.RejectReason, o.CreatedAt }; if (Query.Status.HasValue) { query = query.Where(c => c.Status == Query.Status.Value); } Total = await query.CountAsync(ct); var skip = (Query.PageNum - 1) * Query.PerPage; var rows = await query.OrderByDescending(c => c.ID).Skip(skip).Take(Query.PerPage).ToListAsync(ct); var num = skip + 1; List = [.. rows.Select(c => ( Num: num++, c.ID, c.SID, c.Nickname, c.StockCode, c.Side, c.FillRule, c.Quantity, c.ReservedAmount, TargetDate: c.TargetDate.ToString("yyyy-MM-dd"), c.Status, StatusText: StatusToText(c.Status), c.RejectReason, CreatedAt: c.CreatedAt.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") ))]; Pagination = new Pagination(Total, Query.PageNum, Query.PerPage); } public static string StatusToText(PaperOrderStatus s) => s switch { PaperOrderStatus.Pending => "접수", PaperOrderStatus.Filled => "체결", PaperOrderStatus.Cancelled => "취소", PaperOrderStatus.Rejected => "거부", _ => "?" }; public static string SideToText(PaperOrderSide s) => s switch { PaperOrderSide.Buy => "매수", PaperOrderSide.Sell => "매도", _ => "?" }; public static string FillRuleToText(PaperFillRule r) => r switch { PaperFillRule.Open => "시가", PaperFillRule.Close => "종가", _ => "?" }; }