/*
 * 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.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.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.rjconsultores.ventaboletos.dao.SegVKMDAO;
import com.rjconsultores.ventaboletos.entidad.Ruta;
import com.rjconsultores.ventaboletos.entidad.SegVKM;

/**
 * 
 * @author Administrador
 */
@Repository("segVKMDAO")
public class SegVKMHibernateDAO extends GenericHibernateDAO<SegVKM, Integer>
		implements SegVKMDAO {

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

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

		return c.list();
	}

	public List<SegVKM> buscar(String serie) {
		Criteria c = getSession().createCriteria(getPersistentClass());
		c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));

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

		return c.list();
	}

	public BigDecimal buscarSeguroPorKm(Long km, String serie, Integer orgaoConcedenteId) {
		StringBuilder sb = new StringBuilder();
		Query miQry = null;

		sb = new StringBuilder();
		sb.append(" select seg.valor");
		sb.append(" from SegVKm seg");
		sb.append(" where seg.activo = 1 and seg.km >= :km and seg.serie in (" + serie + ") and seg.ORGAOCONCEDENTE_ID = :orgaoconcedenteId");
		sb.append(" order by seg.km");

		miQry = getSession().createSQLQuery(sb.toString());
		miQry.setLong("km", km);
		miQry.setInteger("orgaoconcedenteId", orgaoConcedenteId);

		miQry.setMaxResults(1);

		List<BigDecimal> list = miQry.list();

		if (list.isEmpty()) {
			sb = new StringBuilder();
			sb.append(" select seg.valor");
			sb.append(" from SegVKm seg");
			sb.append(" where seg.activo = 1 and seg.km >= :km and seg.serie in (" + serie + ") and seg.ORGAOCONCEDENTE_ID is null");
			sb.append(" order by seg.km");
			miQry = getSession().createSQLQuery(sb.toString());

			// miQry.setMaxResults(1);

			miQry.setLong("km", km);
			list = miQry.list();
		}

		return list.isEmpty() ? BigDecimal.ZERO : list.get(0);
	}

	public String seriePorEmpresa(Integer empresaId) {
		StringBuilder sb = new StringBuilder();
		sb.append(" select serie ");
		sb.append(" from SeguradoraEmpresa segEmp");
		sb.append(" where segEmp.activo = 1 and segEmp.empresa = :empresaId");

		Query q = getSession().createQuery(sb.toString());
		q.setInteger("empresaId", empresaId);

		List<String> res = q.list();
		String series = "";
		for (String serie : res) {
			series += "'" + serie + "',";
		}
		if (!series.isEmpty()) {
			series = series.substring(0, series.length() - 1);
		}

		if (StringUtils.isBlank(series)) {
			return "-1";
		}
		return series;
	}
}
