using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using SuperCms.Extensions; using SuperCms.Models; using SuperCms.Repositories; namespace SuperCms.Controllers { public class HomeController : BaseController { private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(Program)); ISuperCmsRepo iSuperCmsRepo; private OracleController oracle; public HomeController(ISuperCmsRepo ISuperCmsRepo) { iSuperCmsRepo = ISuperCmsRepo; } public IActionResult Index(String startDate, String endDate, String page) { // get all services try { if (!CheckAuthToken()) { //return Redirect("/Support/Index"); return Redirect("/Home/Login"); } oracle = new OracleController(iSuperCmsRepo); HomeViewModel model = new HomeViewModel { page = page != null ? page : "1", totalPage = "0", services = new ConnConfigs() }; model.services.data = new List(); model.connDatas = new ConnDatas(); model.connDatas.data = new List(); model.connDatasNow = new ConnDatas(); model.connDatasNow.data = new List(); model.connDatasBefore = new ConnDatas(); model.connDatasBefore.data = new List(); model.distributedDatas = new List(); model.telcos = new ConnTelcos(); model.telcos.data = new List(); model.companies = new ConnCompanies(); model.companies.data = new List(); ConnDatas tempData = new ConnDatas(); tempData.data = new List(); String start = startDate == null ? getStartByDayToString() : DateTime.ParseExact(startDate, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy HH:mm:ss"); String end = endDate == null ? getEndByDayToString() : DateTime.ParseExact(endDate, "yyyy-MM-ddTHH:mm", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy HH:mm:ss"); DateTime startDateTime = startDate == null ? getStartByDayToDate() : DateTime.ParseExact(startDate, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture); DateTime endDateTime = endDate == null ? getEndByDayToDate() : DateTime.ParseExact(endDate, "yyyy-MM-ddTHH:mm:ss", CultureInfo.InvariantCulture); ConnTelcos telcos = HttpContext.Session.GetComplexData("telcos"); if (telcos == null) { telcos = oracle.getConnTelcos(); HttpContext.Session.SetComplexData("telcos", telcos); } model.telcos = telcos; ConnCompanies companies = HttpContext.Session.GetComplexData("companies"); if (companies == null) { companies = oracle.getConnCompanies(); HttpContext.Session.SetComplexData("companies", companies); } model.companies = companies; ConnConfigs services = HttpContext.Session.GetComplexData("services"); if (services == null) { services = oracle.getConnConfigs(); HttpContext.Session.SetComplexData("services", services); } model.services = services; model.totalServices = services; // phan loai cac dich vu vao tung cong ty va tung thi truong List dis = HttpContext.Session.GetComplexData>("distributedByTelcoAndCompany"); if (dis == null) { List distributedByTelcoAndCompanyT = new List(); dis = distributedByTelcoAndCompany(model.telcos, model.companies, model.services, 0, distributedByTelcoAndCompanyT); HttpContext.Session.SetComplexData("distributedByTelcoAndCompany", dis); } int total = services.data.Count / int.Parse(UtilsController.Constant.NUMBER_ROW_ON_PAGE); if (total % int.Parse(UtilsController.Constant.NUMBER_ROW_ON_PAGE) != 0) { total++; } model.totalPage = total.ToString(); model.services = services; // get data var resultD = iSuperCmsRepo.GetConnData(log, start, end); var propertyD = typeof(ICollection).GetProperty("Count"); int countD = (int)propertyD.GetValue(resultD, null); if (resultD != null && countD > 0) { ConnDatas data = new ConnDatas(); data.data = new List(); foreach (IDictionary row in (IEnumerable)resultD) { var json = JsonConvert.SerializeObject(row); ConnData dataGet = new ConnData(json.ToString()); data.data.Add(dataGet); } model.connDatas = data; } // get only ROW services int startRow = (int.Parse(model.page) - 1) * int.Parse(UtilsController.Constant.NUMBER_ROW_ON_PAGE); int endRow = (int.Parse(model.page)) * int.Parse(UtilsController.Constant.NUMBER_ROW_ON_PAGE); ConnConfigs configs = new ConnConfigs(); configs.data = new List(); for (int c = startRow; c < (endRow > model.services.data.Count ? model.services.data.Count : endRow); c++) { configs.data.Add(model.services.data[c]); } model.services = configs; if (model.connDatas.data.Count > 0 && model.services.data.Count > 0) { for (int i = 0; i < model.services.data.Count; i++) { ConnConfig service = model.services.data[i]; List data = model.connDatas.data.FindAll(x => x.configID == service.id); data.Sort(delegate (ConnData x, ConnData y) { if (x.dateGet == null && y.dateGet == null) return 0; else if (x.dateGet == null) return -1; else if (y.dateGet == null) return 1; else return y.dateGet.CompareTo(x.dateGet); }); //if (data.Count > 0) //{ // model.connDatasNow.data.Add(data[0]); //} //if (data.Count > 1) //{ // model.connDatasBefore.data.Add(data[1]); //} DistributedData distributedData = new DistributedData(); distributedData.connDatas = new ConnDatas(); distributedData.connDatas.data = new List(); distributedData.service = service; distributedData.connDatas.data = data.Count > 0 ? data : new List(); model.distributedDatas.Add(distributedData); } } model.listDate = getRangeTime(startDateTime, endDateTime); // distributed by time model.distributedByTimes = new List(); for (int i = 0; i < model.listDate.Count - 1; i++) { DistributedByTime time = new DistributedByTime(); time.time = model.listDate[i]; time.connDatas = new ConnDatas(); time.connDatas.data = model.connDatas.data.FindAll(x => DateTime.Compare(model.listDate[i + 1], DateTime.ParseExact(x.dateGet + " 23:59:59", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture)) < 0 && DateTime.Compare(DateTime.ParseExact(x.dateGet + " 23:59:59", "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture), model.listDate[i]) < 0); model.distributedByTimes.Add(time); } model.connDatasNow = model.distributedByTimes[0].connDatas; return View("Index", model); } catch (Exception ex) { log.Error("Exception: ", ex); return Redirect("/Shared/Error"); } } public IActionResult Login() { return View(); } public IActionResult LoginAction(String account, String password) { if (account == null || password == null) { return Redirect("/Home/Login"); } // get all columns in table var result = iSuperCmsRepo.Login(log, account, password); var property = typeof(ICollection).GetProperty("Count"); int count = (int)property.GetValue(result, null); if (result != null && count > 0) { String number = "0"; foreach (IDictionary row in (IEnumerable)result) { var json = JObject.Parse(JsonConvert.SerializeObject(row)); number = (string)json["COUNT(*)"].ToString(); } if (number == "1") { CreateAuthToken(); return Redirect("/Home"); } else { return Redirect("/Home/Login"); } } else { return Redirect("/Home/Login"); } } public IActionResult Logout() { try { ClearCache(); return Redirect("/Home/Login"); } catch (Exception ex) { log.Error("Exception " + ex); return Redirect("/Shared/Error"); } } public IActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error() { return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } } }