@Override
public Page<ProductInfo> findAll(Pageable pageable, ProductInfo productInfo) {
//創建一個操作聚合operations
List<AggregationOperation> operations = new ArrayList<>();
//創建一個條件類criteria
Criteria criteria = new Criteria();
//商品狀態不為空
if (productInfo.getProductStatus()!=null){
//productStatus等於查詢的商品狀態這個條件添加進操作聚合operations
operations.add(Aggregation.match(criteria.and("productStatus").is(productInfo.getProductStatus())));
}
long totalCount = 0;
//總頁數
if (operations!= null && operations.size() > 0){
//操作聚合
Aggregation aggregationCount = newAggregation(operations);
//查詢總的數據條數,返回一個集合
AggregationResults<ProductInfo> resultsCount = mongoTemplate.aggregate(aggregationCount, "productInfo", ProductInfo.class);
totalCount = resultsCount.getMappedResults().size();
}else{
//操作聚合為空,查詢總的數據條數
totalCount = mongoTemplate.findAll(ProductInfo.class).size();
}
//操作聚合添加頁數乘以每頁大小等於總頁數
operations.add(Aggregation.skip((long) (pageable.getPageNumber()) * pageable.getPageSize()));
//操作聚合添加返回一頁的數據
operations.add(Aggregation.limit(pageable.getPageSize()));
//操作聚合添加根據solveCount降序排序
// operations.add(Aggregation.sort(Sort.Direction.DESC, "solveCount"));
Aggregation aggregation = newAggregation(operations);
//根據條件查詢數據
AggregationResults<ProductInfo> results = mongoTemplate.aggregate(aggregation, "productInfo", ProductInfo.class);
//返回結果的map和分頁信息和總記錄數
return new PageImpl<>(results.getMappedResults(),pageable,totalCount);
}