package com.rjconsultores.ventaboletos.dao.hibernate;

import com.rjconsultores.ventaboletos.entidad.OrgaoConcedente;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
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.SeguroKmDAO;
import com.rjconsultores.ventaboletos.entidad.SeguroKm;

@Repository("seguroKmDAO")
public class SeguroKmHibernateDAO extends GenericHibernateDAO<SeguroKm, Integer> implements SeguroKmDAO {

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

    @Override
    public boolean existe(Integer orgaoConcedenteId) {
        Criteria c = makeCriteria();
        c.add(Restrictions.eq("orgaoconcedente.orgaoConcedenteId", orgaoConcedenteId));
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));
        c.setProjection(Projections.rowCount());


        return HibernateFix.count(c.list()) > 0;
    }

    public List<SeguroKm> buscarPorOrgao(OrgaoConcedente orgao) {
        Criteria c = makeCriteria();
        c.add(Restrictions.eq("orgaoconcedente", orgao));
        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));

        return c.list();
    }
}
