package com.palmnest.dao.estateserve.impl;

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

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

import com.palmnest.dao.estateserve.DateTypeInfoDao;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.model.DateTypeInfo;
@Repository("dateTypeInfoDao")
public class DateTypeInfoDaoImpl extends GenericDaoHibernate<DateTypeInfo, Long> implements DateTypeInfoDao{
	public DateTypeInfoDaoImpl(){
		super(DateTypeInfo.class);
	}

	@Override
	public List<DateTypeInfo> getDateTypeInfoList(String year,String month) {
		String sql=" from DateTypeInfo where YEAR(date)='"+year+"' and MONTH(date)='"+month+"'";
		List<DateTypeInfo> list=getSession().createQuery(sql).list();
		return list;
	}

	@Override
	public DateTypeInfo getDateTypeInfoList(String date) {
		String sql=" from DateTypeInfo where date_format(date,'%Y-%m-%d')='"+date+"' ";
		List<DateTypeInfo> list=getSession().createQuery(sql).list();
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}

	@Override
	public List<DateTypeInfo> getDateTypeInfoList(String year, String month,
			String type) {
		String sql=" from DateTypeInfo where YEAR(date)='"+year+"' and MONTH(date)='"+month+"' ";
		if(StringUtils.isNotBlank(type)){
			sql+="and type in ("+type+")";
		}
		List<DateTypeInfo> list=getSession().createQuery(sql).list();
		return list;
	}

	@Override
	public List<DateTypeInfo> getDateTypeList(String year, String month,
			String type, String date) {
		String sql=" from DateTypeInfo where DATE_FORMAT(date,'%Y-%m-%d')>=DATE_FORMAT('"+date+"','%Y-%m-%d') and (YEAR(date)="+year+" and MONTH(date)<="+month+")";
		if(StringUtils.isNotBlank(type)){
			sql+="and type in ("+type+")";
		}
		sql+=" order by  date ";
		List<DateTypeInfo> list=getSession().createQuery(sql).list();
		return list;
	}

	@Override
	public List<Map<String, Object>> getWeekDateInfoList(String day, String type) {
		StringBuilder sql=new StringBuilder("");
		sql.append(" SELECT temp.* FROM ( SELECT * FROM tb_date_type WHERE DATE_FORMAT(date, '%Y-%m-%d') >= '"+day+"' LIMIT 7 ) temp  ");
		sql.append(" where 1=1 ");
		if(StringUtils.isNotBlank(type)){
			sql.append(" and temp.type in ("+type+") ");
		}
		return getJdbcTemplate().queryForList(sql.toString());
	}
}
