using DataServer.api.EnergyEfficiency; using DongYingAPI.Util; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Configuration; using System.Drawing; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Web.Http; namespace DongYingAPI.Controllers.api.EnergyEfficiency { public class GetEnergyTrendController : ApiController { DataServer.BLL.device_info device_bll = new DataServer.BLL.device_info(); DataServer.BLL.electricity_data bll = new DataServer.BLL.electricity_data(); DataServer.BLL.water_data water_bll = new DataServer.BLL.water_data(); DataServer.BLL.gas_data gas_bll = new DataServer.BLL.gas_data(); DataServer.BLL.planned_energy plan_bll = new DataServer.BLL.planned_energy(); /// /// 获取能耗趋势接口 /// /// 类型 电、水、天然气 /// public HttpResponseMessage Get(string type) { var res = new get_energy_trend_response(); try { var now = DateTime.Now; var data = new List(); var plan_list = plan_bll.GetModelList(" Type='" + type + "' "); if (type == "电") { var list = new List(); var time_count = Convert.ToInt32(now.Month); for (int i = 0; i < time_count; i++) { string sdate; if (i == 0) { sdate = now.AddMonths(-i).AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); } else { //这月 var sdate1 = now.AddMonths(-i + 1).ToString("yyyy-MM-01 00:00:00"); var sdate2 = Convert.ToDateTime(sdate1); sdate = sdate2.AddDays(-1).ToString("yyyy-MM-dd 00:00:00"); } var edate = now.AddMonths(-i).ToString("yyyy-MM-01 00:00:00"); var plan_model = plan_list.Where(a => a.Month == Convert.ToDateTime(sdate).Month).FirstOrDefault(); #region 表是否存在 //表名 var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString(); //今月的表是否存在 var stime = Convert.ToDateTime(sdate).ToString("yyyyMM"); var a1 = bll.IsExistsTable(date_base, "electricity_data_" + stime); if (a1 == false) { bll.CreateTable(stime); } #endregion var alist = bll.GetModelListDate("", stime); var list1 = alist.Where(x => x.EntireTime == Convert.ToDateTime(sdate)).ToList(); var list2=alist.Where(x=>x.EntireTime==Convert.ToDateTime(edate)).ToList(); var num1 = list1.Sum(x => x.EH); var num2 = list2.Sum(x => x.EH); var model = new energy_trend(); model.Month = Convert.ToDateTime(sdate).ToString("MM月"); if (plan_model != null) { model.Plan = plan_model.Plan.Value; } model.Plan = 0; model.Value =Convert.ToDecimal(num1-num2); if (model.Value < 0) { model.Value = 0; } data.Add(model); } } else if (type == "水") { var list = new List(); var time_count = Convert.ToInt32(now.Month); var alist = water_bll.GetModelList(""); for (int i = 0; i < time_count; i++) { string sdate; if (i == 0) { sdate = now.AddMonths(-i).AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); } else { //这月 var sdate1 = now.AddMonths(-i + 1).ToString("yyyy-MM-01 00:00:00"); var sdate2 = Convert.ToDateTime(sdate1); sdate = sdate2.AddDays(-1).ToString("yyyy-MM-dd 00:00:00"); } var edate = now.AddMonths(-i).ToString("yyyy-MM-01 00:00:00"); var plan_model = plan_list.Where(a => a.Month == Convert.ToDateTime(sdate).Month).FirstOrDefault(); var list1 = alist.Where(x => x.EntireTime == Convert.ToDateTime(sdate)).ToList(); var list2 = alist.Where(x => x.EntireTime == Convert.ToDateTime(edate)).ToList(); var num1 = list1.Sum(x => x.WaterYield); var num2 = list2.Sum(x => x.WaterYield); var model = new energy_trend(); model.Month = Convert.ToDateTime(sdate).ToString("MM月"); if (plan_model != null) { model.Plan = plan_model.Plan.Value; } model.Plan = 0; model.Value = Convert.ToDecimal(num1 - num2); if (model.Value < 0) { model.Value = 0; } data.Add(model); } } else if (type == "天然气") { var list = new List(); var time_count = Convert.ToInt32(now.Month); var alist = gas_bll.GetModelList(""); for (int i = 0; i < time_count; i++) { string sdate; if (i == 0) { sdate = now.AddMonths(-i).AddHours(-1).ToString("yyyy-MM-dd HH:00:00"); } else { //这月 var sdate1 = now.AddMonths(-i + 1).ToString("yyyy-MM-01 00:00:00"); var sdate2 = Convert.ToDateTime(sdate1); sdate = sdate2.AddDays(-1).ToString("yyyy-MM-dd 00:00:00"); } var edate = now.AddMonths(-i).ToString("yyyy-MM-01 00:00:00"); var plan_model = plan_list.Where(a => a.Month == Convert.ToDateTime(sdate).Month).FirstOrDefault(); var list1 = alist.Where(x => x.EntireTime == Convert.ToDateTime(sdate)).ToList(); var list2 = alist.Where(x => x.EntireTime == Convert.ToDateTime(edate)).ToList(); var num1 = list1.Sum(x => x.GasConsumption); var num2 = list2.Sum(x => x.GasConsumption); var model = new energy_trend(); model.Month = Convert.ToDateTime(sdate).ToString("MM月"); if (plan_model != null) { model.Plan = plan_model.Plan.Value; } model.Plan = 0; model.Value = Convert.ToDecimal(num1 - num2); if (model.Value < 0) { model.Value = 0; } data.Add(model); } } var adata=data.OrderBy(x=>x.Month).ToList(); res.code = 200; res.msg = "成功"; res.data = adata; } 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; } } }