/*
 * 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.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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.RedondeoCtrlDAO;
import com.rjconsultores.ventaboletos.entidad.RedondeoCtrl;
import com.rjconsultores.ventaboletos.entidad.RedondeoMarca;

/**
 *
 * @author Administrador
 */
@Repository("redondeoCtrlDAO")
public class RedondeoCtrlHibernateDAO extends GenericHibernateDAO<RedondeoCtrl, Integer>
        implements RedondeoCtrlDAO {

    private static final Logger log = LogManager.getLogger(RedondeoCtrlHibernateDAO.class);

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

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

        return c.list();
    }

    public List<RedondeoCtrl> buscar(RedondeoCtrl redondeoCtrl) {
        Criteria c = getSession().createCriteria(getPersistentClass());

        c.add(Restrictions.eq(ACTIVO, Boolean.TRUE));

        List<Short> lsShort = new ArrayList<Short>();
        for (RedondeoMarca rm : redondeoCtrl.getRedondeoMarcaList()) {
            lsShort.add(rm.getMarca().getMarcaId());
        }
        c.createCriteria("redondeoMarcaList").add(Restrictions.in("marca.marcaId", lsShort));

        return c.list();

    }

    public List<RedondeoCtrl> buscarTraslapa(Short[] iDMarcas, BigDecimal valorinicial, BigDecimal valorfinal, Boolean indaplicadescuento, 
    		Boolean indaplicapricing, Boolean indaplicaAtersp) {

        String ids = "";
        for (int i = 0; i < iDMarcas.length; i++) {
            ids = ids.concat(iDMarcas[i].toString().concat(","));
        }
        ids = ids.substring(0, ids.length() - 1);


        StringBuilder hql = new StringBuilder("select rc from RedondeoCtrl rc ");
        hql.append(" join rc.redondeoList r  ");
        hql.append(" join rc.redondeoMarcaList rm  ");
        hql.append(" where rc.activo = 1 and r.activo = 1 and rm.activo = 1 ");
        if (indaplicaAtersp) {
            hql.append(" and rc.indaplicaArtesp = :aplicaArtesp ");
        } else if (indaplicadescuento && !indaplicapricing) {
            hql.append(" and rc.indaplicadescuento = :aplicaDescuento ");
        } else if (!indaplicadescuento && indaplicapricing) {
            hql.append(" and rc.indaplicapricing = :aplicaPricing  ");
        } else if (indaplicadescuento && indaplicapricing) {
            hql.append(" and rc.indaplicadescuento = :aplicaDescuento or rc.indaplicapricing = :aplicaPricing  ");
        } else if (!indaplicadescuento && !indaplicapricing) {
            hql.append(" and rc.indaplicadescuento = :aplicaDescuento and rc.indaplicapricing = :aplicaPricing  ");
        }
        hql.append(" and rm.marca.marcaId in (" + ids + ") and ");
        hql.append("    (((:vlrIni BETWEEN r.valorinicial AND r.valorfinal)  ");
        hql.append(" OR (:vlrFin BETWEEN r.valorinicial AND r.valorfinal)) ");
        hql.append(" OR ((r.valorinicial BETWEEN :vlrIni AND :vlrFin) ");
        hql.append(" OR (r.valorfinal   BETWEEN :vlrIni AND :vlrFin))) ");
        hql.append(" and r.activo =:activo ");

        log.debug("Select: " + hql.toString());
        Query sq = getSession().createQuery(hql.toString());
        sq.setDouble("vlrIni", valorinicial.doubleValue());
        sq.setDouble("vlrFin", valorfinal.doubleValue());
        sq.setInteger("activo", 1);

        if (indaplicaAtersp) {
            sq.setBoolean("aplicaArtesp", indaplicaAtersp);
        } else if (indaplicadescuento && !indaplicapricing) {
            sq.setBoolean("aplicaDescuento", indaplicadescuento);
        } else if (!indaplicadescuento && indaplicapricing) {
            sq.setBoolean("aplicaPricing", indaplicapricing);
        } else {
            sq.setBoolean("aplicaDescuento", indaplicadescuento);
            sq.setBoolean("aplicaPricing", indaplicapricing);
        }


        return sq.list();

    }
}
