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 JinanCementFactoryAPI.Controllers.api { /// /// 全厂用水量 /// public class GetPlantWaterController : ApiController { DataServer.BLL.water_meter bll = new DataServer.BLL.water_meter(); // GET api/ public HttpResponseMessage Get(string date = "") { var res = new get_plant_water(); try { var list = bll.GetModelList(""); var alist = new List(); var blist = new List(); if (date == "日") { var amodel = new plant_waterData(); decimal? num1 = 0; foreach (var item in list) { num1 += item.WaterConsumption; } amodel.Value = num1; for (int i = 0; i < 6; i++) { var model = new plant_water(); var time = DateTime.Now.AddDays(-i).ToString("dd号"); model.time = time; decimal? num = 0; foreach (var item in list) { num += item.WaterConsumption; } model.WaterConsumption = num; blist.Add(model); } amodel.data = blist; alist.Add(amodel); } if (date == "月") { var amodel = new plant_waterData(); decimal? num1 = 0; foreach (var item in list) { num1 += item.WaterConsumption; } amodel.Value = num1; for (int i = 0; i < 6; i++) { var model = new plant_water(); var time = DateTime.Now.AddMonths(-i).ToString("MM月"); model.time = time; decimal? num = 0; foreach (var item in list) { num += item.WaterConsumption; } model.WaterConsumption = num; blist.Add(model); } amodel.data = blist; alist.Add(amodel); } if (date == "年") { var amodel = new plant_waterData(); decimal? num1 = 0; foreach (var item in list) { num1 += item.WaterConsumption; } amodel.Value = num1; for (int i = 0; i < 6; i++) { var model = new plant_water(); var time = DateTime.Now.AddYears(-i).ToString("yyyy"); model.time = time; decimal? num = 0; foreach (var item in list) { num += item.WaterConsumption; } model.WaterConsumption = num; blist.Add(model); } amodel.data = blist; alist.Add(amodel); } res.code = 200; res.msg = "成功"; res.data = alist; } 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; } } }