136 lines
6.2 KiB
C#
136 lines
6.2 KiB
C#
using DataServer.api.EnergyEfficiency;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Data.Services;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Web.Http;
|
|
|
|
namespace DongYingAPI.Controllers.api.EnergyEfficiency
|
|
{
|
|
public class GetEnergyConsumptionController : ApiController
|
|
{
|
|
DataServer.BLL.device_info device_bll = new DataServer.BLL.device_info();
|
|
|
|
DataServer.BLL.electricity_data bll = new DataServer.BLL.electricity_data();
|
|
|
|
/// <summary>
|
|
/// 获取各回路(设备)能耗概况接口
|
|
/// </summary>
|
|
/// <param name="type">类型 年、月、日</param>
|
|
/// <returns></returns>
|
|
public HttpResponseMessage Get(string type)
|
|
{
|
|
var res = new get_energy_consumption_response();
|
|
try
|
|
{
|
|
var now = DateTime.Now;
|
|
var device_list = device_bll.GetModelList("");
|
|
var list = new List<DataServer.Model.electricity_data>();
|
|
//判断表是否存在,不存在就创建
|
|
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
|
|
if (type == "年")
|
|
{
|
|
var start_date = DateTime.Parse(now.ToString("yyyy") + "-01-01 00:00:00");
|
|
var time_count = GetUsedMonth1("月", start_date, now);
|
|
var source = "";
|
|
for (int i = 0; i <= time_count; i++)
|
|
{
|
|
var time = start_date.AddMonths(i).ToString("yyyyMM");
|
|
if (bll.IsExistsTable(date_base, "electricity_data_" + time))
|
|
{
|
|
if (time == start_date.ToString("yyyyMMdd") || time == now.ToString("yyyyMMdd"))
|
|
{
|
|
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 CreateTime>='{1}' and CreateTime<='{2}'", time, start_date, now);
|
|
}
|
|
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, "", "");
|
|
}
|
|
}
|
|
else if (type == "月")
|
|
{
|
|
var start_date = DateTime.Parse(now.ToString("yyyy-MM") + "-01 00:00:00");
|
|
var time = now.ToString("yyyyMM");
|
|
if (!bll.IsExistsTable(date_base, "electricity_data_" + time))
|
|
{
|
|
bll.CreateTable(time);
|
|
}
|
|
list = bll.GetList("(select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_" + time + ")", " CreateTime>='" + start_date + "' and CreateTime<='" + now + "' ", "");
|
|
}
|
|
else if (type == "日")
|
|
{
|
|
var start_date = DateTime.Parse(now.ToString("yyyy-MM-dd") + " 00:00:00");
|
|
var time = now.ToString("yyyyMM");
|
|
if (!bll.IsExistsTable(date_base, "electricity_data_" + time))
|
|
{
|
|
bll.CreateTable(time);
|
|
}
|
|
list = bll.GetList("(select ElectricityId,DeviceId,EH,P,Kvar,Ia,Ib,Ic,Ua,Ub,Uc,ServiceRating,CreateTime,EntireTime,Reserve1,Reserve2,Reserve3,Reserve4,Reserve5 from electricity_data_" + time + ")", " CreateTime>='" + start_date + "' and CreateTime<='" + now + "' ", "");
|
|
}
|
|
|
|
var data = new List<energy_consumption>();
|
|
foreach (var item in device_list)
|
|
{
|
|
var model = new energy_consumption();
|
|
model.DeviceName = item.DeviceName;
|
|
model.OperatingPower = list.Where(a => a.DeviceId == item.DeviceId).Sum(a => a.ServiceRating).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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算两个时间年份月份差
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public int GetUsedMonth1(string type, DateTime dynamicTime, DateTime currentDate)
|
|
{
|
|
try
|
|
{
|
|
int year = currentDate.Year - dynamicTime.Year; //相差的年份
|
|
int month = (currentDate.Year - dynamicTime.Year) * 12 + (currentDate.Month - dynamicTime.Month); //相差的月份
|
|
//int month1 = currentDate.Year * 12 + currentDate.Month - dynamicTime.Year * 12 - dynamicTime.Month; //相差的月份
|
|
|
|
TimeSpan used = DateTime.Now - dynamicTime;
|
|
double totalDays = used.TotalDays; //相差总天数
|
|
if (type == "年")
|
|
{
|
|
return Convert.ToInt32(year);
|
|
}
|
|
else
|
|
{
|
|
return Convert.ToInt32(month);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
}
|