| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- 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<ConnConfig>();
- model.connDatas = new ConnDatas();
- model.connDatas.data = new List<ConnData>();
- model.connDatasNow = new ConnDatas();
- model.connDatasNow.data = new List<ConnData>();
- model.connDatasBefore = new ConnDatas();
- model.connDatasBefore.data = new List<ConnData>();
- model.distributedDatas = new List<DistributedData>();
- model.telcos = new ConnTelcos();
- model.telcos.data = new List<ConnTelco>();
- model.companies = new ConnCompanies();
- model.companies.data = new List<ConnCompany>();
- ConnDatas tempData = new ConnDatas();
- tempData.data = new List<ConnData>();
- 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<ConnTelcos>("telcos");
- if (telcos == null)
- {
- telcos = oracle.getConnTelcos();
- HttpContext.Session.SetComplexData("telcos", telcos);
- }
- model.telcos = telcos;
- ConnCompanies companies = HttpContext.Session.GetComplexData<ConnCompanies>("companies");
- if (companies == null)
- {
- companies = oracle.getConnCompanies();
- HttpContext.Session.SetComplexData("companies", companies);
- }
- model.companies = companies;
- ConnConfigs services = HttpContext.Session.GetComplexData<ConnConfigs>("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<DistributedByTelcoAndCompany> dis = HttpContext.Session.GetComplexData<List<DistributedByTelcoAndCompany>>("distributedByTelcoAndCompany");
- if (dis == null)
- {
- List<DistributedByTelcoAndCompany> distributedByTelcoAndCompanyT = new List<DistributedByTelcoAndCompany>();
- 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<ConnData>();
- foreach (IDictionary<string, object> row in (IEnumerable<object>)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<ConnConfig>();
- 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<ConnData> 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<ConnData>();
- distributedData.service = service;
- distributedData.connDatas.data = data.Count > 0 ? data : new List<ConnData>();
- model.distributedDatas.Add(distributedData);
- }
- }
- model.listDate = getRangeTime(startDateTime, endDateTime);
- // distributed by time
- model.distributedByTimes = new List<DistributedByTime>();
- 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<string, object> row in (IEnumerable<object>)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 });
- }
- }
- }
|