/*
 * 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.taxiservicios.controller.lazy.services;

import com.arrebol.taxiservicios.controller.util.FilterMap;
import com.arrebol.taxiservicios.controller.util.LazyDataModelDAORepository;
import com.arrebol.taxiservicios.controller.util.ModelParameter;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.view.dashboard.DashboardToCollectView;
import java.util.ArrayList;
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 ToCollectServiceImpl extends LazyDataModelDAORepository<DashboardToCollectView> implements ToCollectService {

    @Override
    public long countPaginator(Map<String, Object> filterBy) {
        logger.debug("countPaginator");
        try {
            StringBuilder sqlQuery = new StringBuilder("SELECT COUNT(idTaxi) FROM DashboardToCollectView ");

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

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

            return 0l;
        }
    }

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

            sqlQuery.append(FilterMap.genericFilterByMapping(filterBy, false));
            sqlQuery.append(" ORDER BY ");

            if (null == sortField || "".equals(sortField)) {
                sqlQuery.append(" driverName ASC");
            } else {
                sqlQuery.append(sortField);
                if (null != sortOrder) {
                    sqlQuery.append(" ");
                    sqlQuery.append(sortOrder.equals(SortOrder.ASCENDING) ? "ASC" : "DESC");
                } else {
                    sqlQuery.append(" ASC");
                }
            }

            return lazyLoadingEntityList(DashboardToCollectView.class, sqlQuery.toString(), null, first, pageSize);
        } catch (Exception e) {
            logger.error("lazyEntityListPaginator", e);
            return new ArrayList();
        }
    }

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

    @Override
    public long countPaginator(Map<String, Object> filterBy, String idLocation) {
        logger.debug("countPaginator");
        try {
            StringBuilder sqlQuery = new StringBuilder("SELECT COUNT(idTaxi) FROM DashboardToCollectView WHERE idLocation = :idLocation ");
            List<ModelParameter> parameters = new ArrayList<>();

            sqlQuery.append(FilterMap.genericFilterByMapping(filterBy, true));
            parameters.add(new ModelParameter("idLocation", idLocation));

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

            return 0l;
        }
    }

    @Override
    public List<DashboardToCollectView> lazyEntityListPaginator(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filterBy, String idLocation) {
        logger.debug("fillListPaginator");
        try {
            StringBuilder sqlQuery = new StringBuilder("FROM DashboardToCollectView WHERE idLocation = :idLocation ");
            List<ModelParameter> parameters = new ArrayList<>();
            
            sqlQuery.append(FilterMap.genericFilterByMapping(filterBy, true));
            sqlQuery.append(" ORDER BY ");

            if (null == sortField || "".equals(sortField)) {
                sqlQuery.append(" driverName ASC");
            } else {
                sqlQuery.append(sortField);
                if (null != sortOrder) {
                    sqlQuery.append(" ");
                    sqlQuery.append(sortOrder.equals(SortOrder.ASCENDING) ? "ASC" : "DESC");
                } else {
                    sqlQuery.append(" ASC");
                }
            }
           
            parameters.add(new ModelParameter("idLocation", idLocation));

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