package org.cs.mgr.user.dao.impl;

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

import org.cs.core.dao.impl.BaseDao;
import org.cs.mgr.user.dao.ISmsDao;
import org.cs.mgr.user.model.SMS;
import org.springframework.stereotype.Repository;

@Repository
public class SmsDao extends BaseDao<SMS> implements ISmsDao {
	
	@SuppressWarnings("unchecked")
	@Override
	public SMS getSMSByPhone(String phone, String code, int type) {
		String hql = "from SMS where phone=:phone and code=:code and type=:type";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("phone", phone);
		params.put("code", code);
		params.put("type", type);
		List<SMS> list = (List<SMS>)findByHql(hql, params);
        if (list!=null && list.size() > 0) {
        	return list.get(0);
        }
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public SMS getSMSByPhone(String phone, int type) {
		String hql = "from SMS where phone=:phone and type=:type order by create_date desc";
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("phone", phone);
		params.put("type", type);
		List<SMS> list = (List<SMS>)findByHql(hql, params);
        if (list!=null && list.size() > 0) {
        	return list.get(0);
        }
		return null;
	}

	@Override
	public int countByDay(String phone) {
		String sql = "SELECT COUNT(1) FROM app_sms WHERE phone=? AND to_char(create_date,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd')";
		return jdbcTemplate.queryForObject(sql,Integer.class, phone);
	}

}
