344 lines
15 KiB
C#
344 lines
15 KiB
C#
|
|
using LangGuan.Command.Model.EntityModel;
|
|||
|
|
using Microsoft.Extensions.Configuration;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Linq.Expressions;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Npoi.Mapper;
|
|||
|
|
using System.IO;
|
|||
|
|
using LangGuan.Command.Model;
|
|||
|
|
using LangGuan.Command.Model.RequestModel;
|
|||
|
|
using MongoDB.Driver;
|
|||
|
|
using MongoDB.Bson;
|
|||
|
|
|
|||
|
|
namespace LangGuan.Services
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
///
|
|||
|
|
/// </summary>
|
|||
|
|
public class Hj212Service : BaseService<HJ212>
|
|||
|
|
{
|
|||
|
|
private DeviceSerive _deviceSerive;
|
|||
|
|
/// <summary>
|
|||
|
|
///
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="config"></param>
|
|||
|
|
/// <param name="deviceSerive"></param>
|
|||
|
|
public Hj212Service(IConfiguration config, DeviceSerive deviceSerive) : base(config, nameof(HJ212))
|
|||
|
|
{
|
|||
|
|
_deviceSerive = deviceSerive;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 新加数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="hJ212"></param>
|
|||
|
|
/// <param name="deviceIp"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task Add(HJ212 hJ212, string deviceIp)
|
|||
|
|
{
|
|||
|
|
//先判断当前设备是否存在
|
|||
|
|
await _deviceSerive.Add(new Device()
|
|||
|
|
{
|
|||
|
|
deviceId = hJ212.deviceId,
|
|||
|
|
deviceIp = deviceIp,
|
|||
|
|
state = 1
|
|||
|
|
});
|
|||
|
|
await base.CreateAsync(hJ212);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 最近10个小时的数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="hours"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<List<HJ212>> GetViewTop(int hours = -10)
|
|||
|
|
{
|
|||
|
|
var date = DateTime.Now.AddHours(-8).AddHours(hours);
|
|||
|
|
Expression<Func<HJ212, bool>> exp = filter => filter.CreateDateTime >= date;
|
|||
|
|
var result = (await base.GetListWithExp(exp)).ToList();
|
|||
|
|
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 实时的数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<List<columnView>> Realtime()
|
|||
|
|
{
|
|||
|
|
Expression<Func<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;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 自动报表
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="autoReport"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<byte[]> autoReport(AutoReport autoReport)
|
|||
|
|
{
|
|||
|
|
autoReport = convertDateTime(autoReport);
|
|||
|
|
Expression<Func<HJ212, bool>> exp = filter => (filter.CreateDateTime >= autoReport.begin &&
|
|||
|
|
filter.CreateDateTime <= autoReport.end) &&
|
|||
|
|
filter.a34001 >= autoReport.a34001 || filter.a34002 >= autoReport.a34002 || filter.a34004 >= autoReport.a34004;
|
|||
|
|
var result = (await base.GetListWithExp(exp)).OrderBy(s => s.CreateDateTime).ToList()
|
|||
|
|
.Select(s => new ExportModel
|
|||
|
|
{
|
|||
|
|
deviceId = s.deviceId,
|
|||
|
|
a01001 = s.a01001,
|
|||
|
|
a01002 = s.a01002,
|
|||
|
|
a01006 = s.a01006,
|
|||
|
|
a01007 = s.a01007,
|
|||
|
|
a01008 = s.a01008,
|
|||
|
|
a34001 = s.a34001,
|
|||
|
|
a34002 = s.a34002,
|
|||
|
|
a34004 = s.a34004,
|
|||
|
|
createtime = s.CreateDateTime.AddHours(8).ToString("yyyy-MM-dd HH:mm")
|
|||
|
|
}).ToList();
|
|||
|
|
var mapper = new Mapper();
|
|||
|
|
mapper.Map<ExportModel>("设备编号", s => s.deviceId)
|
|||
|
|
.Map<ExportModel>("温度", s => s.a01001)
|
|||
|
|
.Map<ExportModel>("湿度", s => s.a01002)
|
|||
|
|
.Map<ExportModel>("大气压", s => s.a01006)
|
|||
|
|
.Map<ExportModel>("风速", s => s.a01007)
|
|||
|
|
.Map<ExportModel>("风向", s => s.a01008)
|
|||
|
|
.Map<ExportModel>("TSP浓度", s => s.a34001)
|
|||
|
|
.Map<ExportModel>("PM2.5", s => s.a34002)
|
|||
|
|
.Map<ExportModel>("PM10", s => s.a34004)
|
|||
|
|
.Map<ExportModel>("创建时间", s => s.createtime);
|
|||
|
|
mapper.Put<ExportModel>(result, "sheet1");
|
|||
|
|
MemoryStream stream = new MemoryStream();
|
|||
|
|
mapper.Save(stream);
|
|||
|
|
return stream.ToArray();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
AutoReport convertDateTime(AutoReport autoReport)
|
|||
|
|
{
|
|||
|
|
DateTime AssemblDate = Convert.ToDateTime(DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + "01"); // 组装当前指定月份
|
|||
|
|
autoReport.begin = autoReport.begin.HasValue ? autoReport.begin.Value : AssemblDate;
|
|||
|
|
autoReport.end = autoReport.end.HasValue ? autoReport.end.Value.AddDays(1).AddHours(-8) : DateTime.Now.AddDays(1);
|
|||
|
|
return autoReport;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 计算结果
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="autoReport"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<List<string>> autoReportResult(AutoReport autoReport)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
List<string> result = new List<string>();
|
|||
|
|
string date = autoReport.begin.Value.ToString("yyyy年MM月dd") + "-" + autoReport.end.Value.ToString("yyyy年MM月dd");
|
|||
|
|
result.Add(date);
|
|||
|
|
var days = (int)(autoReport.end.Value.AddDays(1) - autoReport.begin).Value.TotalDays;
|
|||
|
|
result.Add(days.ToString());//查询总天数
|
|||
|
|
autoReport = convertDateTime(autoReport);
|
|||
|
|
Expression<Func<HJ212, bool>> exp = filter => (filter.CreateDateTime > autoReport.begin &&
|
|||
|
|
filter.CreateDateTime <= autoReport.end) &&
|
|||
|
|
(filter.a34001 >= autoReport.a34001 || filter.a34002 >= autoReport.a34002 || filter.a34004 >= autoReport.a34004);
|
|||
|
|
//var builder = Builders<HJ212>.Filter;
|
|||
|
|
//var filter = builder.Gte("CreateDateTime", autoReport.begin.Value) & builder.Lte("CreateDateTime", autoReport.end.Value);
|
|||
|
|
//var fil = await base.FindListyFilter(filter);
|
|||
|
|
//Expression<Func<HJ212, bool>> exp1 = filter => autoReport.begin.Value < filter.CreateDateTime && autoReport.end.Value > filter.CreateDateTime;
|
|||
|
|
|
|||
|
|
//var t1 = (await base.GetListWithExp(exp1)).ToList();
|
|||
|
|
//var o1 = t1.Where(s => s.a34001 >= autoReport.a34001 || s.a34002 >= autoReport.a34002 || s.a34004 >= autoReport.a34004).ToList();
|
|||
|
|
|
|||
|
|
|
|||
|
|
var otemp = (await base.GetListWithExp(exp)).OrderBy(s => s.CreateDateTime).ToList();
|
|||
|
|
var temp = otemp.Select(s => new ExportModel
|
|||
|
|
{
|
|||
|
|
deviceId = s.deviceId,
|
|||
|
|
a01001 = s.a01001,
|
|||
|
|
a01002 = s.a01002,
|
|||
|
|
a01006 = s.a01006,
|
|||
|
|
a01007 = s.a01007,
|
|||
|
|
a01008 = s.a01008,
|
|||
|
|
a34001 = s.a34001,
|
|||
|
|
a34002 = s.a34002,
|
|||
|
|
a34004 = s.a34004,
|
|||
|
|
createtime = s.CreateDateTime.AddHours(8).ToString("yyyy-MM-dd HH:mm")
|
|||
|
|
}).ToList();
|
|||
|
|
//天数
|
|||
|
|
//string date = autoReport.begin.Value.ToString("yyyy年MM月dd") + "-" + autoReport.end.Value.ToString("yyyy年MM月dd");
|
|||
|
|
//result.Add(date);
|
|||
|
|
//var days = (int)(autoReport.end - autoReport.begin).Value.TotalDays;
|
|||
|
|
//result.Add(days.ToString());//查询总天数
|
|||
|
|
var total = temp.Select(s => new { date = s.createtime.Substring(0, 10) }).Distinct().Count();
|
|||
|
|
result.Add(total.ToString());// 污染天数
|
|||
|
|
|
|||
|
|
//计算持续时间最长的一天
|
|||
|
|
|
|||
|
|
HJ212 current = null;
|
|||
|
|
HJ212 pre = otemp.First();
|
|||
|
|
TimeSpan longestDuration = TimeSpan.Zero;
|
|||
|
|
HJ212 longestDurationStart = pre;
|
|||
|
|
HJ212 longestDurationEnd = null;
|
|||
|
|
foreach (HJ212 t in otemp)
|
|||
|
|
{
|
|||
|
|
current = t;
|
|||
|
|
//如果间隔大于2分钟,就是新的数据上来了
|
|||
|
|
if ((current.CreateDateTime - pre.CreateDateTime) > TimeSpan.FromMinutes(2))
|
|||
|
|
{
|
|||
|
|
//如果持续时间大于上一条就取新的时间
|
|||
|
|
if (pre.CreateDateTime - longestDurationStart.CreateDateTime > longestDuration)
|
|||
|
|
{
|
|||
|
|
longestDurationStart = current;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
longestDurationEnd = current;
|
|||
|
|
longestDuration = current.CreateDateTime - longestDurationStart.CreateDateTime;
|
|||
|
|
}
|
|||
|
|
pre = current;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
result.Add(longestDurationStart.CreateDateTime.AddHours(8).ToString("yyyy年MM月dd"));
|
|||
|
|
result.Add(((int)longestDuration.TotalMinutes).ToString());
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
//计算最多次的天数
|
|||
|
|
var groupedResult = otemp.GroupBy(t => t.CreateDateTime.AddHours(8).ToString("yyyy年MM月dd"));
|
|||
|
|
Dictionary<string, int> dic = new Dictionary<string, int>();
|
|||
|
|
foreach (var group in groupedResult)
|
|||
|
|
{
|
|||
|
|
int count = 1;
|
|||
|
|
var previous = otemp.First();
|
|||
|
|
foreach (var curr in group.Skip(1))
|
|||
|
|
{
|
|||
|
|
TimeSpan duration = curr.CreateDateTime - previous.CreateDateTime;
|
|||
|
|
if (duration > TimeSpan.FromMinutes(1))
|
|||
|
|
{
|
|||
|
|
count++;
|
|||
|
|
}
|
|||
|
|
previous = current;
|
|||
|
|
}
|
|||
|
|
dic.Add(group.Key, count);
|
|||
|
|
}
|
|||
|
|
var sortdic = dic.OrderBy(s => s.Value);
|
|||
|
|
result.Add(sortdic.First().Key);
|
|||
|
|
result.Add(sortdic.First().Value.ToString());
|
|||
|
|
|
|||
|
|
var a34004 = temp.OrderByDescending(s => s.a34004).First();//pm 2.5
|
|||
|
|
result.Add(DateTime.Parse(a34004.createtime).ToString("yyyy年MM月dd"));
|
|||
|
|
result.Add(a34004.a34004.ToString());
|
|||
|
|
var a34002 = temp.OrderByDescending(s => s.a34002).First();//pm 10
|
|||
|
|
result.Add(DateTime.Parse(a34002.createtime).ToString("yyyy年MM月dd"));
|
|||
|
|
result.Add(a34002.a34002.ToString());
|
|||
|
|
result.Add("斗轮机上料过程中,粉尘溢散较大,时间较长 \r\n 料棚北门3号位置货车卸料频繁,粉尘溢散较大");
|
|||
|
|
result.Add("斗轮机工作时,增加移动雾炮机在上风口出喷淋 \r\n 料棚北门3号位置货车卸料时,增加移动雾炮机从上风口出喷淋");
|
|||
|
|
// var a34001 = temp.OrderByDescending(s => s.a34004).First();//tsp
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 分页取数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="start"></param>
|
|||
|
|
/// <param name="end"></param>
|
|||
|
|
/// <param name="request"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<ApiResult> GetPageData(DateTime start, DateTime end, RqeustPaging request)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
start = start.AddHours(-8);
|
|||
|
|
end = end.AddHours(-8).AddDays(1);
|
|||
|
|
Expression<Func<HJ212, bool>> exp = filter => filter.CreateDateTime >= start && filter.CreateDateTime < end;
|
|||
|
|
var query = await base.GetListWithExp(exp);
|
|||
|
|
var total = query.Count();
|
|||
|
|
var items = query.Skip(request.pageSize * (request.current - 1)).Take(request.pageSize).ToList()
|
|||
|
|
.Select(s => new ExportModel
|
|||
|
|
{
|
|||
|
|
deviceId = s.deviceId,
|
|||
|
|
a01001 = s.a01001,
|
|||
|
|
a01002 = s.a01002,
|
|||
|
|
a01006 = s.a01006,
|
|||
|
|
a01007 = s.a01007,
|
|||
|
|
a01008 = s.a01008,
|
|||
|
|
a34001 = s.a34001,
|
|||
|
|
a34002 = s.a34002,
|
|||
|
|
a34004 = s.a34004,
|
|||
|
|
createtime = s.CreateDateTime.AddHours(8).ToString("yyyy-MM-dd HH mm")
|
|||
|
|
}).ToList();
|
|||
|
|
return new ApiResult() { data = new { total = total, items = items } };
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 查找数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="start"></param>
|
|||
|
|
/// <param name="end"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
async Task<List<ExportModel>> GetPage(DateTime start, DateTime end)
|
|||
|
|
{
|
|||
|
|
start = start.AddHours(-8);
|
|||
|
|
end = end.AddHours(-8).AddDays(1);
|
|||
|
|
Expression<Func<HJ212, bool>> exp = filter => filter.CreateDateTime >= start && filter.CreateDateTime < end;
|
|||
|
|
var temp = await base.GetListWithExp(exp);
|
|||
|
|
return (await base.GetListWithExp(exp)).ToList().Select(s => new ExportModel
|
|||
|
|
{
|
|||
|
|
deviceId = s.deviceId,
|
|||
|
|
a01001 = s.a01001,
|
|||
|
|
a01002 = s.a01002,
|
|||
|
|
a01006 = s.a01006,
|
|||
|
|
a01007 = s.a01007,
|
|||
|
|
a01008 = s.a01008,
|
|||
|
|
a34001 = s.a34001,
|
|||
|
|
a34002 = s.a34002,
|
|||
|
|
a34004 = s.a34004,
|
|||
|
|
createtime = s.CreateDateTime.AddHours(8).ToString("yyyy-MM-dd HH mm")
|
|||
|
|
}).ToList();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 导出数据
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="start"></param>
|
|||
|
|
/// <param name="end"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<byte[]> Export(DateTime start, DateTime end)
|
|||
|
|
{
|
|||
|
|
var result = (await GetPage(start, end)).ToList();
|
|||
|
|
var mapper = new Mapper();
|
|||
|
|
mapper.Map<ExportModel>("设备编号", s => s.deviceId)
|
|||
|
|
.Map<ExportModel>("温度", s => s.a01001)
|
|||
|
|
.Map<ExportModel>("湿度", s => s.a01002)
|
|||
|
|
.Map<ExportModel>("大气压", s => s.a01006)
|
|||
|
|
.Map<ExportModel>("风速", s => s.a01007)
|
|||
|
|
.Map<ExportModel>("风向", s => s.a01008)
|
|||
|
|
.Map<ExportModel>("TSP浓度", s => s.a34001)
|
|||
|
|
.Map<ExportModel>("PM2.5", s => s.a34002)
|
|||
|
|
.Map<ExportModel>("PM10", s => s.a34004)
|
|||
|
|
.Map<ExportModel>("创建时间", s => s.createtime);
|
|||
|
|
mapper.Put<ExportModel>(result, "sheet1");
|
|||
|
|
MemoryStream stream = new MemoryStream();
|
|||
|
|
mapper.Save(stream);
|
|||
|
|
return stream.ToArray();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|