DongYing/DongYingAPI/Controllers/api/GetDeviceDetailsController.cs

69 lines
2.6 KiB
C#

using DataServer.api;
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.api
{
/// <summary>
/// 照明-设备详情
/// </summary>
public class GetDeviceDetailsController : 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()
{
var res = new get_device_details();
try
{
var data = new List<device_detailsData>();
var time = DateTime.Now.ToString("yyyyMM");
#region
//表名
var date_base = ConfigurationManager.AppSettings["MySQLDataBase"].ToString();
//今月的表是否存在
var jtime = DateTime.Now.ToString("yyyyMM");
var a1 = bll.IsExistsTable(date_base, "electricity_data_" + time);
if (a1 == false)
{
bll.CreateTable(jtime);
}
#endregion
var list = bll_info.GetModelList(" DeviceName like '%照明%'");
foreach (var item in list)
{
var model=new device_detailsData();
model.DeviceName = item.DeviceName;
model.DeviceState = 1;
var sdate = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
var edate = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd 00:00:00");
var alist = bll.GetModelListDate(" DeviceId='"+item.DeviceId+ "' and EntireTime>='"+sdate+ "' and EntireTime<'"+ edate + "'", time);
decimal? num =alist.Sum(x=>Convert.ToDecimal(x.EH));
model.P = num;
model.Illuminance = "30%";
data.Add(model);
}
res.code = 200;
res.msg = "成功";
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;
}
}
}