222 lines
9.3 KiB
C#
222 lines
9.3 KiB
C#
using langguanApi.Extensions.AutoDI;
|
|
using langguanApi.Model;
|
|
using langguanApi.Model.Dto;
|
|
using MongoDB.Bson;
|
|
using MongoDB.Driver;
|
|
using NPOI.HSSF.Record;
|
|
using System.Diagnostics;
|
|
using System.Linq.Expressions;
|
|
using System.Text.RegularExpressions;
|
|
|
|
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,
|
|
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) });
|
|
//});
|
|
}
|
|
|
|
/// <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 },
|
|
week = await GetWeekData(vocList, cemsList)
|
|
};
|
|
}
|
|
/// <summary>
|
|
/// 一周之内的排放数据
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public async Task<object> GetWeekData(List<string> vocList, List<string> cemsList)
|
|
{
|
|
// Expression<Func<Model.HJ212, bool>> exp = filter => filter.CreateDateTime >= DateTime.Now.AddDays(-7);
|
|
// CN = 2031是日数据
|
|
//Expression<Func<Model.HJ212, bool>> exp = filter => filter.CN == 2031 && filter.CreateDateTime >= DateTime.Now.AddDays(-7);
|
|
////a21026_Cou,a21002_Cou,a34013_Cou
|
|
//var result = (await base.GetListWithExp(exp)).ToList().Select(s => new
|
|
//{
|
|
// //
|
|
//});
|
|
|
|
|
|
|
|
|
|
|
|
var filter = Builders<Model.HJ212>.Filter.In(s => s.deviceMN, vocList.Concat(cemsList))
|
|
& Builders<Model.HJ212>.Filter.Gte(s => s.CreateDateTime, DateTime.Now.AddDays(-3));
|
|
var result = await base.FindListyFilter(filter);
|
|
var voc = result.Where(s => vocList.Contains(s.deviceMN)).Select(s => new
|
|
{
|
|
s.a34001,
|
|
date = s.CreateDateTime.ToString("MM-dd")
|
|
}).GroupBy(s => s.date)
|
|
.Select(s => new
|
|
{
|
|
s.Key,
|
|
value = Math.Round(s.Sum(t => t.a34001))
|
|
});
|
|
|
|
var cems = result.Where(s => cemsList.Contains(s.deviceMN)).Select(s => new
|
|
{
|
|
s.a34002,
|
|
date = s.CreateDateTime.ToString("MM-dd")
|
|
}).GroupBy(s => s.date)
|
|
.Select(s => new
|
|
{
|
|
s.Key,
|
|
value = Math.Round(s.Sum(t => t.a34002))
|
|
});
|
|
return new { voc, cems };
|
|
}
|
|
/// <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;
|
|
}
|
|
}
|
|
}
|