DongYing/DongYingAPI/Controllers/api/GetEnergyTrendsController.cs

211 lines
10 KiB
C#

using Antlr.Runtime.Tree;
using DataServer.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 DongYingAPI.Controllers.api
{
/// <summary>
/// 空调-用能趋势
/// </summary>
public class GetEnergyTrendsController : ApiController
{
DataServer.BLL.electricity_data bll = new DataServer.BLL.electricity_data();
DataServer.BLL.device_info bll_info = new DataServer.BLL.device_info();
public HttpResponseMessage Get(string date="")
{
var res = new get_energy_trend();
//表名
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
try
{
if (!string.IsNullOrEmpty(date))
{
var data=new List<energy_trendData>();
DateTime now=DateTime.Now;
var list = bll_info.GetModelList(" FloorName='科技馆负一层地源热泵'");
if (date == "日")
{
var time_count = Convert.ToInt32(DateTime.Now.Hour);
for (int i = 1; i < time_count; i++)
{
// 只处理今年的数据
if (DateTime.Now.AddHours(-i).Day != DateTime.Now.Day)
{
continue;
}
var jdate = now.AddHours(-i).ToString("yyyy-MM-dd HH:00:00");
var jtime = now.AddHours(-i).ToString("yyyyMM");
var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime);
if (a == false)
{
bll.CreateTable(jtime);
}
var sdate = now.AddHours(-(i + 1)).ToString("yyyy-MM-dd HH:00:00");
var stime = now.AddHours(-(i + 1)).ToString("yyyyMM");
var b = bll.IsExistsTable(date_base, "electricity_data_" + stime);
if (b == false)
{
bll.CreateTable(stime);
}
decimal? num1 = 0;
decimal? num2 = 0;
foreach (var aitem in list)
{
var jlist = bll.GetModelListDate(" DeviceId='"+aitem.DeviceId+ "' and EntireTime='"+jdate+"'", jtime);
var slist = bll.GetModelListDate(" DeviceId='" + aitem.DeviceId + "' and EntireTime='" + sdate + "'", stime);
num1 = jlist.Sum(x => Convert.ToDecimal(x.EH));
num2=slist.Sum(x=>Convert.ToDecimal(x.EH));
}
var model = new energy_trendData() {
time = DateTime.Now.AddHours(-i).ToString("HH:00"),
EH = num1 - num2
};
if (model.EH < 0)
{
model.EH = 0;
}
data.Add(model);
}
}
else if (date == "月")
{
var time_count = Convert.ToInt32(DateTime.Now.Day);
for (int i = 0; i < time_count; i++)
{
// 只处理今年的数据
if (DateTime.Now.AddDays(-i).Month != DateTime.Now.Month)
{
continue;
}
string jdate;
if (i == 0)
{
jdate = now.AddDays(-i).AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
}
else
{
jdate = now.AddDays(-i + 1).ToString("yyyy-MM-dd 00:00:00");
}
var jtime = now.AddDays(-i + 1).ToString("yyyyMM");
var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime);
if (a == false)
{
bll.CreateTable(jtime);
}
var sdate = now.AddDays(-(i)).ToString("yyyy-MM-dd 00:00:00");
var stime = now.AddDays(-(i)).ToString("yyyyMM");
var b = bll.IsExistsTable(date_base, "electricity_data_" + stime);
if (b == false)
{
bll.CreateTable(stime);
}
decimal? num1 = 0;
decimal? num2 = 0;
foreach (var aitem in list)
{
var jlist = bll.GetModelListDate(" DeviceId='" + aitem.DeviceId + "' and EntireTime='" + jdate + "'", jtime);
var slist = bll.GetModelListDate(" DeviceId='" + aitem.DeviceId + "' and EntireTime='" + sdate + "'", stime);
num1 = jlist.Sum(x => Convert.ToDecimal(x.EH));
num2 = slist.Sum(x => Convert.ToDecimal(x.EH));
}
var model = new energy_trendData()
{
time = DateTime.Now.AddDays(-i).ToString("MM-dd"),
EH = num1 - num2
};
if (model.EH < 0)
{
model.EH = 0;
}
data.Add(model);
}
}
else if (date == "年")
{
var time_count = Convert.ToInt32(DateTime.Now.Month);
for (int i = 0; i < time_count; i++)
{
// 只处理今年的数据
if (DateTime.Now.AddMonths(-i).Year != DateTime.Now.Year)
{
continue;
}
DateTime jdate;
if (i == 0)
{
var jdate1 = now.AddMonths(-i).AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
jdate = Convert.ToDateTime(jdate1);
}
else
{
var jdate1 = now.AddMonths(-i + 1).ToString("yyyy-MM-01 00:00:00");
var jdate2 = Convert.ToDateTime(jdate1);
jdate = jdate2.AddDays(-i);
}
var jtime = jdate.ToString("yyyyMM");
var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime);
if (a == false)
{
bll.CreateTable(jtime);
}
var sdate1 = now.AddMonths(-(i)).ToString("yyyy-MM-01 00:00:00");
var sdate2 = Convert.ToDateTime(sdate1);
var sdate = sdate2.AddDays(-0);
var stime = sdate.ToString("yyyyMM");
var b = bll.IsExistsTable(date_base, "electricity_data_" + stime);
if (b == false)
{
bll.CreateTable(stime);
}
decimal? num1 = 0;
decimal? num2 = 0;
foreach (var aitem in list)
{
var jlist = bll.GetModelListDate(" DeviceId='" + aitem.DeviceId + "' and EntireTime='" + jdate + "'", jtime);
var slist = bll.GetModelListDate(" DeviceId='" + aitem.DeviceId + "' and EntireTime='" + sdate + "'", stime);
num1 = jlist.Sum(x => Convert.ToDecimal(x.EH));
num2 = slist.Sum(x => Convert.ToDecimal(x.EH));
}
var model = new energy_trendData()
{
time = jdate.ToString("MM月"),
EH = num1 - num2
};
if (model.EH < 0)
{
model.EH = 0;
}
data.Add(model);
}
}
var adata=data.OrderBy(x=>x.time).ToList();
res.code = 200;
res.msg = "成功";
res.data = adata;
}
else
{
res.code = 201;
res.msg = "参数不能为空";
}
}
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;
}
}
}