数据查询方式调整
This commit is contained in:
parent
82906fd1af
commit
f97fe92dac
|
|
@ -2878,6 +2878,13 @@
|
|||
<param name="exp"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:langguanApi.Service.BaseService`1.GetGroupedResultsAsync(MongoDB.Bson.BsonDocument[])">
|
||||
<summary>
|
||||
聚合查询
|
||||
</summary>
|
||||
<param name="pipeline"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:langguanApi.Service.DetectionService.GetIndexData(System.Int32)">
|
||||
<summary>
|
||||
//获取最新的数据
|
||||
|
|
|
|||
|
|
@ -252,5 +252,14 @@ namespace langguanApi.Service
|
|||
.Skip(req.pageSize * (req.current - 1)).Take(req.pageSize).ToList();
|
||||
return new Tuple<long, List<T>>(total, items);
|
||||
}
|
||||
/// <summary>
|
||||
/// 聚合查询
|
||||
/// </summary>
|
||||
/// <param name="pipeline"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<List<BsonDocument>> GetGroupedResultsAsync(BsonDocument[] pipeline)
|
||||
{
|
||||
return await _collection.Aggregate<BsonDocument>(pipeline).ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,23 +25,62 @@ namespace langguanApi.Service
|
|||
/// <returns></returns>
|
||||
public async Task<object> GetIndexData(int day = 7)
|
||||
{
|
||||
Expression<Func<Model.HJ212, bool>> 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<object> result = new List<object>();
|
||||
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<Model.HJ212>.Filter.Eq(x => x.ST, 27),
|
||||
Builders<Model.HJ212>.Filter.Eq(x => x.CN, 2031)
|
||||
)
|
||||
);
|
||||
);
|
||||
var finalFilter = Builders<Model.HJ212>.Filter.And(dateFilter, conditionFilter);
|
||||
var result = await base.FindListyFilter(finalFilter);
|
||||
// 过滤掉voc设备
|
||||
|
|
@ -110,7 +149,7 @@ namespace langguanApi.Service
|
|||
Builders<Model.HJ212>.Filter.Eq(x => x.ST, 27),
|
||||
Builders<Model.HJ212>.Filter.Eq(x => x.CN, 2031)
|
||||
)
|
||||
);
|
||||
);
|
||||
// 组合过滤器(先按时间,再按其他条件)
|
||||
var finalFilter = Builders<Model.HJ212>.Filter.And(dateFilter, conditionFilter);
|
||||
var result = await base.FindListyFilter(finalFilter);
|
||||
|
|
|
|||
Loading…
Reference in New Issue