/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.controller.dashboard;

import com.arrebol.apc.controller.util.FilterMap;
import com.arrebol.apc.controller.util.HibernateUtil;
import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.payroll.TotalExpectedPaymentDailyByUser;
import com.arrebol.apc.model.payroll.constance.TotalExpectedPaymentDailyByUserCfg;
import com.arrebol.apc.model.views.AdvanceUserDailyDetail;
import com.arrebol.apc.model.views.AdvanceUserDailyView;
import com.arrebol.apc.model.views.CustomerWithoutRenovationView;
import com.arrebol.apc.model.views.TotalLoansByOfficeView;
import com.arrebol.apc.model.views.constance.AdvanceUserDailyDetailViewCfg;
import com.arrebol.apc.model.views.constance.AdvanceUserDailyViewCfg;
import com.arrebol.apc.model.views.constance.CustomerWithoutRenovationViewCfg;
import com.arrebol.apc.model.views.constance.TotalLoansApprovedByOfficeViewCfg;
import com.arrebol.apc.model.views.constance.TotalLoansByOfficeViewCfg;
import com.arrebol.apc.repository.GenericEntityRepository;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.persistence.Query;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Oscar Armando Vargas Cardenas <oscar.vargas@arrebol.com.mx>
 */
public class DashboardController implements Serializable {

    /**
     *
     * Searching loans by office.
     *
     * @param officeId
     * @return
     */
    public List<TotalLoansByOfficeView> fillAllLoansByOffice(String officeId) {
        logger.debug("fillClosingDayDatatable");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(TotalLoansByOfficeViewCfg.FIELD_VIEW_OFFICE, officeId));

        return genericEntityRepository.xmlQueryAPCEntities(TotalLoansByOfficeView.class, TotalLoansByOfficeViewCfg.QUERY_FIND_ALL_LOANS_BY_OFFICE, parameters);
    }

    /**
     *
     * Searching loans by office.
     *
     * @param officeId
     * @return
     */
    public BigDecimal sumLoansApprovedByOffice(String officeId) {
        logger.debug("sumLoansApprovedByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(TotalLoansApprovedByOfficeViewCfg.FIELD_VIEW_OFFICE, officeId));

        return (BigDecimal) genericEntityRepository.xmlQueryAPCEntityUniqueResult(BigDecimal.class, TotalLoansApprovedByOfficeViewCfg.QUERY_SUM_ALL_LOANS_BY_OFFICE, parameters);
    }

    public Long getLoansSumPaymentDailyByUser(String userId) {
        logger.debug("getLoansSumPaymentDailyByUser");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(TotalLoansByOfficeViewCfg.FIELD_VIEW_USER, userId));

        return (Long) genericEntityRepository.xmlQueryAPCEntityUniqueResult(Long.class, TotalLoansByOfficeViewCfg.QUERY_COUNT_ALL_LOANS_BY_USER, parameters);
    }

    public BigDecimal getLoansSumPaymentDailyByUserMoney(String userId) {
        logger.debug("getLoansSumPaymentDailyByUserMoney");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(TotalLoansByOfficeViewCfg.FIELD_VIEW_USER, userId));

        return (BigDecimal) genericEntityRepository.xmlQueryAPCEntityUniqueResult(BigDecimal.class, TotalLoansByOfficeViewCfg.QUERY_FIND_ALL_LOANS_BY_USER, parameters);
    }

    /**
     *
     * @param advance
     * @return boolean
     */
    public boolean saveTotalExpectedPaymentDailyByUser(TotalExpectedPaymentDailyByUser totalExpectedPaymentDailyByUser) {
        logger.debug("saveTotalExpectedPaymentDailyByUser");
        boolean success = genericEntityRepository.insertAPCEntity(totalExpectedPaymentDailyByUser);

        return success;

    }

    public Long verifyTotalExpectedPaymentDailyByUserCreatedByOffice(String officeid) {
        logger.debug("verifyTotalExpectedPaymentDailyByUserCreatedByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(TotalExpectedPaymentDailyByUserCfg.FIELD_OFFICE, new Office(officeid)));

        return (Long) genericEntityRepository.xmlQueryAPCEntityUniqueResult(Long.class, TotalExpectedPaymentDailyByUserCfg.QUERY_SELECT_MAX_DATE_BY_CURDATE, parameters);
    }

    public List<AdvanceUserDailyView> fillAllAdvanceUserDailyByOffice(String officeId) {
        logger.debug("fillAllAdvanceUserDailyByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(AdvanceUserDailyViewCfg.FIELD_VIEW_OFFICE, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(AdvanceUserDailyView.class, AdvanceUserDailyViewCfg.QUERY_FIND_ALL_ADVANCE_USER_DAILY_BY_OFFICE, parameters);
    }

    public List<CustomerWithoutRenovationView> findAllCustomerWithOutRenovationByOffice(String officeId) {
        logger.debug("findAllCustomerWithOutRenovationByOffice");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(CustomerWithoutRenovationViewCfg.FIELD_VIEW_OFFICE, new Office(officeId)));

        return genericEntityRepository.xmlQueryAPCEntities(CustomerWithoutRenovationView.class, CustomerWithoutRenovationViewCfg.QUERY_FIND_ALL_CUSTOMER_WITHOUT_RENOVATION_BY_OFFICE, parameters);
    }

    public List<AdvanceUserDailyDetail> findAllAdvancesUserDailyDetailByUser(String userId) {
        logger.debug("findAllAdvancesUserDailyDetailByUser");
        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(AdvanceUserDailyDetailViewCfg.FIELD_VIEW_USER, userId));

        return genericEntityRepository.xmlQueryAPCEntities(AdvanceUserDailyDetail.class, AdvanceUserDailyDetailViewCfg.QUERY_FIND_ALL_ADVANCE_USER_DAILY_DETAIL_BY_USER, parameters);
    }

    final Logger logger = LogManager.getLogger(DashboardController.class);
    private final GenericEntityRepository genericEntityRepository;

    public DashboardController() {
        this.genericEntityRepository = new GenericEntityRepository();
    }

}
