using DataServer.api; using Microsoft.Ajax.Utilities; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Web.Http; namespace JinanCementFactoryAPI.Controllers.api { /// /// 煤磨系统实时负荷 /// public class GetGrindLoadController : ApiController { DataServer.BLL.electrical_installation bll = new DataServer.BLL.electrical_installation(); DataServer.BLL.gw_data bll_gw = new DataServer.BLL.gw_data(); // GET api/ public HttpResponseMessage Get(string date = "") { var res = new get_one_load(); try { var list = bll.GetModelList("").Where(x=>x.EquipmentName.Contains("煤磨")).ToList(); var alist = new List(); var now = DateTime.Now; if (date == "日") { for (int i = 0; i < 6; i++) { var model = new one_loadData(); model.time = DateTime.Now.AddDays(-i).ToString("dd号"); var sdate = now.AddDays(-i).ToString("yyyy-MM-dd 00:00:00"); var edate = now.AddDays(-i + 1).ToString("yyyy-MM-dd 00:00:00"); decimal? num = 0; foreach (var aitem in list) { var blist = bll_gw.GetModelList(" XTagName like '%" + aitem.P + "' and XTimeStamp>='" + sdate + "' and XTimeStamp<'" + edate + "'").DistinctBy(x =>new { x.XTagName, x.XTimeStamp }).ToList(); foreach (var item in blist) { num += Convert.ToDecimal(item.XValue); } } model.RealTimeLoad = num; if (model.RealTimeLoad == null) { model.RealTimeLoad = 0; } alist.Add(model); } } if (date == "月") { for (int i = 0; i < 6; i++) { var model = new one_loadData(); model.time = DateTime.Now.AddMonths(-i).ToString("MM月"); var sdate = now.AddMonths(-i).ToString("yyyy-MM-01 00:00:00"); var edate = now.AddMonths(-i + 1).ToString("yyyy-MM-01 00:00:00"); decimal? num = 0; foreach (var aitem in list) { var blist = bll_gw.GetModelList(" XTagName like '%" + aitem.P + "' and XTimeStamp>='" + sdate + "' and XTimeStamp<'" + edate + "'").DistinctBy(x => new { x.XTagName, x.XTimeStamp }).ToList(); foreach (var item in blist) { num += Convert.ToDecimal(item.XValue); } } model.RealTimeLoad = num; if (model.RealTimeLoad == null) { model.RealTimeLoad = 0; } alist.Add(model); } } if (date == "年") { for (int i = 0; i < 6; i++) { var sdate = now.AddYears(-i).ToString("yyyy-01-01 00:00:00"); var edate = now.AddYears(-i + 1).ToString("yyyy-01-01 00:00:00"); if(Convert.ToDateTime(sdate)>=Convert.ToDateTime("2024-01-01 00:00:00")) { var model = new one_loadData(); model.time = DateTime.Now.AddYears(-i).ToString("yyyy年"); decimal? num = 0; foreach (var aitem in list) { var blist = bll_gw.GetModelList(" XTagName like '%" + aitem.P + "' and XTimeStamp>='" + sdate + "' and XTimeStamp<'" + edate + "'").DistinctBy(x => new { x.XTagName, x.XTimeStamp }).ToList(); foreach (var item in blist) { num += Convert.ToDecimal(item.XValue); } } model.RealTimeLoad = num; if (model.RealTimeLoad == null) { model.RealTimeLoad = 0; } alist.Add(model); } else { var model = new one_loadData(); model.time = DateTime.Now.AddYears(-i).ToString("yyyy年"); decimal? num = 0; model.RealTimeLoad = num; if (model.RealTimeLoad == null) { model.RealTimeLoad = 0; } alist.Add(model); } } } res.msg = "成功"; res.data = alist; } 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; } } }