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

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
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.PrecioFixoPedagioDAO;
import com.rjconsultores.ventaboletos.entidad.PrecioFixoPedagio;

/**
 * 
 * @author Administrador
 */
@Repository("precoFixoPedagioDAO")
public class PrecioFixoPedagioHibernateDAO extends GenericHibernateDAO<PrecioFixoPedagio, Integer> implements PrecioFixoPedagioDAO {

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

	@Override
	public List<PrecioFixoPedagio> obtenerTodos() {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
//		c.addOrder(Order.asc("descclase"));

		return c.list();
	}
	
	public List<PrecioFixoPedagio> buscarPorEmpresasDoUsuario(String empresasUsuario) {

		String consulta = "SELECT DISTINCT mcs.marcaClaseservicioPK.claseservicio "
				+ "FROM MarcaClaseServicio mcs "
				+ "WHERE mcs.marcaClaseservicioPK.marca.empresa.empresaId IN (:empresas) ";

		List<Integer> ids = new ArrayList<Integer>();
		for (String empresa : empresasUsuario.split(",")) {
			ids.add(new Integer(empresa));
		}
		Query query = getSession().createQuery(consulta);
		query.setParameterList("empresas", ids);

		List<PrecioFixoPedagio> result = (List<PrecioFixoPedagio>) query.list();
		return result;
	}

	public List<PrecioFixoPedagio> buscarPorNome(String nomeClaseServicio) {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
		c.add(Restrictions.eq("descclase", nomeClaseServicio));

		return c.list();
	}

	public List<PrecioFixoPedagio> buscarTodosExceto(Integer... idClase) {
		Criteria c = this.makeCriteria();
		for (Integer id : idClase) {
			c.add(Restrictions.ne("claseservicioId", id));
		}
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
		c.addOrder(Order.asc("descclase"));

		return c.list();
	}
	@Override
	public List<PrecioFixoPedagio> buscarPrecioFixoPedagio(PrecioFixoPedagio obj) {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq("pracaPedagioId.casetaPeajeId", obj.getPracaPedagioId().getCasetaPeajeId()));
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
		if(obj.getOrgaoConcedenteId() != null){
			c.add(Restrictions.eq("orgaoConcedenteId.orgaoConcedenteId", obj.getOrgaoConcedenteId().getOrgaoConcedenteId()));
		}
		if(obj.getClasseId()!= null){
			c.add(Restrictions.eq("classeId.claseservicioId", obj.getClasseId().getClaseservicioId()));
		}
		
		c.setMaxResults(1);
		return c.list();
	}
}
