diff --git a/langguanApi.xml b/langguanApi.xml index ccbb527..25bb0c4 100644 --- a/langguanApi.xml +++ b/langguanApi.xml @@ -39,6 +39,11 @@ 首页趋势数据 + + + 缓存今日数据 + + 缓存清洁数据 @@ -1753,7 +1758,7 @@ - filterdefinition + filter查找 流式数据 @@ -1873,6 +1878,18 @@ + + + 今日排放数据 + + + + + + 一周之内的排放数据 + + + 新加数据 diff --git a/langguanApi/Common/Redis/RedisKeylist.cs b/langguanApi/Common/Redis/RedisKeylist.cs index 6093aa6..8fbf7a8 100644 --- a/langguanApi/Common/Redis/RedisKeylist.cs +++ b/langguanApi/Common/Redis/RedisKeylist.cs @@ -16,6 +16,10 @@ /// public static string Trend = "tred"; /// + /// 缓存今日数据 + /// + public static string Today = "today"; + /// /// 缓存清洁数据 /// public static string CleanData = "cleandata"; diff --git a/langguanApi/Service/BaseService.cs b/langguanApi/Service/BaseService.cs index bae2756..3163ffc 100644 --- a/langguanApi/Service/BaseService.cs +++ b/langguanApi/Service/BaseService.cs @@ -1,6 +1,8 @@ using langguanApi.Model; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.Extensions.Options; +using MongoDB.Bson; using MongoDB.Driver; using System.Linq.Expressions; @@ -184,16 +186,27 @@ namespace langguanApi.Service { FilterDefinition filters = Builders.Filter.Where(filter); return await _collection.Find(filters).ToListAsync(); - } + /// - /// filterdefinition + /// filter查找 流式数据 /// /// /// public async Task> FindListyFilter(FilterDefinition filter) { - return await _collection.Find(filter).ToListAsync(); + List result = new List(); + var cursor = await _collection.FindAsync(filter); + while (await cursor.MoveNextAsync()) + { + var batch = cursor.Current; + foreach (var document in batch) + { + result.Add(document); + // Console.WriteLine(document.ToString()); + } + } + return result; } /// /// 是否存在 diff --git a/langguanApi/Service/Hj212Service.cs b/langguanApi/Service/Hj212Service.cs index 08b5b76..3cd4b5b 100644 --- a/langguanApi/Service/Hj212Service.cs +++ b/langguanApi/Service/Hj212Service.cs @@ -1,8 +1,12 @@ 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 { @@ -21,8 +25,6 @@ namespace langguanApi.Service /// public async Task GetIndexData(int day = 7) { - Stopwatch sw = new Stopwatch(); - sw.Start(); Expression> exp = filter => filter.CreateDateTime >= DateTime.Now.AddDays(-day); var result = (await base.GetListWithExp(exp)).ToList(); var temp = result.Select(s => new @@ -32,15 +34,13 @@ namespace langguanApi.Service s.a34004, date = s.CreateDateTime.AddHours(8).ToString("MM-dd") }).ToList(); - sw.Stop(); - Console.WriteLine($"GetIndexData:{sw.ElapsedMilliseconds}ms"); return temp.GroupBy(g => new { g.date }) .Select(s => new { s.Key.date, - a34001 = s.Sum(t => t.a34001), - a34002 = s.Sum(t => t.a34002), - a34004 = s.Sum(t => t.a34004) + 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(); @@ -54,6 +54,61 @@ namespace langguanApi.Service // 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(-7)); + 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 }; + } /// /// 新加数据 /// diff --git a/langguanApi/Service/HomeService.cs b/langguanApi/Service/HomeService.cs index 82ba3ab..5f57bf1 100644 --- a/langguanApi/Service/HomeService.cs +++ b/langguanApi/Service/HomeService.cs @@ -49,7 +49,6 @@ namespace langguanApi.Service Yesterday = 0.8, LastWeek = 0.6, }; - Expression> filter = exp => true; // 首页设备报警数据 var alerts = await _alertService.IndexData(); @@ -69,15 +68,9 @@ namespace langguanApi.Service yesterday = 0.8, lastWeek = 0.6 }; - var day = new - { - day = new { voc = "70mg/m3", cemes = "100mg/m3" }, - week = new - { - - } - }; - + var d1 = await _hj212Service.GetTodayData(); + Func> getTodayFunc = async () => await _hj212Service.GetTodayData(); + var today = await _cacheManager.GetConvertVale(RedisKeylist.Today, getTodayFunc, 60 * 30); return new ApiResult { code = 0, @@ -92,14 +85,18 @@ namespace langguanApi.Service }, title = _configuration.GetValue("Home:Title"), }, - weather, + devices, - cleanData, + weather, ariQuality, - rate = new { }, + rate = new[] { + new { key = "cems", val = 0.7 } , + new { key = "VOC", val = 0.6 } + }, trend, - // trend = await _hj212Service.GetIndexData(), alerts, + cleanData, + today, getViewTop = "", detections }