From ed278ad8ff6fd46b8d47faa342b1a68dec0bdf71 Mon Sep 17 00:00:00 2001 From: liquanyu Date: Tue, 21 Apr 2020 14:00:34 +0800 Subject: [PATCH] update --- .../operate/web/facade/TrackDownFacade.java | 36 ++++++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/TrackDownFacade.java b/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/TrackDownFacade.java index 6dc1d38..f86e039 100644 --- a/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/TrackDownFacade.java +++ b/platform-operate-api/src/main/java/com/lyms/platform/operate/web/facade/TrackDownFacade.java @@ -32,6 +32,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; 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.AggregationResults; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; @@ -1336,23 +1339,36 @@ public class TrackDownFacade { } public Integer sumField(String collection,String filedName,Criteria criteria) { + Integer total = 0; - String reduce = "function(doc, aggr){ aggr.total +=doc." + filedName + "; }"; Query query = new Query(); if(criteria!=null){ query.addCriteria(criteria); } - DBObject result = mongoTemplate.getCollection(collection).group(null, - query.getQueryObject(), - new BasicDBObject("total", total), - reduce); - Map map = result.toMap(); - if(map.size() > 0){ - BasicDBObject bdbo = map.get("0"); - if(bdbo != null && bdbo.get("total") != null) - total = bdbo.getInt("total"); + List operations = new ArrayList<>(); + operations.add(Aggregation.match(criteria)); + operations.add(Aggregation.group().sum(filedName).as("total")); + Aggregation aggregation = Aggregation.newAggregation(operations); + AggregationResults results =mongoTemplate.aggregate(aggregation, collection, Total.class); + List totals = results.getMappedResults(); + if (CollectionUtils.isNotEmpty(totals)) + { + total = totals.get(0).getTotal(); } return total; } } + +class Total +{ + private int total; + + public int getTotal() { + return total; + } + + public void setTotal(int total) { + this.total = total; + } +} -- 1.8.3.1