188 lines
9.1 KiB
C#
188 lines
9.1 KiB
C#
using DataServer.api.EnergyEfficiency;
|
|
using DongYingAPI.Util;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Web.Http;
|
|
|
|
namespace DongYingAPI.Controllers.api.EnergyEfficiency
|
|
{
|
|
public class GetEnergyTrendController : 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();
|
|
|
|
DataServer.BLL.planned_energy plan_bll = new DataServer.BLL.planned_energy();
|
|
|
|
/// <summary>
|
|
/// 获取能耗趋势接口
|
|
/// </summary>
|
|
/// <param name="type">类型 电、水、天然气</param>
|
|
/// <returns></returns>
|
|
public HttpResponseMessage Get(string type)
|
|
{
|
|
var res = new get_energy_trend_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");
|
|
var time_count = Tool.GetUsedMonth1("月", start_time, end_time);
|
|
var data = new List<energy_trend>();
|
|
var plan_list = plan_bll.GetModelList(" Type='" + type + "' ");
|
|
if (type == "电")
|
|
{
|
|
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("");
|
|
|
|
for (int i = 0; i <= time_count; i++)
|
|
{
|
|
var month = start_time.AddMonths(i).Month;
|
|
var time = start_time.AddMonths(i);
|
|
var end = time.AddMonths(1);
|
|
if (time.ToString("yyyy-MM") == end_time.ToString("yyyy-MM"))
|
|
{
|
|
end = end_time;
|
|
}
|
|
var plan_model = plan_list.Where(a => a.Month == month).FirstOrDefault();
|
|
decimal value = 0;
|
|
foreach (var item in device_list)
|
|
{
|
|
var start_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == time).FirstOrDefault();
|
|
var end_data = list.Where(a => a.DeviceId == item.DeviceId && a.EntireTime.Value == end).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;
|
|
value += eh;
|
|
}
|
|
}
|
|
}
|
|
|
|
var model = new energy_trend();
|
|
model.Month = month.ToString() + "月";
|
|
if (plan_model != null)
|
|
{
|
|
model.Plan = plan_model.Plan.Value;
|
|
}
|
|
model.Value = value;
|
|
data.Add(model);
|
|
}
|
|
}
|
|
else if (type == "水")
|
|
{
|
|
var water_list = water_bll.GetModelList(" EntireTime>='" + start_time + "' and EntireTime<='" + end_time + "' ");
|
|
for (int i = 0; i <= time_count; i++)
|
|
{
|
|
var month = start_time.AddMonths(i).Month;
|
|
var time = start_time.AddMonths(i);
|
|
var end = time.AddMonths(1);
|
|
if (time.ToString("yyyy-MM") == end_time.ToString("yyyy-MM"))
|
|
{
|
|
end = end_time;
|
|
}
|
|
var plan_model = plan_list.Where(a => a.Month == month).FirstOrDefault();
|
|
var start_data = water_list.Where(a => a.EntireTime.Value == time).FirstOrDefault();
|
|
var end_data = water_list.Where(a => a.EntireTime.Value == end).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;
|
|
var model = new energy_trend();
|
|
model.Month = month.ToString() + "月";
|
|
model.Value = water_yield;
|
|
if (plan_model != null)
|
|
{
|
|
model.Plan = plan_model.Plan.Value;
|
|
}
|
|
data.Add(model);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (type == "天然气")
|
|
{
|
|
var gas_list = gas_bll.GetModelList(" EntireTime>='" + start_time + "' and EntireTime<='" + end_time + "' ");
|
|
for (int i = 0; i <= time_count; i++)
|
|
{
|
|
var month = start_time.AddMonths(i).Month;
|
|
var time = start_time.AddMonths(i);
|
|
var end = time.AddMonths(1);
|
|
if (time.ToString("yyyy-MM") == end_time.ToString("yyyy-MM"))
|
|
{
|
|
end = end_time;
|
|
}
|
|
var plan_model = plan_list.Where(a => a.Month == month).FirstOrDefault();
|
|
var start_data = gas_list.Where(a => a.EntireTime.Value == time).FirstOrDefault();
|
|
var end_data = gas_list.Where(a => a.EntireTime.Value == end).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;
|
|
var model = new energy_trend();
|
|
model.Month = month.ToString() + "月";
|
|
model.Value = gas_consumption;
|
|
if (plan_model != null)
|
|
{
|
|
model.Plan = plan_model.Plan.Value;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|