All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.anwen.mongo.toolkit.LambdaOperate Maven / Gradle / Ivy

There is a newer version: 2.1.6.1
Show newest version
package com.anwen.mongo.toolkit;

import com.anwen.mongo.conditions.BuildCondition;
import com.anwen.mongo.conditions.interfaces.aggregate.pipeline.Projection;
import com.anwen.mongo.conditions.interfaces.condition.CompareCondition;
import com.anwen.mongo.conditions.interfaces.condition.Order;
import com.anwen.mongo.convert.Converter;
import com.anwen.mongo.convert.DocumentMapperConvert;
import com.anwen.mongo.model.BaseLambdaQueryResult;
import com.anwen.mongo.model.PageParam;
import com.anwen.mongo.model.PageResult;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * lambda形式调用,预构建条件
 *
 * @author JiaChaoYang
 * @project mongo-plus
 * @date 2023-12-28 17:03
 **/
public class LambdaOperate {

    Logger logger = LoggerFactory.getLogger(LambdaOperate.class);

    public  List getLambdaQueryResult(FindIterable iterable, Class clazz) {
        return DocumentMapperConvert.mapDocumentList(iterable, clazz);
    }

    public  T getLambdaQueryResultOne(FindIterable iterable, Class clazz) {
        try (MongoCursor cursor = iterable.iterator()) {
            if (cursor.hasNext()) {
                return DocumentMapperConvert.mapDocument(cursor.next(), clazz);
            }
            return null;
        }
    }

    public List> getLambdaQueryResult(FindIterable iterable,Integer size) {
        return Converter.convertDocumentToMap(iterable,size);
    }

    public BaseLambdaQueryResult baseLambdaQuery(List compareConditionList, List orderList,List projectionList,List basicDBObjectList) {
        BasicDBObject sortCond = new BasicDBObject();
        if (CollUtil.isNotEmpty(orderList)) {
            orderList.forEach(order -> sortCond.put(order.getColumn(), order.getType()));
        }
        BasicDBObject basicDBObject = BuildCondition.buildQueryCondition(compareConditionList);
        if (CollUtil.isNotEmpty(basicDBObjectList)){
            basicDBObjectList.forEach(basic -> {
                basicDBObject.putAll(basic.toMap());
            });
        }
        return new BaseLambdaQueryResult(basicDBObject,BuildCondition.buildProjection(projectionList),sortCond);
    }

    public  PageResult getLambdaQueryResultPage(FindIterable documentFindIterable,long totalSize, PageParam pageParams,Class clazz) {
        PageResult pageResult = new PageResult<>();
        pageResult.setPageNum(pageParams.getPageNum());
        pageResult.setPageSize(pageParams.getPageSize());
        pageResult.setContentData(DocumentMapperConvert.mapDocumentList(documentFindIterable.skip((pageParams.getPageNum() - 1) * pageParams.getPageSize()).limit(pageParams.getPageSize()), clazz));
        // 不查询总条数,总条数=当前页的总数
        if (totalSize == -1) {
            totalSize = pageResult.getContentData().size();
        }
        pageResult.setTotalSize(totalSize);
        pageResult.setTotalPages((totalSize + pageParams.getPageSize() - 1) / pageParams.getPageSize());
        return pageResult;
    }

    public PageResult> getLambdaQueryResultPage(FindIterable documentFindIterable,long totalSize, PageParam pageParams) {
        PageResult> pageResult = new PageResult<>();
        pageResult.setPageNum(pageParams.getPageNum());
        pageResult.setPageSize(pageParams.getPageSize());
        pageResult.setTotalSize(totalSize);
        pageResult.setTotalPages((totalSize + pageParams.getPageSize() - 1) / pageParams.getPageSize());
        pageResult.setContentData(Converter.convertDocumentToMap(documentFindIterable.skip((pageParams.getPageNum() - 1) * pageParams.getPageSize()).limit(pageParams.getPageSize())));
        return pageResult;
    }

//    public PageResult> getLambdaQueryResultPage(String collectionName, List compareConditionList, List orderList,List projectionList,List basicDBObjectList, PageParam pageParams) {
//        return getLambdaQueryResultPage(MongoTransactionContext.getClientSessionContext(),collectionName,compareConditionList,orderList,projectionList,basicDBObjectList,pageParams);
//    }
//
//    public PageResult> getLambdaQueryResultPage(ClientSession clientSession,String collectionName, List compareConditionList, List orderList,List projectionList,List basicDBObjectList, PageParam pageParams) {
//        PageResult> pageResult = new PageResult<>();
//        FindIterable documentFindIterable = baseLambdaQuery(clientSession,collectionName, compareConditionList, orderList,projectionList,basicDBObjectList);
//        long totalSize = doCount(collectionName,compareConditionList);
//        pageResult.setPageNum(pageParams.getPageNum());
//        pageResult.setPageSize(pageParams.getPageSize());
//        pageResult.setTotalSize(totalSize);
//        pageResult.setTotalPages((totalSize + pageParams.getPageSize() - 1) / pageParams.getPageSize());
//        pageResult.setContentData(Converter.convertDocumentToMap(documentFindIterable.skip((pageParams.getPageNum() - 1) * pageParams.getPageSize()).limit(pageParams.getPageSize())));
//        return pageResult;
//    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy