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 { /// /// /// public class Hj212Service : BaseService { private DeviceSerive _deviceSerive; /// /// /// /// /// public Hj212Service(IConfiguration config, DeviceSerive deviceSerive) : base(config, nameof(HJ212)) { _deviceSerive = deviceSerive; } /// /// 新加数据 /// /// /// /// public async Task Add(HJ212 hJ212, string deviceIp) { //先判断当前设备是否存在 await _deviceSerive.Add(new Device() { deviceId = hJ212.deviceId, deviceIp = deviceIp, state = 1 }); await base.CreateAsync(hJ212); } /// /// 最近10个小时的数据 /// /// /// public async Task> GetViewTop(int hours = -10) { var date = DateTime.Now.AddHours(-8).AddHours(hours); Expression> exp = filter => filter.CreateDateTime >= date; var result = (await base.GetListWithExp(exp)).ToList(); return result; } /// /// 实时的数据 /// /// public async Task> Realtime() { Expression> exp = filter => true; var result = (await base.GetListWithExp(exp)) .OrderByDescending(s => s.CreateDateTime) .Take(60).OrderBy(s => s.CreateDateTime).ToList(); List list = new List(); 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; } /// /// 自动报表 /// /// /// public async Task autoReport(AutoReport autoReport) { autoReport = convertDateTime(autoReport); Expression> 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("设备编号", s => s.deviceId) .Map("温度", s => s.a01001) .Map("湿度", s => s.a01002) .Map("大气压", s => s.a01006) .Map("风速", s => s.a01007) .Map("风向", s => s.a01008) .Map("TSP浓度", s => s.a34001) .Map("PM2.5", s => s.a34002) .Map("PM10", s => s.a34004) .Map("创建时间", s => s.createtime); mapper.Put(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; } /// /// 计算结果 /// /// /// public async Task> autoReportResult(AutoReport autoReport) { List result = new List(); 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> 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.Filter; //var filter = builder.Gte("CreateDateTime", autoReport.begin.Value) & builder.Lte("CreateDateTime", autoReport.end.Value); //var fil = await base.FindListyFilter(filter); //Expression> 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 dic = new Dictionary(); 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; } /// /// 分页取数据 /// /// /// /// /// public async Task GetPageData(DateTime start, DateTime end, RqeustPaging request) { start = start.AddHours(-8); end = end.AddHours(-8).AddDays(1); Expression> 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 } }; } /// /// 查找数据 /// /// /// /// async Task> GetPage(DateTime start, DateTime end) { start = start.AddHours(-8); end = end.AddHours(-8).AddDays(1); Expression> 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(); } /// /// 导出数据 /// /// /// /// public async Task Export(DateTime start, DateTime end) { var result = (await GetPage(start, end)).ToList(); var mapper = new Mapper(); mapper.Map("设备编号", s => s.deviceId) .Map("温度", s => s.a01001) .Map("湿度", s => s.a01002) .Map("大气压", s => s.a01006) .Map("风速", s => s.a01007) .Map("风向", s => s.a01008) .Map("TSP浓度", s => s.a34001) .Map("PM2.5", s => s.a34002) .Map("PM10", s => s.a34004) .Map("创建时间", s => s.createtime); mapper.Put(result, "sheet1"); MemoryStream stream = new MemoryStream(); mapper.Save(stream); return stream.ToArray(); } } }