package com.palmnest.dao.notice.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.palmnest.application.bean.Page;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.notice.WxtemplateMessageConfigDao;
import com.palmnest.model.notice.WxtemplateMessageConfigModel;

@Repository("wxtemplateMessageConfigDao")
public class WxtemplateMessageConfigDaoImpl extends GenericDaoHibernate<WxtemplateMessageConfigModel, Integer> implements WxtemplateMessageConfigDao {

	public WxtemplateMessageConfigDaoImpl() {
		super(WxtemplateMessageConfigModel.class);

	}

	@Override
	public void delByTemplateMessageId(Integer templateMessageId) {

		this.getSession().createQuery("delete from tb_wxtemplate_message_config  where wechat_template_msg_id =(:ids)").setParameter("ids", templateMessageId).executeUpdate();
	}

	@Override
	public void delByTemplateId(Integer templateMessageId) throws Exception {

//		this.getSession().createQuery("delete tb_wxtemplate_message_config  where wechat_template_id ="+templateMessageId+" ").executeUpdate();
		String strSql = "delete from tb_wxtemplate_message_config  where wechat_template_id =" + templateMessageId;
		getSession().createSQLQuery(strSql).executeUpdate();

	}

	@Override
	public Page findAll(int id)
			throws Exception {

		StringBuffer strSQL = new StringBuffer("SELECT id, wechat_template_id, name, param_key,value, wechat_template_msg_id, DATE_FORMAT(createTime,'%Y/%m/%d %T') createTime, creator, modifier, DATE_FORMAT(modifyTime,'%Y/%m/%d %T')  modifyTime, isEnabled from tb_wxtemplate_message_config where wechat_template_id =" + id);
		strSQL.append(" AND 1=1");
		return pagedQueryByJdbcTemplate(strSQL.toString(), 0, Integer.MAX_VALUE);

	}


	@Override
	public Page findTemplateByTempIdFromLibrary(int tempId)
			throws Exception {

		StringBuffer strSQL = new StringBuffer("SELECT ID, type, organizationId, templateId, templateContent, DATE_FORMAT(createTime,'%Y/%m/%d %T') createTime, creator from tb_template_library where id =" + tempId);
		strSQL.append(" AND 1=1");
		return pagedQueryByJdbcTemplate(strSQL.toString(), 0, Integer.MAX_VALUE);

	}


	@Override
	public Page findTemplateByTempId(int tempId)
			throws Exception {

		StringBuffer strSQL = new StringBuffer("SELECT id, wechat_template_id, name, param_key,value, wechat_template_msg_id, DATE_FORMAT(createTime,'%Y/%m/%d %T') createTime, creator, modifier, DATE_FORMAT(modifyTime,'%Y/%m/%d %T')  modifyTime, isEnabled from tb_wxtemplate_message_config where wechat_template_id =" + tempId);
		strSQL.append(" AND 1=1");
		return pagedQueryByJdbcTemplate(strSQL.toString(), 0, Integer.MAX_VALUE);
	}

	@Override
	public List<WxtemplateMessageConfigModel> findTemplateByTempIdAndTempMsgId(int tempId, int tempMsgId)
			throws Exception {

		String sql = "from WxtemplateMessageConfigModel where wechat_template_id =? AND wechat_template_msg_id =? ";
		List<WxtemplateMessageConfigModel> list = getSession().createQuery(sql).setInteger(0, tempId).setInteger(1, tempMsgId).list();
		return list;
	}

	@Override
	public List<WxtemplateMessageConfigModel> findTemplateByTempIdDefault(int tempId)
			throws Exception {

		String sql = "from WxtemplateMessageConfigModel where wechat_template_id =? ";
		List<WxtemplateMessageConfigModel> list = getSession().createQuery(sql).setInteger(0, tempId).list();
		return list;
	}

	@Override
	public List<WxtemplateMessageConfigModel> getWxTemplConfigByTypeId(
			int typeId, int msgId) throws Exception {
		String strHQL = "from WxtemplateMessageConfigModel where  wechat_template_id=?  AND isEnabled=1 AND wechat_template_msg_id=?";
		List<WxtemplateMessageConfigModel> list = getSession().createQuery(strHQL).setInteger(0, typeId).setInteger(1, msgId).list();
		return list;
	}

	@Override
	public List<WxtemplateMessageConfigModel> getWxTemplConfigByTypeId(
			int typeId) throws Exception {
		String strHQL = "from WxtemplateMessageConfigModel where  wechat_template_id=?  AND isEnabled=1 ";
		List<WxtemplateMessageConfigModel> list = getSession().createQuery(strHQL).setInteger(0, typeId).list();
		return list;
	}
}
	


