using DataServer.api; using Microsoft.Ajax.Utilities; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Web.Http; namespace DongYingAPI.Controllers.api { /// /// 用能监测1-系统用电排名 /// public class GetElectricalRankingController : ApiController { DataServer.BLL.electricity_data bll = new DataServer.BLL.electricity_data(); DataServer.BLL.device_info bll_info = new DataServer.BLL.device_info(); public HttpResponseMessage Get() { var res = new get_electrical_ranking(); //表名 var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString(); try { var data = new List(); #region 日期 //今天 var now = DateTime.Now; var jdate = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); var jtime = now.ToString("yyyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime); if (a == false) { bll.CreateTable(jtime); } //昨天 var zdate = DateTime.Now.ToString("yyyy-MM-dd 00:00:00"); var ztime = now.ToString("yyyyMM"); var b = bll.IsExistsTable(date_base, "electricity_data_" + ztime); if (b == false) { bll.CreateTable(ztime); } #endregion var alist = bll_info.GetModelList(" DeviceName LIKE '%空调%'"); var model1 = new electrical_rankingData(); decimal? num = 0; decimal? anum = 0; foreach (var item in alist) { //今天 var list = bll.GetModelListDate(" DeviceId='"+ item.DeviceId + "' and EntireTime='"+ jdate + "'", jtime); num +=Convert.ToDecimal(list.Sum(x => Convert.ToDecimal(x.EH))); //昨天 var zlist = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + zdate + "'", jtime); anum += Convert.ToDecimal(zlist.Sum(x =>Convert.ToDecimal(x.EH))); } model1.ElectricaValue = num-anum; model1.ElectricaName = "空调系统"; data.Add(model1); var blist = bll_info.GetModelList(" DeviceName LIKE '%照明%'"); var model2 = new electrical_rankingData(); decimal? num1 = 0; decimal? anum1 = 0; foreach (var item in blist) { //今天 //今天 var list = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + jdate + "'", jtime); num1 += Convert.ToDecimal(list.Sum(x => Convert.ToDecimal(x.EH))); //昨天 var zlist = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + zdate + "'", jtime); anum1 += Convert.ToDecimal(zlist.Sum(x => Convert.ToDecimal(x.EH))); } model2.ElectricaValue = num1-anum1; model2.ElectricaName = "照明系统"; data.Add(model2); var clist = bll_info.GetModelList(" DeviceName like '%电梯%' or DeviceName like '%货梯%'"); var model3 = new electrical_rankingData(); decimal? num2 = 0; decimal? anum2 = 0; foreach (var item in clist) { //今天 var list = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + jdate + "'", jtime); num2 += Convert.ToDecimal(list.Sum(x => Convert.ToDecimal(x.EH))); //昨天 var zlist = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + zdate + "'", jtime); anum2 += Convert.ToDecimal(zlist.Sum(x => Convert.ToDecimal(x.EH))); } model3.ElectricaValue = num2-anum2; model3.ElectricaName = "电梯系统"; data.Add(model3); var elist=data.OrderByDescending(x=>x.ElectricaValue).ToList(); res.code = 200; res.msg = "成功"; res.data = elist; } 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; } } }