256 lines
11 KiB
C#
256 lines
11 KiB
C#
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.Security.Policy;
|
|
using System.Text;
|
|
using System.Web.Http;
|
|
|
|
namespace DongYingAPI.Controllers.api
|
|
{
|
|
/// <summary>
|
|
/// 空调-用电情况
|
|
/// </summary>
|
|
public class GetElectricalConditionController : 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()
|
|
{
|
|
var res = new get_electrical_condition();
|
|
//表名
|
|
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
|
|
try
|
|
{
|
|
var data = new List<electrical_conditionData>();
|
|
var list = bll_info.GetModelList(" FloorName='科技馆负一层地源热泵'");
|
|
#region 天
|
|
//今天
|
|
var now = DateTime.Now;
|
|
var jdate = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
|
|
var jtime = now.ToString("yyyyMM");
|
|
var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime);
|
|
if (a == false)
|
|
{
|
|
bll.CreateTable(jtime);
|
|
}
|
|
//昨天
|
|
var zdate = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
|
|
var ztime = now.ToString("yyyyMM");
|
|
var b = bll.IsExistsTable(date_base, "electricity_data_" + ztime);
|
|
if (b == false)
|
|
{
|
|
bll.CreateTable(ztime);
|
|
}
|
|
//前天
|
|
var qdate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
|
|
decimal? num1 = 0;
|
|
decimal? num2 = 0;
|
|
decimal? num3 = 0;
|
|
var jlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime);
|
|
var zlist= bll.GetModelListDate(" EntireTime='" + zdate + "'", ztime);
|
|
var qlist = bll.GetModelListDate(" EntireTime='" + qdate + "'", jtime);
|
|
foreach (var aitem in list)
|
|
{
|
|
//今天列表
|
|
var list1 = jlist.Where(x=>x.DeviceId==aitem.DeviceId).ToList();
|
|
num1 += list1.Sum(x=>Convert.ToDecimal(x.EH));
|
|
//昨天列表
|
|
var list2 = zlist.Where(x => x.DeviceId == aitem.DeviceId).ToList();
|
|
num2 += list2.Sum(x => Convert.ToDecimal(x.EH));
|
|
//前天列表
|
|
var list3 = qlist.Where(x => x.DeviceId == aitem.DeviceId).ToList();
|
|
num3 += list3.Sum(x => Convert.ToDecimal(x.EH));
|
|
}
|
|
|
|
#endregion
|
|
#region 月
|
|
//今月
|
|
var jmdate = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
|
|
var jmtime = now.ToString("yyyyMM");
|
|
var am = bll.IsExistsTable(date_base, "electricity_data_" + jmtime);
|
|
if (am == false)
|
|
{
|
|
bll.CreateTable(jmtime);
|
|
}
|
|
//昨月
|
|
var zmdate = DateTime.Now.ToString("yyyy-MM-01 00:00:00");
|
|
var zmtime = now.ToString("yyyyMM");
|
|
var bm = bll.IsExistsTable(date_base, "electricity_data_" + zmtime);
|
|
if (bm == false)
|
|
{
|
|
bll.CreateTable(zmtime);
|
|
}
|
|
//前月
|
|
var qmdate1 = DateTime.Now.AddMonths(-1).ToString("yyyy-MM-01 00:00:00");
|
|
var qmdate=Convert.ToDateTime(qmdate1);
|
|
//var qmdate = qmdate2.AddDays(-1);
|
|
var qmtime = qmdate.ToString("yyyyMM");
|
|
var qm = bll.IsExistsTable(date_base, "electricity_data_" + qmtime);
|
|
if (qm == false)
|
|
{
|
|
bll.CreateTable(qmtime);
|
|
}
|
|
decimal? mnum1 = 0;
|
|
decimal? mnum2 = 0;
|
|
decimal? mnum3 = 0;
|
|
var jmlist = bll.GetModelListDate(" EntireTime='" + jmdate + "'", jmtime);
|
|
var zmlist = bll.GetModelListDate(" EntireTime='" + zmdate + "'", zmtime);
|
|
var qmlist = bll.GetModelListDate(" EntireTime='" + qmdate + "'", qmtime);
|
|
foreach (var aitem in list)
|
|
{
|
|
//今月列表
|
|
var mlist1 = jmlist.Where(x=>x.DeviceId== aitem.DeviceId).ToList();
|
|
mnum1 += mlist1.Sum(x => Convert.ToDecimal(x.EH));
|
|
//昨月列表
|
|
var mlist2 = zmlist.Where(x=>x.DeviceId==aitem.DeviceId).ToList();
|
|
mnum2 += mlist2.Sum(x => Convert.ToDecimal(x.EH));
|
|
//前月列表
|
|
var mlist3 = qmlist.Where(x=>x.DeviceId== aitem.DeviceId).ToList();
|
|
mnum3 += mlist3.Sum(x => Convert.ToDecimal(x.EH));
|
|
}
|
|
#endregion
|
|
#region 年
|
|
//今年
|
|
var jydate = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
|
|
var jytime = now.ToString("yyyyMM");
|
|
var ay = bll.IsExistsTable(date_base, "electricity_data_" + jytime);
|
|
if (ay == false)
|
|
{
|
|
bll.CreateTable(jmtime);
|
|
}
|
|
//昨年
|
|
var zydate = DateTime.Now.ToString("yyyy-12-31 00:00:00");
|
|
var zytime = DateTime.Now.ToString("yyyy12");
|
|
var by = bll.IsExistsTable(date_base, "electricity_data_" + zytime);
|
|
if (by == false)
|
|
{
|
|
bll.CreateTable(zytime);
|
|
}
|
|
//前年
|
|
var qydate1 = DateTime.Now.AddYears(-1).ToString("yyyy-12-31 00:00:00");
|
|
var qydate = Convert.ToDateTime(qydate1);
|
|
var qytime = qmdate.ToString("yyyyMM");
|
|
var qy = bll.IsExistsTable(date_base, "electricity_data_" + qytime);
|
|
if (qy == false)
|
|
{
|
|
bll.CreateTable(qytime);
|
|
}
|
|
decimal? ynum1 = 0;
|
|
decimal? ynum2 = 0;
|
|
decimal? ynum3 = 0;
|
|
var jylist= bll.GetModelListDate(" EntireTime='" + jydate + "'", jytime);
|
|
var zylist = bll.GetModelListDate(" EntireTime='" + zydate + "'", zytime);
|
|
var qylist= bll.GetModelListDate(" EntireTime='" + qydate + "'", qytime);
|
|
foreach (var aitem in list)
|
|
{
|
|
//今年列表
|
|
var ylist1 = jylist.Where(x=>x.DeviceId==aitem.DeviceId).ToList();
|
|
ynum1 += ylist1.Sum(x => Convert.ToDecimal(x.EH));
|
|
//昨年列表
|
|
var ylist2 = zylist.Where(x => x.DeviceId == aitem.DeviceId).ToList();
|
|
ynum2 += ylist2.Sum(x => Convert.ToDecimal(x.EH));
|
|
//前年列表
|
|
var ylist3 = qylist.Where(x => x.DeviceId == aitem.DeviceId).ToList();
|
|
ynum3 += ylist3.Sum(x => Convert.ToDecimal(x.EH));
|
|
}
|
|
|
|
#endregion
|
|
var model = new electrical_conditionData();
|
|
model.ToDay = num1 - num2;
|
|
model.YearDay = num2 - num3;
|
|
model.ThisMonth = mnum1 - mnum2;
|
|
model.LastMonth = mnum2 - mnum3;
|
|
model.ThisYear= ynum1 - ynum2;
|
|
model.LastYear= ynum2 - ynum3;
|
|
if (model.ToDay < 0)
|
|
{
|
|
model.ToDay = 0;
|
|
}
|
|
if (model.YearDay < 0)
|
|
{
|
|
model.YearDay = 0;
|
|
}
|
|
if (model.ThisMonth < 0)
|
|
{
|
|
model.ThisMonth = 0;
|
|
}
|
|
if (model.LastMonth < 0)
|
|
{
|
|
model.LastMonth = 0;
|
|
}
|
|
if (model.ThisYear < 0)
|
|
{
|
|
model.ThisYear = 0;
|
|
}
|
|
if (model.LastYear < 0)
|
|
{
|
|
model.LastYear = 0;
|
|
}
|
|
if (model.YearDay == 0)
|
|
{
|
|
model.DayDency = 100;
|
|
}
|
|
else
|
|
{
|
|
model.DayDency =Convert.ToDecimal( Math.Round(Convert.ToDouble(model.YearDay /model.ToDay * 100),3));
|
|
if (model.DayDency < 0)
|
|
{
|
|
model.DayDency = model.DayDency * -1;
|
|
}
|
|
if (model.ToDay < model.DayDency)
|
|
{
|
|
model.DayDency = model.DayDency*-1;
|
|
}
|
|
}
|
|
if (model.LastMonth == 0)
|
|
{
|
|
model.MonthDency = 100;
|
|
}
|
|
else
|
|
{
|
|
model.MonthDency =Convert.ToDecimal(Math.Round(Convert.ToDouble( model.LastMonth / model.ThisMonth * 100),3));
|
|
if (model.MonthDency < 0)
|
|
{
|
|
model.MonthDency = model.MonthDency * -1;
|
|
}
|
|
if (model.ThisMonth < model.LastMonth)
|
|
{
|
|
model.MonthDency = model.MonthDency*-1;
|
|
}
|
|
}
|
|
if (model.LastYear == 0)
|
|
{
|
|
model.YearDency = 100;
|
|
}
|
|
else
|
|
{
|
|
model.YearDency = Convert.ToDecimal(Math.Round(Convert.ToDouble(model.LastYear / model.ThisYear * 100), 3));
|
|
if (model.YearDency < 0)
|
|
{
|
|
model.YearDency = model.YearDency * -1;
|
|
}
|
|
if (model.ThisYear < model.LastYear)
|
|
{
|
|
model.YearDency = model.YearDency*-1;
|
|
}
|
|
}
|
|
data.Add(model);
|
|
res.code = 200;
|
|
res.msg = "成功";
|
|
res.data = data;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
} |