package com.zhiwei.manage.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

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.Message;
import com.zhiwei.manage.handle.DbDepot;
import com.zhiwei.manage.util.Change;

@Component
public class MessageDaoImpl implements MessageDao {

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

	@Autowired
	private DbDepot dbDepot;

	@Override
	public String insert(Message message) {
		mongo.insert(message);
		return null;
	}

	@Override
	public String update(Message message,String pt) {
		DBCollection con = null;
		if (DbDepot.dbCons.get(pt+"msg") != null) {
			con = (DBCollection) DbDepot.dbCons.get(pt+"msg");
		} else {
			con = dbDepot.getDBCollection("msg",pt);
		}
		if (con == null) {
			return null;
		}		
		DBObject obj = new BasicDBObject();
		obj.put("handle", message.isHandle());
		obj.put("handleDate", new Date());
		obj.put("handler", message.getHandler());
		obj.put("handleMsg", message.getHandleMsg());
		DBObject update = new BasicDBObject();
		update.put("$set", obj);
		WriteResult result = con.update(new BasicDBObject("_id", new ObjectId(message.getMessageId())), update, false,
				false);
		if (result.getN() == 1) {
			return "修改成功";
		} else {
			return "修改失败";
		}
	}

	@Override
	public List<Message> findAll(int pageNo, int pageSize,String pt) {
		List<Message> msgList = new ArrayList<Message>();
		DBCollection con = null;
		if (DbDepot.dbCons.get(pt+"msg") != null) {
			con = (DBCollection) DbDepot.dbCons.get(pt+"msg");
		} else {
			con = dbDepot.getDBCollection("msg",pt);
		}
		if (con == null) {
			return null;
		}
		System.out.println(pt);
		DBCursor cursor = con.find(new BasicDBObject("pt", pt).append("handle", false))
				.skip((pageNo - 1) * pageSize).limit(pageSize).sort(new BasicDBObject("createDate", -1));
		while (cursor.hasNext()) {
			Map<String, Object> map = (Map<String, Object>) cursor.next();
			Message msg = Change.toBean(Message.class, map);
			msg.setMessageId(String.valueOf(map.get("_id")));
			msgList.add(msg);
		}
		return msgList;
	}

	@Override
	public List<Message> findByTmpName(String templateName, int pageNo, int pageSize,String pt) {
		List<Message> msgList = new ArrayList<Message>();
		DBCollection con = null;
		if (DbDepot.dbCons.get(pt+"msg") != null) {
			con = (DBCollection) DbDepot.dbCons.get(pt+"msg");
		} else {
			con = dbDepot.getDBCollection("msg",pt);
		}
		if (con == null) {
			return null;
		}
		DBCursor cursor = con
				.find(new BasicDBObject("pt", pt).append("tempName",
						new BasicDBObject("$regex", templateName)))
				.skip((pageNo - 1) * pageSize).limit(pageSize).sort(new BasicDBObject("createDate", -1));
		while (cursor.hasNext()) {
			Map<String, Object> map = (Map<String, Object>) cursor.next();
			Message msg = Change.toBean(Message.class, map);
			msg.setMessageId(String.valueOf(map.get("_id")));
			msgList.add(msg);
		}
		return msgList;
		//
		// return
		// mongo.find(Query.query(Criteria.where("templateName").regex(templateName)).skip((pageNo
		// - 1) * pageSize)
		// .limit(pageSize), Message.class);
	}

	@Override
	public boolean delete(List<String> messageId,String pt) {
		DBCollection con = null;
		if (DbDepot.dbCons.get(pt+"msg") != null) {
			con = (DBCollection) DbDepot.dbCons.get(pt+"msg");
		} else {
			con = dbDepot.getDBCollection("msg",pt);
		}
		if (con == null) {
			return false;
		}
		System.out.println(con);
		for (String msg : messageId) {
			WriteResult wr = con.remove(new BasicDBObject("_id", new ObjectId(msg)));
			if(wr.getN()==0) {
				return false;
			}
		}
		return true;
	}

	@Override
	public int allCount(String pt) {
		DBCollection con = null;
		if (DbDepot.dbCons.get(pt+"msg") != null) {
			con = (DBCollection) DbDepot.dbCons.get(pt+"msg");
		} else {
			con = dbDepot.getDBCollection("msg",pt);
		}
		if (con == null) {
			return 0;
		}
		return (int) con.count(new BasicDBObject("handle", false).append("pt",pt ));
	}

	@Override
	public int findByTmpCount(String templateName,String pt) {
		DBCollection con = null;
		if (DbDepot.dbCons.get(pt+"msg") != null) {
			con = (DBCollection) DbDepot.dbCons.get(pt+"msg");
		} else {
			con = dbDepot.getDBCollection("msg",pt);
		}
		if (con == null) {
			return 0;
		}
		return (int) con.count(new BasicDBObject("templateName", new BasicDBObject("$regex", templateName)).append("pt",pt ));
		// return (int)
		// mongo.count(Query.query(Criteria.where("templateName").regex(templateName)),
		// Message.class);
	}

	@Override
	public List<Message> findByHandle(int pageNo, int pageSize, boolean handle, String tempName,String pt) {
		DBCollection con = null;
		if (DbDepot.dbCons.get(pt+"msg") != null) {
			con = (DBCollection) DbDepot.dbCons.get(pt+"msg");
		} else {
			con = dbDepot.getDBCollection("msg",pt);
		}
		if (con == null) {
			return null;
		}
		List<Message> msgList = new ArrayList<Message>();
		if (tempName == null || tempName.equals("")) {
			DBCursor cur = con.find(new BasicDBObject("handle", handle).append("pt",pt )).sort(new BasicDBObject("createDate", -1))
					.skip((pageNo - 1) * pageSize).limit(pageSize);
			while (cur.hasNext()) {
				Map<String, Object> map = (Map<String, Object>) cur.next();
				Message msg = Change.toBean(Message.class, map);
				msg.setMessageId(String.valueOf(map.get("_id")));
				msgList.add(msg);
			}
			return msgList;
		} else {
			DBCursor cur = con
					.find(new BasicDBObject("handle", handle).append("templateName",
							new BasicDBObject("$regex", tempName)).append("pt",pt))
					.sort(new BasicDBObject("createDate", -1)).skip((pageNo - 1) * pageSize).limit(pageSize);
			while (cur.hasNext()) {
				Map<String, Object> map = (Map<String, Object>) cur.next();
				Message msg = Change.toBean(Message.class, map);
				msg.setMessageId(String.valueOf(map.get("_id")));
				msgList.add(msg);
			}
			return msgList;
		}

	}

	@Override
	public int findByHandleCount(int pageNo, int pageSize, boolean handle, String tempName,String pt) {
		DBCollection con = null;
		if (DbDepot.dbCons.get(pt+"msg") != null) {
			con = (DBCollection) DbDepot.dbCons.get(pt+"msg");
		} else {
			con = dbDepot.getDBCollection("msg",pt);
		}
		if (con == null) {
			return 0;
		}
		if (tempName == null || tempName.equals("")) {
			return (int) con.count(new BasicDBObject("handle", handle).append("pt",pt));
		} else {
			return (int) con.count(new BasicDBObject("handle", handle)
					.append("templateName", new BasicDBObject("$regex",tempName)).append("pt",pt));
		}
	}
}
