diff --git a/langguanApi.xml b/langguanApi.xml index 34a1e25..66a2272 100644 --- a/langguanApi.xml +++ b/langguanApi.xml @@ -2878,6 +2878,13 @@ + + + 聚合查询 + + + + //获取最新的数据 diff --git a/langguanApi/Service/BaseService.cs b/langguanApi/Service/BaseService.cs index 3163ffc..39a68bb 100644 --- a/langguanApi/Service/BaseService.cs +++ b/langguanApi/Service/BaseService.cs @@ -252,5 +252,14 @@ namespace langguanApi.Service .Skip(req.pageSize * (req.current - 1)).Take(req.pageSize).ToList(); return new Tuple>(total, items); } + /// + /// 聚合查询 + /// + /// + /// + public async Task> GetGroupedResultsAsync(BsonDocument[] pipeline) + { + return await _collection.Aggregate(pipeline).ToListAsync(); + } } } diff --git a/langguanApi/Service/Hj212Service.cs b/langguanApi/Service/Hj212Service.cs index e47a4bb..a1d56ae 100644 --- a/langguanApi/Service/Hj212Service.cs +++ b/langguanApi/Service/Hj212Service.cs @@ -25,23 +25,62 @@ namespace langguanApi.Service /// 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 + var now = DateTime.UtcNow; // 获取当前时间(UTC) + var sevenDaysAgo = now.AddDays(-2); // 计算 7 天前的时间 + var pipeline = new[] + { + new BsonDocument("$match", new BsonDocument { - 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(); + { "CreateDateTime", new BsonDocument { { "$gte", sevenDaysAgo }, { "$lt", now } } } + }), + new BsonDocument("$project", new BsonDocument + { + { "date", new BsonDocument("$dateToString", new BsonDocument { { "format", "%m-%d" }, { "date", "$CreateDateTime" } }) }, + { "a34001", 1 }, + { "a34002", 1 }, + { "a34004", 1 } + }), + new BsonDocument("$group", new BsonDocument + { + { "_id", "$date" }, + { "a34001", new BsonDocument("$sum", "$a34001") }, + { "a34002", new BsonDocument("$sum", "$a34002") }, + { "a34004", new BsonDocument("$sum", "$a34004") } + }), + new BsonDocument("$sort", new BsonDocument { { "_id", 1 } }) // 按日期排序 + }; + + var temp = await base.GetGroupedResultsAsync(pipeline); + List result = new List(); + foreach (var item in temp) + { + result.Add(new + { + date = item["_id"].ToString(), + a34001 = Math.Round(item["a34001"].ToDouble(), 2), + a34002 = Math.Round(item["a34002"].ToDouble(), 2), + a34004 = Math.Round(item["a34004"].ToDouble(), 2) + }); + } + return result; + //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 => @@ -72,7 +111,7 @@ namespace langguanApi.Service Builders.Filter.Eq(x => x.ST, 27), Builders.Filter.Eq(x => x.CN, 2031) ) - ); + ); var finalFilter = Builders.Filter.And(dateFilter, conditionFilter); var result = await base.FindListyFilter(finalFilter); // 过滤掉voc设备 @@ -110,7 +149,7 @@ namespace langguanApi.Service Builders.Filter.Eq(x => x.ST, 27), Builders.Filter.Eq(x => x.CN, 2031) ) - ); + ); // 组合过滤器(先按时间,再按其他条件) var finalFilter = Builders.Filter.And(dateFilter, conditionFilter); var result = await base.FindListyFilter(finalFilter);