package com.rjconsultores.ventaboletos.dao.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.rjconsultores.ventaboletos.dao.DescontoComissaoDAO;
import com.rjconsultores.ventaboletos.entidad.DescontoComissao;
import com.rjconsultores.ventaboletos.utilerias.DateUtil;

@Repository("descontoComissaoDAO")
public class DescontoComissaoHibernateDAO extends GenericHibernateDAO<DescontoComissao, Long> implements DescontoComissaoDAO {

	@Autowired
	public DescontoComissaoHibernateDAO(@Qualifier("sessionFactory") SessionFactory factory) {
		setSessionFactory(factory);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DescontoComissao> buscaDescontoComissao(Integer puntoVentaId, Integer empresaId, Date inicioPeriodo, Date fimPeriodo) {

		StringBuilder sql = new StringBuilder();
		sql.append("from DescontoComissao dc ");
		sql.append("where activo = 1 ");
		sql.append("      and dc.puntoVenta.puntoventaId = :puntoventaId ");
		sql.append("      and dc.empresa.empresaId = :empresaId ");
		sql.append("      and dc.datainicial <= TO_DATE(:datainicial,'DD/MM/YYYY HH24:MI') ");
		sql.append("      and (dc.datafinal >= TO_DATE(:datafinal,'DD/MM/YYYY HH24:MI') or dc.datafinal is null)");

		Query query = getSession().createQuery(sql.toString());
		query.setInteger("puntoventaId", puntoVentaId);
		query.setInteger("empresaId", empresaId);
		query.setString("datainicial", DateUtil.getStringDate(inicioPeriodo, "dd/MM/yyyy HH:mm"));
		query.setString("datafinal", DateUtil.getStringDate(fimPeriodo, "dd/MM/yyyy HH:mm"));

		return query.list();
	}

}
