DongYing/DongYingAPI/Controllers/api/GetElectricEquipmentControl...

80 lines
3.1 KiB
C#

using DataServer.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.Security.Policy;
using System.Text;
using System.Web.Http;
namespace DongYingAPI.Controllers.api
{
/// <summary>
/// 用能监测2-用电设备排名
/// </summary>
public class GetElectricEquipmentController : ApiController
{
DataServer.BLL.electricity_data bll = new DataServer.BLL.electricity_data();
DataServer.BLL.device_info bll_info = new DataServer.BLL.device_info();
public HttpResponseMessage Get()
{
var res = new get_electrical_ranking();
try
{
var data = new List<electrical_rankingData>();
//表名
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
#region
//今天
var now = DateTime.Now;
var jdate = DateTime.Now.AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
var jtime = now.ToString("yyyyMM");
var a = bll.IsExistsTable(date_base, "electricity_data_" + jtime);
if (a == false)
{
bll.CreateTable(jtime);
}
//昨天
var zdate = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
var ztime = now.ToString("yyyyMM");
var b = bll.IsExistsTable(date_base, "electricity_data_" + ztime);
if (b == false)
{
bll.CreateTable(ztime);
}
#endregion
var alist = bll_info.GetModelList("");
decimal? num = 0;
decimal? num1 = 0;
var ljlist = bll.GetModelListDate(" EntireTime='" + jdate + "'", jtime);
var lzlist = bll.GetModelListDate(" EntireTime='" + zdate + "'", ztime);
foreach (var item in alist)
{
var model = new electrical_rankingData();
model.ElectricaName = item.DeviceName;
var jlist = ljlist.Where(x=>x.DeviceId== item.DeviceId).ToList();
var zlist = lzlist.Where(x=>x.DeviceId== item.DeviceId).ToList();
num = jlist.Sum(x => Convert.ToDecimal(x.EH));
num1 = zlist.Sum(x => Convert.ToDecimal(x.EH));
model.ElectricaValue = num-num1;
data.Add(model);
}
var elist = data.OrderByDescending(x => x.ElectricaValue).ToList();
res.code = 200;
res.msg = "成功";
res.data = elist;
}
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;
}
}
}