package com.palmnest.dao.notice.impl;

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

import cn.superdesk.libs.common.json.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import com.palmnest.application.core.util.StringUtil;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.notice.WxMassmessageSendDao;
import com.palmnest.model.notice.WxMassmessageSendModel;
@Slf4j
@Repository("wxMassmessageSendDao")
public class WxMassmessageSendDaoImpl extends GenericDaoHibernate<WxMassmessageSendModel, Integer> implements WxMassmessageSendDao {
	public WxMassmessageSendDaoImpl() {
		super(WxMassmessageSendModel.class);

	}

//	@Override
//	public List<Map<String, Object>> getUserByOrgId(String orgIds, String nameStr) {
//		StringBuilder sql = new StringBuilder();
//		sql.append(" SELECT DISTINCT u.id userId, u.userName NAME, u.userMobile mobile, u.organizationId orgId, o. NAME orgName, wu.openId openId FROM tb_user u ");
//		sql.append(" LEFT JOIN tb_wxuser wu ON wu.id = u.wxUserId LEFT JOIN tb_organization o ON o.id = u.organizationId  ");
//		sql.append(" left join tr_wxpublicno_org wg on wg.organizationId=o.id ");
//		sql.append(" left join tb_wxpublicno wp on wp.id=wg.publicNoId ");
//		sql.append(" WHERE u.isEnabled = 1 ");
//		if (StringUtil.isNotEmpty(orgIds)) {
//			sql.append(" AND u.organizationId IN (" + orgIds + ") ");
//		}
//		sql.append("  AND wu.openId IS NOT NULL AND u.userType = 0  ");
//		if (StringUtil.isNotEmpty(nameStr)) {
//			sql.append("  AND  (u.userName like '%" + nameStr + "%' or o. NAME like '%" + nameStr + "%' or u.userMobile like '%" + nameStr + "%') ");
//		}
//		sql.append(" and wu.developId=wp.developId");
//		sql.append("  ORDER BY u.organizationId, u.id  ");
//		return getJdbcTemplate().queryForList(sql.toString());
//	}
	
	
	@Override
	public List<Map<String, Object>> getUserByOrgId(String orgIds, String nameStr) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT DISTINCT u.id userId, ( CASE WHEN u.userType =- 1 AND u.userName IS NULL THEN '游客' WHEN u.userName IS NOT NULL THEN u.userName ELSE '' END ) name ");
		sql.append(" , u.userMobile mobile, u.organizationId orgId, o. NAME orgName, ifnull(u.bind_openid, u.openId) openId, ifnull( u.bind_developid, wu.developId ) developId  ");
		sql.append(" FROM tb_user u  ");
		sql.append(" LEFT JOIN tb_wxuser wu ON wu.openId = u.openId LEFT JOIN tb_organization o ON o.id = u.organizationId ");
		sql.append(" WHERE u.isEnabled = 1  ");
		if (StringUtil.isNotEmpty(orgIds)) {
			sql.append(" AND u.organizationId IN (" + orgIds + ") ");
		}
		sql.append("  AND ( u.openId IS NOT NULL AND u.openId <> '' ) AND wu.developId IS NOT NULL  ");
		if (StringUtil.isNotEmpty(nameStr)) {
			sql.append("  AND  (u.userName like '%" + nameStr + "%' or o. NAME like '%" + nameStr + "%' or u.userMobile like '%" + nameStr + "%') ");
		}
		sql.append("  ORDER BY u.organizationId, u.id  ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getUserByDevelopid(String developid) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select wu.openId from tb_wxuser wu where developId='" + developid + "' ");
		return getJdbcTemplate().queryForList(sql.toString());
	}

	@Override
	public List<Map<String, Object>> getByUser(String  userId) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select id,username,openId,bind_openid bindOpenid from  tb_user where id in("+userId+")");
		log.info("getByUser:" + JsonUtils.toJson(sql.toString()));
		return getJdbcTemplate().queryForList(sql.toString());
	}
}
