package com.zhiwei.manage.dao;

import java.lang.reflect.Field;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
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.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.WriteResult;
import com.zhiwei.manage.bean.Channel;
import com.zhiwei.manage.bean.ServerBean;
import com.zhiwei.manage.handle.DbDepot;
import com.zhiwei.manage.util.MongoConnect;

@Component
public class ServerDaoImpl implements ServerDao {

	@Resource(name = "mongoTemplate")
	private MongoTemplate mongo;
	@Autowired
	private DbDepot dbDepot;
	@Override
	public void insert(ServerBean serverBean) {
		mongo.insert(serverBean);
	}

	@Override
	public List<ServerBean> findDb(int pageNo, int pageSize, String dBName) {
		if (dBName == null || dBName.equals("")) {
			Query query = new Query();
			query.skip((pageNo - 1) * pageSize).limit(pageSize);
			return mongo.find(query, ServerBean.class);
		} else {
			return mongo.find(
					Query.query(Criteria.where("dbName").regex(dBName)).skip((pageNo - 1) * pageSize).limit(pageSize),
					ServerBean.class);
		}
	}

	@Override
	public boolean update(ServerBean serverBean) {
		Update update = new Update();
		Field[] fields = serverBean.getClass().getDeclaredFields();
		for (int j = 0; j < fields.length; j++) {
			fields[j].setAccessible(true);
			try {
				if (fields[j].getName().equals("id")) {
					continue;
				}
				if (fields[j].get(serverBean) != null)
					// 字段名，字段值
					update.set(fields[j].getName(), fields[j].get(serverBean));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		WriteResult result = mongo.updateFirst(Query.query(Criteria.where("_id").is(serverBean.getId())), update,
				ServerBean.class);
		return result.getN() == 1 ? true : false;
	}

	@Override
	public boolean delete(List<String> id) {
		System.out.println(id);
		WriteResult result = mongo.remove(Query.query(Criteria.where("_id").in(id)), ServerBean.class);
		return result.getN() > 0 ? true : false;
	}

	@Override
	public void findPt(String pt) {
		List<ServerBean> list = mongo.find(Query.query(Criteria.where("pt").is(pt)), ServerBean.class);
//		DbDepot.dbCons = new ConcurrentHashMap<String, Object>();
		for (ServerBean sb : list) {
			System.out.println(sb);
			if (sb.getDbType().equals("mongo")) {
				if (sb.getDbUsername() != null && !sb.getDbUsername().equals("")) {
					DBCollection con = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbUsername(),
							sb.getDbPassword(), sb.getDbName()).getCollection(sb.getCollection());
					DbDepot.dbCons.put(sb.getOther(), con);
				} else {
					DBCollection con = new MongoConnect(sb.getDbHost(), sb.getDbPort(), sb.getDbName())
							.getCollection(sb.getCollection());
					DbDepot.dbCons.put(sb.getOther(), con);
				}
			}
		}
		DbDepot.nowPt = pt;
	}

	@Override
	public List<Channel> findAllChannels(int pageNo,int pageSize) {
		Query query=new Query();
		query.skip((pageNo-1)*pageSize).limit(pageSize);
		List<Channel> list = mongo.find(query, Channel.class);
		return list;
	}

	@Override
	public void insertChannels(Channel channel) {
		mongo.insert(channel);
	}

	@Override
	public boolean updateChannel(Channel channel) {
		Update update = new Update();
		Field[] fields = channel.getClass().getDeclaredFields();
		for (int j = 0; j < fields.length; j++) {
			fields[j].setAccessible(true);
			try {
				if (fields[j].getName().equals("id")) {
					continue;
				}
				if (fields[j].get(channel) != null)
					// 字段名，字段值
					update.set(fields[j].getName(), fields[j].get(channel));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		WriteResult result = mongo.updateFirst(Query.query(Criteria.where("_id").is(channel.getId())), update,
				Channel.class);
		return result.getN() == 1 ? true : false;
	}

	@Override
	public boolean deleteChannel(List<String> id) {
		WriteResult result = mongo.remove(Query.query(Criteria.where("_id").in(id)), Channel.class);
		return result.getN() >0 ? true : false;
	}

	@Override
	public int findServerCount(String dbName) {
		if (dbName.equals("") || dbName == null) {
			System.out.println("this");
			return (int) mongo.count(null, ServerBean.class);
		} else {
			return (int) mongo.count(Query.query(Criteria.where("dbName").regex(dbName)), ServerBean.class);
		}
	}

	@Override
	public int findChannelsCount() {
		return (int) mongo.count(null, Channel.class);
	}

	@Override
	public List<Channel> findDisChannels() {
		return mongo.findAll(Channel.class);
	}

	@Override
	public List<String> findDisChan() {
		return mongo.getCollection("Channel").distinct("pt");
	}

}
