package com.palmnest.dao.notice.impl;

import java.math.BigInteger;
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.ElementConstant;
import com.palmnest.application.bean.Page;
import com.palmnest.application.dto.output.NoticeUserOutDto;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.notice.WxtemplateMessageDao;
import com.palmnest.model.notice.WxtemplateMessageConfigModel;
import com.palmnest.model.notice.WxtemplateMessageModel;
import com.palmnest.model.notice.WxtemplateMessageTypeModel;

@Repository("wxtemplateMessageDao")
public class WxtemplateMessageDaoImpl extends GenericDaoHibernate<WxtemplateMessageModel, Integer> implements WxtemplateMessageDao {

	public WxtemplateMessageDaoImpl() {
		super(WxtemplateMessageModel.class);

	}

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

		StringBuffer sbf = new StringBuffer();
		sbf.append("  select wm.id, wechat_template_id, open_id, title, url, state, approval_user  ");
		sbf.append("  , DATE_FORMAT(approval_time,'%Y/%m/%d %T') approval_time  ");
		sbf.append("  , DATE_FORMAT(wm.createTime,'%Y/%m/%d %T') createTime, wm.creator, wm.modifier  ");
		sbf.append("  , DATE_FORMAT(wm.modifyTime,'%Y/%m/%d %T') modifyTime, wm.isEnabled  ");
		sbf.append("  , total ,aus.FULL_NAME fullName,aus.USERNAME,wm.status_str,wm.fail_str   ");
		sbf.append("  ,wm.approval_type approvalType,wm.orgId ");
		sbf.append("  from tb_wxtemplate_message  wm  ");
		sbf.append("  left join tb_app_user aus on wm.creator=aus.id  ");
		sbf.append("  where 1=1  and wm.isEnabled=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("orgId") != null) {
			sbf.append(" and orgId="+map.get("orgId").toString());
		}
		if (map.get("orgIds") != null) {
			sbf.append(" and orgId in (" + map.get("orgIds") + ")");
		}
		if (map.get("receiveType") != null) {
			sbf.append(" and receive_type=" + map.get("receiveType").toString());
		}
		
		//多级审核下的过滤条件
		if(null != map.get("approvalType")){
			sbf.append(" and  wm.approval_type="+map.get("approvalType"));
		}
		
		sbf.append(" ORDER BY state,createTime DESC");
		return pagedQueryByJdbcTemplate(sbf.toString(), pageNo, pageSize);
	}

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

		StringBuffer strSQL = new StringBuffer("SELECT t1.ID,t1.`name`,t1.developType,t1.url,t1.creator,t1.createTime,group_concat(t3.`name`) as organizationName");
		strSQL.append(" from tb_wxpublicno t1");
