public JsonResult GetGasData() { using (DBModel db = new DBModel()) { DateTime today = DateTime.Today.Date; var gas = db.gas.FirstOrDefault(); var gas_recoder_begin = db.recoder_gas.Where(c => c.Thoigian >= startYesterdayTime && c.Thoigian <= endYesterdayTime).OrderByDescending(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var gas_view = new GasView(); if (gas != null) { var gas_total_last = (gas_recoder_begin != null) ? gas_recoder_begin.luu_luong_tong : "0"; gas_view.luu_luong_hien_tai = ((float)Math.Round(float.Parse(gas.luu_luong_hien_tai) * 10f) / 10f).ToString(); gas_view.luu_luong_tong = ((float)Math.Round(float.Parse(gas.luu_luong_tong) * 10f) / 10f).ToString(); gas_view.luu_luong_tong_ngay = ((float)Math.Round((float.Parse(gas.luu_luong_tong) - float.Parse(gas_total_last)) * 10f) / 10f).ToString(); gas_view.status = gas.status; } return(Json(gas_view, JsonRequestBehavior.AllowGet)); } }
public ActionResult Index() { if (Session["UserID"] != null) { using (DBModel db = new DBModel()) { //Get Gas table var gas = db.gas.FirstOrDefault(); var gas_recoder_begin = db.recoder_gas.Where(c => c.Thoigian >= startYesterdayTime && c.Thoigian <= endYesterdayTime).OrderByDescending(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var gas_view = new GasView(); if (gas != null) { var gas_total_last = (gas_recoder_begin != null) ? gas_recoder_begin.luu_luong_tong : "0"; gas_view.luu_luong_hien_tai = string.Format("{0:#,##0.##}", ((float)Math.Round(float.Parse(gas.luu_luong_hien_tai) * 10f) / 10f)); gas_view.luu_luong_tong = string.Format("{0:#,##0.##}", ((float)Math.Round(float.Parse(gas.luu_luong_tong) * 10f) / 10f)); gas_view.luu_luong_tong_ngay = string.Format("{0:#,##0.##}", ((float)Math.Round((float.Parse(gas.luu_luong_tong) - float.Parse(gas_total_last)) * 10f) / 10f)); gas_view.status = gas.status; } //Get Water table var water = db.water.FirstOrDefault(); var water_recoder_begin = db.recoder_water.Where(c => c.Thoigian >= today).OrderBy(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var water_view = new WaterView(); if (water != null) { var water_total_last = (water_recoder_begin != null) ? water_recoder_begin.luu_luong_tong : "0"; water_view.luu_luong_hien_tai = string.Format("{0:#,##0.##}", ((float)Math.Round(float.Parse(water.luu_luong_hien_tai) * 10f) / 10f)); water_view.luu_luong_tong = string.Format("{0:#,##0.##}", ((float)Math.Round(float.Parse(water.luu_luong_tong) * 10f) / 10f)); water_view.luu_luong_tong_ngay = string.Format("{0:#,##0.##}", ((float)Math.Round((float.Parse(water.luu_luong_tong) - float.Parse(water_total_last)) * 10f) / 10f)); water_view.status = water.status; } var hienthi = db.hienthi.Select(i => new HienthiOverView { Kenh = i.Kenh, Ptotal = i.Ptotal, Kwh = i.Kwh, KWhDay = "0" }).ToList(); //var hienthi = db.hienthi.Select(i => new { i.Kenh, i.Ptotal, i.Kwh }).ToList(); var recoder_kenh1_begin = db.recoder_kenh1.Where(c => c.Thoigian >= today).OrderBy(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var recoder_kenh2_begin = db.recoder_kenh2.Where(c => c.Thoigian >= today).OrderBy(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var recoder_kenh3_begin = db.recoder_kenh3.Where(c => c.Thoigian >= today).OrderBy(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var recoder_kenh4_begin = db.recoder_kenh4.Where(c => c.Thoigian >= today).OrderBy(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var recoder_kenh5_begin = db.recoder_kenh5.Where(c => c.Thoigian >= today).OrderBy(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var recoder_kenh6_begin = db.recoder_kenh6.Where(c => c.Thoigian >= today).OrderBy(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var kenh1 = new HienthiOverView(); var kenh2 = new HienthiOverView(); var kenh3 = new HienthiOverView(); var kenh4 = new HienthiOverView(); var kenh5 = new HienthiOverView(); var kenh6 = new HienthiOverView(); float sum_Ptotal = 0; float sum_Kwh = 0; float sum_Kwh_inday = 0; if (hienthi != null) { var kenh1_Kwh_last = (recoder_kenh1_begin != null) ? recoder_kenh1_begin.Kwh : "0"; var kenh2_Kwh_last = (recoder_kenh2_begin != null) ? recoder_kenh2_begin.Kwh : "0"; var kenh3_Kwh_last = (recoder_kenh3_begin != null) ? recoder_kenh3_begin.Kwh : "0"; var kenh4_Kwh_last = (recoder_kenh4_begin != null) ? recoder_kenh4_begin.Kwh : "0"; var kenh5_Kwh_last = (recoder_kenh5_begin != null) ? recoder_kenh5_begin.Kwh : "0"; var kenh6_Kwh_last = (recoder_kenh6_begin != null) ? recoder_kenh6_begin.Kwh : "0"; for (var i = 0; i < hienthi.Count; i++) { switch (hienthi[i].Kenh) { case "1": kenh1.Kenh = hienthi[i].Kenh; kenh1.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh1.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh1_2009) * 10f) / 10f).ToString(); kenh1.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh1_Kwh_last)) * 10f) / 10f).ToString(); sum_Kwh_inday += float.Parse(kenh1.KWhDay); sum_Kwh += float.Parse(kenh1.Kwh); break; case "2": kenh2.Kenh = hienthi[i].Kenh; kenh2.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh2.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh2_2009) * 10f) / 10f).ToString(); kenh2.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh2_Kwh_last)) * 10f) / 10f).ToString(); sum_Kwh_inday += float.Parse(kenh2.KWhDay); sum_Kwh += float.Parse(kenh2.Kwh); break; case "3": kenh3.Kenh = hienthi[i].Kenh; kenh3.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh3.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh3_2009) * 10f) / 10f).ToString(); kenh3.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh3_Kwh_last)) * 10f) / 10f).ToString(); sum_Kwh_inday += float.Parse(kenh3.KWhDay); sum_Kwh += float.Parse(kenh3.Kwh); break; case "4": kenh4.Kenh = hienthi[i].Kenh; kenh4.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh4.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh4_2009) * 10f) / 10f).ToString(); kenh4.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh4_Kwh_last)) * 10f) / 10f).ToString(); sum_Kwh_inday += float.Parse(kenh4.KWhDay); sum_Kwh += float.Parse(kenh4.Kwh); break; case "5": kenh5.Kenh = hienthi[i].Kenh; kenh5.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh5.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh5_2009) * 10f) / 10f).ToString(); kenh5.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh5_Kwh_last)) * 10f) / 10f).ToString(); sum_Kwh_inday += float.Parse(kenh5.KWhDay); sum_Kwh += float.Parse(kenh5.Kwh); break; case "6": kenh6.Kenh = hienthi[i].Kenh; kenh6.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh6.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh6_2009) * 10f) / 10f).ToString(); kenh6.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh6_Kwh_last)) * 10f) / 10f).ToString(); sum_Kwh_inday += float.Parse(kenh6.KWhDay); sum_Kwh += float.Parse(kenh6.Kwh); break; default: break; } sum_Ptotal += float.Parse(hienthi[i].Ptotal); } } ViewBag.sumPtotal = string.Format("{0:#,##0.##}", (float)Math.Round(sum_Ptotal * 10f) / 10f); ViewBag.sumKwh = string.Format("{0:#,##0.##}", (float)Math.Round((sum_Kwh / 1000) * 10f) / 10f); ViewBag.sumKwhInday = string.Format("{0:#,##0.##}", (float)Math.Round(sum_Kwh_inday * 10f) / 10f); ViewBag.gas = gas_view; ViewBag.water = water_view; } return(View()); } else { return(RedirectToAction("Login", "Account")); } }
public JsonResult GetIndexData() { using (DBModel db = new DBModel()) { //Get Gas table var gas = db.gas.FirstOrDefault(); var gas_recoder_begin = db.recoder_gas.Where(c => c.Thoigian >= startYesterdayTime && c.Thoigian <= endYesterdayTime).OrderByDescending(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var gas_view = new GasView(); if (gas != null) { var gas_total_last = (gas_recoder_begin != null) ? gas_recoder_begin.luu_luong_tong : "0"; gas_view.luu_luong_hien_tai = ((float)Math.Round(float.Parse(gas.luu_luong_hien_tai) * 10f) / 10f).ToString(); gas_view.luu_luong_tong = ((float)Math.Round(float.Parse(gas.luu_luong_tong) * 10f) / 10f).ToString(); gas_view.luu_luong_tong_ngay = ((float)Math.Round((float.Parse(gas.luu_luong_tong) - float.Parse(gas_total_last)) * 10f) / 10f).ToString(); gas_view.status = gas.status; } //Get Water table var water = db.water.FirstOrDefault(); var water_recoder_begin = db.recoder_water.Where(c => c.Thoigian >= startYesterdayTime && c.Thoigian <= endYesterdayTime).OrderByDescending(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var water_view = new WaterView(); if (water != null) { var water_total_last = (water_recoder_begin != null) ? water_recoder_begin.luu_luong_tong : "0"; water_view.luu_luong_hien_tai = ((float)Math.Round(float.Parse(water.luu_luong_hien_tai) * 10f) / 10f).ToString(); water_view.luu_luong_tong = ((float)Math.Round(float.Parse(water.luu_luong_tong) * 10f) / 10f).ToString(); water_view.luu_luong_tong_ngay = ((float)Math.Round((float.Parse(water.luu_luong_tong) - float.Parse(water_total_last)) * 10f) / 10f).ToString(); water_view.status = water.status; } var hienthi = db.hienthi.Select(i => new HienthiOverView { Kenh = i.Kenh, Ptotal = i.Ptotal, Kwh = i.Kwh, KWhDay = "0" }).ToList(); //var hienthi = db.hienthi.Select(i => new { i.Kenh, i.Ptotal, i.Kwh }).ToList(); var recoder_kenh1_begin = db.recoder_kenh1.Where(c => c.Thoigian >= startYesterdayTime && c.Thoigian <= endYesterdayTime).OrderByDescending(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var recoder_kenh2_begin = db.recoder_kenh2.Where(c => c.Thoigian >= startYesterdayTime && c.Thoigian <= endYesterdayTime).OrderByDescending(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var recoder_kenh3_begin = db.recoder_kenh3.Where(c => c.Thoigian >= startYesterdayTime && c.Thoigian <= endYesterdayTime).OrderByDescending(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var recoder_kenh4_begin = db.recoder_kenh4.Where(c => c.Thoigian >= startYesterdayTime && c.Thoigian <= endYesterdayTime).OrderByDescending(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var recoder_kenh5_begin = db.recoder_kenh5.Where(c => c.Thoigian >= startYesterdayTime && c.Thoigian <= endYesterdayTime).OrderByDescending(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var recoder_kenh6_begin = db.recoder_kenh6.Where(c => c.Thoigian >= startYesterdayTime && c.Thoigian <= endYesterdayTime).OrderByDescending(x => x.Thoigian) .Take(1).ToList().FirstOrDefault(); var kenh1 = new HienthiOverView(); var kenh2 = new HienthiOverView(); var kenh3 = new HienthiOverView(); var kenh4 = new HienthiOverView(); var kenh5 = new HienthiOverView(); var kenh6 = new HienthiOverView(); float sum_ptotal = 0; float sum_kwh = 0; float sum_kwh_inday = 0; var hienthi_overview = new HienthiOverView(); if (hienthi != null) { var kenh1_Kwh_last = (recoder_kenh1_begin != null) ? recoder_kenh1_begin.Kwh : "0"; var kenh2_Kwh_last = (recoder_kenh2_begin != null) ? recoder_kenh2_begin.Kwh : "0"; var kenh3_Kwh_last = (recoder_kenh3_begin != null) ? recoder_kenh3_begin.Kwh : "0"; var kenh4_Kwh_last = (recoder_kenh4_begin != null) ? recoder_kenh4_begin.Kwh : "0"; var kenh5_Kwh_last = (recoder_kenh5_begin != null) ? recoder_kenh5_begin.Kwh : "0"; var kenh6_Kwh_last = (recoder_kenh6_begin != null) ? recoder_kenh6_begin.Kwh : "0"; for (var i = 0; i < hienthi.Count; i++) { switch (hienthi[i].Kenh) { case "1": kenh1.Kenh = hienthi[i].Kenh; kenh1.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh1.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh1_2009) * 10f) / 10f).ToString(); kenh1.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh1_Kwh_last)) * 10f) / 10f).ToString(); sum_ptotal += float.Parse(kenh1.Ptotal); sum_kwh_inday += float.Parse(kenh1.KWhDay); sum_kwh += float.Parse(kenh1.Kwh); break; case "2": kenh2.Kenh = hienthi[i].Kenh; kenh2.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh2.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh2_2009) * 10f) / 10f).ToString(); kenh2.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh2_Kwh_last)) * 10f) / 10f).ToString(); sum_ptotal += float.Parse(kenh2.Ptotal); sum_kwh_inday += float.Parse(kenh2.KWhDay); sum_kwh += float.Parse(kenh2.Kwh); break; case "3": kenh3.Kenh = hienthi[i].Kenh; kenh3.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh3.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh3_2009) * 10f) / 10f).ToString(); kenh3.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh3_Kwh_last)) * 10f) / 10f).ToString(); sum_ptotal += float.Parse(kenh3.Ptotal); sum_kwh_inday += float.Parse(kenh3.KWhDay); sum_kwh += float.Parse(kenh3.Kwh); break; case "4": kenh4.Kenh = hienthi[i].Kenh; kenh4.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh4.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh4_2009) * 10f) / 10f).ToString(); kenh4.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh4_Kwh_last)) * 10f) / 10f).ToString(); sum_ptotal += float.Parse(kenh4.Ptotal); sum_kwh_inday += float.Parse(kenh4.KWhDay); sum_kwh += float.Parse(kenh4.Kwh); break; case "5": kenh5.Kenh = hienthi[i].Kenh; kenh5.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh5.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh5_2009) * 10f) / 10f).ToString(); kenh5.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh5_Kwh_last)) * 10f) / 10f).ToString(); sum_ptotal += float.Parse(kenh5.Ptotal); sum_kwh_inday += float.Parse(kenh5.KWhDay); sum_kwh += float.Parse(kenh5.Kwh); break; case "6": kenh6.Kenh = hienthi[i].Kenh; kenh6.Ptotal = ((float)Math.Round(float.Parse(hienthi[i].Ptotal) * 10f) / 10f).ToString(); kenh6.Kwh = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - kenh6_2009) * 10f) / 10f).ToString(); kenh6.KWhDay = ((float)Math.Round((float.Parse(hienthi[i].Kwh) - float.Parse(kenh6_Kwh_last)) * 10f) / 10f).ToString(); sum_ptotal += float.Parse(kenh6.Ptotal); sum_kwh_inday += float.Parse(kenh6.KWhDay); sum_kwh += float.Parse(kenh6.Kwh); break; default: break; } } } hienthi_overview.Ptotal = ((float)Math.Round(sum_ptotal * 10f) / 10f).ToString(); hienthi_overview.KWhDay = ((float)Math.Round(sum_kwh_inday * 10f) / 10f).ToString(); hienthi_overview.Kwh = ((float)Math.Round((sum_kwh / 1000) * 10f) / 10f).ToString(); var data = new List <object>(); data.Add(gas_view); data.Add(water_view); data.Add(hienthi_overview); var result = data; return(Json(result, JsonRequestBehavior.AllowGet)); } }