package com.fp.general.rules.query;

import com.fp.common.helper.BeanManager;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.dto.Response;
import com.fp.dto.query.QueryBean;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.SQLQuery;
import org.hibernate.ScrollableResults;

/**
 * Clase que se encarga de armar los tipos de balance por modulo y provicion
 * @author scastillo
 */
public class BalanceTypeProvision extends QueryRule {

    /**
     * Proceso que devuevelve los tipos de balance en el response
     * @param pQueryRequest
     * @return
     * @throws Exception
     */
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        Response response = pQueryRequest.getResponse();
        QueryBean queryBeanDebit = (QueryBean) pQueryRequest.get("BALANCETYPEPROVITION");
        String modulecode = (String) BeanManager.convertObject(queryBeanDebit.getCriteriaValue("modulecode"), String.class);
        String isprovision = (String) BeanManager.convertObject(queryBeanDebit.getCriteriaValue("isprovision"), String.class);
        List<Map<String, Object>> data = getBalanceType(modulecode, isprovision);
        response.put("BALANCETYPEPROVITION", data);
        return pQueryRequest;
    }

    /**
     * Método que ejecuta el query y los asigna en un map a todo los tipos de saldo
     * @param module
     * @param isprovision
     * @return
     * @throws Exception
     */
    private List<Map<String, Object>> getBalanceType(String module, String isprovision) throws Exception {
        ScrollableResults scrollableResults = null;
        SQLQuery qry = PersistenceHelper.getSession().createSQLQuery(obtainSelect(module, isprovision));
        scrollableResults = qry.scroll();
        List<Map<String, Object>> balancetype = new ArrayList<Map<String, Object>>();
        try {
            while (scrollableResults.next()) {
                Object[] object = scrollableResults.get();
                Map<String, Object> map = new HashMap<String, Object>();
                String pk_balancetype = (String) object[0];
                String pk_balancegroup = (String) object[1];
                String description = (String) object[2];
                String balancecategory = (String) object[3];
                String modulecode = (String) object[4];
                map.put("pk_balancetype", pk_balancetype);
                map.put("pk_balancegroup", pk_balancegroup);
                map.put("description", description);
                map.put("balancecategory", balancecategory);
                map.put("modulecode", modulecode);
                balancetype.add(map);
            }
        } finally {
            scrollableResults.close();
        }
        return balancetype;
    }

    /**
     * Metodo que arma el select a ejecutar para obtener los tipos de balance
     * @param module
     * @param isprovision
     * @return
     */
    private String obtainSelect(String module, String isprovision) {
        String filtermodule = "";
        if (module != null) {
            filtermodule = " where tgbt.MODULECODE = '" + module + "' and";
        }
        String filterprovision = ")";
        if (isprovision != null) {
            filterprovision = "           and tgbtd.ISPROVISON = '" + isprovision + "')";
        }
        String sentencia =
                "select tgbt.BALANCETYPE,"
                + "       tgbt.BALANCEGROUP,"
                + "       tgbt.DESCRIPTION,"
                + "       tgbt.BALANCECATEGORY,"
                + "       tgbt.MODULECODE"
                + "  from TGENEBALANCETYPE tgbt"
                + filtermodule
                + "   exists (select 1"
                + "          from TGENEBALANCETYPEDETAIL tgbtd"
                + "         where tgbt.BALANCETYPE = tgbtd.BALANCETYPE"
                + "           and tgbt.BALANCEGROUP = tgbtd.BALANCEGROUP"
                + filterprovision
                + "  order by tgbt.BALANCETYPE";
        return sentencia;
    }
}
