DongYing/DongYingAPI/Controllers/api/EnergyEfficiency/GetItemizeEnergyController.cs

93 lines
3.9 KiB
C#

using DataServer.api.EnergyEfficiency;
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.EnergyEfficiency
{
public class GetItemizeEnergyController : ApiController
{
DataServer.BLL.device_info device_bll = new DataServer.BLL.device_info();
DataServer.BLL.electricity_data bll = new DataServer.BLL.electricity_data();
/// <summary>
/// 获取分项能耗概况接口
/// </summary>
/// <returns></returns>
public HttpResponseMessage Get()
{
var res = new get_itemize_energy_response();
try
{
//空调
decimal air_conditioning = 0;
//照明
decimal lighting = 0;
//电梯
decimal lift = 0;
//其他
decimal other = 0;
var now = DateTime.Now;
//判断表是否存在,不存在就创建
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
var time = now.ToString("yyyyMM");
if (!bll.IsExistsTable(date_base, "electricity_data_" + time))
{
bll.CreateTable(time);
}
//查询今天0点和当前小时整点的数据出来
var start_time = DateTime.Parse(now.ToString("yyyy-MM-dd") + " 00:00:00");
var end_time = DateTime.Parse(now.AddHours(-1).ToString("yyyy-MM-dd HH") + ":00:00");
var list = bll.GetModelListDate(" EntireTime='" + start_time + "' or EntireTime='" + end_time + "' ", time);
//查询电设备表,计算每个设备的用电量,然后按空调、电梯、照明、其它累加用电量
var device_list = device_bll.GetModelList("");
foreach (var item in device_list)
{
var start_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == start_time).FirstOrDefault();
var end_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == end_time).FirstOrDefault();
if (start_data != null && end_data != null)
{
if (start_data.EH != null && end_data.EH != null)
{
decimal eh = end_data.EH.Value - start_data.EH.Value;
if (item.DeviceName.Contains("照明"))
{
lighting += eh;
}
else if (item.DeviceName.Contains("空调"))
{
air_conditioning += eh;
}
else if (item.DeviceName.Contains("梯"))
{
lift += eh;
}
else
{
other += eh;
}
}
}
}
res.code = 200;
res.msg = "成功";
res.data = new itemize_energy() { Lift = lift, AirConditioning = air_conditioning, Lighting = lighting, Other = other };
}
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;
}
}
}