using DataServer.api.EnergyEfficiency; using DongYingAPI.Util; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Configuration; using System.Data.Services; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Web.Http; namespace DongYingAPI.Controllers.api.EnergyEfficiency { public class GetEnergyConsumptionController : ApiController { DataServer.BLL.device_info device_bll = new DataServer.BLL.device_info(); DataServer.BLL.electricity_data bll = new DataServer.BLL.electricity_data(); /// /// 获取各回路(设备)能耗概况接口 /// /// 类型 年、月、日 /// public HttpResponseMessage Get(string type) { var res = new get_energy_consumption_response(); try { var now = DateTime.Now; var device_list = device_bll.GetModelList(""); var list = new List(); //判断表是否存在,不存在就创建 var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString(); var start_date = DateTime.Parse(now.ToString("yyyy") + "-01-01 00:00:00"); if (type == "年") { start_date = DateTime.Parse(now.ToString("yyyy") + "-01-01 00:00:00"); } else if (type == "月") { start_date = DateTime.Parse(now.ToString("yyyy-MM") + "-01 00:00:00"); } else if (type == "日") { start_date = DateTime.Parse(now.ToString("yyyy-MM-dd") + " 00:00:00"); } var time_count = Tool.GetUsedMonth1("月", start_date, now); var source = ""; for (int i = 0; i <= time_count; i++) { var time = start_date.AddMonths(i).ToString("yyyyMM"); if (bll.IsExistsTable(date_base, "electricity_data_" + time)) { if (time == start_date.ToString("yyyyMM") || time == now.ToString("yyyyMM")) { source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime>='{1}' and EntireTime<='{2}'", time, start_date, now); } else { source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where 1=1 ", time); } source += ") UNION all "; } } if (!string.IsNullOrEmpty(source)) { source = source.Substring(0, source.Length - 11); list = bll.GetList(source, "", ""); } var data = new List(); foreach (var item in device_list) { var model = new energy_consumption(); model.DeviceName = item.DeviceName; model.OperatingPower = list.Where(a => a.DeviceId == item.DeviceId).Sum(a => a.ServiceRating).Value; 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; } } }