using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using SuperCms.Database; using SuperCms.Models; namespace SuperCms.Controllers { public class StockController : BaseController { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); private readonly ModelContext _context; IConfiguration configuration; public StockController(ModelContext context) { _context = context; } // GET: ConnCkDatas public async Task Index(String fromDate, String toDate) { if (!CheckAuthToken()) { return Redirect("/Home/Login"); } StockViewModel model = new StockViewModel(); try { var nganh = await _context.ConnCkNganh.ToListAsync(); //var data = await _context.ConnCkData.ToListAsync(); model.stockNganh = nganh; DateTime fromDateR = fromDate == null ? DateTime.Now.AddDays(-10) : DateTime.ParseExact(fromDate, UtilsController.validformats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); DateTime toDateR = toDate == null ? DateTime.Now.AddDays(-1) : DateTime.ParseExact(toDate, UtilsController.validformats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); model.fromDate = fromDateR.ToString("dd/MM/yyyy"); model.toDate = toDateR.ToString("dd/MM/yyyy"); var dataGet = _context.ConnCkData.ToList().Where(a => DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) >= fromDateR && DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) <= toDateR ).ToList(); model.stockData = dataGet; model.listDate = getRangeTime(fromDateR, toDateR); Dictionary> dictionaryData = new Dictionary>(); // distribution to nhom nganh for (int i = 0; i < nganh.Count; i++) { // phan bo ve cac ngay //List>> keyValuePairsList = new List>>(); Dictionary keyValuePairs = new Dictionary(); for (int j = 0; j < model.listDate.Count; j++) { ConnCkData dataNganh = dataGet.Find(a => a.NganhId == nganh[i].Id && DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) == model.listDate[j] ); keyValuePairs.Add(model.listDate[j], dataNganh); //keyValuePairsList.Add(keyValuePairs); } dictionaryData.Add(nganh[i], keyValuePairs); } model.distributeData = dictionaryData; } catch (Exception ex) { log.Error("Exception: ", ex); return Redirect("/Home"); } return View(model); } [ValidateAntiForgeryToken] public async Task ExportAsync(String fromDate, String toDate) { try { var nganh = await _context.ConnCkNganh.ToListAsync(); DateTime fromDateR = fromDate == null ? DateTime.Now.AddDays(-10) : DateTime.ParseExact(fromDate, UtilsController.validformats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); DateTime toDateR = toDate == null ? DateTime.Now.AddDays(-1) : DateTime.ParseExact(toDate, UtilsController.validformats, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal); var dataGet = _context.ConnCkData.ToList().Where(a => DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) >= fromDateR && DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) <= toDateR ).ToList(); List listDate = getRangeTime(fromDateR, toDateR); Dictionary> dictionaryData = new Dictionary>(); // distribution to nhom nganh for (int i = 0; i < nganh.Count; i++) { // phan bo ve cac ngay //List>> keyValuePairsList = new List>>(); Dictionary keyValuePairs = new Dictionary(); for (int j = 0; j < listDate.Count; j++) { ConnCkData dataNganh = dataGet.Find(a => a.NganhId == nganh[i].Id && DateTime.ParseExact(a.Ngay, "dd/MM/yyyy", CultureInfo.InvariantCulture) == listDate[j] ); keyValuePairs.Add(listDate[j], dataNganh); //keyValuePairsList.Add(keyValuePairs); } dictionaryData.Add(nganh[i], keyValuePairs); } return ToExcelStock(listDate, dictionaryData); } catch (Exception ex) { log.Error("Exception: ", ex); return Redirect("/Home"); } } } }