package com.zhiwei.manage.dao;

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

import javax.annotation.Resource;

import org.bson.types.ObjectId;
import org.elasticsearch.common.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.zhiwei.manage.bean.Template;
import com.zhiwei.manage.handle.DbDepot;
import com.zhiwei.manage.util.Change;

@Service
public class TemplateDaoImpl implements TemplateDao {
	@Resource(name = "mongoTemplate")
	private MongoTemplate mongo;

	@Override
	public boolean insert(Template template) {
		mongo.insert(template);
		return true;
	}

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

	@Override
	public Template findById(String templateId, String pt) {
		Template temp = mongo.findOne(Query.query(Criteria.where("_id").is(templateId)), Template.class);
		return temp;
	}

	@Override
	public List<Template> findByTmpName(String templateName, int pageNo, int pageSize, String pt) {
		List<Template> list = mongo.find(Query.query(Criteria.where("tempName").regex(templateName))
				.skip((pageNo - 1) * pageSize).limit(pageSize).addCriteria(Criteria.where("pt").is(pt)),
				Template.class);
		return list;
	}

	@Override
	public boolean delete(String templateId, String pt) {
		WriteResult result = mongo.remove(Query.query(Criteria.where("_id").is(templateId)), Template.class);
		return result.getN() == 0 ? true : false;
	}

	@Override
	public List<Template> findAll(int pageNo, int pageSize, String pt) {
		List<Template> list = mongo.find(
				Query.query(Criteria.where("pt").is(pt)).skip((pageNo - 1) * pageSize).limit(pageSize), Template.class);
		return list;
	}

	@Override
	public int allCount(String pt) {
		return (int) mongo.count(Query.query(Criteria.where("pt").is(pt)), Template.class);
	}

	@Override
	public int findByUserCount(String templateName, String pt) {
		return (int) mongo.count(
				Query.query(Criteria.where("tempName").regex(templateName).andOperator(Criteria.where("pt").is(pt))),
				Template.class);
	}

	@Override
	public List<Template> findAll() {
		return mongo.find(null, Template.class);
	}

}
