LonglslandExhibitionCenter/LonglslandExhibitionCenter/Controllers/api/GetTotalEnergyController.cs

134 lines
5.9 KiB
C#

using DataService.api;
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 LonglslandExhibitionCenter.Controllers.api
{
/// <summary>
/// 能耗-能耗总量
/// </summary>
public class GetTotalEnergyController : ApiController
{
DataService.BLL.electricity_data bll = new DataService.BLL.electricity_data();
// GET api/<controller>
public HttpResponseMessage Get(string date = "")
{
var res = new get_total_energy();
#region
var time = DateTime.Now.ToString("yyyyMM");
//表名
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
var a = bll.IsExistsTable(date_base, "electricity_data_" + time);
if (a == false)
{
bll.CreateTable(time);
}
#endregion
try
{
if (!string.IsNullOrEmpty(date))
{
var data = new List<total_energyData>();
var now = DateTime.Now;
var list = bll.GetModelListDate(" Reserve1='配电室低压'", time);
if (date == "日")
{
var time_count = Convert.ToInt32(DateTime.Now.Day);
for (int i = 0; i < time_count; i++)
{
string sdate;
if (i == 0)
{
sdate = now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
}
else
{
sdate = now.AddDays(-i + 1).ToString("yyyy-MM-dd 00:00:00");
}
var edate = now.AddDays(-i).ToString("yyyy-MM-dd 00:00:00");
var alist = list.Where(x => x.EntireTime == Convert.ToDateTime(sdate)).ToList();
var blist = list.Where(x => x.EntireTime == Convert.ToDateTime(edate)).ToList();
var num = alist.Sum(x => x.EH) - blist.Sum(x => x.EH);
var model = new total_energyData()
{
time = DateTime.Now.AddDays(-i).ToString("MM-dd"),
EH = Convert.ToDecimal(Math.Round(Convert.ToDouble(num / 10000 * Convert.ToDecimal(1.229)), 3))
};
if (model.EH < 0)
{
model.EH = 0;
}
data.Add(model);
}
}
if (date == "月")
{
var time_count = Convert.ToInt32(DateTime.Now.Month);
for (int i = 0; i < time_count; i++)
{
string sdate;
if (i == 0)
{
sdate = now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
}
else
{
sdate = now.AddMonths(-i + 1).ToString("yyyy-MM-01 00:00:00");
}
var edate = now.AddMonths(-i).ToString("yyyy-MM-01 00:00:00");
var stime = Convert.ToDateTime(sdate).ToString("yyyyMM");
var etime = Convert.ToDateTime((edate)).ToString("yyyyMM");
var b = bll.IsExistsTable(date_base, "electricity_data_" + stime);
if (b == false)
{
bll.CreateTable(stime);
}
var c = bll.IsExistsTable(date_base, "electricity_data_" + etime);
if (c == false)
{
bll.CreateTable(etime);
}
var alist = bll.GetModelListDate(" EntireTime='" + sdate + "' and Reserve1='配电室低压'", stime);
var blist = bll.GetModelListDate(" EntireTime='" + edate + "' and Reserve1='配电室低压'", etime);
var num = alist.Sum(x => x.EH) - blist.Sum(x => x.EH);
var model = new total_energyData()
{
time = Convert.ToDateTime(now).AddMonths(-i).ToString("MM月"),
EH = Convert.ToDecimal(Math.Round(Convert.ToDouble(num / 10000 * Convert.ToDecimal(1.229)), 3))
};
if (model.EH < 0)
{
model.EH = 0;
}
data.Add(model);
}
}
var adata = data.OrderBy(x => x.time).ToList();
res.code = 200;
res.msg = "成功";
res.data = adata;
}
else
{
res.code = 201;
res.msg = "参数不能为空";
}
}
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;
}
}
}