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

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.IntegerType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.rjconsultores.ventaboletos.dao.PricingDAO;
import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.utilerias.DateUtil;

/**
 * 
 * @author Rafius
 */
@Repository("pricingDAO")
public class PricingHibernateDAO extends GenericHibernateDAO<Pricing, Integer>
		implements PricingDAO {

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

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

		return c.list();
	}

	public List<Pricing> buscar(String nombPricing, Empresa empresa,
			Short cantboleto, Integer cantdiasanticipacion,
			BigDecimal descuentoporcentaje, BigDecimal descuentoporcredondo) {

		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq(ACTIVO, Pricing.ATIVO));

		c.add(Restrictions.eq("nombPricing", nombPricing));
		c.add(Restrictions.eq("empresa", empresa));
		c.add(Restrictions.eq("cantboleto", cantboleto));
		c.add(Restrictions.eq("cantdiasanticipacion", cantdiasanticipacion));
		if (descuentoporcentaje != null) {
			c.add(Restrictions.eq("descuentoporcentaje", descuentoporcentaje));
		}
		if (descuentoporcredondo != null) {
			c.add(Restrictions.eq("descuentoporcredondo", descuentoporcredondo));
		}

		return c.list();
	}

	public List<Pricing> buscarPorNombre(String nombPricing) {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq(ACTIVO, Pricing.ATIVO));

		c.add(Restrictions.eq("nombPricing", nombPricing));

		return c.list();
	}

	@Override
	public List<Pricing> buscar(Integer pricingId, String nomepricing, Integer empresaId, Date dataViagemInicio, Date dataViagemFim, Integer origemId, Integer destinoId) {
		StringBuilder sQuery = new StringBuilder("SELECT p FROM Pricing p WHERE p.activo = 1 ");
		
		if(pricingId != null) {
			sQuery.append("AND p.pricingId = :pricingId ");
		}

		if(StringUtils.isNotBlank(nomepricing)) {
			sQuery.append("AND p.nombPricing like :nomepricing ");
		}
		
		if(empresaId != null) {
			sQuery.append("AND p.empresa.empresaId = :empresaId ");
		}
		
		if(dataViagemInicio != null) {
			sQuery.append("AND p.pricingVigenciaList.fecinicioviaje >= :dataViagemInicio ");
		}
		
		if(dataViagemFim != null) {
			sQuery.append("AND p.pricingVigenciaList.fecfinviaje <= :dataViagemFim ");
		}
		
		if(origemId != null) {
			sQuery.append("AND p.pricingMercadoList.origen.paradaId = :origemId ");
		}
		
		if(destinoId != null) {
			sQuery.append("AND p.pricingMercadoList.destino.paradaId = :destinoId ");
		}
		
		Query qr = getSession().createQuery(sQuery.toString());
		if(pricingId != null) {
			qr.setParameter("pricingId", pricingId);
		}
		
		if(StringUtils.isNotBlank(nomepricing)) {
			qr.setParameter("nomepricing", nomepricing);
		}
		
		if(empresaId != null) {
			qr.setParameter("empresaId", empresaId);
		}
		
		if(dataViagemInicio != null) {
			qr.setParameter("dataViagemInicio", dataViagemInicio);
		}
		
		if(dataViagemFim != null) {
			qr.setParameter("dataViagemFim", dataViagemFim);
		}
		
		if(origemId != null) {
			qr.setParameter("origemId", origemId);
		}
		
		if(destinoId != null) {
			qr.setParameter("destinoId", destinoId);
		}
		
		return qr.list();
	}

	@Override
	public List<Integer> buscar(Integer diasFimViagem) {
		if(diasFimViagem == null || diasFimViagem == 0) {
			return null;
		}
		StringBuilder sQuery = new StringBuilder();
		sQuery.append("SELECT p.pricing_id ")
			  .append("FROM pricing p ")
			  .append("JOIN pricing_vigencia pv ON p.pricing_id = pv.pricing_id and pv.activo = 1 ")
			  .append("WHERE p.activo = 1 ")
			  .append("HAVING to_date(:dataAtual,'dd/mm/yyyy') - max(pv.fecfinviaje) >= :diasFimViagem ")
			  .append("GROUP BY p.pricing_id ");
		
		Query qr = getSession().createSQLQuery(sQuery.toString()).addScalar("pricing_id", IntegerType.INSTANCE);
		qr.setParameter("dataAtual", DateUtil.getStringDate(new Date(), "dd/MM/yyyy"));
		qr.setParameter("diasFimViagem", diasFimViagem);
		return qr.list();
	}
}
