116 lines
5.5 KiB
C#
116 lines
5.5 KiB
C#
using DataServer.api;
|
|
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 GetUnitConsumptionController : 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_unit_consumption_response();
|
|
try
|
|
{
|
|
var now = DateTime.Now;
|
|
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");
|
|
//今日用水量
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
var end_time1 = DateTime.Parse(now.AddHours(-1).ToString("yyyy-MM-dd HH") + ":00:00");
|
|
//今日电量
|
|
decimal electricity = 0;
|
|
//判断表是否存在,不存在就创建
|
|
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
|
|
var time = now.ToString("yyyyMM");
|
|
if (!bll.IsExistsTable(date_base, "electricity_data_" + time))
|
|
{
|
|
bll.CreateTable(time);
|
|
}
|
|
var list = bll.GetModelListDate(" EntireTime='" + start_time + "' or EntireTime='" + end_time1 + "' ", 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_time1).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 avg_energy_consumption = Math.Round((water + natural_gas + electricity) / 3300, 2);
|
|
//单位建筑面积综合能耗
|
|
decimal unit_building_area = Math.Round((water + natural_gas + electricity) / 59000, 2);
|
|
res.code = 200;
|
|
res.msg = "成功";
|
|
res.data = new unit_consumption() { avg_energy_consumption = avg_energy_consumption, avg_water_consumption = avg_water_consumption, unit_building_area = unit_building_area };
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|