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

import com.arrebol.apc.controller.mobile.repository.admin.ClosingDayRepository;
import com.arrebol.apc.controller.mobile.repository.admin.StableSmallBoxRepository;
import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.admin.constance.StableSmallBoxCfg;
import com.arrebol.apc.model.admin.constance.StableSmallBoxCfg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.enums.ActiveStatus;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
public class StableSmallBoxController implements Serializable {
    
    /**
     *
     * @param id
     * @param updatedBy
     * @return
     * @throws Exception
     */
    public boolean disableIdSmallBoxById(String id, String updatedBy) throws Exception {
        try {
            ClosingDayRepository repository = new ClosingDayRepository();
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter(StableSmallBoxCfg.FIELD_ID, id));
            parameters.add(new ModelParameter(StableSmallBoxCfg.FIELD_ACTIVE_STATUS, ActiveStatus.DISABLED));
            parameters.add(new ModelParameter(StableSmallBoxCfg.FIELD_LAST_UPDATED_BY, updatedBy));
            parameters.add(new ModelParameter(StableSmallBoxCfg.FIELD_LAST_UPDATED_ON, new Date()));

            return repository.updateClosingDay(StableSmallBoxCfg.QUERY_UPDATE_STABLE_SMALL_BOX_BY_STATUS, parameters);
        } catch (Exception e) {
            logger.error("disableIdSmallBoxById", e);
            throw e;
        }
    }
    
    /**
     *
     * @param idOffice
     * @return
     * @throws Exception
     */
    public boolean existStableSmallBoxByCreatedOn(String idOffice) throws Exception {
        try {
            StableSmallBoxRepository repository = new StableSmallBoxRepository();
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter(StableSmallBoxCfg.FIELD_OFFICE, new Office(idOffice)));

            return new Long(1).compareTo(repository.countStableSmallBoxByOfficeEqualsToCurrentDate(StableSmallBoxCfg.QUERY_COUNT_STABLE_SMALL_BOX_BY_OFFICE_EQUALS_TO_CURDATE, parameters)) == 0;
        } catch (Exception e) {
            logger.error("existStableSmallBoxByCreatedOn", e);
            throw e;
        }
    }

    /**
     * 
     * @param idOffice
     * @return
     * @throws Exception 
     */
    public String findIdStableSmallBoxByOfficeInCurrentDate(String idOffice) throws Exception {
        try {
            ClosingDayRepository repository = new ClosingDayRepository();
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter(StableSmallBoxCfg.FIELD_OFFICE, new Office(idOffice)));

            return repository.findIdEntity(StableSmallBoxCfg.QUERY_FIND_STABLE_SMALL_BOX_BY_OFFICE_EQUALS_TO_CURRENT_DATE, parameters);
        } catch (Exception e) {
            logger.error("findIdStableSmallBoxByOfficeInCurrentDate", e);
            throw e;
        }
    }

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