package com.rjconsultores.ventaboletos.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.rjconsultores.ventaboletos.dao.TipoTarifaPacoteDAO;
import com.rjconsultores.ventaboletos.entidad.Pacote;
import com.rjconsultores.ventaboletos.entidad.TipoTarifaPacote;

@Repository("tipoTarifaPacoteDAO")
public class TipoTarifaPacoteHibernateDAO extends GenericHibernateDAO<TipoTarifaPacote, Integer>
		implements TipoTarifaPacoteDAO {

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

	@SuppressWarnings("unchecked")
	@Override
	public List<TipoTarifaPacote> obtenerTodos() {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
		c.addOrder(Order.asc("desctipotarifa"));

		return c.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TipoTarifaPacote> buscaTipoTarifaPacote(Pacote pacote) {

		StringBuilder sb = new StringBuilder();
		sb.append(" select pt.tipoTarifaPacote ");
		sb.append(" from PacoteTarifa pt ");
		sb.append(" where pt.pacote.pacoteId = :pacoteId ");
		sb.append("   and pt.activo = 1 ");

		Query query = getSession().createQuery(sb.toString());
		query.setInteger("pacoteId", pacote.getPacoteId());

		return query.list();
	}
}
