|
@@ -65,21 +65,32 @@ public class MongoMeterReadRecordServiceImpl implements MongoMeterReadRecordServ
|
|
|
ConditionalOperators.Cond readTimesCond = ConditionalOperators.when(new Criteria("readStatus").is(2)).then(1).otherwise(0);
|
|
|
|
|
|
operations.add(Aggregation.project("lastCost").
|
|
|
- and("readDate").divide(100).substring(0,6).as("month")
|
|
|
+ and("readDate").divide(100).as("readDateTemp")
|
|
|
);
|
|
|
-
|
|
|
+ operations.add(Aggregation.project("lastCost").
|
|
|
+ and("readDateTemp").substring(0,6).as("month")
|
|
|
+ );
|
|
|
operations.add(Aggregation.group("month")
|
|
|
.sum("lastCost").as("lastCost")
|
|
|
);
|
|
|
operations.add(Aggregation.project().
|
|
|
- and("month").as("date").
|
|
|
- and("lastCost").as("useVolume"));
|
|
|
+ and("_id").as("date").
|
|
|
+ and("lastCost").as("useVolume").andExclude("_id"));
|
|
|
// 3,聚合查询所有信息
|
|
|
Aggregation aggregation = Aggregation.newAggregation(operations).withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());
|
|
|
// 4,查询结果
|
|
|
AggregationResults<UseWaterDto> aggregationResults = mongoTemplate.aggregate(aggregation, "sc_meter_read_record", UseWaterDto.class);
|
|
|
// 5,获取结果
|
|
|
- List<UseWaterDto> results = aggregationResults.getMappedResults();
|
|
|
+ List<UseWaterDto> list = aggregationResults.getMappedResults();
|
|
|
+ List<UseWaterDto> results = new ArrayList<>();
|
|
|
+ if (list != null && list.size() > 0) {
|
|
|
+ for (UseWaterDto dto : list) {
|
|
|
+ UseWaterDto useWaterDto = new UseWaterDto();
|
|
|
+ useWaterDto.setUseVolume(dto.getUseVolume());
|
|
|
+ useWaterDto.setDate(dto.getDate());
|
|
|
+ results.add(useWaterDto);
|
|
|
+ }
|
|
|
+ }
|
|
|
return results;
|
|
|
}
|
|
|
|
|
@@ -87,11 +98,18 @@ public class MongoMeterReadRecordServiceImpl implements MongoMeterReadRecordServ
|
|
|
public Double getDeviceVolume(Long deviceId, Integer startDate, Integer endDate) {
|
|
|
List<AggregationOperation> operations = new ArrayList<>();
|
|
|
operations.add(Aggregation.match(Criteria.where("deviceId").is(deviceId)));
|
|
|
- operations.add(Aggregation.match(Criteria.where("status").is(1)));
|
|
|
operations.add(Aggregation.match(Criteria.where("readDate").gte(startDate).lte(endDate)));
|
|
|
- operations.add(Aggregation.group("deviceId")
|
|
|
- .sum("").as("realReadTimes"));
|
|
|
-
|
|
|
+ operations.add(Aggregation.group()
|
|
|
+ .sum("lastCost").as("useVolume"));
|
|
|
+ // 3,聚合查询所有信息
|
|
|
+ Aggregation aggregation = Aggregation.newAggregation(operations).withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build());
|
|
|
+ // 4,查询结果
|
|
|
+ AggregationResults<UseWaterDto> aggregationResults = mongoTemplate.aggregate(aggregation, "sc_meter_read_record", UseWaterDto.class);
|
|
|
+ // 5,获取结果
|
|
|
+ List<UseWaterDto> list = aggregationResults.getMappedResults();
|
|
|
+ if (list != null && list.size() > 0) {
|
|
|
+ return list.get(0).getUseVolume();
|
|
|
+ }
|
|
|
return null;
|
|
|
}
|
|
|
}
|