package com.rjconsultores.ventaboletos.dao.hibernate;

import java.util.ArrayList;
import java.util.Date;
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.CobrancaAdcPuntoVentaDAO;
import com.rjconsultores.ventaboletos.entidad.CobrancaAdcPuntoVenta;
import com.rjconsultores.ventaboletos.entidad.CobrancaAdicionalHistorico;
import com.rjconsultores.ventaboletos.entidad.PuntoVenta;

@Repository("cobrancaAdcPuntoVentaHibernateDAO")
public class CobrancaAdcPuntoVentaHibernateDAO extends GenericHibernateDAO<CobrancaAdcPuntoVenta, Integer> implements CobrancaAdcPuntoVentaDAO {
	
    @Autowired
    public CobrancaAdcPuntoVentaHibernateDAO(
            @Qualifier("sessionFactory") SessionFactory factory) {
        setSessionFactory(factory);
    }

    @Override
    @SuppressWarnings("unchecked")
	public List<CobrancaAdicionalHistorico> buscaHistoricoCobranca(CobrancaAdcPuntoVenta cobranca, Date dataInicial, Date dataFinal) {
		List<CobrancaAdicionalHistorico> cobrancaHistorico = new ArrayList<CobrancaAdicionalHistorico>();
	
		StringBuilder sQuery = new StringBuilder();
		sQuery.append("SELECT   ")
			  .append("   h     ")
			  .append("FROM     ")
			  .append("   CobrancaAdicionalHistorico h   ")
			  .append("WHERE    ")
			  .append("   h.cobrancaAdicional.cobrancaAddPuntoVenta_id = :cobrancaAdicionalId and ")
			  .append("   h.dataRegistro between :dataInicial and :dataFinal   ")
			  .append("ORDER BY ")
			  .append("   h.cobrancaAdicionalHistoricoID ");
			
		Query qr = getSession().createQuery(sQuery.toString());
			
		qr.setParameter("dataInicial", dataInicial);
		qr.setParameter("dataFinal", dataFinal);
		qr.setParameter("cobrancaAdicionalId", cobranca == null ? 0 : cobranca.getCobrancaAddPuntoVenta_id());
			
		cobrancaHistorico = qr.list();
		
		return cobrancaHistorico;
	}
    
	@Override
	@SuppressWarnings("unchecked")
   	public List<CobrancaAdcPuntoVenta> buscarTodasAsCobrancas(PuntoVenta puntoVenta) {

		if (puntoVenta == null)
			return null;
		
   		List<CobrancaAdcPuntoVenta> cobrancaAdcList = new ArrayList<CobrancaAdcPuntoVenta>();
   	
   		StringBuilder sQuery = new StringBuilder();
   		sQuery.append("SELECT   ")
   			  .append("   h     ")
   			  .append("FROM     ")
   			  .append("   CobrancaAdcPuntoVenta h   ")
   			  .append("WHERE    ")
   			  .append("   h.puntoVenta.puntoventaId = :puntoVentaId ")
   			  .append("ORDER BY ")
   			  .append("   h.cobrancaAddPuntoVenta_id ");
   				
   		Query qr = getSession().createQuery(sQuery.toString());
   			
   		qr.setParameter("puntoVentaId", puntoVenta.getPuntoventaId());
   			
   		cobrancaAdcList = qr.list();
   		
   		return cobrancaAdcList;
   	}
    
}
