using DataServer.api; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Net; using System.Net.Http; using System.Reflection; using System.Text; using System.Web.Http; namespace DongYingAPI.Controllers.api { /// /// 照明-楼宇照明 /// public class GetBuildingLightingController : 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_building_lighting(); try { var data = new List(); #region 表是否存在 //表名 var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString(); //今月的表是否存在 var jtime = DateTime.Now.ToString("yyyyMM"); var a1 = bll.IsExistsTable(date_base, "electricity_data_" + jtime); if (a1 == false) { bll.CreateTable(jtime); } //昨月的表是否存在 var ztime = DateTime.Now.AddMonths(-1).ToString("yyyyMM"); var a = bll.IsExistsTable(date_base, "electricity_data_" + ztime); if (a == false) { bll.CreateTable(ztime); } //前月的表是否存在 var qtime = DateTime.Now.AddMonths(-2).ToString("yyyyMM"); var b = bll.IsExistsTable(date_base, "electricity_data_" + qtime); if (b == false) { bll.CreateTable(qtime); } //昨年的表是否存在 var zntime = DateTime.Now.AddYears(-1).ToString("yyyy12"); var an = bll.IsExistsTable(date_base, "electricity_data_" + zntime); if (an == false) { bll.CreateTable(zntime); } //前年的表是否存在 var qntime = DateTime.Now.AddYears(-2).ToString("yyyy12"); var bn = bll.IsExistsTable(date_base, "electricity_data_" + qntime); if (bn == false) { bll.CreateTable(qntime); } #endregion //科技馆 var list = bll_info.GetModelList(" DeviceName like '%照明%' and FloorName like '%科技馆%'"); //少年宫 var list1= bll_info.GetModelList(" DeviceName like '%照明%' and FloorName like '%少年宫%'"); //图书馆 var list2 = bll_info.GetModelList(" DeviceName like '%照明%' and FloorName like '%图书馆%'"); var sdate = DateTime.Now.ToString("yyyy-MM-dd 00:00:00"); var edate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 00:00:00"); var model1 = new building_lightingData(); model1.BuildingName = "科技馆"; var lalist = bll.GetModelListDate(" EntireTime>='" + sdate + "' and EntireTime<'" + edate + "'", jtime); //科技馆 decimal? num1 = 0; foreach (var item in list) { var alist=lalist.Where(x=>x.DeviceId==item.DeviceId).ToList(); decimal? num = alist.Sum(x=>Convert.ToDecimal(x.P)); num1 += Convert.ToDecimal(num); } model1.BuildingValue = num1; data.Add(model1); var model2 = new building_lightingData(); model2.BuildingName = "少年宫"; decimal? num2 = 0; foreach (var item in list1) { var alist = lalist.Where(x => x.DeviceId == item.DeviceId).ToList(); decimal? num = alist.Sum(x => Convert.ToDecimal(x.P)); num2 += Convert.ToDecimal(num); } model2.BuildingValue = num2; if (list1 == null) { model2.BuildingValue = 0; } data.Add(model2); var model3 = new building_lightingData(); model3.BuildingName = "图书馆"; decimal? num3 = 0; foreach (var item in list2) { var alist = lalist.Where(x => x.DeviceId == item.DeviceId).ToList(); decimal? num = alist.Sum(x => Convert.ToDecimal(x.P)); num3+= Convert.ToDecimal(num); } model3.BuildingValue = num3; if (list2 == null) { model3.BuildingValue= 0; } data.Add(model3); res.code = 200; res.msg = "成功"; res.data = data; } 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; } } }