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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.PricingDiaDAO;
import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingDia;

/**
 * 
 * @author Rafius
 */
@Repository("pricingDiaDAO")
public class PricingDiaHibernateDAO extends GenericHibernateDAO<PricingDia, Integer>
		implements PricingDiaDAO {

	private static final Logger log = LogManager.getLogger(PricingDiaHibernateDAO.class);

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

	@Override
	public List<PricingDia> obtenerTodos() {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq(ACTIVO, Pricing.ATIVO));

		return c.list();
	}

	public List<PricingDia> buscarTraslapa(PricingDia pricingDia) {
		Date horarioinicio = pricingDia.getHorarioinicio();
		Date horariofin = pricingDia.getHorariofin();

		SimpleDateFormat sf = new SimpleDateFormat("HHmm");

		StringBuilder hql = new StringBuilder();
		hql.append("select pv from PricingDia pv ");
		hql.append(" where pv.activo = 1 ");
		hql.append(" and pricing.pricingId = ").append(pricingDia.getPricing().getPricingId());
		// hql.append(" and ( 1=1 or ");
		// if (pricingDia.getIndlunes()) {
		// hql.append(" pv.indlunes = ").append(pricingDia.getIndlunes());
		// }
		// if (pricingDia.getIndmartes()) {
		// hql.append(" or pv.indmartes = ").append(pricingDia.getIndmartes());
		// }
		// if (pricingDia.getIndmiercoles()) {
		// hql.append(" or pv.indmiercoles = ").append(pricingDia.getIndmiercoles());
		// }
		// if (pricingDia.getIndjueves()) {
		// hql.append(" or pv.indjueves = ").append(pricingDia.getIndjueves());
		// }
		// if (pricingDia.getIndviernes()) {
		// hql.append(" or pv.indviernes = ").append(pricingDia.getIndviernes());
		// }
		// if (pricingDia.getIndsabado()) {
		// hql.append(" or pv.indsabado = ").append(pricingDia.getIndsabado());
		// }
		// if (pricingDia.getInddomingo()) {
		// hql.append(" or pv.inddomingo = ").append(pricingDia.getInddomingo());
		// }
		// hql.append(" ) ");
		// hql.append(" and ((( to_char('").append(sf.format(horarioinicio)).append("')  BETWEEN to_char(pv.horarioinicio, 'HH24MI') AND to_char(pv.horariofin, 'HH24MI') ) ");
		// hql.append(" OR (to_char('").append(sf.format(horariofin)).append("')  BETWEEN to_char(pv.horarioinicio, 'HH24MI') AND to_char(pv.horariofin, 'HH24MI'))) ");
		// hql.append(" OR ((to_char(pv.horarioinicio, 'HH24MI') BETWEEN to_char('").append(sf.format(horarioinicio)).append("')  AND to_char('").append(sf.format(horariofin)).append("') ) ");
		// hql.append(" OR (to_char(pv.horariofin, 'HH24MI') BETWEEN to_char('").append(sf.format(horarioinicio)).append("')  AND to_char('").append(sf.format(horariofin)).append("') ))) ");

		hql.append(" and pv.horariofin >= :horariofin ");
		hql.append(" and pv.horarioinicio >= :horarioinicio ");

		log.info("Hora Ini: " + sf.format(horarioinicio));
		log.info("Hora Fin: " + sf.format(horariofin));
		log.info("SQL: " + hql.toString());

		Query sq = getSession().createQuery(hql.toString());
		sq.setParameter("horariofin", horariofin);
		sq.setParameter("horarioinicio", horarioinicio);

		return sq.list();
	}
}
