| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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<IActionResult> 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<ConnCkNganh, Dictionary<DateTime, ConnCkData>> dictionaryData =
- new Dictionary<ConnCkNganh, Dictionary<DateTime, ConnCkData>>();
- // distribution to nhom nganh
- for (int i = 0; i < nganh.Count; i++)
- {
- // phan bo ve cac ngay
- //List<Dictionary<DateTime, List<ConnCkData>>> keyValuePairsList = new List<Dictionary<DateTime, List<ConnCkData>>>();
- Dictionary<DateTime, ConnCkData> keyValuePairs = new Dictionary<DateTime, ConnCkData>();
- 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<IActionResult> 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<DateTime> listDate = getRangeTime(fromDateR, toDateR);
- Dictionary<ConnCkNganh, Dictionary<DateTime, ConnCkData>> dictionaryData =
- new Dictionary<ConnCkNganh, Dictionary<DateTime, ConnCkData>>();
- // distribution to nhom nganh
- for (int i = 0; i < nganh.Count; i++)
- {
- // phan bo ve cac ngay
- //List<Dictionary<DateTime, List<ConnCkData>>> keyValuePairsList = new List<Dictionary<DateTime, List<ConnCkData>>>();
- Dictionary<DateTime, ConnCkData> keyValuePairs = new Dictionary<DateTime, ConnCkData>();
- 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");
- }
- }
- }
- }
|