package com.fp.person.rules.query.code;

import com.fp.bpm.query.Query;
import com.fp.common.helper.CalculationBase;
import com.fp.dto.Response;
import com.fp.dto.query.QueryBean;
import com.fp.dto.query.QueryCriteria;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.commondb.helper.APPDates;
import com.fp.persistence.commondb.helper.FormatDates;
import com.fp.persistence.pcustomer.company.TcustCompanyManagers;
import com.fp.persistence.pcustomer.gene.TcustPersonDetail;
import com.fp.persistence.pcustomer.gene.TcustPersonDetailKey;
import com.fp.persistence.pgeneral.date.TgeneAccountingDateBranch;
import com.fp.persistence.pgeneral.gene.TgeneLabel;
import com.fp.persistence.pgeneral.gene.TgeneLabelKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Clase que se va a encargar de verificar si los accionistas o directivos ya caducaron su fecha de expiracion
 *
 * @author scastillo
 */
public class VerifyManagers extends QueryRule {

    private static final String JPQL = "from TcustCompanyManagers t"
            + " where t.pk.personcode = :personcode"
            + " and t.pk.dateto = :dateto"
            + " and t.expirationdate < :expirationdate";

    /**
     * Metodo que consulta los datos normalemente según elq uery enviado
     *
     * @param pQueryRequest
     * @return
     * @throws Exception
     */
    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        Response response = pQueryRequest.getResponse();
        QueryBean queryBean = (QueryBean) pQueryRequest.get("TCUSTCOMPANYMANAGERS");
        List<QueryCriteria> lqueryCriteria = queryBean.getCriteria();
        QueryCriteria queryCriteria = lqueryCriteria.get(0);
        Integer personcode = new Integer(String.valueOf(queryCriteria.getValue()));
        TgeneAccountingDateBranch tgadb = com.fp.persistence.commondb.db.DataHelper.getInstance().getTgeneAccountingDateBranch(pQueryRequest.getCompany(), 0);
        APPDates workDate = new APPDates(tgadb.getWorkingdate(), CalculationBase.B365365);
        javax.persistence.Query query = PersistenceHelper.getEntityManager().createQuery(String.valueOf(JPQL));
        query.setParameter("personcode", personcode);
        query.setParameter("dateto", FormatDates.getDefaultExpiryTimestamp());
        query.setParameter("expirationdate", workDate.getDate());
        List<TcustCompanyManagers> ldata = query.getResultList();
        if (!ldata.isEmpty()) {
            TgeneLabelKey tgeneLabelKey = new TgeneLabelKey("msg056", pQueryRequest.getLanguage(), pQueryRequest.getChannelCode());
            TgeneLabel tgeneLabel = TgeneLabel.find(PersistenceHelper.getEntityManager(), tgeneLabelKey);
            String mensaje = tgeneLabel.getDescription()+":<br/>";
            int contador = 1;
            for (TcustCompanyManagers tcustCompanyManagers : ldata) {
                Integer personCodeManager = tcustCompanyManagers.getPk().getManagerpersoncode();
                TcustPersonDetailKey tcustPersonDetailKey = new TcustPersonDetailKey(personCodeManager, FormatDates.getDefaultExpiryTimestamp());
                TcustPersonDetail tcustPersonDetail = TcustPersonDetail.find(PersistenceHelper.getEntityManager(), tcustPersonDetailKey);
                String nombre = tcustPersonDetail.getName();
                mensaje += "<bold>"+contador+".-</bold> "+nombre+"<br/>";
                contador++;
            }
            contador--;
            if(mensaje.compareTo(tgeneLabel.getDescription()+":<br/>") != 0){
                response.put("mensaje", mensaje);
            }
        }
        Query qry = new Query();
        qry.process(pQueryRequest);
        return pQueryRequest;
    }
}
