lg_backend/langguanApi/Service/Hj212Service.cs

114 lines
4.5 KiB
C#

using langguanApi.Extensions.AutoDI;
using langguanApi.Model;
using langguanApi.Model.Dto;
using System.Linq.Expressions;
namespace langguanApi.Service
{
[ServiceInjection(InjectionType.Transient)]
public class Hj212Service : BaseService<Model.HJ212>
{
private DeviceService _deviceSerive;
public Hj212Service(IConfiguration config, DeviceService deviceSerive) : base(config, nameof(Model.HJ212))
{
_deviceSerive = deviceSerive;
}
/// <summary>
/// 新加数据
/// </summary>
/// <param name="hJ212"></param>
/// <param name="deviceIp"></param>
/// <returns></returns>
public async Task Add(Model.HJ212 hJ212, string deviceIp)
{
////判断设备类型 tsp 会有经纬度数据
// int deviceType = 1;//设备类型为1 =voc
////先判断当前设备是否存在
//await _deviceSerive.Add(new DeviceAddDto()
//{
// deviceMN = hJ212.deviceMN,
// Ip = deviceIp,
// lat = hJ212.lat,
// lng = hJ212.lng,
// DeviceType = deviceType
//});
await base.CreateAsync(hJ212);
}
/// <summary>
/// 最近10个小时的数据
/// </summary>
/// <param name="hours"></param>
/// <returns></returns>
public async Task<List<Model.HJ212>> GetViewTop(int hours = -5)
{
var date = DateTime.Now.AddHours(-8).AddHours(hours);
Expression<Func<Model.HJ212, bool>> exp = filter => filter.CreateDateTime >= date;
var result = (await base.GetListWithExp(exp)).ToList();
return result;
}
/// <summary>
/// 按设备号查询数据
/// </summary>
/// <param name="deviceMn"></param>
/// <returns></returns>
public async Task<object> GetViewByDeviceMn(string deviceMn)
{
Expression<Func<Model.HJ212, bool>> exp = filter => filter.deviceMN == deviceMn;
var result = (await base.GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime).Take(60).ToList();
List<columnView> list = new List<columnView>();
var temp = result.Select(s => new
{
s.a34001,
s.a34002,
s.a34004,
hour = s.CreateDateTime.AddHours(8).ToString("yyyy-MM-dd HH:mm")
}).ToList();
temp.GroupBy(g => new { g.hour }).ToList().ForEach(s =>
{
var v1 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34001);
var v2 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34002);
var v3 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34004);
list.Add(new columnView() { hour = s.Key.hour, type = "a34001", value = v1 });
list.Add(new columnView() { hour = s.Key.hour, type = "a34002", value = Math.Round(v2, 2) });
list.Add(new columnView() { hour = s.Key.hour, type = "a34004", value = Math.Round(v3, 2) });
});
return list;
}
/// <summary>
/// 实时的数据
/// </summary>
/// <returns></returns>
public async Task<List<columnView>> Realtime()
{
Expression<Func<Model.HJ212, bool>> exp = filter => true;
var result = (await base.GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime)
.Take(60).OrderBy(s => s.CreateDateTime).ToList();
List<columnView> list = new List<columnView>();
var temp = result.Select(s => new
{
s.a34001,
s.a34002,
s.a34004,
hour = s.CreateDateTime.AddHours(8).ToString("yyyy-MM-dd HH:mm")
}).ToList();
temp.GroupBy(g => new { g.hour }).ToList().ForEach(s =>
{
var v1 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34001);
var v2 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34002);
var v3 = temp.Where(m => m.hour == s.Key.hour).Sum(t => t.a34004);
list.Add(new columnView() { hour = s.Key.hour, type = "a34001", value = v1 });
list.Add(new columnView() { hour = s.Key.hour, type = "a34002", value = Math.Round(v2, 2) });
list.Add(new columnView() { hour = s.Key.hour, type = "a34004", value = Math.Round(v3, 2) });
});
return list;
}
}
}