DongYing/DongYingAPI/Controllers/api/EnergyEfficiency/GetEnergyBenchmarkingContro...

160 lines
7.6 KiB
C#

using DataServer.api.EnergyEfficiency;
using DongYingAPI.Util;
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
{
/// <summary>
/// 获取能效对标接口
/// </summary>
public class GetEnergyBenchmarkingController : ApiController
{
DataServer.BLL.device_info device_bll = new DataServer.BLL.device_info();
DataServer.BLL.electricity_data bll = new DataServer.BLL.electricity_data();
DataServer.BLL.water_data water_bll = new DataServer.BLL.water_data();
DataServer.BLL.gas_data gas_bll = new DataServer.BLL.gas_data();
/// <summary>
/// 获取能效对标接口
/// </summary>
/// <returns></returns>
public HttpResponseMessage Get()
{
var res = new get_energy_benchmarking_response();
try
{
var now = DateTime.Now;
var start_time = DateTime.Parse(now.ToString("yyyy") + "-01-01 00:00:00");
var end_time = DateTime.Parse(now.ToString("yyyy-MM-dd HH") + ":00:00");
decimal water_consumption = decimal.Parse(ConfigurationManager.AppSettings["WaterConsumption"].ToString());
decimal energy_consumption = decimal.Parse(ConfigurationManager.AppSettings["EnergyConsumption"].ToString());
decimal building_area = decimal.Parse(ConfigurationManager.AppSettings["BuildingArea"].ToString());
//今年电量
decimal electricity = 0;
var time_count = Tool.GetUsedMonth1("月", start_time, end_time);
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
var list = new List<DataServer.Model.electricity_data>();
var source = "";
for (int i = 0; i <= time_count; i++)
{
var time = start_time.AddMonths(i).ToString("yyyyMM");
if (bll.IsExistsTable(date_base, "electricity_data_" + time))
{
if (time == start_time.ToString("yyyyMM") || time == end_time.ToString("yyyyMM"))
{
source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where EntireTime>='{1}' and EntireTime<='{2}'", time, start_time, end_time);
}
else
{
source += string.Format(" (select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_{0} where 1=1 ", time);
}
source += ") UNION all ";
}
}
if (!string.IsNullOrEmpty(source))
{
source = source.Substring(0, source.Length - 11);
list = bll.GetList(source, "", "");
}
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;
electricity += eh;
}
}
}
//今年用水量
decimal water = 0;
//今年人均用水量
decimal avg_water_consumption = 0;
//根据事件查询两条数据,然后值相减为用水量
var water_list = water_bll.GetModelList(" EntireTime='" + start_time + "' or EntireTime='" + end_time + "' ");
if (water_list.Count >= 2)
{
var start_data = water_list.Where(a => a.EntireTime.Value == start_time).FirstOrDefault();
var end_data = water_list.Where(a => a.EntireTime.Value == end_time).FirstOrDefault();
if (start_data != null && end_data != null)
{
if (start_data.WaterYield != null && end_data.WaterYield != null)
{
decimal water_yield = end_data.WaterYield.Value - start_data.WaterYield.Value;
avg_water_consumption = Math.Round(water_yield / 3300, 2);
water = water_yield;
}
}
}
//今年天然气
decimal natural_gas = 0;
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;
}
}
}
//今年人均综合能耗
decimal avg_energy_consumption = Math.Round((water + natural_gas + electricity) / 3300, 2);
//今年单位建筑面积综合能耗
decimal unit_building_area = Math.Round((water + natural_gas + electricity) / 59000, 2);
decimal yoy_water = avg_water_consumption - water_consumption;
decimal yoy_energy_consumption = avg_energy_consumption - energy_consumption;
decimal yoy_building_area = unit_building_area - building_area;
res.code = 200;
res.msg = "成功";
res.data = new energy_benchmarking()
{
WateConsumption = water_consumption,
EnergyConsumption = energy_consumption,
BuildingArea = building_area,
AvgWaterConsumption = avg_water_consumption,
AvgEnergyConsumption = avg_energy_consumption,
UnitBuildingArea = unit_building_area,
YoyBuildingArea = yoy_building_area,
YoyEnergyConsumption = yoy_energy_consumption,
YoyWaterConsumption = yoy_water
};
}
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;
}
}
}