/*
 * 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.catalog.RouteCtlg;
import com.arrebol.apc.model.catalog.constance.PeopleCfg;
import com.arrebol.apc.model.enums.ActiveStatus;
import com.arrebol.apc.model.enums.CustomerClassification;
import com.arrebol.apc.model.enums.PeopleType;
import com.arrebol.apc.model.views.CustomerView;
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 CustomerServiceImpl extends LazyDataModelDAORepository<CustomerView> implements CustomerService {

    @Override
    public boolean updatePeopleTypeById(PeopleType type, String peopleIdToUpdate, String lastUpdatedBy) {
        logger.debug("updatePeopleTypeById");

        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(PeopleCfg.FIELD_PEOPLE_TYPE, type));
        parameters.add(new ModelParameter(PeopleCfg.FIELD_LAST_UPDATED_BY, lastUpdatedBy));
        parameters.add(new ModelParameter(PeopleCfg.FIELD_LAST_UPDATED_ON, new Date()));
        parameters.add(new ModelParameter(PeopleCfg.FIELD_ID, peopleIdToUpdate));

        return xmlUpdateOrDelete(PeopleCfg.UPDATE_PEOPLE_TYPE_BY_STATUS, parameters);
    }

    @Override
    public boolean updatePeopleByStatus(ActiveStatus status, String peopleIdToUpdate, String lastUpdatedBy) {
        logger.debug("updatePeopleByStatus");

        List<ModelParameter> parameters = new ArrayList<>();

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

        return xmlUpdateOrDelete(PeopleCfg.UPDATE_PEOPLE_BY_STATUS, parameters);
    }
    
    @Override
    public boolean updatePeopleByClassification(CustomerClassification classification, String peopleIdToUpdate, String lastUpdatedBy) {
        logger.debug("updatePeopleByClassification");

        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(PeopleCfg.FIELD_CLASSIFICATION, classification));
        parameters.add(new ModelParameter(PeopleCfg.FIELD_LAST_UPDATED_BY, lastUpdatedBy));
        parameters.add(new ModelParameter(PeopleCfg.FIELD_LAST_UPDATED_ON, new Date()));
        parameters.add(new ModelParameter(PeopleCfg.FIELD_ID, peopleIdToUpdate));

        return xmlUpdateOrDelete(PeopleCfg.UPDATE_PEOPLE_BY_CLASSIFICATION, parameters);
    }

    @Override
    public boolean updateRouteById(RouteCtlg route, String peopleIdToUpdate, String lastUpdatedBy) {
        logger.debug("updateRouteById");

        List<ModelParameter> parameters = new ArrayList<>();

        parameters.add(new ModelParameter(PeopleCfg.FIELD_ROUTE, route));
        parameters.add(new ModelParameter(PeopleCfg.FIELD_LAST_UPDATED_BY, lastUpdatedBy));
        parameters.add(new ModelParameter(PeopleCfg.FIELD_LAST_UPDATED_ON, new Date()));
        parameters.add(new ModelParameter(PeopleCfg.FIELD_ID, peopleIdToUpdate));

        return xmlUpdateOrDelete(PeopleCfg.UPDATE_ROUTE_BY_PEOPLE, parameters);
    }

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

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

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

            return 0l;
        }
    }

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

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

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

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

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

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