package com.palmnest.dao.estateserve.impl;

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

import org.springframework.stereotype.Repository;

import com.palmnest.application.Constants;
import com.palmnest.application.ElementConstant;
import com.palmnest.application.bean.Page;
import com.palmnest.dao.estateserve.PublicNoDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.estateserve.WxPublicno;
import com.palmnest.model.estateserve.WxPublicnoOrgRelation;


@Repository("publicNoDao")
public class PublicNoDaoImpl extends GenericDaoHibernate<Object, Integer> implements PublicNoDao {

	public PublicNoDaoImpl() {
		super(Object.class);
	}

	@Override
	public WxPublicno getByDevelopId(String developId) {
		String strHql = "from WxPublicno where developId='" + developId + "'";
		List<WxPublicno> list = getSession().createQuery(strHql).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Page queryPublicNoPage(int pageNo, String whereSQL) {
		return pagedQueryByJdbcTemplate(whereSQL, pageNo, Constants.LIMIT_PAGESIZE);
	}

	@Override
	public WxPublicno getWxPublicnoById(int id) {
		return (WxPublicno) getSession().get(WxPublicno.class, id);
	}

	@Override
	public List<WxPublicnoOrgRelation> getWxPublicnoOrgRelations(int publicnoId) {
		String strHQL = "from WxPublicnoOrgRelation where publicnoId=?";
		return getSession().createQuery(strHQL).setInteger(0, publicnoId).list();
	}

	@Override
	public List<Map<String, Object>> getWxPubOrgRelations(int publicnoId) {
		StringBuffer strSQL = new StringBuffer("SELECT t1.* ");
		strSQL.append(" FROM tb_wxpublicno t2 left JOIN  tr_wxpublicno_org t1 on t1.publicNoId = t2.id ");
		strSQL.append(" left join  tb_organization t3 on t3.id=t1.organizationId ");
		strSQL.append(" WHERE  t2.isEnabled = '" + ElementConstant.YESORNO_1 + "' and t3.isEnabled='" + ElementConstant.YESORNO_1 + "' AND t1.publicNoId=" + publicnoId);
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public void deleteWxPublicnoOrgRelation(int publicnoId) {
		String strSQL = "delete from tr_wxpublicno_org where publicNoId=" + publicnoId;
		getSession().createSQLQuery(strSQL).executeUpdate();
	}

	@Override
	public List<WxPublicno> getAllWxPublicno() {
		String strHQL = "from WxPublicno where isEnabled='" + ElementConstant.YESORNO_1 + "'";
		return getSession().createQuery(strHQL).list();
	}

	@Override
	public List<Map<String, Object>> getAllWxPublicno(String organizationIdsStr) {
		StringBuffer strSQL = new StringBuffer("SELECT DISTINCT(t1.publicNoId) as publicNoId,t2.developId,t2.`name`");
		strSQL.append(" FROM tr_wxpublicno_org t1 INNER JOIN tb_wxpublicno t2");
		strSQL.append(" WHERE t1.publicNoId = t2.id AND t2.isEnabled = '" + ElementConstant.YESORNO_1 + "' AND organizationId IN (" + organizationIdsStr + ")");
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public List<Map<String, Object>> getWxPublicnoOrgRelations(String developId) {
		StringBuffer strSQL = new StringBuffer("SELECT t3.id,t3.`name` as organizationName,t3.lng,t3.lat");
		strSQL.append(" FROM tb_wxpublicno t1 INNER JOIN tr_wxpublicno_org t2 ON t1.id = t2.publicNoId");
		strSQL.append(" AND t1.developId = '" + developId + "' and t1.isEnabled='1'");
		strSQL.append(" INNER JOIN tb_organization t3 on t2.organizationId=t3.ID and t3.isEnabled='1'");
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public WxPublicno getByApp(String appId, String appSecret) {
		String strHql = "from WxPublicno where appId=? and appSecret=?";
		List<WxPublicno> list = getSession().createQuery(strHql).setString(0, appId).setString(1, appSecret).list();
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<WxPublicno> getAllWxPublicnoWithOuterUrl() {
		String strHQL = "from WxPublicno where isEnabled='" + ElementConstant.YESORNO_1 + "'" + " and getAccessTokenUrl is not null";
		return getSession().createQuery(strHQL).list();
	}

	@Override
	public WxPublicno getByOrgClientCode(String clientCode) {
		StringBuffer strSQL = new StringBuffer("SELECT t2.* ");
		strSQL.append(" FROM tb_wxpublicno t2 left JOIN  tr_wxpublicno_org t1 on t1.publicNoId = t2.id ");
		strSQL.append(" left join  tb_organization t3 on t3.id=t1.organizationId ");
		strSQL.append(" WHERE  t2.isEnabled = '" + ElementConstant.YESORNO_1 + "' and t3.isEnabled='" + ElementConstant.YESORNO_1 + "' AND t3.mettingClient='" + clientCode + "'");
		List<WxPublicno> list = getSession().createSQLQuery(strSQL.toString()).addEntity(WxPublicno.class).list();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
}
