EmissionController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.AspNetCore.Mvc.Filters;
  3. using Microsoft.AspNetCore.WebUtilities;
  4. using System.Globalization;
  5. using economy.Helpers;
  6. using economy.Models;
  7. using economy.Models.Price.Domestic;
  8. using economy.Models.Price.Domestic.Emission;
  9. namespace economy.Controllers.Price.Domestic
  10. {
  11. public class EmissionController : Controller
  12. {
  13. private readonly DataGoKR _dataGoKR;
  14. private Dictionary<string, string> _queryString;
  15. public EmissionController(DataGoKR dataGoKR)
  16. {
  17. _dataGoKR = dataGoKR;
  18. _queryString = [];
  19. }
  20. public override void OnActionExecuting(ActionExecutingContext context)
  21. {
  22. _queryString = QueryHelpers.ParseQuery(HttpContext.Request.QueryString.Value).ToDictionary(k => k.Key, v => string.Join(",", v.Value));
  23. ViewBag.QueryString = _queryString;
  24. base.OnActionExecuting(context);
  25. }
  26. [HttpGet("Price/Domestic/Emission")]
  27. public async Task<IActionResult> Index(Request request)
  28. {
  29. if (!ModelState.IsValid)
  30. {
  31. return BadRequest(ModelState);
  32. }
  33. DomesticModel domesticModel = new DomesticModel(_dataGoKR, null);
  34. Response itemList = await domesticModel.GetEmissionPriceInfo(request);
  35. if (itemList.Body is not null)
  36. {
  37. int listNum = Common.CalcListNumber(itemList.Body.TotalCount, request.PageNo, request.NumOfRows);
  38. itemList.Body.Items.ItemList = itemList.Body.Items.ItemList.Select((row, index) =>
  39. {
  40. row.Num = listNum - index;
  41. row.Clpr = Common.NumberFormat(row.Clpr);
  42. row.Vs = Common.NumberFormat(row.Vs);
  43. row.FltRt = Common.NumberFormat(row.FltRt, "0.##");
  44. row.Mkp = Common.NumberFormat(row.Mkp);
  45. row.Hipr = Common.NumberFormat(row.Hipr);
  46. row.Lopr = Common.NumberFormat(row.Lopr);
  47. row.Trqu = Common.NumberFormat(row.Trqu);
  48. row.TrPrc = Common.NumberFormat(row.TrPrc);
  49. return row;
  50. }).ToList();
  51. }
  52. else
  53. {
  54. ViewBag.isError = true;
  55. ViewBag.errorMessage = "시세 정보를 가져오지 못했습니다. (외부 API 오류)";
  56. }
  57. // 차트용 데이터 조회 (같은 검색조건, 최대 300행)
  58. ChartData? chart = null;
  59. var chartRequest = new Request
  60. {
  61. PageNo = 1,
  62. NumOfRows = 300,
  63. StartDate = request.StartDate,
  64. EndDate = request.EndDate,
  65. LikeSrtnCd = request.LikeSrtnCd
  66. };
  67. Response chartList = await domesticModel.GetEmissionPriceInfo(chartRequest);
  68. if (chartList.Body?.Items?.ItemList is not null)
  69. {
  70. var rows = chartList.Body.Items.ItemList;
  71. var labels = rows.Select(r => r.BasDt).Distinct().OrderBy(d => d).ToList();
  72. chart = new ChartData
  73. {
  74. Labels = labels.Select(d => Common.StringToDateFormat(d)).ToList(),
  75. Series = rows.GroupBy(r => r.ItmsNm).Select(g => new ChartSeries
  76. {
  77. Name = g.Key,
  78. Values = labels.Select(d =>
  79. {
  80. var row = g.FirstOrDefault(r => r.BasDt == d);
  81. return (row is not null && decimal.TryParse(row.Clpr, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal value)) ? value : (decimal?)null;
  82. }).ToList()
  83. }).ToList()
  84. };
  85. }
  86. var viewModel = new View<Request, Response>();
  87. viewModel.SelectedListPerPage = request.NumOfRows;
  88. viewModel.Request = request;
  89. viewModel.Response = itemList;
  90. viewModel.Chart = chart;
  91. var queryString = new
  92. {
  93. sDate = request.StartDate.ToString("yyyy-MM-dd"),
  94. eDate = request.EndDate.ToString("yyyy-MM-dd")
  95. };
  96. var pagination = new Pagination(itemList.Body?.TotalCount, request.PageNo, request.NumOfRows, queryString);
  97. viewModel.Pagination = pagination;
  98. return View("/Views/Price/Domestic/Emission.cshtml", viewModel);
  99. }
  100. }
  101. }