|
@@ -30,7 +30,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
import org.springframework.data.mongodb.core.aggregation.Aggregation;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.AggregationOptions;
|
|
|
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -39,7 +41,6 @@ import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
-import java.time.Instant;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.ZoneId;
|
|
@@ -219,20 +220,26 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
|
|
|
.map(DeviceInformation::getId)
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
- // 构建 Aggregation 管道
|
|
|
- Aggregation aggregation = Aggregation.newAggregation(
|
|
|
+ // 构建 AggregationOptions
|
|
|
+ AggregationOptions aggregationOptions = AggregationOptions.builder()
|
|
|
+ .allowDiskUse(true) // 启用外部排序
|
|
|
+ .build();
|
|
|
+
|
|
|
+ // 创建 TypedAggregation 并附加选项
|
|
|
+ TypedAggregation<DeviceStatiscsModelMongodb> typedAggregation = Aggregation.newAggregation(
|
|
|
+ DeviceStatiscsModelMongodb.class, // 指定聚合操作针对的类型
|
|
|
Aggregation.match(Criteria.where("deviceInformationId").in(deviceIds)), // 过滤条件
|
|
|
Aggregation.sort(Sort.by(Sort.Direction.DESC, "datestr")), // 按 datestr 降序排列
|
|
|
- Aggregation.group("deviceInformationId") // 按 deviceInformationId 分组
|
|
|
- .first("deviceInformationId").as("deviceInformationId") // 获取分组后第一条记录的字段
|
|
|
- .first("power").as("power") // 获取分组后第一条记录的 power
|
|
|
- .first("datestr").as("datestr"), // 获取分组后第一条记录的 datestr
|
|
|
- Aggregation.project("deviceInformationId", "power", "datestr") // 只返回需要的字段
|
|
|
- );
|
|
|
-
|
|
|
- // 执行 Aggregation 查询
|
|
|
+ Aggregation.group("deviceInformationId")
|
|
|
+ .first("deviceInformationId").as("deviceInformationId")
|
|
|
+ .first("power").as("power")
|
|
|
+ .first("datestr").as("datestr"),
|
|
|
+ Aggregation.project("deviceInformationId", "power", "datestr")
|
|
|
+ ).withOptions(aggregationOptions); // 将选项绑定到聚合管道
|
|
|
+
|
|
|
+ // 执行聚合查询
|
|
|
AggregationResults<DeviceStatiscsModelMongodb> aggregationResults = mongoTemplate.aggregate(
|
|
|
- aggregation,
|
|
|
+ typedAggregation,
|
|
|
"leanmodel_run_realtime", // 集合名称
|
|
|
DeviceStatiscsModelMongodb.class // 返回的对象类型
|
|
|
);
|