|
@@ -29,6 +29,8 @@ import org.jeecg.modules.systemConfig.peaksAndValleysTimeConfig.service.IPeaksAn
|
|
|
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.AggregationResults;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -217,16 +219,34 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
|
|
|
.map(DeviceInformation::getId)
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
- Query query = new Query();
|
|
|
- query.addCriteria(Criteria.where("deviceInformationId").in(deviceIds));
|
|
|
+ // 构建 Aggregation 管道
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(
|
|
|
+ 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 查询
|
|
|
+ AggregationResults<DeviceStatiscsModelMongodb> aggregationResults = mongoTemplate.aggregate(
|
|
|
+ aggregation,
|
|
|
+ "leanmodel_run_realtime", // 集合名称
|
|
|
+ DeviceStatiscsModelMongodb.class // 返回的对象类型
|
|
|
+ );
|
|
|
|
|
|
- //只获取必要字段,减少查询返回的数据量,提升查询速度
|
|
|
- query.fields().include("deviceInformationId").include("power").include("datestr");
|
|
|
- List<DeviceStatiscsModelMongodb> allMongoData = mongoTemplate.find(query, DeviceStatiscsModelMongodb.class, "leanmodel_run_realtime");
|
|
|
+ // 获取结果列表
|
|
|
+ List<DeviceStatiscsModelMongodb> allMongoData = aggregationResults.getMappedResults();
|
|
|
+
|
|
|
+ // 按 deviceInformationId 分组
|
|
|
+ Map<String, DeviceStatiscsModelMongodb> mongoDataGrouped = allMongoData.stream()
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ DeviceStatiscsModelMongodb::getDeviceInformationId,
|
|
|
+ data -> data
|
|
|
+ ));
|
|
|
|
|
|
- // 按 deviceInformationId 分组
|
|
|
- Map<String, List<DeviceStatiscsModelMongodb>> mongoDataGrouped = allMongoData.stream()
|
|
|
- .collect(Collectors.groupingBy(DeviceStatiscsModelMongodb::getDeviceInformationId));
|
|
|
|
|
|
// 分组统计数据
|
|
|
Map<String, BigDecimal> powerAccumulation = new HashMap<>();
|
|
@@ -239,17 +259,11 @@ public class HomePageDataServiceImpl implements IHomePageDataService {
|
|
|
statusZeroCountMap.merge(deviceType, "0".equals(device.getStatus()) ? 1L : 0L, Long::sum);
|
|
|
|
|
|
// 获取最新 MongoDB 数据
|
|
|
- List<DeviceStatiscsModelMongodb> mongoData = mongoDataGrouped.getOrDefault(device.getId(), Collections.emptyList())
|
|
|
- .stream()
|
|
|
- .sorted(Comparator.comparing(DeviceStatiscsModelMongodb::getDatestr).reversed())
|
|
|
- .limit(1)
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- mongoData.forEach(data -> {
|
|
|
- if (data.getPower() != null) {
|
|
|
- powerAccumulation.merge(deviceType, data.getPower(), BigDecimal::add);
|
|
|
- }
|
|
|
- });
|
|
|
+ DeviceStatiscsModelMongodb mongoData = mongoDataGrouped.get(device.getId());
|
|
|
+
|
|
|
+ if (mongoData != null && mongoData.getPower() != null) {
|
|
|
+ powerAccumulation.merge(device.getDeviceType(), mongoData.getPower(), BigDecimal::add);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
// 计算实时总电压
|