package com.palmnest.dao.notice.impl;

import java.math.BigInteger;
import java.util.Map;

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

import com.palmnest.application.bean.Page;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.notice.EmailSendDao;
import com.palmnest.model.notice.EmailSendModel;

@Repository("emailSendDao")
public class EmailSendDaoImpl extends GenericDaoHibernate<EmailSendModel, Integer> implements EmailSendDao {
	public EmailSendDaoImpl() {
		super(EmailSendModel.class);

	}

	@Override
	public Page getDataList(Map<String, Object> map, int pageNo, int pageSize)
			throws Exception {

		StringBuffer sbf = new StringBuffer();
		sbf.append(" select em.id,recipient_email,DATE_FORMAT(send_time,'%Y/%m/%d %T') send_time ");
		sbf.append(" ,ssender_email sendEmail,total,em.state,email_theme,email_centent,content_txt contentTxt ");
		sbf.append(" ,emailaccount_id,attach_ment,ifnull(apus.USERNAME,approval_user) approval_user,DATE_FORMAT(approval_time,'%Y/%m/%d %T') approval_time ");
		sbf.append(" ,DATE_FORMAT(em.createTime,'%Y/%m/%d %T') createTime,em.creator,em.modifier ");
		sbf.append(" ,DATE_FORMAT(em.modifyTime,'%Y/%m/%d %T') modifyTime,em.isEnabled,aus.FULL_NAME fullName,aus.USERNAME ");
		sbf.append(" ,'' roleId,0 isApproval,0 isPublish,'' roleName,em.approval_type approvalType,em.orgId ");
		sbf.append(" from tb_email_send em  ");
		sbf.append(" left join tb_app_user aus on em.creator=aus.id ");
		sbf.append(" left join tb_app_user apus on em.approval_user=apus.id ");
		sbf.append(" where 1=1 ");

		if (map.get("c_beginTime") != null) {
			sbf.append(" and createTime>'");
			sbf.append(map.get("c_beginTime").toString() + " 00:00:00'");
		}
		if (map.get("c_endTime") != null) {
			sbf.append(" and createTime<'");
			sbf.append(map.get("c_endTime").toString() + " 23:59:59'");
		}
		if (map.get("a_beginTime") != null) {
			sbf.append(" and approval_time>'");
			sbf.append(map.get("a_beginTime").toString() + " 00:00:00'");
		}
		if (map.get("a_endTime") != null) {
			sbf.append(" and approval_time<'");
			sbf.append(map.get("a_endTime").toString() + " 23:59:59'");
		}
		if (map.get("state") != null) {
			sbf.append(" and state=" + map.get("state").toString());
		}
		if (map.get("orgIds") != null) {
			sbf.append(" and orgId in(" + map.get("orgIds") + ")");
		}
		//多级审核下的过滤条件
		if(null != map.get("approvalType")){
			sbf.append(" and  em.approval_type="+map.get("approvalType"));
		}
		sbf.append(" ORDER BY state,createTime DESC");
		return pagedQueryByJdbcTemplate(sbf.toString(), pageNo, pageSize);
	}


	@Override
	public Page getEmailUser(String orgIds)
			throws Exception {

		StringBuffer sbf = new StringBuffer();
		sbf.append("select * from tb_email_config where 1=1 and isEnabled='1' ");
		if (StringUtils.isNotEmpty(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);
				sbf.append(" and orgId in (" + str + ")");
			}

		}
		return pagedQueryByJdbcTemplate(sbf.toString(), 0, Integer.MAX_VALUE);
	}

	@Override
	public Integer getTotal(String orgIds) {
		String sql = "SELECT COUNT(t.id) total" +
				" FROM tb_email_send t where state = 0 AND isEnabled = 1 AND orgId IN (" + orgIds + ")";
		return ((BigInteger) getSession().createSQLQuery(sql).uniqueResult()).intValue();
	}
}
