DongYing/DongYingAPI/Controllers/api/GetGeneralCatalogueControll...

88 lines
3.7 KiB
C#

using DataServer.api;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text;
using System.Web.Http;
namespace DongYingAPI.Controllers.api
{
/// <summary>
/// 碳-总览
/// </summary>
public class GetGeneralCatalogueController : ApiController
{
DataServer.BLL.device_data bll = new DataServer.BLL.device_data();
// GET api/<controller>
public HttpResponseMessage Get(string date="")
{
var res = new get_general_catalogue();
try
{
if (!string.IsNullOrEmpty(date))
{
var data = new List<general_catalogueData>();
var list = bll.GetModelList("");
if (date == "年")
{
var model=new general_catalogueData();
var now=DateTime.Now;
var sdate = now.ToString("yyyy-01-01 00:00:00");
var edate=now.AddYears(1).ToString("yyyy-MM-dd 00:00:00");
var alist=list.Where(x=>x.CreateTime>=Convert.ToDateTime(sdate)&&x.CreateTime<Convert.ToDateTime(edate)).ToList();
decimal? num = 0;
foreach (var item in alist)
{
num += item.EH;
}
model.TotalRelease=Math.Round(Convert.ToDecimal(num / 1000 * Convert.ToDecimal(0.5703)),3);
model.CarbonOffset = 0;
model.CarbonReduction = 1;
model.Electricity =Math.Round(Convert.ToDecimal(num / 1000 * Convert.ToDecimal(0.5703)),3);
model.NaturalGas = 0;
data.Add(model);
}
else if (date == "月")
{
var model = new general_catalogueData();
var now = DateTime.Now;
var sdate = now.ToString("yyyy-01-01 00:00:00");
var edate = now.AddYears(1).ToString("yyyy-MM-dd 00:00:00");
var alist = list.Where(x => x.CreateTime >= Convert.ToDateTime(sdate) && x.CreateTime < Convert.ToDateTime(edate)).ToList();
decimal? num = 0;
foreach (var item in alist)
{
num += item.EH;
}
model.TotalRelease = Math.Round(Convert.ToDecimal(num / 1000 * Convert.ToDecimal(0.5703)), 3);
model.CarbonOffset = 0;
model.CarbonReduction = 333;
model.Electricity = Math.Round(Convert.ToDecimal(num / 1000 * Convert.ToDecimal(0.5703)), 3);
model.NaturalGas = 0;
data.Add(model);
}
res.code = 200;
res.msg = "成功";
res.data = data;
}
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;
}
}
}