/** 
 * @Title: GroupsDao.java 
 * @Package com.zhiwei.dao.impl 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author hero 
 * @date 2016年1月29日 上午10:37:24 
 * @version V1.0 
 */ 
package com.zhiwei.dao.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.zhiwei.DBTemplate.MongoDBTemplate;
import com.zhiwei.conf.Conf;
import com.zhiwei.dao.GroupsDao;
import com.zhiwei.weixin.pojo.Group;

/** 
 * @ClassName: GroupsDao 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author hero 
 * @date 2016年1月29日 上午10:37:24  
 */
public class GroupsDaoImpl extends MongoDBTemplate implements GroupsDao {
	
	private String collName = "groups";
	private static Logger logger = LoggerFactory.getLogger(TemplatesDaoImpl.class);
	public GroupsDaoImpl()
	{
		super();
		super.setDbName(Conf.dbName);
		super.setCollName(collName);
	}
	@Override
	public int addGroup(Group group, String appId) {
		try {
			DBObject saveDoc = new BasicDBObject();
			saveDoc.put("_id", group.getId()+"_"+appId);
			saveDoc.put("appid", appId);
			saveDoc.put("groupid", group.getId());
			saveDoc.put("groupName", group.getGroupName());
			saveDoc.put("groupCount", group.getCount());
			this.getReadColl().insert(saveDoc);
			return 1;
			
		} catch (Exception e) {
			logger.error("向数据库中添加模版消息失败:【{}】",e.getMessage());
			return 0;
		}
	}

	@Override
	public int getGroupId(String appId, String groupName) {
		
		int groupid = -100;
		DBObject query = new BasicDBObject();
		query.put("appid", appId);
		query.put("groupName", groupName);
		try {
			DBObject doc = this.getReadColl().findOne(query);
			if(null != doc)
			{
				groupid = Integer.valueOf(doc.get("groupid").toString());
			}
		} catch (Exception e) {
			logger.error("从数据查询模版消息长id时连接数据库超时:【{}】，【{}】",this.getReadColl().getDB().getMongo().getAddress(),e.getMessage());
			groupid = -100;
		}
		
		return groupid;
	}

}
