/*
 * 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.model.ModelParameter;
import com.arrebol.apc.model.admin.constance.ClosingDayCfg;
import com.arrebol.apc.model.core.Office;
import com.arrebol.apc.model.core.User;
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 ClosingDayController implements Serializable {

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

            parameters.add(new ModelParameter(ClosingDayCfg.FIELD_USER, new User(idEmployee)));
            parameters.add(new ModelParameter(ClosingDayCfg.FIELD_OFFICE, new Office(idOffice)));

            return repository.findIdEntity(ClosingDayCfg.QUERY_FIND_ID_BY_EMPLOYEE_AND_OFFICE_EQUALS_CURRDATE, parameters);
        } catch (Exception e) {
            logger.error("findIdClosingDayByEmployee", e);
            throw e;
        }
    }

    /**
     *
     * @param id
     * @param updatedBy
     * @return
     * @throws Exception
     */
    public boolean disableClosingDayById(String id, String updatedBy) throws Exception {
        try {
            ClosingDayRepository repository = new ClosingDayRepository();
            List<ModelParameter> parameters = new ArrayList<>();

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

            return repository.updateClosingDay(ClosingDayCfg.QUERY_UPDATE_CLOSING_DAY_BY_STATUS, parameters);
        } catch (Exception e) {
            logger.error("disableClosingDayById", e);
            throw e;
        }
    }
    
     public Long countIdClosingDayByEmployee(String idEmployee, String idOffice) throws Exception {
        try {
            ClosingDayRepository repository = new ClosingDayRepository();
            List<ModelParameter> parameters = new ArrayList<>();

            parameters.add(new ModelParameter(ClosingDayCfg.FIELD_USER, new User(idEmployee)));
            parameters.add(new ModelParameter(ClosingDayCfg.FIELD_OFFICE, new Office(idOffice)));

            return repository.countIdClosingDayByEmployee(ClosingDayCfg.QUERY_FIND_ID_BY_EMPLOYEE_AND_OFFICE_EQUALS_CURRDATE, parameters);
        } catch (Exception e) {
            logger.error("findIdClosingDayByEmployee", e);
            throw e;
        }
    }

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