package com.palmnest.dao.estateserve.impl;

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

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

import com.palmnest.dao.estateserve.CountServerDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;

/**
 * 统计服务底层实现类（系统中所有统计方面的）
 *
 * @author yubing
 * @since 2017-08-10
 */
@Repository("countServerDao")
public class CountServerDaoImpl extends GenericDaoHibernate<Object, Integer> implements CountServerDao {

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

	@Override
	public List<Map<String, Object>> querySumRechargeGain(
			String organizationId, String month, String keyWords) {
		StringBuffer strSQL = new StringBuffer("SELECT t2.`name`,sum(t1.price) totalAmonut,t2.ID");
		strSQL.append(" FROM tb_recharge t1");
		strSQL.append(" INNER JOIN tb_organization t2 ON t1.organizationId = t2.ID");
		strSQL.append(" WHERE (t1.`status` = '0' OR t1.`status` = '1') AND t1.isEnabled = '1' AND t1.createTime LIKE '" + month + "%'");
		if (StringUtils.isNotBlank(organizationId)) {
			strSQL.append(" AND t1.organizationId=" + organizationId);
		}
		if (StringUtils.isNotBlank(keyWords)) {
			strSQL.append(" AND (t2.`name` like '%" + keyWords + "%' or t2.`code` like '%" + keyWords + "%')");
		}
		strSQL.append(" GROUP BY t1.organizationId ORDER BY totalAmonut DESC");
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public List<Map<String, Object>> queryCountOrganizationFans(
			String organizationId, String keyWords) {
		StringBuffer strSQL = new StringBuffer("SELECT t2.`name`,count(*) totalAmonut");
		strSQL.append(" FROM tb_wxuser t1");
		strSQL.append(" INNER JOIN tb_organization t2 ON t1.organizationId = t2.ID");
		strSQL.append(" where 1=1");
		if (StringUtils.isNotBlank(organizationId)) {
			strSQL.append(" and t1.organizationId=" + organizationId);
		}
		if (StringUtils.isNotBlank(keyWords)) {
			strSQL.append(" and (t2.`name` like '%" + keyWords + "%' or t2.`code` like '%" + keyWords + "%')");
		}
		strSQL.append(" GROUP BY t1.organizationId ORDER BY totalAmonut DESC");
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

	@Override
	public List<Map<String, Object>> queryCountOrganizationCodeChargeGain(
			String organizationId, String month, String keyWords) {
		StringBuffer strSQL = new StringBuffer("SELECT t2.`name`,sum(t1.price) totalAmonut,t2.ID,t1.createTime  from tb_recharge t1");
		strSQL.append(" INNER JOIN tb_organization t2 on t1.channelCode=t2.`code`");
		strSQL.append(" where (t1.`status` = '0' OR t1.`status` = '1') AND t1.isEnabled = '1' and t1.channelCode!='0000' and t1.channelCode is NOT NULL and t1.organizationId is NULL");
		strSQL.append(" AND t1.createTime LIKE '" + month + "%'");
		if (StringUtils.isNotBlank(organizationId)) {
			strSQL.append(" AND t2.ID=" + organizationId);
		}
		if (StringUtils.isNotBlank(keyWords)) {
			strSQL.append(" AND (t2.`name` like '%" + keyWords + "%' or t2.`code` like '%" + keyWords + "%')");
		}
		strSQL.append(" GROUP BY t2.ID ORDER BY totalAmonut DESC");
		return getJdbcTemplate().queryForList(strSQL.toString());
	}

}
