Aggregation managerSum = Aggregation.newAggregation(
Aggregation.unwind("asset_manager_lease"),
Aggregation.match(criteria),
Aggregation.group("asset_manager_lease.asset_operator")
.first("asset_manager_lease.asset_operator").as("assetOperator")
.count().as("assetTotal"),
Aggregation.sort(Sort.Direction.DESC,"_id")
);
//操作数据库获取集合
AggregationResults<OperatorDTO> aggregate = mongoTemplate.aggregate(managerSum, manager, OperatorDTO.class);
List<OperatorDTO> operatorDTOS = aggregate.getMappedResults();
public List<AssetManagerDTO> findByAll(AssetManagerFrom assetManagerFrom, Pageable pageable,String dept) {
//待查询字段 租赁状态 面积 资产价值 资产数量
List<AggregationOperation> aggregationOperations = getAllQuery(assetManagerFrom,dept);
aggregationOperations.addAll(CommentUtils.addPageCriteria(pageable));
Aggregation managerSum = Aggregation.newAggregation(aggregationOperations);
AggregationResults<AssetManagerDTO> aggregate = mongoTemplate.aggregate(managerSum, manager, AssetManagerDTO.class);
List<AssetManagerDTO> assetManagerEntityList = aggregate.getMappedResults();
return assetManagerEntityList;
}
//getAllQuery()方法
private List<AggregationOperation> getAllQuery(AssetManagerFrom assetManagerFrom, String dept) {
Criteria criteria = Criteria.where("deleted").is(false);
//根据资产运营人查询
Criteria criteriaC = new Criteria();
List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.unwind("asset_manager_lease"));
Criteria criteria1 = CoverUtil.analysis(dept, assetManagerFrom.getAssetOperator());
operations.add(Aggregation.match(criteria1));
//模糊匹配 //关联从表名 //主表关联字段 //从表关联的字段 //查询结果名
LookupOperation lookupOperation = LookupOperation.newLookup().
from(managerSenior).
localField("senior_id").
foreignField("_id").
as("inventory_docs");
operations.add(lookupOperation);
operations.add(Aggregation.unwind("inventory_docs"));
Criteria criteriaA = new Criteria();
Criteria criteriaB = new Criteria();
Criteria criteriaD = new Criteria();
if (CommentUtils.isNotEmpty(assetManagerFrom)) {
//按上次盘点时间查询
if (CommentUtils.isNotEmpty(assetManagerFrom.getLatestDate())) {
String inventoryDate = DateUtils.getDateByMonth(assetManagerFrom.getLatestDate());
criteriaA.orOperator(Criteria.where("inventory_date").lte(inventoryDate),
Criteria.where("inventory_date").is(null));
}
//按照租赁状态查询
if (CommentUtils.isNotEmpty(assetManagerFrom.getLeaseDeadline())) {
//查询即将到期
if (DictionaryEnum.EXPIRE.getCode().equals(assetManagerFrom.getLeaseDeadline())) {
String endDate = DateUtils.getDateByMonth(-1);
criteria.and("asset_manager_lease.reality_lease_end_time").lte(endDate).and("asset_manager_lease.lease_state")
.is(DictionaryEnum.OCCUPY.getCode());
} else {
criteria.and("asset_manager_lease.lease_state").is(assetManagerFrom.getLeaseDeadline());
}
criteria.and("asset_state").is(DictionaryEnum.ASSET_AGREE.getCode());
}
if (CommentUtils.isNotEmpty(assetManagerFrom.getAssetState())) {
criteriaD.and("asset_state").is(assetManagerFrom.getAssetState());
}
//根据资产名称、产权所有人、资产运营人、租赁位置、房产证编号模糊匹配
if (CommentUtils.isNotEmpty(assetManagerFrom.getFuzzyQuery())) {
String fuzzyQuery = assetManagerFrom.getFuzzyQuery();
String regex = "^.*" + fuzzyQuery + ".*$";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
criteriaB.orOperator(Criteria.where("inventory_docs.asset_name").regex(pattern),
Criteria.where("asset_manager_lease.property_owner").regex(pattern),
Criteria.where("asset_manager_lease.asset_operator").regex(pattern),
Criteria.where("rental_location").regex(pattern),
Criteria.where("property_no").regex(pattern));
}
criteria.andOperator(criteriaA, criteriaB, criteriaC, criteriaD);
}
operations.add(Aggregation.match(criteria));
operations.add(Aggregation.sort(Sort.Direction.DESC, "asset_manager_lease.asset_operator", "_id"));
return operations;
}
//addPageCriteria()
public static List<AggregationOperation> addPageCriteria(Pageable pageable) {
long start = (pageable.getPageNumber() - 1) * pageable.getPageSize();
//设置排序
Sort sort = pageable.getSort();
List<AggregationOperation> operations = new ArrayList<>();
operations.add(Aggregation.skip(start));
operations.add(Aggregation.limit(pageable.getPageSize()));
if(!sort.isEmpty()) {
operations.add(Aggregation.sort(sort));
}
return operations;
}