package com.jeequan.jeepay.core.model.context;

import com.jeequan.jeepay.core.entity.MchApp;
import com.jeequan.jeepay.core.entity.MchApplyment;
import com.jeequan.jeepay.core.entity.MchInfo;
import com.jeequan.jeepay.core.entity.MchStore;
import com.jeequan.jeepay.core.model.applyment.PaywayFee;
import com.jeequan.jeepay.core.model.oauth2.Oauth2Params;
import com.jeequan.jeepay.core.model.params.IsvsubMchParams;
import com.jeequan.jeepay.core.model.params.NormalMchParams;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.Map;

/*
* 商户应用支付参数信息
* 放置到内存， 避免多次查询操作
*
* @author terrfly
*
* @date 2021/6/8 17:29
*/
@Data
@Accessors(chain = true)
public class MchAppConfigContext {


    /** 商户信息缓存 */
    private String mchNo;
    private String appId;
    private Byte mchType;
    private MchInfo mchInfo;
    private MchApp mchApp;
    private MchApplyment mchApplyment;
    private PaywayFee tradeRate;
    private PaywayFee d0Rate;
    private MchStore mchStore;

    /** 商户支付配置信息缓存,  <接口代码, 支付参数>  */
    private Map<String, NormalMchParams> normalMchParamsMap = new HashMap<>();
    private Map<String, IsvsubMchParams> isvsubMchParamsMap = new HashMap<>();

    /** oauth2参数信息缓存 (应该只有普通商户拥有该参数) */
    private Map<String, Oauth2Params> oauth2ParamsMap = new HashMap<>();

    /** 放置所属服务商的信息 **/
    protected IsvConfigContext isvConfigContext;

    /** 获取普通商户配置信息 **/
    public NormalMchParams getNormalMchParamsByIfCode(String ifCode){
        return normalMchParamsMap.get(ifCode);
    }


    /** 获取isv配置信息 **/
    public <T extends NormalMchParams> T getNormalMchParamsByIfCode(String ifCode, Class<T> cls) {
        NormalMchParams mchParams = normalMchParamsMap.get(ifCode);
        if (cls.isInstance(mchParams)) {
            return cls.cast(mchParams);
        }
        return null;
    }

    /** 获取特约商户配置信息 **/
    public IsvsubMchParams getIsvsubMchParamsByIfCode(String ifCode){
        return isvsubMchParamsMap.get(ifCode);
    }

    /** 获取isv配置信息 **/
    public <T extends IsvsubMchParams> T getIsvsubMchParamsByIfCode(String ifCode, Class<T> cls) {
        IsvsubMchParams mchParams = isvsubMchParamsMap.get(ifCode);
        if (cls.isInstance(mchParams)) {
            return cls.cast(mchParams);
        }
        return null;
    }

    /** 获取isv oatuh2配置信息 **/
    public Oauth2Params getOauth2ParamsByIfCode(String ifCode){
        return oauth2ParamsMap.get(ifCode);
    }

    /** 获取isv oatuh2配置信息 **/
    public <T extends Oauth2Params> T getOauth2ParamsByIfCode(String ifCode, Class<T> cls) {
        Oauth2Params mchParams = oauth2ParamsMap.get(ifCode);
        if (cls.isInstance(mchParams)) {
            return cls.cast(mchParams);
        }
        return null;
    }

    /** 是否为 服务商特约商户 **/
    public boolean isIsvsubMch(){
        return this.mchType == MchInfo.TYPE_ISVSUB;
    }

}
