Commit ed278ad8ff6fd46b8d47faa342b1a68dec0bdf71
1 parent
322fa140f1
Exists in
master
and in
6 other branches
update
Showing 1 changed file with 26 additions and 10 deletions
platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/TrackDownFacade.java
View file @
ed278ad
... | ... | @@ -32,6 +32,9 @@ |
32 | 32 | import org.springframework.beans.factory.annotation.Qualifier; |
33 | 33 | import org.springframework.data.domain.Sort; |
34 | 34 | import org.springframework.data.mongodb.core.MongoTemplate; |
35 | +import org.springframework.data.mongodb.core.aggregation.Aggregation; | |
36 | +import org.springframework.data.mongodb.core.aggregation.AggregationOperation; | |
37 | +import org.springframework.data.mongodb.core.aggregation.AggregationResults; | |
35 | 38 | import org.springframework.data.mongodb.core.query.Criteria; |
36 | 39 | import org.springframework.data.mongodb.core.query.Query; |
37 | 40 | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
38 | 41 | |
39 | 42 | |
40 | 43 | |
... | ... | @@ -1336,24 +1339,37 @@ |
1336 | 1339 | } |
1337 | 1340 | |
1338 | 1341 | public Integer sumField(String collection,String filedName,Criteria criteria) { |
1342 | + | |
1339 | 1343 | Integer total = 0; |
1340 | - String reduce = "function(doc, aggr){ aggr.total +=doc." + filedName + "; }"; | |
1341 | 1344 | Query query = new Query(); |
1342 | 1345 | if(criteria!=null){ |
1343 | 1346 | query.addCriteria(criteria); |
1344 | 1347 | } |
1345 | - DBObject result = mongoTemplate.getCollection(collection).group(null, | |
1346 | - query.getQueryObject(), | |
1347 | - new BasicDBObject("total", total), | |
1348 | - reduce); | |
1349 | - Map<String,BasicDBObject> map = result.toMap(); | |
1350 | - if(map.size() > 0){ | |
1351 | - BasicDBObject bdbo = map.get("0"); | |
1352 | - if(bdbo != null && bdbo.get("total") != null) | |
1353 | - total = bdbo.getInt("total"); | |
1348 | + List<AggregationOperation> operations = new ArrayList<>(); | |
1349 | + operations.add(Aggregation.match(criteria)); | |
1350 | + operations.add(Aggregation.group().sum(filedName).as("total")); | |
1351 | + Aggregation aggregation = Aggregation.newAggregation(operations); | |
1352 | + AggregationResults<Total> results =mongoTemplate.aggregate(aggregation, collection, Total.class); | |
1353 | + List<Total> totals = results.getMappedResults(); | |
1354 | + if (CollectionUtils.isNotEmpty(totals)) | |
1355 | + { | |
1356 | + total = totals.get(0).getTotal(); | |
1354 | 1357 | } |
1355 | 1358 | return total; |
1356 | 1359 | } |
1357 | 1360 | |
1361 | +} | |
1362 | + | |
1363 | +class Total | |
1364 | +{ | |
1365 | + private int total; | |
1366 | + | |
1367 | + public int getTotal() { | |
1368 | + return total; | |
1369 | + } | |
1370 | + | |
1371 | + public void setTotal(int total) { | |
1372 | + this.total = total; | |
1373 | + } | |
1358 | 1374 | } |