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 org.springframework.util.CollectionUtils;

import com.palmnest.application.bean.Page;
import com.palmnest.application.core.util.DateUtils;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.input.SMSSearchDto;
import com.palmnest.dao.estateserve.SmsAuthCodeDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.estateserve.SmsAuthCode;

@Repository("smsAuthCodeDao")
public class SmsAuthCodeDaoImpl extends GenericDaoHibernate<SmsAuthCode, Integer> implements SmsAuthCodeDao {

	public SmsAuthCodeDaoImpl() {
		super(SmsAuthCode.class);
	}

	@Override
	public SmsAuthCode getSmsAuthCode(String userMobile, String code,
									  String type, int minute) {
		StringBuffer strSQL = new StringBuffer("SELECT id FROM tb_sms_authcode WHERE usermobile='" + userMobile + "' and type='" + type + "' and isEnabled='1'");

		if (StringUtils.isNotBlank(code)) {
			strSQL.append(" and code='" + code + "'");
		}
		//无时间限制
		if (minute != 0) {
			strSQL.append(" and date_add(createTime, INTERVAL " + minute + " MINUTE) >= '" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss") + "'");
		}
		strSQL.append(" ORDER BY createTime DESC");
		List<Map<String, Object>> list = getJdbcTemplate().queryForList(strSQL.toString());
		if (!CollectionUtils.isEmpty(list)) {
			Integer codeId = (Integer) list.get(0).get("id");
			return get(codeId);
		}
		return null;
	}

	/**
	 * 重新发送了验证码后，之前的验证码皆设置成无效的
	 *
	 * @param userMobile
	 * @return
	 */
	@Override
	public int updateSmsAuthCode(String userMobile) {
		String mobile = "";
		if (StringUtils.isNotEmpty(userMobile)) {
			String[] mobiles = userMobile.split(",");
			for (int i = 0; i < mobiles.length; i++) {
				mobile = mobile + "'" + mobiles[i] + "',";
			}
		}
		if (StringUtils.isNotEmpty(mobile)) {
			mobile = mobile.substring(0, mobile.length() - 1);
			StringBuilder sql = new StringBuilder("");
			sql.append(" update tb_sms_authcode set isEnabled='0' where  usermobile in (" + mobile + ") ");
			return getSession().createSQLQuery(sql.toString()).executeUpdate();
		} else {
			return 0;
		}

	}

	@Override
	public Page findList(SMSSearchDto dto) {
		StringBuilder sql = new StringBuilder();
		sql.append("  select org.name orgName,sau.userMobile mobile,sau.type,sau.content  ");
		sql.append("  ,1 status,date_format(sau.createTime,'%Y-%m-%d %T') sendTime   ");
		sql.append("  from tb_sms_authcode sau  ");
		sql.append(" left join tb_organization org on org.id=sau.orgId   ");
		sql.append("  where 1=1  ");
		if (StringUtil.isNotEmpty(dto.getOrgId())) {
			sql.append(" and sau.orgId in (" + dto.getOrgId() + ")   ");
		}

		if (StringUtil.isNotEmpty(dto.getMobile())) {
			sql.append("  and sau.userMobile like '%" + dto.getMobile() + "%'  ");
		}

		if (StringUtil.isNotEmpty(dto.getType()) && !"0".equals(dto.getType())) {
			sql.append("  and sau.type=" + dto.getType() + "  ");
		}
		if (StringUtil.isNotEmpty(dto.getStatus())) {
			sql.append(" and status=" + dto.getStatus() + "   ");
		}
		if (StringUtil.isNotEmpty(dto.getBeginTime())) {
			String beginTime = dto.getBeginTime() + " 00:00:00 ";
			if (StringUtils.isNotEmpty(dto.getEndTime())) {
				String endTime = dto.getEndTime() + " 23:59:59 ";
				sql.append(" and (sau.createTime>='" + beginTime + "' and sau.createTime<='" + endTime + "' ) ");
			} else {
				sql.append(" and  sau.createTime>='" + beginTime + "' ");
			}
		} else if (StringUtil.isNotEmpty(dto.getEndTime())) {
			String endTime = dto.getEndTime() + " 23:59:59 ";
			sql.append(" and sau.createTime<='" + endTime + "'  ");
		}

		sql.append("  order by sau.createTime desc ");
		return pagedQueryByJdbcTemplate(sql.toString(), dto.getPageNo(), dto.getPageSize());
	}
}
