LonglslandExhibitionCenter/LonglslandExhibitionCenter/Controllers/api/GetRealLoadController.cs

90 lines
3.3 KiB
C#

using DataService.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 LonglslandExhibitionCenter.Controllers
{
/// <summary>
/// 用能监测-实时负荷
/// </summary>
public class GetRealLoadController : ApiController
{
DataService.BLL.electricity_data bll=new DataService.BLL.electricity_data();
// GET api/<controller>
public HttpResponseMessage Get()
{
var res = new get_real_load ();
//表名
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
try
{
var data = new List<real_loadData>();
var time = DateTime.Now.ToString("yyyyMM");
var now = DateTime.Now;
var time_count = Convert.ToInt32(DateTime.Now.Hour);
for (int i = 1; i < time_count; i++)
{
var jdate = now.AddHours(-i).ToString("yyyy-MM-dd HH:00:00");
// 只处理今天的数据
if (DateTime.Now.AddHours(-i).Day != DateTime.Now.Day)
{
continue;
}
var jtime = now.AddHours(-i).ToString("yyyyMM");
var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime);
if (a == false)
{
bll.CreateTable(jtime);
}
var jlist = bll.GetModelListDate(" EntireTime='" + jdate + "' and Reserve1='配电室高压'", jtime);
if (jlist.Count == 0)
{
var model = new real_loadData
{
time = Convert.ToDateTime(jdate).ToString("HH:00"),
P = 0
};
if (model.P < 0)
{
model.P = 0;
}
data.Add(model);
}
else
{
decimal? num1 = jlist.Sum(item => Convert.ToDecimal(item.P));
var model = new real_loadData
{
time = Convert.ToDateTime(jdate).ToString("HH:00"),
P = num1
};
if (model.P < 0)
{
model.P = 0;
}
data.Add(model);
}
}
var adata=data.OrderBy(x=>x.time).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;
}
}
}