package com.wxz.mongodb;

import com.alibaba.fastjson.JSON;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.util.*;

/**
 * @author [email protected]
 * @type class
 * @date 2017/9/21 -16:54
 */
public class SimpleMongodbAccessor {
    private MongoClient mongoClient;
    private MongoDatabase mongoDatabase;

    public SimpleMongodbAccessor(String ip, int port, String databaseName) {
        mongoClient = new MongoClient(ip, port);
        mongoDatabase = mongoClient.getDatabase(databaseName);
    }

    /**
     * 查找全部数据
     */
    public <T> List<T> findAll(String collectionName, Class<T> clazz) {
        return find(collectionName, null, null, clazz);
    }

    /**
     * 查找指定条数的数据
     */
    public <T> List<T> find(String collectionName, Integer pageNumber, Integer pageSize, Class<T> clazz) {
        MongoCollection collection = mongoDatabase.getCollection(collectionName);
        List<T> list = new ArrayList<>();
        if (collection == null) {
            return list;
        }
        FindIterable findIterable = collection.find();

        if (pageSize != null && pageSize >= 0) {
            if (pageNumber != null && pageNumber >= 1) {
                findIterable = findIterable.skip((pageNumber - 1) * pageSize);
            }
            findIterable = findIterable.limit(pageSize);
        }
        Iterator<Document> iterator = findIterable.iterator();
        while (iterator.hasNext()) {
            Document document = iterator.next();
            document.remove("_id");
            T t = JSON.parseObject(document.toJson(), clazz);
            list.add(t);
        }
        return list;
    }

    /**
     * 查找指定条数的数据
     */
    public List<Map<String, Object>> findAll(String collectionName) {
        return find(collectionName, null, null);
    }

    /**
     * 查找指定条数的数据
     */
    public List<Map<String, Object>> find(String collectionName, Integer pageNumber, Integer pageSize) {
        MongoCollection collection = mongoDatabase.getCollection(collectionName);
        List<Map<String, Object>> list = new ArrayList<>();
        if (collection == null) {
            return list;
        }
        FindIterable findIterable = collection.find();

        if (pageSize != null && pageSize >= 0) {
            if (pageNumber != null && pageNumber >= 1) {
                findIterable = findIterable.skip((pageNumber - 1) * pageSize);
            }
            findIterable = findIterable.limit(pageSize);
        }
        Iterator<Document> iterator = findIterable.iterator();
        while (iterator.hasNext()) {
            Document document = iterator.next();
            document.remove("_id");
            Map<String, Object> map = new HashMap<>(document);
            list.add(map);
        }
        return list;
    }

    /**
     * 批量插入数据
     */
    public void batchInsert(String collectionName, List<Map<String, Object>> list) {
        MongoCollection collection = mongoDatabase.getCollection(collectionName);
        List<Document> documents = new ArrayList<>();
        for (Map<String, Object> map : list) {
            documents.add(new Document(map));
        }
        collection.insertMany(documents);
    }

    /**
     * 关闭数据库连接
     */
    public void close() {
        try {
            if (mongoClient != null) {
                mongoClient.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}