package com.zhiwei.manage.util;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

/**
 * 连接mongodb，执行操作
 * 
 * @author shenjinzhu
 *
 */
public class MongoConnect {
	private static final Logger log = LoggerFactory.getLogger(MongoConnect.class);
	private static MongoClient mongoClient = null;
	private static String dbName;

	public MongoConnect(String host, int port, String username, String password, String dbName) {
		this.dbName = dbName;
		if (mongoClient == null) {
			MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
			optionsBuilder.connectionsPerHost(100); // 与目标数据库可创建的最大连接数
			optionsBuilder.connectTimeout(1000 * 60 * 40); // 与数据库建立连接的超时时间
			optionsBuilder.maxWaitTime(1000 * 60 * 5); // 一个线程成功获取到一个可用数据库连接之前的最大等待时间
			optionsBuilder.threadsAllowedToBlockForConnectionMultiplier(100);// 等待一个连接
			optionsBuilder.maxConnectionIdleTime(0);
			optionsBuilder.maxConnectionLifeTime(0);
			optionsBuilder.socketKeepAlive(true);
			optionsBuilder.socketTimeout(1000 * 20);
			MongoClientOptions options = optionsBuilder.build();
			char[] passwords = password.toCharArray();
			List<MongoCredential> credentials = new ArrayList<>();
			MongoCredential credential = null;
			if (username != null && !username.equals("")) {
				credential = MongoCredential.createCredential(username, dbName, passwords);
			}
			credentials.add(credential);
			List<ServerAddress> serverAddresses = new ArrayList<>();
			try {
				serverAddresses.add(new ServerAddress(host, port));
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
			mongoClient = new MongoClient(serverAddresses, credentials, options);
		}
	}	
	
	
	public MongoConnect(String host, int port, String dbName) {
		this.dbName = dbName;
		if (mongoClient == null) {
			MongoClientOptions.Builder optionsBuilder = new MongoClientOptions.Builder();
			optionsBuilder.connectionsPerHost(100); // 与目标数据库可创建的最大连接数
			optionsBuilder.connectTimeout(1000 * 60 * 40); // 与数据库建立连接的超时时间
			optionsBuilder.maxWaitTime(1000 * 60 * 5); // 一个线程成功获取到一个可用数据库连接之前的最大等待时间
			optionsBuilder.threadsAllowedToBlockForConnectionMultiplier(100);// 等待一个连接
																				// 允许阻塞的线程数
			optionsBuilder.maxConnectionIdleTime(0);
			optionsBuilder.maxConnectionLifeTime(0);
			optionsBuilder.socketKeepAlive(true);
			optionsBuilder.socketTimeout(1000 * 20);
			MongoClientOptions options = optionsBuilder.build();
			List<MongoCredential> credentials = new ArrayList<>();
			List<ServerAddress> serverAddresses = new ArrayList<>();
			try {
				serverAddresses.add(new ServerAddress(host, port));
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
			mongoClient = new MongoClient(serverAddresses, credentials, options);
		}
	}	
	
	public List<Map<String,Object>> getData(Date date,String collection){
		List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
		DB db = mongoClient.getDB(dbName);
		DBCollection dbCollection = db.getCollection(collection);
		// 查询条件---大于rsid
		DBObject queryObj = new BasicDBObject();
		// queryObj.put("savetime", 1);
		queryObj.put("time", new BasicDBObject("$gt", date));
		DBCursor dbCursor = dbCollection.find(queryObj);
		while (dbCursor.hasNext()) {
			if (list.size() > 10000)
				break;
			list.add((Map<String, Object>) dbCursor.next());
		}
		dbCursor.close();
		return list;		
	}
	
	public DBCollection getCollection(String collection){
		DB db = mongoClient.getDB(dbName);
		return db.getCollection(collection);
	}
	
	public static void test(){
		MongoConnect mongo = new MongoConnect("115.236.59.91",27017, "mediaspider");
		DBCollection col=mongo.getCollection("net_media_201712");
		System.out.println(col.count(new BasicDBObject("spyderInfoId","JsonData65a3df29-0792-481d-ba05-78344facd2f6")));
	}
	
	public static void main(String[] args) {
	}

}
