DongYing/DongYingAPI/Controllers/api/GetCarbonIntensityControlle...

117 lines
4.7 KiB
C#

using DataServer.api;
using DataServer.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Web.Http;
namespace DongYingAPI.Controllers.api
{
/// <summary>
/// 碳-排放强度
/// </summary>
public class GetCarbonIntensityController : 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_carbon_intensity();
#region
//表名
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
//今月的表是否存在
var time = DateTime.Now.ToString("yyyyMM");
var a1 = bll.IsExistsTable(date_base, "electricity_data_" + time);
if (a1 == false)
{
bll.CreateTable(time);
}
#endregion
try
{
var data =new List<carbon_intensityData>();
var time_count = Convert.ToInt32(DateTime.Now.Month);
for (int i = 0; i < time_count; i++)
{
// 只处理今年的数据
if (DateTime.Now.AddMonths(-i).Year != DateTime.Now.Year)
{
continue;
}
var now = DateTime.Now;
time = now.AddMonths(-i).ToString("yyyMM");
var a = bll.IsExistsTable(date_base, "electricity_data_" + time);
if (a == false)
{
bll.CreateTable(time);
}
var jtime = now.AddMonths(-i).ToString("MM月");
var ztime1 = "";
DateTime ztime;
if (i == 0)
{
//这月
ztime1 = now.AddMonths(-i).AddHours(-1).ToString("yyyy-MM-dd HH:00:00");
ztime = Convert.ToDateTime(ztime1);
}
else
{
//这月
ztime1 = now.AddMonths(-i+1).ToString("yyyy-MM-01 00:00:00");
var ztime2 = Convert.ToDateTime(ztime1);
ztime = ztime2.AddDays(-1);
}
//上月
var stime1 = now.AddMonths(-(i)).ToString("yyyy-MM-01 00:00:00");
var stime2 = Convert.ToDateTime(stime1);
var stime = stime2.AddDays(-0);
var atime = stime.ToString("yyyyMM");
var b = bll.IsExistsTable(date_base, "electricity_data_" + atime);
if (b == false)
{
bll.CreateTable(atime);
}
//这天列表
decimal? num1=0;
var list1 = bll.GetModelListDate(" EntireTime='" + ztime + "'", time);
num1 = list1.Sum(x => x.EH);
//上个小时
decimal? num2 = 0;
var list2 = bll.GetModelListDate(" EntireTime='" + stime + "'", atime);
num2=list2.Sum(x => x.EH);
var model = new carbon_intensityData();
model.time = jtime;
model.CarbonPer=Convert.ToDecimal(Math.Round(Convert.ToDouble(((num1 - num2) / 1000 * Convert.ToDecimal(0.5703)) / 100000), 3));
model.ArealCarbon= Convert.ToDecimal(Math.Round(Convert.ToDouble(((num1 - num2) / 1000 * Convert.ToDecimal(0.5703)) / 59000), 3));
if (model.CarbonPer < 0)
{
model.CarbonPer = 0;
}
if(model.ArealCarbon< 0) {
model.ArealCarbon = 0;
}
data.Add(model);
}
res.code = 200;
res.msg = "成功";
var adata=data.OrderBy(x=>x.time).ToList();
res.data = adata;
}
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;
}
}
}