using DataServer.api; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Web.Http; namespace DongYingAPI.Controllers.api { /// ///用能监测1-用气量 /// public class GetGasConsumptionController : ApiController { public HttpResponseMessage Get(string date = "") { var res = new get_gas_consumption(); try { var data = new List(); if (date == "日") { for (int i = 0; i < 6; i++) { // 只处理今年的数据 if (DateTime.Now.AddDays(-i).Day != DateTime.Now.Day) { continue; } var now = DateTime.Now; var time = now.AddHours(-i).ToString("HH:00"); var model = new gas_consumptionData(); model.time = time; model.GasValue =Math.Round( Convert.ToDecimal(4400.00/12/31/24),3); if (model.GasValue == null) { model.GasValue = 0; } data.Add(model); } } if (date == "月") { for (int i = 0; i < 6; i++) { // 只处理今年的数据 if (DateTime.Now.AddMonths(-i).Month != DateTime.Now.Month) { continue; } var now = DateTime.Now; var time = now.AddDays(-i).ToString("MM-dd"); var model = new gas_consumptionData(); model.time = time; model.GasValue = 4400 / 12/31; if (model.GasValue == null) { model.GasValue = 0; } data.Add(model); } } if (date == "年") { for (int i = 0; i < 6; i++) { // 只处理今年的数据 if (DateTime.Now.AddYears(-i).Year != DateTime.Now.Year) { continue; } var now = DateTime.Now; var time = now.AddMonths(-i).ToString("MM月"); var model = new gas_consumptionData(); model.time = time; model.GasValue = 4400 / 12 ; if (model.GasValue == null) { model.GasValue = 0; } data.Add(model); } } var adata=data.OrderBy(x=>x.time).ToList(); res.code = 200; res.msg = "成功"; 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; } } }