using DataServer.api.EnergyEfficiency; 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.EnergyEfficiency { /// /// 获取电费电价接口 /// public class GetElectricityPriceController : ApiController { DataServer.BLL.electricity_price bll = new DataServer.BLL.electricity_price(); /// /// 获取电费电价接口 /// /// public HttpResponseMessage Get() { var res = new get_electricity_price_response(); try { res.code = 200; res.msg = "成功"; var now = DateTime.Now; var month = now.Month;//当前月份 var start_month = now.AddMonths(-5); var list = bll.GetModelList(""); var data = new List(); for (int i = 0; i < 5; i++) { var model = new electricity_price(); var new_month = start_month.AddMonths(i); model.Month = new_month.Month.ToString() + "月"; var price_model = list.Where(a => a.Month == new_month.Month).FirstOrDefault(); if (price_model != null) { model.UnitPrice = price_model.UnitPrice.Value; model.Cost = price_model.Cost.Value; } data.Add(model); } res.data = data; } 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; } } }