package com.prolog.cs.biz.outbound.service.strategy.handles;

import com.prolog.cs.biz.outbound.service.strategy.STRATEGY_CODE;
import com.prolog.framework.utils.ObjectUtils;
import com.prolog.framework.utils.StringUtils;
import com.prolog.upcloud.base.strategy.model.BusinessControl;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @program: upcloud-base-wh-outbound
 * @description: 业务配置参数抽象类
 * @author: Huanglh
 * @create: 2022-01-12 16:45
 **/
@Slf4j
public abstract class AbstractStrategyHandle implements IStrategyHandle {
    // 配置点
    @Setter
    protected String point;
    // 企业配置参数信息
    @Setter
    protected Map<STRATEGY_CODE, BusinessControl> businessControlMap;

    public AbstractStrategyHandle() {
    }

    public AbstractStrategyHandle(String point, Map<STRATEGY_CODE, BusinessControl> businessControlMap) {
        this.point = point;
        this.businessControlMap = businessControlMap;
    }

    /**
     * 获取配置值
     *
     * @param code
     * @param defaultValue
     * @return
     */
    protected String getRgeValue(STRATEGY_CODE code, String defaultValue) {
        BusinessControl inCheckBC = businessControlMap.get(code);
        // 传入的默认值为空，则取枚举的默认值
        if (ObjectUtils.isNull(defaultValue)) {
            defaultValue = code.getDefaultValue();
        }
        return ObjectUtils.isNull(inCheckBC) || StringUtils.isBlank(inCheckBC.getRegValue())
          ? defaultValue : inCheckBC.getRegValue();
    }

    /**
     * 获取参数
     *
     * @param index
     * @param type
     * @param args
     * @param <T>
     * @return
     */
    protected <T> T getArg(Integer index, Class<T> type, Object... args) {
        T res = null;
        if (ObjectUtils.isNull(args)) {
            log.warn("配置参数操作没有任何输入参数");
        } else {
            res = (index < args.length && type.isInstance(args[index])) ? type.cast(args[index]) : null;
        }

        return res;
    }
}
