using DataServer.api.EnergyEfficiency; using DongYingAPI.Util; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Data.Services; using System.Linq; using System.Net; using System.Net.Http; using System.Reflection; using System.Text; using System.Web.Http; namespace DongYingAPI.Controllers.api.EnergyEfficiency { public class GetEnergyConsumptionController : ApiController { DataServer.BLL.device_info device_bll = new DataServer.BLL.device_info(); DataServer.BLL.electricity_data bll = new DataServer.BLL.electricity_data(); /// /// 获取各回路(设备)能耗概况接口 /// /// 类型 年、月、日 /// public HttpResponseMessage Get(string type) { var res = new get_energy_consumption_response(); try { var now = DateTime.Now; var device_list = device_bll.GetModelList(""); var list = new List(); //判断表是否存在,不存在就创建 var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString(); var data = new List(); var edata = new List(); if (type == "日") { var time_count = Convert.ToInt32(DateTime.Now.Hour); for (int i = 0; i < time_count; i++) { var jdate = now.AddHours(-i).ToString("yyyy-MM-dd HH:00:00"); // 只处理今天的数据 if (DateTime.Now.AddHours(-i).Day != DateTime.Now.Day) { continue; } var jtime = now.AddHours(-i).ToString("yyyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime); if (a == false) { bll.CreateTable(jtime); } var sdate = now.AddHours(-(i + 1)).ToString("yyyy-MM-dd HH:00:00"); var stime = now.AddHours(-(i + 1)).ToString("yyyyMM"); var b = bll.IsExistsTable(date_base, "electricity_data_" + stime); if (b == false) { bll.CreateTable(stime); } var jlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var slist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var item in device_list) { var ljlist = jlist.FirstOrDefault(x => x.DeviceId == item.DeviceId); var lslist=slist.FirstOrDefault(x=>x.DeviceId== item.DeviceId); decimal? num1 = 0; decimal? num2 = 0; if (ljlist == null) { num1 = 0; } else { num1 = Convert.ToDecimal(ljlist.EH); } if (lslist == null) { num2 = 0; } else { num2 = Convert.ToDecimal(lslist.EH); } var EH = num1 - num2; if (EH < 0) { EH = 0; } var model = new energy_consumption { DeviceName = item.DeviceName, OperatingPower = Convert.ToDecimal(EH), time = Convert.ToDateTime(jdate).ToString("yyyy-MM-dd HH") }; data.Add(model); } } } if (type == "月") { var time_count = Convert.ToInt32(DateTime.Now.Day); for (int i = 0; i < time_count; i++) { string jdate; if (i == 0) { jdate = now.AddDays(-i).ToString("yyyy-MM-dd HH:00:00"); } else { jdate = now.AddDays(-i + 1).ToString("yyyy-MM-dd 00:00:00"); } // 只处理今月的数据 if (DateTime.Now.AddDays(-i).Month != DateTime.Now.Month) { continue; } var jtime = Convert.ToDateTime(jdate).ToString("yyyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime); if (a == false) { bll.CreateTable(jtime); } var sdate = now.AddDays(-(i)).ToString("yyyy-MM-dd 00:00:00"); var stime = now.AddDays(-(i)).ToString("yyyyMM"); var b = bll.IsExistsTable(date_base, "electricity_data_" + stime); if (b == false) { bll.CreateTable(stime); } var jlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var slist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var item in device_list) { var ljlist = jlist.FirstOrDefault(x => x.DeviceId == item.DeviceId); var lslist = slist.FirstOrDefault(x => x.DeviceId == item.DeviceId); decimal? num1 = 0; decimal? num2 = 0; if (ljlist == null) { num1 = 0; } else { num1 = Convert.ToDecimal(ljlist.EH); } if (lslist == null) { num2 = 0; } else { num2 = Convert.ToDecimal(lslist.EH); } var EH = num1 - num2; if (EH < 0) { EH = 0; } var model = new energy_consumption { DeviceName = item.DeviceName, OperatingPower = Convert.ToDecimal(EH), time = Convert.ToDateTime(jdate).ToString("yyyy-MM-dd ") }; data.Add(model); } } } if (type == "年") { var time_count = Convert.ToInt32(DateTime.Now.Month); for (int i = 0; i < time_count; i++) { // 只处理今年的数据 if (DateTime.Now.AddMonths(-i).Year != DateTime.Now.Year) { continue; } DateTime jdate; if (i == 0) { var jdate1 = now.AddMonths(-i).AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); jdate = Convert.ToDateTime(jdate1); } else { var jdate1 = now.AddMonths(-i + 1).ToString("yyyy-MM-01 00:00:00"); var jdate2 = Convert.ToDateTime(jdate1); jdate = jdate2.AddDays(-i); } // 只处理今年的数据 if (jdate.Year != DateTime.Now.Year) { continue; } var jtime = jdate.ToString("yyyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime); if (a == false) { bll.CreateTable(jtime); } var sdate1 = now.AddMonths(-(i)).ToString("yyyy-MM-01 00:00:00"); var sdate2 = Convert.ToDateTime(sdate1); var sdate = sdate2.AddDays(-0); var stime = sdate.ToString("yyyyMM"); var b = bll.IsExistsTable(date_base, "electricity_data_" + stime); if (b == false) { bll.CreateTable(stime); } var jlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var slist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var item in device_list) { var ljlist = jlist.FirstOrDefault(x => x.DeviceId == item.DeviceId); var lslist = slist.FirstOrDefault(x => x.DeviceId == item.DeviceId); decimal? num1 = 0; decimal? num2 = 0; if(ljlist== null) { num1 = 0; } else { num1 = Convert.ToDecimal(ljlist.EH); } if(lslist== null) { num2 = 0; } else { num2= Convert.ToDecimal(lslist.EH); } var EH = num1 - num2; if (EH < 0) { EH = 0; } var model = new energy_consumption { DeviceName = item.DeviceName, OperatingPower = Convert.ToDecimal(EH), time = Convert.ToDateTime(jdate).ToString("yyyy-MM") }; data.Add(model); } } } foreach (var item in device_list) { var alist=data.Where(x=>x.DeviceName==item.DeviceName).ToList(); var datemodel = alist.OrderByDescending(x => x.OperatingPower).FirstOrDefault(); var model = new energy_consumption { DeviceName = datemodel.DeviceName, OperatingPower = datemodel.OperatingPower, time = datemodel.time }; edata.Add(model); } var adata = edata.OrderBy(x => x.time).ToList(); res.code = 200; res.msg = "成功"; res.data = adata; } catch (Exception ex) { res.code = 500; res.msg = "失败," + ex.Message; } HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(JsonConvert.SerializeObject(res), Encoding.GetEncoding("UTF-8"), "application/json") }; return result; } } }