|
@@ -7,8 +7,8 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
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.AggregationOperation;
|
|
|
+import org.springframework.data.mongodb.core.aggregation.*;
|
|
|
+import org.springframework.data.mongodb.core.mapping.Document;
|
|
|
import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
import org.springframework.data.mongodb.core.query.Query;
|
|
|
import org.springframework.stereotype.Service;
|
|
@@ -54,12 +54,33 @@ public class MongoMeterReadRecordServiceImpl implements MongoMeterReadRecordServ
|
|
|
|
|
|
@Override
|
|
|
public List<UseWaterDto> getUseWaterByMonth(Long deviceId, Integer startDate, Integer endDate) {
|
|
|
- Query query = new Query();
|
|
|
- query.addCriteria(Criteria.where("deviceId").is(deviceId));
|
|
|
- if(startDate!=null && endDate!=null)query.addCriteria(Criteria.where("readDate").gte(startDate).lte(endDate));
|
|
|
- query.with(Sort.by(Sort.Order.asc("dateCreate")));
|
|
|
- List<MongoMeterReadRecord> list = this.findMongoMeterReadRecordList(query);
|
|
|
- return null;
|
|
|
+
|
|
|
+
|
|
|
+ ConditionalOperators.Cond a = null;
|
|
|
+ List<AggregationOperation> operations = new ArrayList<>();
|
|
|
+ operations.add(Aggregation.match(Criteria.where("deviceId").is(deviceId)));
|
|
|
+ operations.add(Aggregation.match(Criteria.where("readDate").gte(startDate).lte(endDate)));
|
|
|
+
|
|
|
+ //new Document("$subtract",Array.asList("$modifiedon", "$createdon"));
|
|
|
+ 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")
|
|
|
+ );
|
|
|
+
|
|
|
+ operations.add(Aggregation.group("month")
|
|
|
+ .sum("lastCost").as("lastCost")
|
|
|
+ );
|
|
|
+ operations.add(Aggregation.project().
|
|
|
+ and("month").as("date").
|
|
|
+ and("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> results = aggregationResults.getMappedResults();
|
|
|
+ return results;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -70,6 +91,7 @@ public class MongoMeterReadRecordServiceImpl implements MongoMeterReadRecordServ
|
|
|
operations.add(Aggregation.match(Criteria.where("readDate").gte(startDate).lte(endDate)));
|
|
|
operations.add(Aggregation.group("deviceId")
|
|
|
.sum("").as("realReadTimes"));
|
|
|
+
|
|
|
return null;
|
|
|
}
|
|
|
}
|