lg_backend/langguanApi/Service/Hj212Service.cs

216 lines
9.2 KiB
C#
Raw Normal View History

2024-05-20 14:56:49 +00:00
using langguanApi.Extensions.AutoDI;
using langguanApi.Model;
using langguanApi.Model.Dto;
2024-06-15 19:17:41 +00:00
using MongoDB.Bson;
using MongoDB.Driver;
using NPOI.HSSF.Record;
using System.Diagnostics;
2024-05-20 14:56:49 +00:00
using System.Linq.Expressions;
2024-06-15 19:17:41 +00:00
using System.Text.RegularExpressions;
2024-05-20 14:56:49 +00:00
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="day"></param>
/// <returns></returns>
public async Task<object> GetIndexData(int day = 7)
{
Expression<Func<Model.HJ212, bool>> exp = filter => filter.CreateDateTime >= DateTime.Now.AddDays(-day);
var result = (await base.GetListWithExp(exp)).ToList();
var temp = result.Select(s => new
{
s.a34001,
s.a34002,
s.a34004,
date = s.CreateDateTime.AddHours(8).ToString("MM-dd")
}).ToList();
return temp.GroupBy(g => new { g.date })
.Select(s => new
{
s.Key.date,
2024-06-15 19:17:41 +00:00
a34001 = Math.Round(s.Sum(t => t.a34001), 2),
a34002 = Math.Round(s.Sum(t => t.a34002), 2),
a34004 = Math.Round(s.Sum(t => t.a34004), 2)
}).ToList();
//temp.GroupBy(g => new { g.date }).ToList().ForEach(s =>
//{
// var v1 = temp.Where(m => m.date == s.Key.date).Sum(t => t.a34001);
// var v2 = temp.Where(m => m.date == s.Key.date).Sum(t => t.a34002);
// var v3 = temp.Where(m => m.date == s.Key.date).Sum(t => t.a34004);
// list.Add(new columnView() { hour = s.Key.date, type = "a34001", value = v1 });
// list.Add(new columnView() { hour = s.Key.date, type = "a34002", value = Math.Round(v2, 2) });
// list.Add(new columnView() { hour = s.Key.date, type = "a34004", value = Math.Round(v3, 2) });
//});
}
2024-06-15 19:17:41 +00:00
/// <summary>
/// 今日排放数据
/// </summary>
/// <returns></returns>
public async Task<object> GetTodayData()
{
Expression<Func<Model.Device, bool>> exp = filter => filter.DeviceType == 1 || filter.DeviceType == 2;
var devices = (await _deviceSerive.GetListWithExp(exp)).ToList();
var tody = Convert.ToDateTime(DateTime.Now.ToString("D").ToString());
Expression<Func<Model.HJ212, bool>> expall = filter => devices.Select(s => s.deviceMN).Contains(filter.deviceMN) && filter.CreateDateTime >= tody;
var result = (await base.GetListWithExp(expall)).ToList();
var vocList = devices.Where(s => s.DeviceType == 1).Select(s => s.deviceMN).ToList();
var cemsList = devices.Where(s => s.DeviceType == 2).Select(s => s.deviceMN).ToList();
var voc = Math.Round(result.Where(s => vocList.Contains(s.deviceMN)).ToList().Select(s => s.a34004).Sum(), 2);
var cems = Math.Round(result.Where(s => vocList.Contains(s.deviceMN)).ToList().Select(s => s.a34002).Sum(), 2);
return new
{
today = new { voc, cems },
2024-08-19 15:53:36 +00:00
week = await GetWeekData()
2024-06-15 19:17:41 +00:00
};
}
/// <summary>
/// 一周之内的排放数据
/// </summary>
/// <returns></returns>
2024-08-19 15:53:36 +00:00
public async Task<object> GetWeekData()
2024-06-15 19:17:41 +00:00
{
2024-08-19 12:57:21 +00:00
// Expression<Func<Model.HJ212, bool>> exp = filter => filter.CreateDateTime >= DateTime.Now.AddDays(-7);
// CN = 2031是日数据
2024-08-19 15:53:36 +00:00
Expression<Func<Model.HJ212, bool>> exp = filter => filter.ST == 31 || filter.ST == 27 && filter.CN == 2031 && filter.CreateDateTime >= DateTime.Now.AddDays(-7);
2024-08-19 12:57:21 +00:00
////a21026_Cou,a21002_Cou,a34013_Cou
2024-08-19 15:53:36 +00:00
var result = (await base.GetListWithExp(exp)).ToList();
//cems
var cems = result.Where(s => s.ST == 31).Select(s => new
2024-06-15 19:17:41 +00:00
{
2024-08-19 15:53:36 +00:00
s.a21002_Cou,
s.a21026_Cou,
s.a34013_Cou,
s.a00000_Cou,
2024-06-15 19:17:41 +00:00
date = s.CreateDateTime.ToString("MM-dd")
2024-08-19 15:53:36 +00:00
}).GroupBy(g => g.date).Select(s => new {
s.Key,
value = Math.Round(s.Sum(t => t.a21002_Cou + t.a21026_Cou + t.a34013_Cou + t.a00000_Cou))
2024-06-15 19:17:41 +00:00
});
2024-08-19 15:53:36 +00:00
//voc
var voc = result.Where(s => s.ST == 27).Select(s => new
2024-06-15 19:17:41 +00:00
{
2024-08-19 15:53:36 +00:00
s.a05002_Cou,
s.a24087_Cou,
s.a24088_Cou,
s.a25002_Cou,
s.a25005_Cou,
s.a00000_Cou,
2024-06-15 19:17:41 +00:00
date = s.CreateDateTime.ToString("MM-dd")
2024-08-19 15:53:36 +00:00
}).GroupBy(g => g.date).Select(s => new {
2024-06-15 19:17:41 +00:00
s.Key,
2024-08-19 15:53:36 +00:00
value = Math.Round(s.Sum(t => t.a05002_Cou + t.a24087_Cou + t.a24088_Cou + t.a25002_Cou+t.a25005_Cou+t.a00000_Cou))
2024-06-15 19:17:41 +00:00
});
return new { voc, cems };
}
/// <summary>
2024-05-20 14:56:49 +00:00
/// 新加数据
/// </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
2024-06-10 09:55:56 +00:00
////先判断当前设备是否存在
//await _deviceSerive.Add(new DeviceAddDto()
//{
// deviceMN = hJ212.deviceMN,
// Ip = deviceIp,
// lat = hJ212.lat,
// lng = hJ212.lng,
// DeviceType = deviceType
//});
2024-05-20 14:56:49 +00:00
await base.CreateAsync(hJ212);
}
/// <summary>
/// 最近10个小时的数据
/// </summary>
/// <param name="hours"></param>
/// <returns></returns>
2024-06-12 16:47:19 +00:00
public async Task<List<Model.HJ212>> GetViewTop(int hours = -5)
2024-05-20 14:56:49 +00:00
{
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)
2024-05-20 14:56:49 +00:00
{
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;
}
}
}