package com.zhiwei.manage.dao;

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

import javax.annotation.Resource;

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.WriteResult;
import com.zhiwei.manage.bean.Channel;
import com.zhiwei.manage.bean.ServerBean;

@Component
public class ServerDaoImpl implements ServerDao {

	@Resource(name = "mongoTemplate")
	private MongoTemplate mongo;

	@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 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 List<Channel> findChannels() {
		List<Channel> list = mongo.find(null, 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");
	}

	@Override
	public ServerBean findServerBean(String pt) {
		return mongo.findOne(
				Query.query(Criteria.where("pt").is(pt).andOperator(Criteria.where("dbType").is("Elasticsearch"))),
				ServerBean.class);
	}

}
