/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.dao.hibernate;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
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.ReservacionCtrlDAO;
import com.rjconsultores.ventaboletos.entidad.ReservacionCategoria;
import com.rjconsultores.ventaboletos.entidad.ReservacionCtrl;

/**
 *
 * @author Administrador
 */
@Repository("reservacionCtrlDAO")
public class ReservacionCtrlHibernateDAO
		extends GenericHibernateDAO<ReservacionCtrl, Integer>
		implements ReservacionCtrlDAO {

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

	@Override
	public List<ReservacionCtrl> obtenerTodos() {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));

		return c.list();
	}

	@Override
	public List<ReservacionCtrl> buscar(ReservacionCtrl reservacionCtrl) {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
		c.add(Restrictions.eq("descConfig", reservacionCtrl.getDescConfig()));

		return c.list();
	}

	@Override
	public List<ReservacionCtrl> buscarFechaTraslapa(ReservacionCtrl reservacionCtrlt) {

		Calendar fecinicio = Calendar.getInstance();
		fecinicio.setTime(reservacionCtrlt.getFecinicio());

		Calendar fecfinal = Calendar.getInstance();
		fecfinal.setTime(reservacionCtrlt.getFecfinal());
		String ids = "";
		for (ReservacionCategoria r : reservacionCtrlt.getReservacionCategoriaList()) {
			ids = ids + r.getCategoria().getCategoriaId() + " ,";
		}
		ids = ids.substring(0, ids.length() - 1);

		StringBuilder hql = new StringBuilder();
		hql.append(" select distinct rc from ReservacionCtrl rc  ");
		hql.append(" inner join rc.reservacionCategoriaList lsCat  ");
		hql.append(" where lsCat.categoria.categoriaId in (" + ids + ") ");
		hql.append(" and rc.activo = TRUE ");
		// vigencia
		hql.append("   AND (( rc.fecfinal between :fecfinal and :fecinicio ");
		hql.append("   OR rc.fecinicio between :fecfinal and :fecinicio ) ");
		hql.append(" OR  ( :fecfinal between rc.fecfinal and rc.fecinicio ");
		hql.append("   OR :fecinicio between rc.fecfinal and rc.fecinicio ) ");
		hql.append("   OR  (  rc.fecinicio <= :fecinicio and rc.fecfinal >= :fecfinal ) ");
		hql.append("   OR  ( :fecinicio <= rc.fecinicio and :fecfinal  >= rc.fecfinal ) ");
		hql.append("   OR  (  rc.fecinicio >= :fecinicio and rc.fecfinal <= :fecfinal ) ");
		hql.append("   OR  ( :fecinicio >= rc.fecinicio and :fecfinal  <= rc.fecfinal )) ");
		// vigencia

		Query query = getSession().createQuery(hql.toString());
		query.setDate("fecfinal", fecfinal.getTime());
		query.setDate("fecinicio", fecinicio.getTime());

		return query.list();

	}
}
