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 { private DeviceService _deviceSerive; public Hj212Service(IConfiguration config, DeviceService deviceSerive) : base(config, nameof(Model.HJ212)) { _deviceSerive = deviceSerive; } /// /// 首页数据 /// /// /// public async Task GetIndexData(int day = 7) { Expression> 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) }); //}); } /// /// 今日排放数据 /// /// public async Task GetTodayData() { Expression> 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> 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) }; } /// /// 一周之内的排放数据 /// /// public async Task GetWeekData(List vocList, List cemsList) { // Expression> exp = filter => filter.CreateDateTime >= DateTime.Now.AddDays(-7); var filter = Builders.Filter.In(s => s.deviceMN, vocList.Concat(cemsList)) & Builders.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 }; } /// /// 新加数据 /// /// /// /// 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); } /// /// 最近10个小时的数据 /// /// /// public async Task> GetViewTop(int hours = -5) { 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 GetViewByDeviceMn(string deviceMn) { Expression> exp = filter => filter.deviceMN == deviceMn; var result = (await base.GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime).Take(60).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> 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; } } }