//		strSQL.append(" INNER JOIN tr_wxpublicno_org t2 ON t1.ID = t2.publicNoId");
//		strSQL.append(" INNER JOIN tb_organization t3 on t2.organizationId=t3.ID");
		strSQL.append(" LEFT JOIN tr_wxpublicno_org t2 ON t1.ID = t2.publicNoId");
		strSQL.append(" LEFT JOIN tb_organization t3 on t2.organizationId=t3.ID");
		strSQL.append(" where 1=1");
		strSQL.append(" and t1.isEnabled='" + ElementConstant.YESORNO_1 + "'");
		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);
				strSQL.append(" and t2.organizationId in (" + str + ") ");
			}

		}
		strSQL.append(" GROUP BY t1.id");
		return pagedQueryByJdbcTemplate(strSQL.toString(), 0, Integer.MAX_VALUE);
	}

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

		StringBuffer strSQL = new StringBuffer("SELECT * from tb_wxpublicno where id ='" + id + "' ");
		strSQL.append(" AND 1=1");
		return pagedQueryByJdbcTemplate(strSQL.toString(), 0, Integer.MAX_VALUE);
	}

	@Override
	public Page getAllTempByWxPub(int id, int pageNo, int pageSize)
			throws Exception {

		StringBuffer strSQL = new StringBuffer(" SELECT t3.id, t3.type, t3.organizationId, t3.templateId, t3.templateContent,DATE_FORMAT(t3.createTime,'%Y/%m/%d %T') createTime,t3.creator ");
		strSQL.append(" FROM tb_wxpublicno t1 ");
		strSQL.append(" LEFT JOIN tr_wxpublicno_org t2 ON t2.publicNoId = t1.id ");
		strSQL.append(" LEFT JOIN tb_template_library t3 ON t3.organizationId = t2.organizationId ");
		strSQL.append(" WHERE t1.id=" + id + " ");
		strSQL.append(" AND 1=1 ORDER BY t3.id");
//		StringBuffer strSQL=new StringBuffer(" SELECT t3.id, t3.type, t3.organizationId, t3.templateId, t3.templateContent,DATE_FORMAT(t3.createTime,'%Y/%m/%d %T') createTime,t3.creator,t4.title ");
//		strSQL.append(" FROM tb_wxpublicno t1 ");
//		strSQL.append(" LEFT JOIN tr_wxpublicno_org t2 ON t2.publicNoId = t1.id ");
//		strSQL.append(" LEFT JOIN tb_template_library t3 ON t3.organizationId = t2.organizationId ");
//		strSQL.append(" LEFT JOIN tb_wxtemplate_message t4 ON t4.wechat_template_id = t3.id ");
//		strSQL.append(" WHERE t1.id="+id+" ");
//		strSQL.append(" AND 1=1 ORDER BY t3.id");


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

	@Override
	public ArrayList<WxtemplateMessageTypeModel> getWxtemplateMessageTypeModelList(int wxId)
			throws Exception {

		String sql = " from WxtemplateMessageTypeModel WHERE wechat_account_id = ? AND isEnabled=1";
		ArrayList<WxtemplateMessageTypeModel> list = (ArrayList<WxtemplateMessageTypeModel>) getSession().createQuery(sql).setInteger(0, wxId).list();

		return list;
	}

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

//		StringBuffer strSQL=new StringBuffer("SELECT t3.* FROM tb_wxtemplate_message ");
//		strSQL.append(" LEFT JOIN tr_wxpublicno_org t2 ON t2.publicNoId=t1.ID ");
//		strSQL.append(" LEFT JOIN tb_template_library t3 ON t3.organizationId=t2.organizationId ");
//		strSQL.append(" WHERE t1.ID='"+id+"'");
//		strSQL.append(" AND 1=1 ");
		StringBuffer strSQL = new StringBuffer(" SELECT t1.ID,t1.developId,t1.name,t1.appId, t1.appSecret, t1.url, t1.developType, t1.serviceType, t1.wxSecret,t1.creator, DATE_FORMAT(t1.createTime,'%Y/%m/%d %T') createTime, t1.modifier,DATE_FORMAT(t1.modifyTime,'%Y/%m/%d %T') modifyTime, t1.isEnabled, t1.mchId, t1.wxApiKey  FROM tb_wxpublicno t1 ");
		strSQL.append(" INNER JOIN tr_wxpublicno_org t2 ON t2.publicNoId=t1.id ");
		strSQL.append(" INNER JOIN tb_template_library t3 ON t3.organizationId= t2.organizationId ");
		strSQL.append(" INNER JOIN tb_wxtemplate_message t4 ON t4.wechat_template_id = t3.ID ");
		strSQL.append(" WHERE t4.ID= " + id + " AND 1=1");


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


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

		StringBuffer strSQL = new StringBuffer(" SELECT t1.* FROM tb_wxtemplate_message t1");
		strSQL.append(" LEFT JOIN tb_template_library t2 ON t1.wechat_template_id=t2.ID ");
		strSQL.append(" LEFT JOIN tr_wxpublicno_org t3 ON t2.organizationId = t3.organizationId ");
		strSQL.append(" LEFT JOIN tb_wxpublicno t4 ON t4.ID = t3.publicNoId ");
		strSQL.append(" WHERE t4.id = " + id + " AND 1=1");

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

//	@Override
//	public WxtemplateMessageModel getWxtemplateMessageModelByWechat_template_id(int wechat_template_id)
//			throws Exception {
//
//		String sql="from WxtemplateMessageModel where isEnabled=1 AND wechat_template_id=?";
//		WxtemplateMessageModel temp=(WxtemplateMessageModel) getSession().createQuery(sql).setInteger(0, wechat_template_id).uniqueResult();
//		return temp;
//
//	}

	@Override
	public Page getDataListWithWxPub(int pageNo, int pageSize)
			throws Exception {

		StringBuffer sbf = new StringBuffer();
		sbf.append(" SELECT t1.id, t1.wechat_template_id, t1.open_id, t1.title, t1.url, t1.state, t1.approval_user, DATE_FORMAT(t1.approval_time,'%Y/%m/%d %T') approval_time, DATE_FORMAT(t1.createTime,'%Y/%m/%d %T') createTime, t1.creator, t1.modifier, DATE_FORMAT(t1.modifyTime,'%Y/%m/%d %T') modifyTime, t1.isEnabled, t1.total,t4.name");
		sbf.append(" FROM tb_wxtemplate_message t1");
		sbf.append(" INNER JOIN tb_template_library t2 ON t1. wechat_template_id=t2.id");
		sbf.append(" INNER JOIN tr_wxpublicno_org t3 ON t2.organizationId =t3.organizationId");
		sbf.append(" INNER JOIN tb_wxpublicno t4 ON t3.publicNoId=t4.id");
		sbf.append(" ORDER BY t1.id,t2.organizationId,t4.name DESC");


		return pagedQueryByJdbcTemplate(sbf.toString(), pageNo, pageSize);
	}

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

	@Override
	public List<Map<String,Object>> getUserlistByUserIds(String userIds) {
		StringBuilder sql=new StringBuilder("");
		sql.append("  SELECT u.id,userName name,userMobile mobile,v.`name` virtName  ");
		sql.append("  FROM tb_user u   ");
		sql.append("  LEFT JOIN tb_virtualarchitecture v ON v.id = u.virtualArchId  ");
		sql.append("  WHERE u.id IN ("+userIds+")  ");
//		List<NoticeUserOutDto> list=getSession().createSQLQuery(sql.toString()).list();
		return getJdbcTemplate().queryForList(sql.toString());
	}


}
