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.Text; using System.Web.Http; namespace DongYingAPI.Controllers.api { public class GetDeviceDetailsController : 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_device_details(); try { var data = new List(); var time = DateTime.Now.ToString("yyyyMM"); #region 表是否存在 //表名 var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString(); //今月的表是否存在 var jtime = DateTime.Now.ToString("yyyyMM"); var a1 = bll.IsExistsTable(date_base, "electricity_data_" + time); if (a1 == false) { bll.CreateTable(jtime); } #endregion var list = bll_info.GetModelList("").Where(x=>x.DeviceName.Contains("照明")).ToList(); foreach (var item in list) { var model=new device_detailsData(); model.DeviceName = item.DeviceName; model.DeviceState = "正常"; 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 alist = bll.GetModelListDate("", time).Where(x =>x.DeviceId==item.DeviceId&& x.EntireTime >= Convert.ToDateTime(sdate) && x.EntireTime < Convert.ToDateTime(edate)); decimal? num = 0; foreach (var aitem in alist) { num += aitem.P; } model.P = num; model.Illuminance = "30%"; data.Add(model); } 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; } } }