using DataServer.api; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using System.Net.Http; using System.Security.Policy; using System.Text; using System.Web.Http; namespace DongYingAPI.Controllers.api { /// /// 用能监测3-配电室用电量 /// public class GetRoomElectricityController : 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 = "", string date = "") { var res = new get_room_electricity(); //表名 var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString(); try { if (!string.IsNullOrEmpty(date) && !string.IsNullOrEmpty(type)) { if (type == "配电室1") { var data = new List(); var list = bll_info.GetModelList(" FloorName='科技馆一楼'"); var now=DateTime.Now; if (date == "日") { var time_count = Convert.ToInt32(DateTime.Now.Hour); for (int i = 1; i < time_count; i++) { // 只处理今年的数据 if (DateTime.Now.AddHours(-i).Day != DateTime.Now.Day) { continue; } var jdate = now.AddHours(-i).ToString("yyyy-MM-dd HH:00:00"); 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); } decimal? num1 = 0; decimal? num2 = 0; var ljlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var lslist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var aitem in list) { var jlist = ljlist.Where(x=>x.DeviceId== aitem.DeviceId).ToList(); var slist = lslist.Where(x=>x.DeviceId== aitem.DeviceId).ToList(); num1 += jlist.Sum(x => Convert.ToDecimal(x.EH)); num2 += slist.Sum(x => Convert.ToDecimal(x.EH)); } var model = new room_electricityData() { time = DateTime.Now.AddHours(-i).ToString("HH:00"), EH = num1 - num2 }; if (model.EH < 0) { model.EH = 0; } data.Add(model); } } if (date == "月") { var time_count = Convert.ToInt32(DateTime.Now.Day); for (int i = 0; i < time_count; i++) { // 只处理今年的数据 if (DateTime.Now.AddDays(-i).Month != DateTime.Now.Month) { continue; } string jdate; if (i == 0) { jdate = now.AddDays(-i).AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); } else { jdate = now.AddDays(-i+1).ToString("yyyy-MM-dd 00:00:00"); } var jtime = now.AddDays(-i+1).ToString("yyyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime); if (a == false) { bll.CreateTable(jtime); } var sdate = now.AddDays(-(i + 1)).ToString("yyyy-MM-dd 00:00:00"); var stime = now.AddDays(-(i + 1)).ToString("yyyyMM"); var b = bll.IsExistsTable(date_base, "electricity_data_" + stime); if (b == false) { bll.CreateTable(stime); } decimal? num1 = 0; decimal? num2 = 0; var ljlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var lslist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var aitem in list) { var jlist = ljlist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); var slist = lslist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); num1 += jlist.Sum(x => Convert.ToDecimal(x.EH)); num2 += slist.Sum(x => Convert.ToDecimal(x.EH)); } var model = new room_electricityData() { time = DateTime.Now.AddDays(-i).ToString("MM-dd"), EH = num1 - num2 }; if (model.EH < 0) { model.EH = 0; } data.Add(model); } } if (date == "年") { 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); } 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); } decimal? num1 = 0; decimal? num2 = 0; var ljlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var lslist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var aitem in list) { var jlist = ljlist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); var slist = lslist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); num1 += jlist.Sum(x => Convert.ToDecimal(x.EH)); num2 += slist.Sum(x => Convert.ToDecimal(x.EH)); } var model = new room_electricityData() { time = DateTime.Now.AddMonths(-i).ToString("MM月"), EH = num1 - num2 }; if (model.EH < 0) { model.EH = 0; } data.Add(model); } } var adata = data.OrderBy(x => x.time).ToList(); res.code = 200; res.msg = "成功"; res.data = adata; } if (type == "配电室2") { var data = new List(); var list = bll_info.GetModelList("").Where(x => x.FloorName == "科技馆负一层地源热泵").ToList(); var now = DateTime.Now; if (date == "日") { var time_count = Convert.ToInt32(DateTime.Now.Hour); for (int i = 1; i < time_count; i++) { // 只处理今年的数据 if (DateTime.Now.AddHours(-i).Day != DateTime.Now.Day) { continue; } var jdate = now.AddHours(-i).ToString("yyyy-MM-dd HH:00:00"); 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); } decimal? num1 = 0; decimal? num2 = 0; var ljlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var lslist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var aitem in list) { var jlist = ljlist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); var slist = lslist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); num1 += jlist.Sum(x => Convert.ToDecimal(x.EH)); num2 += slist.Sum(x => Convert.ToDecimal(x.EH)); } var model = new room_electricityData() { time = DateTime.Now.AddHours(-i).ToString("HH:00"), EH = num1 - num2 }; if (model.EH < 0) { model.EH = 0; } data.Add(model); } } if (date == "月") { var time_count = Convert.ToInt32(DateTime.Now.Day); for (int i = 0; i < time_count; i++) { // 只处理今年的数据 if (DateTime.Now.AddDays(-i).Month != DateTime.Now.Month) { continue; } string jdate; if (i == 0) { jdate = now.AddDays(-i).AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); } else { jdate = now.AddDays(-i + 1).ToString("yyyy-MM-dd 00:00:00"); } var jtime = now.AddDays(-i + 1).ToString("yyyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime); if (a == false) { bll.CreateTable(jtime); } var sdate = now.AddDays(-(i + 1)).ToString("yyyy-MM-dd 00:00:00"); var stime = now.AddDays(-(i + 1)).ToString("yyyyMM"); var b = bll.IsExistsTable(date_base, "electricity_data_" + stime); if (b == false) { bll.CreateTable(stime); } decimal? num1 = 0; decimal? num2 = 0; var ljlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var lslist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var aitem in list) { var jlist = ljlist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); var slist = lslist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); num1 += jlist.Sum(x => Convert.ToDecimal(x.EH)); num2 += slist.Sum(x => Convert.ToDecimal(x.EH)); } var model = new room_electricityData() { time = DateTime.Now.AddDays(-i).ToString("MM-dd"), EH = num1 - num2 }; if (model.EH < 0) { model.EH = 0; } data.Add(model); } } if (date == "年") { 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); } 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); } decimal? num1 = 0; decimal? num2 = 0; var ljlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var lslist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var aitem in list) { var jlist = ljlist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); var slist = lslist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); num1 += jlist.Sum(x => Convert.ToDecimal(x.EH)); num2 += slist.Sum(x => Convert.ToDecimal(x.EH)); } var model = new room_electricityData() { time = DateTime.Now.AddMonths(-i).ToString("MM月"), EH = num1 - num2 }; if (model.EH < 0) { model.EH = 0; } data.Add(model); } } var adata = data.OrderBy(x => x.time).ToList(); res.code = 200; res.msg = "成功"; res.data = adata; } if (type == "配电室3") { var data = new List(); var list = bll_info.GetModelList("").Where(x => x.FloorName == "图书馆一楼").ToList(); var now = DateTime.Now; if (date == "日") { var time_count = Convert.ToInt32(DateTime.Now.Hour); for (int i = 1; i < time_count; i++) { // 只处理今年的数据 if (DateTime.Now.AddHours(-i).Day != DateTime.Now.Day) { continue; } var jdate = now.AddHours(-i).ToString("yyyy-MM-dd HH:00:00"); 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); } decimal? num1 = 0; decimal? num2 = 0; var ljlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var lslist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var aitem in list) { var jlist = ljlist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); var slist = lslist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); num1 += jlist.Sum(x => Convert.ToDecimal(x.EH)); num2 += slist.Sum(x => Convert.ToDecimal(x.EH)); } var model = new room_electricityData() { time = DateTime.Now.AddHours(-i).ToString("HH:00"), EH = num1 - num2 }; if (model.EH < 0) { model.EH = 0; } data.Add(model); } } if (date == "月") { var time_count = Convert.ToInt32(DateTime.Now.Day); for (int i = 0; i < time_count; i++) { // 只处理今年的数据 if (DateTime.Now.AddDays(-i).Month != DateTime.Now.Month) { continue; } string jdate; if (i == 0) { jdate = now.AddDays(-i).AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); } else { jdate = now.AddDays(-i + 1).ToString("yyyy-MM-dd 00:00:00"); } var jtime = now.AddDays(-i + 1).ToString("yyyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime); if (a == false) { bll.CreateTable(jtime); } var sdate = now.AddDays(-(i + 1)).ToString("yyyy-MM-dd 00:00:00"); var stime = now.AddDays(-(i + 1)).ToString("yyyyMM"); var b = bll.IsExistsTable(date_base, "electricity_data_" + stime); if (b == false) { bll.CreateTable(stime); } decimal? num1 = 0; decimal? num2 = 0; var ljlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var lslist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var aitem in list) { var jlist = ljlist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); var slist = lslist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); num1 += jlist.Sum(x => Convert.ToDecimal(x.EH)); num2 += slist.Sum(x => Convert.ToDecimal(x.EH)); } var model = new room_electricityData() { time = DateTime.Now.AddDays(-i).ToString("MM-dd"), EH = num1 - num2 }; if (model.EH < 0) { model.EH = 0; } data.Add(model); } } if (date == "年") { 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); } 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); } decimal? num1 = 0; decimal? num2 = 0; var ljlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime); var lslist = bll.GetModelListDate(" EntireTime='" + sdate + "'", stime); foreach (var aitem in list) { var jlist = ljlist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); var slist = lslist.Where(x => x.DeviceId == aitem.DeviceId).ToList(); num1 += jlist.Sum(x => Convert.ToDecimal(x.EH)); num2 += slist.Sum(x => Convert.ToDecimal(x.EH)); } var model = new room_electricityData() { time = DateTime.Now.AddMonths(-i).ToString("MM月"), EH = num1 - num2 }; if (model.EH < 0) { model.EH = 0; } data.Add(model); } } var adata = data.OrderBy(x => x.time).ToList(); res.code = 200; res.msg = "成功"; res.data = adata; } } 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; } } }