/*
 * 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.service.admin;

import com.arrebol.apc.controller.util.FilterMap;
import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.admin.Transfer;
import com.arrebol.apc.model.admin.constance.TransferCfg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.model.views.TransferView;
import com.arrebol.apc.repository.LazyDataModelDAORepository;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.enterprise.context.RequestScoped;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.model.SortOrder;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@RequestScoped
public class TransferServiceImpl extends LazyDataModelDAORepository<TransferView> implements TransferService {

    final Logger logger = LogManager.getLogger(getClass());

    @Override
    public boolean updateTransferByStatus(ActiveStatus status, String transferIdToUpdate, String lastUpdatedBy) {
        logger.debug("updateTransferByStatus");
        try {
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter(TransferCfg.FIELD_ACTIVE_STATUS, status));
            parameters.add(new ModelParameter(TransferCfg.FIELD_LAST_UPDATED_BY, lastUpdatedBy));
            parameters.add(new ModelParameter(TransferCfg.FIELD_LAST_UPDATED_ON, new Date()));
            parameters.add(new ModelParameter(TransferCfg.FIELD_ID, transferIdToUpdate));

            return xmlUpdateOrDelete(TransferCfg.QUERY_UPDATE_TRANSFER_BY_ACTIVE_STATUS, parameters);
        } catch (Exception e) {
            logger.error("updateTransferByStatus", e);
            return false;
        }
    }

    @Override
    public boolean saveTransfer(Transfer transfer) {
        logger.debug("saveTransfer");
        return save(transfer);
    }

    @Override
    public long countPaginator(Map<String, Object> filterBy, String officeId, Date starDate, Date endDate) {
        logger.debug("countPaginator");
        try {
            StringBuilder sqlQuery = new StringBuilder("SELECT COUNT(id) FROM TransferView ");

            sqlQuery.append(FilterMap.filterByExcludeGlobalFilter(filterBy, false));

            if (null != filterBy && !filterBy.isEmpty()) {
                sqlQuery.append(" AND DATE(createdOn) BETWEEN DATE(:startDate) AND DATE(:endDate) ");
                sqlQuery.append(" AND idOffice = :idOffice ");
            } else {
                sqlQuery.append(" WHERE DATE(createdOn) BETWEEN DATE(:startDate) AND DATE(:endDate) ");
                sqlQuery.append(" AND idOffice = :idOffice ");
            }
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter("startDate", starDate));
            parameters.add(new ModelParameter("endDate", endDate));
            parameters.add(new ModelParameter("idOffice", officeId));

            return lazyLoadingEntityCount(sqlQuery.toString(), parameters);
        } catch (Exception e) {
            logger.error("countPaginator", e);

            return 0l;
        }
    }

    @Override
    public List<TransferView> lazyEntityListPaginator(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filterBy, String officeId, Date starDate, Date endDate) {
        logger.debug("fillListPaginator");
        try {
            StringBuilder sqlQuery = new StringBuilder("FROM TransferView ");

            sqlQuery.append(FilterMap.filterByExcludeGlobalFilter(filterBy, false));

            if (null != filterBy && !filterBy.isEmpty()) {
                sqlQuery.append(" AND DATE(createdOn) BETWEEN DATE(:startDate) AND DATE(:endDate) ");
                sqlQuery.append(" AND idOffice = :idOffice ");
            } else {
                sqlQuery.append(" WHERE DATE(createdOn) BETWEEN DATE(:startDate) AND DATE(:endDate) ");
                sqlQuery.append(" AND idOffice = :idOffice ");
            }

            sqlQuery.append(" ORDER BY ");

            if (null == sortField || "".equals(sortField)) {
                sqlQuery.append(" createdOn ASC");
            } else {
                sqlQuery.append(sortField);

                if (null != sortOrder) {
                    sqlQuery.append(" ");
                    sqlQuery.append(sortOrder.equals(SortOrder.ASCENDING) ? "ASC" : "DESC");
                } else {
                    sqlQuery.append(" ASC");
                }
            }
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter("startDate", starDate));
            parameters.add(new ModelParameter("endDate", endDate));
            parameters.add(new ModelParameter("idOffice", officeId));

            return lazyLoadingEntityList(TransferView.class, sqlQuery.toString(), parameters, first, pageSize);
        } catch (Exception e) {
            logger.error("CustomerView", e);
            return new ArrayList();
        }
    }
}
