DongYing/DongYingAPI/Controllers/api/GetDailyElectricityControll...

102 lines
3.9 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;
using System.Web.Mvc;
namespace DongYingAPI.Controllers.api
{
/// <summary>
/// 空调-日用电量
/// </summary>
public class GetDailyElectricityController : 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_daily_electricity();
try
{
var data = new List<daily_electricityData>();
var time = DateTime.Now.ToString("yyyyMM");
var stime = DateTime.Now.AddDays(-1).ToString("yyyyMM");
#region
//表名
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
//今月的表是否存在
var a1 = bll.IsExistsTable(date_base, "electricity_data_" + time);
if (a1 == false)
{
bll.CreateTable(time);
}
//上月
var a = bll.IsExistsTable(date_base, "electricity_data_" + stime);
if (a1 == false)
{
bll.CreateTable(stime);
}
#endregion
//今天
var sdate = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
//昨天
var edate = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
//前天
var qdate=DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd 00:00:00");
var list = bll_info.GetModelList("").Where(x => x.FloorName == "科技馆负一层地源热泵").ToList();
decimal? num1 = 0;
decimal? num2 = 0;
decimal? num3 = 0;
string slist=string.Join(",", list.Select(x=>x.DeviceId));
//今天列表
var list1 = bll.GetModelListsDate(slist, time).Where(x => x.EntireTime == Convert.ToDateTime(sdate)).ToList();
foreach (var item in list1)
{
num1 += item.EH;
}
//昨天列表
var list2 = bll.GetModelListsDate(slist, stime).Where(x => x.EntireTime == Convert.ToDateTime(edate)).ToList();
foreach (var item in list2)
{
num2 += item.EH;
}
//前天列表
var list3 = bll.GetModelListsDate(slist, stime).Where(x => x.EntireTime == Convert.ToDateTime(qdate)).ToList();
foreach (var item in list3)
{
num3 += item.EH;
}
var model = new daily_electricityData();
model.CurrentDate = num1 - num2;
if (model.CurrentDate == 0)
{
model.CurrentDate = 0;
}
model.YesterDay=num2- num3;
if(model.YesterDay == 0)
{
model.YesterDay = 0;
}
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;
}
}
}