StockController.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.AspNetCore.Mvc.Rendering;
  8. using Microsoft.EntityFrameworkCore;
  9. using Microsoft.Extensions.Configuration;
  10. using SuperCms.Database;
  11. using SuperCms.Models;
  12. namespace SuperCms.Controllers
  13. {
  14. public class StockController : BaseController
  15. {
  16. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program));
  17. private readonly ModelContext _context;
  18. IConfiguration configuration;
  19. public StockController(ModelContext context)
  20. {
  21. _context = context;
  22. }
  23. // GET: ConnCkDatas
  24. public async Task<IActionResult> Index(String fromDate, String toDate)
  25. {
  26. if (!CheckAuthToken())
  27. {
  28. return Redirect("/Home/Login");
  29. }
  30. StockViewModel model = new StockViewModel();
  31. try
  32. {
  33. var nganh = await _context.ConnCkNganh.ToListAsync();
  34. //var data = await _context.ConnCkData.ToListAsync();
  35. model.stockNganh = nganh;
  36. DateTime fromDateR = fromDate == null ? DateTime.Now.AddDays(-10) :
  37. DateTime.ParseExact(fromDate, UtilsController.validformats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
  38. DateTime toDateR = toDate == null ? DateTime.Now.AddDays(-1) :
  39. DateTime.ParseExact(toDate, UtilsController.validformats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
  40. model.fromDate = fromDateR.ToString("dd/MM/yyyy");
  41. model.toDate = toDateR.ToString("dd/MM/yyyy");
  42. var dataGet = _context.ConnCkData.ToList().Where(a =>
  43. DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) >= fromDateR &&
  44. DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) <= toDateR
  45. ).ToList();
  46. model.stockData = dataGet;
  47. model.listDate = getRangeTime(fromDateR, toDateR);
  48. Dictionary<ConnCkNganh, Dictionary<DateTime, ConnCkData>> dictionaryData =
  49. new Dictionary<ConnCkNganh, Dictionary<DateTime, ConnCkData>>();
  50. // distribution to nhom nganh
  51. for (int i = 0; i < nganh.Count; i++)
  52. {
  53. // phan bo ve cac ngay
  54. //List<Dictionary<DateTime, List<ConnCkData>>> keyValuePairsList = new List<Dictionary<DateTime, List<ConnCkData>>>();
  55. Dictionary<DateTime, ConnCkData> keyValuePairs = new Dictionary<DateTime, ConnCkData>();
  56. for (int j = 0; j < model.listDate.Count; j++)
  57. {
  58. ConnCkData dataNganh = dataGet.Find(a => a.NganhId == nganh[i].Id &&
  59. DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) == model.listDate[j]
  60. );
  61. keyValuePairs.Add(model.listDate[j], dataNganh);
  62. //keyValuePairsList.Add(keyValuePairs);
  63. }
  64. dictionaryData.Add(nganh[i], keyValuePairs);
  65. }
  66. model.distributeData = dictionaryData;
  67. }
  68. catch (Exception ex)
  69. {
  70. log.Error("Exception: ", ex);
  71. return Redirect("/Home");
  72. }
  73. return View(model);
  74. }
  75. [ValidateAntiForgeryToken]
  76. public async Task<IActionResult> ExportAsync(String fromDate, String toDate)
  77. {
  78. try
  79. {
  80. var nganh = await _context.ConnCkNganh.ToListAsync();
  81. DateTime fromDateR = fromDate == null ? DateTime.Now.AddDays(-10) :
  82. DateTime.ParseExact(fromDate, UtilsController.validformats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
  83. DateTime toDateR = toDate == null ? DateTime.Now.AddDays(-1) :
  84. DateTime.ParseExact(toDate, UtilsController.validformats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
  85. var dataGet = _context.ConnCkData.ToList().Where(a =>
  86. DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) >= fromDateR &&
  87. DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) <= toDateR
  88. ).ToList();
  89. List<DateTime> listDate = getRangeTime(fromDateR, toDateR);
  90. Dictionary<ConnCkNganh, Dictionary<DateTime, ConnCkData>> dictionaryData =
  91. new Dictionary<ConnCkNganh, Dictionary<DateTime, ConnCkData>>();
  92. // distribution to nhom nganh
  93. for (int i = 0; i < nganh.Count; i++)
  94. {
  95. // phan bo ve cac ngay
  96. //List<Dictionary<DateTime, List<ConnCkData>>> keyValuePairsList = new List<Dictionary<DateTime, List<ConnCkData>>>();
  97. Dictionary<DateTime, ConnCkData> keyValuePairs = new Dictionary<DateTime, ConnCkData>();
  98. for (int j = 0; j < listDate.Count; j++)
  99. {
  100. ConnCkData dataNganh = dataGet.Find(a => a.NganhId == nganh[i].Id &&
  101. DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) == listDate[j]
  102. );
  103. keyValuePairs.Add(listDate[j], dataNganh);
  104. //keyValuePairsList.Add(keyValuePairs);
  105. }
  106. dictionaryData.Add(nganh[i], keyValuePairs);
  107. }
  108. return ToExcelStock(listDate, dictionaryData);
  109. }
  110. catch (Exception ex)
  111. {
  112. log.Error("Exception: ", ex);
  113. return Redirect("/Home");
  114. }
  115. }
  116. }
  117. }