using DataServer.api; using DataServer.Model; 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 { /// /// 用能监测3-用电排名 /// public class GetCellRankingController : 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(string type = "") { var res = new get_cell_ranking(); #region 表是否存在 //表名 var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString(); //今月的表是否存在 var time = DateTime.Now.ToString("yyyyMM"); var a1 = bll.IsExistsTable(date_base, "electricity_data_" + time); if (a1 == false) { bll.CreateTable(time); } #endregion try { if (!string.IsNullOrEmpty(type)) { var data=new List(); if (type == "配电室1") { var now = DateTime.Now; //今天 var ztime = now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); //上天 var stime = now.ToString("yyyy-MM-dd 00:00:00"); var atime = now.ToString("yyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + atime); if (a == false) { bll.CreateTable(atime); } var list = bll_info.GetModelList("").Where(x => x.FloorName == "科技馆一楼").ToList(); foreach (var item in list) { //今天 var alist = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + ztime + "'", time); var model = new cell_rankingData(); decimal? num1 = 0; model.CellName = item.DeviceName; // model.CellName = item.DeviceName; num1 =Convert.ToDecimal( alist.Sum(x => Convert.ToDecimal(x.EH))); //上天 var blist = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + stime + "'", atime); decimal? num2 = 0; num2 =Convert.ToDecimal( blist.Sum(x => Convert.ToDecimal(x.EH))); model.EH = num1 - num2; if (model.EH < 0) { model.EH = 0; } data.Add(model); } var elist = data.OrderByDescending(x => x.EH).ToList(); res.code = 200; res.msg = "成功"; res.data = elist; } if (type == "配电室2") { //科技馆负一层地源热泵 var now = DateTime.Now; //今天 var ztime = now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); //上天 var stime = now.ToString("yyyy-MM-dd 00:00:00"); var atime = now.ToString("yyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + atime); if (a == false) { bll.CreateTable(atime); } var list = bll_info.GetModelList("").Where(x => x.FloorName == "科技馆负一层地源热泵").ToList(); foreach (var item in list) { //今天 var alist = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + ztime + "'", time); var model = new cell_rankingData(); decimal? num1 = 0; model.CellName = item.DeviceName; // model.CellName = item.DeviceName; num1 = Convert.ToDecimal(alist.Sum(x => Convert.ToDecimal(x.EH))); //上天 var blist = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + stime + "'", atime); decimal? num2 = 0; num2 = Convert.ToDecimal(blist.Sum(x => Convert.ToDecimal(x.EH))); model.EH = num1 - num2; if (model.EH < 0) { model.EH = 0; } data.Add(model); } var elist = data.OrderByDescending(x => x.EH).ToList(); res.code = 200; res.msg = "成功"; res.data = elist; } if (type == "配电室3") { //图书馆一楼 var now = DateTime.Now; //今天 var ztime = now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); //上天 var stime = now.ToString("yyyy-MM-dd 00:00:00"); var atime = now.ToString("yyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + atime); if (a == false) { bll.CreateTable(atime); } var list = bll_info.GetModelList("").Where(x => x.FloorName == "图书馆一楼").ToList(); foreach (var item in list) { //今天 var alist = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + ztime + "'", time); var model = new cell_rankingData(); decimal? num1 = 0; model.CellName = item.DeviceName; // model.CellName = item.DeviceName; num1 = Convert.ToDecimal(alist.Sum(x => Convert.ToDecimal(x.EH))); //上天 var blist = bll.GetModelListDate(" DeviceId='" + item.DeviceId + "' and EntireTime='" + stime + "'", atime); decimal? num2 = 0; num2 = Convert.ToDecimal(blist.Sum(x => Convert.ToDecimal(x.EH))); model.EH = num1 - num2; if (model.EH < 0) { model.EH = 0; } data.Add(model); } var elist = data.OrderByDescending(x => x.EH).ToList(); res.code = 200; res.msg = "成功"; res.data = elist; } } else { res.code = 201; res.msg = "参数为空"; } } 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; } } }