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.Security.Policy; using System.Text; using System.Web.Http; namespace JinanCementFactoryAPI.Controllers.api { /// /// 三号生产线实时负荷 /// public class GetThreeLoadController : 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(" EquipmentName like '%三号线%'"); 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"); string stime; if (Convert.ToDateTime(sdate).Month < 10) { stime = Convert.ToDateTime(sdate).ToString("yyyy_M"); } else { stime = Convert.ToDateTime(sdate).ToString("yyyy_MM"); } decimal? num = 0; var slists=string.Join(",", list.Select(x=>x.P)); var blist = bll_gw.GetModelListsDate(slists,stime).Where(x=> x.XTimeStamp>=Convert.ToDateTime(sdate)&&x.XTimeStamp 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.AddMonths(-i).ToString("yyyy-MM-01 00:00:00"); var edate = now.AddMonths(-i + 1).ToString("yyyy-MM-01 00:00:00"); if(Convert.ToDateTime(sdate)>=Convert.ToDateTime("2024-03-01 00:00:00")) { var model = new one_loadData(); model.time = DateTime.Now.AddMonths(-i).ToString("MM月"); string stime; if (Convert.ToDateTime(sdate).Month < 10) { stime = Convert.ToDateTime(sdate).ToString("yyyy_M"); } else { stime = Convert.ToDateTime(sdate).ToString("yyyy_MM"); } decimal? num = 0; var slists = string.Join(",", list.Select(x => x.P)); var blist = bll_gw.GetModelListsDate(slists, stime).Where(x => x.XTimeStamp >= Convert.ToDateTime(sdate) && x.XTimeStamp < Convert.ToDateTime(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.AddMonths(-i).ToString("MM月"); 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年"); model.RealTimeLoad = 0; alist.Add(model); } } } res.code = 200; 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; } } }