LonglslandExhibitionCenter/LonglslandExhibitionCenter/Controllers/api/GetEnergyCalendarController.cs

96 lines
4.1 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 GetEnergyCalendarController : ApiController
{
DataService.BLL.electricity_data bll = new DataService.BLL.electricity_data();
// GET api/<controller
public HttpResponseMessage Get(string date = "")
{
var res = new get_electricity_consumption();
try
{
var data = new List<electricity_consumptionData>();
var now = DateTime.Now;
if (!string.IsNullOrEmpty(date))
{
var time = date + "-01 00:00:00";
var time1=Convert.ToDateTime(time);
var time2 = time1.AddMonths(1).AddDays(-1);
var stime =Convert.ToDateTime(time2);
var atime = stime.ToString("yyyyMM");
var btime=Convert.ToDateTime(time).AddMonths(1).ToString("yyyyMM");
//表名
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
var a= bll.IsExistsTable(date_base, "electricity_data_" + atime);
if (a == false)
{
bll.CreateTable(atime);
}
var b = bll.IsExistsTable(date_base, "electricity_data_" + btime);
if (b == false)
{
bll.CreateTable(btime);
}
var list1 = bll.GetModelListDate(" Reserve1='配电室高压'", atime);
var list2 = bll.GetModelListDate(" Reserve1='配电室高压'", btime);
var time_count = Convert.ToInt32(stime.Day);
for (int i = 0; i < time_count; i++)
{
var num1 = Convert.ToInt32(stime.AddDays(-i).Day);
var num2 = Convert.ToInt32(DateTime.Now.Day);
string sdate;
if (num1 == num2)
{
sdate= DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
}
else
{
sdate =stime.AddDays(-i+1).ToString("yyyy-MM-dd 00:00:00");
}
var edate=stime.AddDays(-i).ToString("yyyy-MM-dd 00:00:00");
var alist = list1.Where(x => x.EntireTime ==Convert.ToDateTime(sdate)).ToList();
var blist=list2.Where(x=>x.EntireTime==Convert.ToDateTime(edate)).ToList();
decimal? anum = alist.Sum(x => x.EH);
decimal? bnum = blist.Sum(x => x.EH);
var model = new electricity_consumptionData()
{
time = stime.AddDays(-i).ToString("dd"),
EH =Convert.ToDecimal(Math.Round(Convert.ToDouble(anum - bnum),3))
};
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;
}
}
}