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

156 lines
6.7 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;
using System.Xml.Linq;
namespace DongYingAPI.Controllers.api.EnergyEfficiency
{
/// <summary>
/// 获取能源流向接口
/// </summary>
public class GetEnergyFlowController : ApiController
{
DataServer.BLL.electricity_data bll = new DataServer.BLL.electricity_data();
DataServer.BLL.device_info device_bll = new DataServer.BLL.device_info();
DataServer.BLL.gas_data gas_bll = new DataServer.BLL.gas_data();
/// <summary>
/// 获取设备运行情况接口
/// </summary>
/// <returns></returns>
public HttpResponseMessage Get()
{
var res = new get_energy_flow_response();
try
{
var now = DateTime.Now;
//空调
decimal air_conditioning = 0;
//照明
decimal lighting = 0;
//电梯
decimal lift = 0;
//其他
decimal other = 0;
//天然气
decimal natural_gas = 0;
//判断表是否存在,不存在就创建
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.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;
}
}
}
}
//查询天然气的设备表,计算每个设备的用气量
var gas_list = gas_bll.GetModelList(" EntireTime='" + start_time + "' or EntireTime='" + end_time + "' ");
if (gas_list.Count >= 2)
{
var start_data = gas_list.Where(a => a.EntireTime.Value == start_time).FirstOrDefault();
var end_data = gas_list.Where(a => a.EntireTime.Value == end_time).FirstOrDefault();
if (start_data != null && end_data != null)
{
if (start_data.GasConsumption != null && end_data.GasConsumption != null)
{
decimal gas_consumption = end_data.GasConsumption.Value - start_data.GasConsumption.Value;
natural_gas = gas_consumption;
}
}
}
res.code = 200;
res.msg = "成功";
res.data = new energy_flow()
{
Name = "原始值折标煤",
Value = (air_conditioning + lighting + lift + other + natural_gas),
data = new List<secondary_energy_flow>() {
new secondary_energy_flow() {
Name = "天然气",
Value = natural_gas,
data=new List<three_level_energy_flow>(){
new three_level_energy_flow()
{
Name="食堂",
Value=natural_gas
}
}
},
new secondary_energy_flow() {
Name = "电",
Value = (air_conditioning + lighting + lift + other),
data = new List<three_level_energy_flow>() {
new three_level_energy_flow() {
Name="空调",
Value=air_conditioning
},new three_level_energy_flow()
{
Name="照明",
Value=lighting
},new three_level_energy_flow()
{
Name="电梯",
Value=lift
},new three_level_energy_flow()
{
Name="其他",
Value=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;
}
}
}