package com.palmnest.dao.notice.impl;

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

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.palmnest.application.bean.Page;
import com.palmnest.application.dto.WxtemplateUserDto;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.notice.WxtemplateMessageTypeDao;
import com.palmnest.model.notice.AppNoticeModel;
import com.palmnest.model.notice.WxtemplateMessageTypeModel;

@Repository("wxtemplateMessageTypeDao")
public class WxtemplateMessageTypeDaoImpl extends GenericDaoHibernate<WxtemplateMessageTypeModel, Integer> implements WxtemplateMessageTypeDao {

	public WxtemplateMessageTypeDaoImpl() {
		super(WxtemplateMessageTypeModel.class);

	}

	/**
	 * 查询微信模板配置
	 * longmeizhen
	 */
	@Override
	public Page getTemplateMessageTypeList(String orgIds, int wechatAccountId, int pageSize,
										   int pageNo) throws Exception {
		StringBuilder strHql = new StringBuilder("select t3.name as orgName,t.id,t.wechat_account_id,p.name pubName,t.name,t.templateid,DATE_FORMAT(t.createTime,'%Y-%m-%d') createTime, ");
		strHql.append(" t.creator,t.modifier,DATE_FORMAT(t.modifyTime,'%Y-%m-%d') modifyTime,t.isEnabled  from tb_wxtemplate_message_type t left join tb_wxpublicno p on t.wechat_account_id=p.id  ");
		strHql.append(" LEFT JOIN tr_wxpublicno_org t2 ON p.ID = t2.publicNoId");
		strHql.append(" LEFT JOIN tb_organization t3 on t2.organizationId=t3.ID");

		strHql.append(" where t.isEnabled='1'  ");
		if (wechatAccountId != 0) {//为0时查询所有的
			strHql.append(" and wechat_account_id=" + wechatAccountId + " ");
		}
		if (StringUtils.isNotBlank(orgIds)) {
			String[] orgStr = orgIds.split(";");
			String str = "";
			if (orgStr != null && orgStr.length > 0) {
				for (int i = 0; i < orgStr.length; i++) {
					str = str + orgStr[i] + ",";
				}
			}
			if (StringUtils.isNotEmpty(str)) {
				str = str.substring(0, str.length() - 1);
				strHql.append(" and t2.organizationId in (" + str + ") ");
			}

		}
		strHql.append(" order by t.createTime desc ");
		return pagedQueryByJdbcTemplate(strHql.toString(), pageNo, pageSize);
	}

	@Override
	public WxtemplateMessageTypeModel findBy(String templateid) {
		String sql = " from WxtemplateMessageTypeModel WHERE templateid = ? AND isEnabled=1";
		ArrayList<WxtemplateMessageTypeModel> list = (ArrayList<WxtemplateMessageTypeModel>) getSession().createQuery(sql).setString(0, templateid).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<Map<String,Object>> findByOrgIdAndDevelopId(Integer orgId,
			String developId) {
		StringBuilder strHql=new StringBuilder("");
		strHql.append(" SELECT DISTINCT w.id wxUserId,w.developId,u.id userId,w.openId ");
		strHql.append(" FROM tb_user u,tb_wxuser w ");
		strHql.append(" WHERE (u.wxUserId = w.id or (u.bind_openid=w.openId and u.bind_developid = w.developId) ) and u.isEnabled = 1  AND w.developId ='"+developId+"'  ");
		strHql.append(" AND u.organizationId = "+orgId+" ");
		strHql.append(" ORDER BY w.id ");
		return getJdbcTemplate().queryForList(strHql.toString());
	}
}
