package com.rjconsultores.ventaboletos.dao.hibernate;

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.PacoteItemDAO;
import com.rjconsultores.ventaboletos.entidad.ItemAdicional;
import com.rjconsultores.ventaboletos.entidad.Pacote;
import com.rjconsultores.ventaboletos.entidad.PacoteItem;

@Repository("pacoteItemDAO")
public class PacoteItemHibernateDAO extends GenericHibernateDAO<PacoteItem, Integer> implements PacoteItemDAO {

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

	@Override
	public PacoteItem buscaPacoteItem(Pacote pacote, ItemAdicional item) {

		StringBuilder sb = new StringBuilder();
		sb.append(" from PacoteItem pi ");
		sb.append(" where pi.pacote.pacoteId = :pacoteId ");
		sb.append("   and pi.itemAdicional.itemadicionalId = :itemadicionalId ");
		sb.append("   and pi.activo = 1 ");

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

		return (PacoteItem) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PacoteItem> buscaItensPacote(Pacote pacote) {

		StringBuilder sb = new StringBuilder();
		sb.append(" from PacoteItem pi ");
		sb.append(" where pi.pacote.pacoteId = :pacoteId ");
		sb.append("   and pi.activo = 1 ");

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

		return query.list();
	}

}
