package com.zhiwei.manage.handle;

import java.util.Calendar;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.zhiwei.manage.bean.ServerBean;
import com.zhiwei.manage.util.MongoConnect;

@Service("firstRun")
public class DbDepot {

	@Resource(name = "mongoTemplate")
	private MongoTemplate mongo;
	// 信息和检测模板切换链接
	public static ConcurrentMap<String, Object> dbCons;
	public static ConcurrentMap<String, Object> newsCol;
	public static ConcurrentMap<String, Object> dataCol;
	public static String nowPt = "网媒";
	public static int month = 0;
	private static final Log log = LogFactory.getLog(DbDepot.class);

	public void init() {
		log.info("数据连接map初始化");
		dataCol = new ConcurrentHashMap<String, Object>();
		dbCons = new ConcurrentHashMap<String, Object>();
		newsCol = new ConcurrentHashMap<String, Object>();
		List<ServerBean> list = mongo.find(Query.query(Criteria.where("pt").is(nowPt)), ServerBean.class);
		Calendar cal = Calendar.getInstance();
		month = cal.get(Calendar.MONTH + 1);
		for (ServerBean sb : list) {
			String end = "";
			if (sb.isTable()) {
				if (sb.getTableKind().equals("month")) {
					int month=cal.get(Calendar.MONTH) + 1;
					end = cal.get(Calendar.YEAR) + (month>10?""+month:"0"+month);
				} else {

				}
			}
			if (sb.getDbType().equals("mongo")) {
				if (sb.getDbUsername() != null && !sb.getDbUsername().trim().equals("")) {
					DBCollection con = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbUsername(),
							sb.getDbPassword(), sb.getDbName()).getCollection(sb.getCollection() + end);
					dbCons.put(sb.getPt()+sb.getOther(), con);
				} else {
					DBCollection con = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbName())
							.getCollection(sb.getCollection() + end);
					dbCons.put(sb.getPt()+sb.getOther(), con);
				}
			}
		}
		System.out.println("连接初始化成功");
	}

	public DBCollection getDBCollection(String other,String pt) {
		DBCollection con = null;
		ServerBean sb = mongo.findOne(
				Query.query(Criteria.where("pt").is(pt).orOperator(Criteria.where("other").is(other))),
				ServerBean.class);
		System.out.println(pt);
		System.out.println(sb);
		Calendar cal=Calendar.getInstance();
		month=cal.get(Calendar.MONTH+1);
		if (sb != null) {
			if (sb.getDbType().equals("mongo")) {
				String end="";
				if(sb.isTable()) {
					if(sb.getTableKind().equals("month")) {
						int month=cal.get(Calendar.MONTH) + 1;
						end = cal.get(Calendar.YEAR) + (month>10?""+month:"0"+month);
					}else {
						
					}
				}
				if (sb.getDbUsername() != null && !sb.getDbUsername().equals("")) {
					con = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbUsername(), sb.getDbPassword(),
							sb.getDbName()).getCollection(sb.getCollection()+end);
					dbCons.put(pt+sb.getOther(), con);
				} else {
					con = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbName())
							.getCollection(sb.getCollection()+end);
					dbCons.put(pt+sb.getOther(), con);
				}
			}
		}
		return con;
	}

}
