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.TemplatesDao;

/** 
 * @ClassName: TemplatesDaoImpl 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author hero 
 * @date 2016年1月29日 上午10:16:07  
 */
public class TemplatesDaoImpl extends MongoDBTemplate implements TemplatesDao{
	private String collName = "templates";
	private static Logger logger = LoggerFactory.getLogger(TemplatesDaoImpl.class);
	public TemplatesDaoImpl()
	{
		super();
		super.setDbName(Conf.dbName);
		super.setCollName(collName);
	}
	
	
	@Override
	public int addTemplates(String template_id, String appid, String template_id_short) {
		try {
			DBObject saveDoc = new BasicDBObject();
			saveDoc.put("_id", template_id);
			saveDoc.put("appid", appid);
			saveDoc.put("template_id_short", template_id_short);
			this.getReadColl().insert(saveDoc);
			return 1;
			
		} catch (Exception e) {
			logger.error("向数据库中添加模版消息失败",e.getMessage());
			return 0;
		}
	}

	@Override
	public String getTemplate_id(String template_id_short, String appid) {
		
		String template_id = null;
		DBObject query = new BasicDBObject();
		query.put("template_id_short", template_id_short);
		query.put("appid", appid);
		try {
			DBObject doc = this.getReadColl().findOne(query);
			if(null != doc)
			{
				template_id = doc.get("_id").toString();
			}
		} catch (Exception e) {
			logger.error("从数据查询模版消息长id时连接数据库超时",e.getMessage());
			template_id = null;
		}
		
		return template_id;
	}
}
