OilController.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.Oil;
  9. namespace economy.Controllers.Price.Domestic
  10. {
  11. public class OilController : Controller
  12. {
  13. private readonly DataGoKR _dataGoKR;
  14. private Dictionary<string, string> _queryString;
  15. public OilController(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/Oil")]
  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.GetOilPriceInfo(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.OilCtg = Common.NumberFormat(row.OilCtg);
  42. row.WtAvgPrcCptn = Common.NumberFormat(row.WtAvgPrcCptn, "0.##");
  43. row.WtAvgPrcDisc = Common.NumberFormat(row.WtAvgPrcDisc);
  44. row.Trqu = Common.NumberFormat(row.Trqu);
  45. row.TrPrc = Common.NumberFormat(row.TrPrc);
  46. return row;
  47. }).ToList();
  48. }
  49. else
  50. {
  51. ViewBag.isError = true;
  52. ViewBag.errorMessage = "시세 정보를 가져오지 못했습니다. (외부 API 오류)";
  53. }
  54. // 차트용 데이터 조회 (같은 검색조건, 최대 300행)
  55. ChartData? chart = null;
  56. var chartRequest = new Request
  57. {
  58. PageNo = 1,
  59. NumOfRows = 300,
  60. StartDate = request.StartDate,
  61. EndDate = request.EndDate
  62. };
  63. Response chartList = await domesticModel.GetOilPriceInfo(chartRequest);
  64. if (chartList.Body?.Items?.ItemList is not null)
  65. {
  66. var rows = chartList.Body.Items.ItemList;
  67. var labels = rows.Select(r => r.BasDt).Distinct().OrderBy(d => d).ToList();
  68. chart = new ChartData
  69. {
  70. Labels = labels.Select(d => Common.StringToDateFormat(d)).ToList(),
  71. Series = rows.GroupBy(r => r.OilCtg).Select(g => new ChartSeries
  72. {
  73. Name = g.Key,
  74. Values = labels.Select(d =>
  75. {
  76. var row = g.FirstOrDefault(r => r.BasDt == d);
  77. return (row is not null && decimal.TryParse(row.WtAvgPrcCptn, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal value)) ? value : (decimal?)null;
  78. }).ToList()
  79. }).ToList()
  80. };
  81. }
  82. var viewModel = new View<Request, Response>();
  83. viewModel.SelectedListPerPage = request.NumOfRows;
  84. viewModel.Request = request;
  85. viewModel.Response = itemList;
  86. viewModel.Chart = chart;
  87. var queryString = new
  88. {
  89. sDate = request.StartDate.ToString("yyyy-MM-dd"),
  90. eDate = request.EndDate.ToString("yyyy-MM-dd")
  91. };
  92. var pagination = new Pagination(itemList.Body?.TotalCount, request.PageNo, request.NumOfRows, queryString);
  93. viewModel.Pagination = pagination;
  94. return View("/Views/Price/Domestic/Oil.cshtml", viewModel);
  95. }
  96. }
  97. }