package com.zhiwei.manage.dao;

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

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.domain.Sort;
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.Message;

@Component
public class MessageDaoImpl implements MessageDao {
	private static final Log log = LogFactory.getLog(MessageDaoImpl.class);

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

	@SuppressWarnings("all")
	@Override
	public String insert(Message message) {
		Message msg = mongo.findOne(Query
				.query(Criteria.where("tempName").is(message.getTempName())
						.andOperator(Criteria.where("handle").is(false)))
				.addCriteria(Criteria.where("errorType").is(message.getErrorType())), Message.class);
		if (msg == null) {
			mongo.insert(message);
			return "添加成功";
		}
		log.info("有类似错误存在不添加");
		return "未添加";
	}

	@Override
	public String update(Message message, String pt) {
		Update update = new Update();
		Field[] fields = message.getClass().getDeclaredFields();
		for (int j = 0; j < fields.length; j++) {
			fields[j].setAccessible(true);
			try {
				if (fields[j].getName().equals("messageId")) {
					continue;
				}
				if (fields[j].get(message) != null)
					update.set(fields[j].getName(), fields[j].get(message));
				// 字段名，字段值
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		WriteResult result = mongo.upsert(Query.query(Criteria.where("_id").is(message.getMessageId())), update,
				Message.class);
		if (result.getN() == 1) {
			return "修改成功";
		} else {
			return "修改失败";
		}
	}

	@Override
	public List<Message> findAll(int pageNo, int pageSize, String pt) {
		return mongo.find(Query.query(Criteria.where("pt").is(pt).andOperator(Criteria.where("handle").is(false)))
				.skip((pageNo - 1) * pageSize).with(new Sort(Sort.Direction.ASC, "templateLv"))
				.with(new Sort(Sort.Direction.DESC, "createDate")).limit(pageSize), Message.class);
	}

	@Override
	public List<Message> findByTmpName(String templateName, int pageNo, int pageSize, String pt) {
		return mongo.find(Query.query(Criteria.where("tempName").regex(templateName)).skip((pageNo - 1) * pageSize)
				.limit(pageSize).with(new Sort(Sort.Direction.DESC, "createDate")), Message.class);
	}

	@Override
	public boolean delete(List<String> messageId, String pt) {
		WriteResult wr = mongo.remove(Query.query(Criteria.where("_id").in(messageId)), Message.class);
		if (wr.getN() == 0) {
			return false;
		}
		return true;
	}

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

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

	@Override
	public List<Message> findByHandle(int pageNo, int pageSize, boolean handle, String tempName, String pt) {
		return mongo.find(
				Query.query(Criteria.where("tempName").regex(tempName)
						.andOperator(Criteria.where("handle").is(handle).andOperator(Criteria.where("pt").is(pt))))
						.skip((pageNo - 1) * pageSize).limit(pageSize)
						.with(new Sort(Sort.Direction.DESC, "handleDate")),
				Message.class);
	}

	@Override
	public int findByHandleCount(int pageNo, int pageSize, boolean handle, String tempName, String pt) {
		return (int) mongo.count(
				Query.query(Criteria.where("tempName").regex(tempName)
						.andOperator(Criteria.where("handle").is(handle).andOperator(Criteria.where("pt").is(pt)))),
				Message.class);
	}

	@Override
	public Message findMsg(String tempName, String errorType) {
		return mongo.findOne(
				Query.query(
						Criteria.where("tempName").is(tempName).andOperator(Criteria.where("errotType").is(errorType))),
				Message.class);
	}
}
