194 lines
8.6 KiB
C#
194 lines
8.6 KiB
C#
using DataServer.api;
|
|
using DataServer.Model;
|
|
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 DongYingAPI.Controllers
|
|
{
|
|
/// <summary>
|
|
/// 碳-碳排放量
|
|
/// </summary>
|
|
public class GetCarbonMeasureController : 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(string date = "")
|
|
{
|
|
var res = new get_carbon_measure();
|
|
#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
|
|
{
|
|
if (!string.IsNullOrEmpty(date))
|
|
{
|
|
var data = new List<carbon_measureData>();
|
|
if (date == "月")
|
|
{
|
|
var time_count = Convert.ToInt32(DateTime.Now.Day);
|
|
for (int i = 0; i < time_count; i++)
|
|
{
|
|
// 只处理今年的数据
|
|
if (DateTime.Now.AddDays(-i).Month != DateTime.Now.Month)
|
|
{
|
|
continue;
|
|
}
|
|
var now = DateTime.Now;
|
|
time = now.AddDays(-i).ToString("yyyMM");
|
|
var a = bll.IsExistsTable(date_base, "electricity_data_" + time);
|
|
if (a == false)
|
|
{
|
|
bll.CreateTable(time);
|
|
}
|
|
var jtime = now.AddDays(-i).ToString("MM-dd");
|
|
var ztime = "";
|
|
if (i == 0)
|
|
{
|
|
//这天
|
|
ztime = now.AddHours(-1).AddDays(-i).ToString("yyyy-MM-dd HH:00:00");
|
|
}
|
|
else
|
|
{
|
|
//这天
|
|
ztime = now.AddDays(-i+1).ToString("yyyy-MM-dd 00:00:00");
|
|
}
|
|
//上天
|
|
var stime = now.AddDays(-(i)).ToString("yyyy-MM-dd 00:00:00");
|
|
var atime = now.AddDays(-(i)).ToString("yyyyMM");
|
|
var b = bll.IsExistsTable(date_base, "electricity_data_" + atime);
|
|
if (b == false)
|
|
{
|
|
bll.CreateTable(time);
|
|
}
|
|
//这天列表
|
|
decimal? num1 = 0;
|
|
var list1 = bll.GetModelListDate(" EntireTime='"+ ztime + "'", time);
|
|
foreach (var aitem in list1)
|
|
{
|
|
num1 +=Convert.ToDecimal(aitem.EH);
|
|
}
|
|
//上个小时
|
|
decimal? num2 = 0;
|
|
|
|
var list2 = bll.GetModelListDate(" EntireTime='"+ stime + "'", atime);
|
|
foreach (var aitem in list2)
|
|
{
|
|
num2 += Convert.ToDecimal(aitem.EH);
|
|
}
|
|
var model = new carbon_measureData();
|
|
model.time = jtime;
|
|
model.CarbonValue =Convert.ToDecimal(Math.Round(Convert.ToDouble(((num1 - num2) / 1000 * Convert.ToDecimal(0.5703)) / 100000),3));
|
|
if (model.CarbonValue < 0)
|
|
{
|
|
model.CarbonValue = 0;
|
|
}
|
|
data.Add(model);
|
|
|
|
}
|
|
}
|
|
else if (date == "年")
|
|
{
|
|
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);
|
|
foreach (var aitem in list1)
|
|
{
|
|
num1 += Convert.ToDecimal(aitem.EH);
|
|
}
|
|
//上个小时
|
|
decimal? num2 = 0;
|
|
|
|
var list2 = bll.GetModelListDate(" EntireTime='" + stime + "'", atime);
|
|
foreach (var aitem in list2)
|
|
{
|
|
num2 += Convert.ToDecimal(aitem.EH);
|
|
}
|
|
var model = new carbon_measureData();
|
|
model.time = jtime;
|
|
model.CarbonValue = Convert.ToDecimal(Math.Round(Convert.ToDouble(((num1 - num2) / 1000 * Convert.ToDecimal(0.5703)) / 100000), 3));
|
|
if (model.CarbonValue < 0)
|
|
{
|
|
model.CarbonValue = 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;
|
|
}
|
|
}
|
|
} |