package com.jeequan.jeepay.thirdparty.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.converter.MchInfoConverter;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.MchApp;
import com.jeequan.jeepay.core.entity.MchInfo;
import com.jeequan.jeepay.core.entity.TransferSubject;
import com.jeequan.jeepay.core.interfaces.IConfigContextQueryService;
import com.jeequan.jeepay.core.model.QRCodeParams;
import com.jeequan.jeepay.core.model.context.MchAppConfigContext;
import com.jeequan.jeepay.core.model.oauth2.AlipayOauth2Params;
import com.jeequan.jeepay.core.model.oauth2.Oauth2Params;
import com.jeequan.jeepay.core.model.oauth2.WxpayOauth2Params;
import com.jeequan.jeepay.core.model.params.IsvParams;
import com.jeequan.jeepay.core.model.params.IsvsubMchParams;
import com.jeequan.jeepay.core.model.params.NormalMchParams;
import com.jeequan.jeepay.core.model.params.alipay.AlipayIsvParams;
import com.jeequan.jeepay.core.model.params.alipay.AlipayNormalMchParams;
import com.jeequan.jeepay.core.model.params.pppay.PpPayNormalMchParams;
import com.jeequan.jeepay.core.model.params.wxpay.WxpayIsvParams;
import com.jeequan.jeepay.core.model.params.wxpay.WxpayNormalMchParams;
import com.jeequan.jeepay.core.model.params.zftpay.ZftpayIsvParams;
import com.jeequan.jeepay.db.entity.*;
import com.jeequan.jeepay.service.impl.*;
import com.jeequan.jeepay.thirdparty.model.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * 配置信息查询服务 （兼容 缓存 和 直接查询方式）
 *
 * @author terrfly
 *
 * @date 2021/11/18 14:41
 */
@Slf4j
@Service
public class ConfigContextQueryService implements IConfigContextQueryService {

    @Autowired
    ConfigContextService configContextService;
    @Autowired
    private MchInfoService mchInfoService;
    @Autowired
    private MchAppService mchAppService;
    @Autowired
    private PayInterfaceConfigService payInterfaceConfigService;
    @Autowired
    private MchPayInterfaceConfigService mchPayInterfaceConfigService;
    @Autowired
    private MchPayPassageService mchPayPassageService;
    @Autowired
    private MchApplymentService mchApplymentService;
    @Autowired
    private MchInfoConverter mchInfoConverter;
    @Autowired
    private MchStoreService mchStoreService;
    @Autowired
    private TransferInterfaceConfigService transferInterfaceConfigService;

    private boolean isCache() {
        return SysConfigService.IS_USE_CACHE;
    }

    @Override
    public MchApp queryMchApp(String mchNo, String mchAppId) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchNo, mchAppId).getMchApp();
        }

        return mchInfoConverter.toModel(mchAppService.getOneByMch(mchNo, mchAppId));
    }

    @Override
    public MchAppConfigContext queryMchInfoAndAppInfo(String mchAppId) {
        return queryMchInfoAndAppInfo(mchAppService.getById(mchAppId).getMchNo(), mchAppId);
    }

    @Override
    public MchAppConfigContext queryMchInfoAndAppInfo(String mchNo, String mchAppId) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchNo, mchAppId);
        }

        MchInfo mchInfo = mchInfoService.getById(mchNo);
        MchApp mchApp = queryMchApp(mchNo, mchAppId);

        if (mchInfo == null || mchApp == null) {
            return null;
        }

        MchAppConfigContext result = new MchAppConfigContext();
        result.setMchInfo(mchInfo);
        result.setMchNo(mchNo);
        result.setMchType(mchInfo.getType());

        result.setMchApp(mchApp);
        result.setAppId(mchAppId);

        return result;
    }

    @Override
    public NormalMchParams queryNormalMchParams(String mchNo, String mchAppId, String ifCode) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchNo, mchAppId).getNormalMchParamsByIfCode(ifCode);
        }

        // 查询商户的所有支持的参数配置
        PayInterfaceConfig payInterfaceConfig = payInterfaceConfigService.lambdaQuery()
                .select(PayInterfaceConfig::getIfCode, PayInterfaceConfig::getIfParams)
                .eq(PayInterfaceConfig::getState, CS.YES)
                .eq(PayInterfaceConfig::getInfoType, CS.SYS_ROLE_TYPE.MCH_APP)
                .eq(PayInterfaceConfig::getInfoId, mchAppId)
                .eq(PayInterfaceConfig::getIfCode, ifCode)
                .one();

        if (payInterfaceConfig == null) {
            return null;
        }

        return NormalMchParams.factory(payInterfaceConfig.getIfCode(), payInterfaceConfig.getIfParams());
    }


    @Override
    public IsvsubMchParams queryIsvsubMchParams(String mchNo, String mchAppId, String ifCode) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchNo, mchAppId).getIsvsubMchParamsByIfCode(ifCode);
        }

        // 查询商户的所有支持的参数配置
        PayInterfaceConfig payInterfaceConfig = payInterfaceConfigService.getOne(PayInterfaceConfig.gw()
                .select(PayInterfaceConfig::getIfCode, PayInterfaceConfig::getIfParams)
                .eq(PayInterfaceConfig::getState, CS.YES)
                .eq(PayInterfaceConfig::getInfoType, CS.SYS_ROLE_TYPE.MCH_APP)
                .eq(PayInterfaceConfig::getInfoId, mchAppId)
                .eq(PayInterfaceConfig::getIfCode, ifCode)
        );

        if (payInterfaceConfig == null) {
            return null;
        }

        return IsvsubMchParams.factory(payInterfaceConfig.getIfCode(), payInterfaceConfig.getIfParams());
    }

    /**
     * 商户号
     *
     * @param mchExtNo
     * @return
     */
    @Override
    public IsvsubMchParams queryNewIsvsubMchParams(String mchExtNo) {
        MchApplyment merchant = mchApplymentService.getById(mchExtNo);
        if (merchant == null) {
            return null;
        }
        return IsvsubMchParams.factory(merchant.getIfCode(), merchant.getSuccResParameter());
    }


    @Override
    public IsvParams queryIsvParams(String isvNo, String ifCode) {

        if (isCache()) {
            IsvConfigBy3rdContext isvConfigContext = configContextService.getIsvConfigContext(isvNo);
            return isvConfigContext == null ? null : isvConfigContext.getIsvParams(ifCode);
        }

        // 查询商户的所有支持的参数配置
        PayInterfaceConfig payInterfaceConfig = payInterfaceConfigService.lambdaQuery()
                .select(PayInterfaceConfig::getIfCode, PayInterfaceConfig::getIfParams, PayInterfaceConfig::getAltId)
                .eq(PayInterfaceConfig::getState, CS.YES)
                .eq(PayInterfaceConfig::getInfoType, CS.SYS_ROLE_TYPE.ISV)
                .eq(PayInterfaceConfig::getInfoId, isvNo)
                .eq(PayInterfaceConfig::getIfCode, ifCode)
                .one();

        if (payInterfaceConfig == null) {
            return null;
        }

        IsvParams isvParams = IsvParams.factory(payInterfaceConfig.getIfCode(), payInterfaceConfig.getIfParams());
        isvParams.setAltId(payInterfaceConfig.getAltId());
        return isvParams;
    }

    @Override
    public IsvParams queryTransferIsvParams(TransferSubject transferSubject) {
        return queryTransferIsvParams(transferSubject.getIsvNo(), transferSubject.getTransIfCode());
    }

    @Override
    public IsvParams queryTransferIsvParams(String isvNo, String transIfCode) {

        if (isCache()) {
            IsvConfigBy3rdContext isvConfigContext = configContextService.getIsvConfigContext(isvNo);
            return isvConfigContext == null ? null : isvConfigContext.getIsvParams(transIfCode);
        }

        // 查询商户的所有支持的参数配置
        TransferInterfaceConfigEntity interfaceConfig = transferInterfaceConfigService.lambdaQuery()
                .eq(TransferInterfaceConfigEntity::getState, CS.YES)
                .eq(TransferInterfaceConfigEntity::getInfoType, CS.SYS_ROLE_TYPE.ISV)
                .eq(TransferInterfaceConfigEntity::getInfoId, isvNo)
                .eq(TransferInterfaceConfigEntity::getTransIfCode, transIfCode)
                .one();

        if (interfaceConfig == null) {
            return null;
        }

        return IsvParams.factory(interfaceConfig.getTransIfCode(), interfaceConfig.getTransIfParams());
    }

    /**
     * 商户进件后，通过该方法获取渠道参数
     *
     * @return
     */
    @Override
    public IsvParams queryIsvParams(com.jeequan.jeepay.core.entity.MchApplyment mchApplyment) {
        return queryIsvParams(mchApplyment.getIsvNo(), mchApplyment.getIfCode());
    }

    @Override
    public Oauth2Params queryIsvOauth2Params(String isvNo, String ifCode) {
        if (isCache()) {
            IsvConfigBy3rdContext isvConfigContext = configContextService.getIsvConfigContext(isvNo);
            return isvConfigContext == null ? null : isvConfigContext.getOauth2ParamsByIfCode(ifCode);
        }

        PayInterfaceConfig dbRecord = payInterfaceConfigService.getByInfoIdAndIfCode(CS.SYS_ROLE_TYPE.ISV_OAUTH2, isvNo, ifCode);
        if (dbRecord == null || StringUtils.isEmpty(dbRecord.getIfParams())) {
            return null;
        }

        return Oauth2Params.factory(ifCode, dbRecord.getIfParams());
    }


    // 查询参数，  自动判断是否根据商户选择的 infoId进行查询
    public Oauth2Params queryIsvOauth2ParamsByAutoSelectOauth2InfoId(String isvNo, String ifCode, String selectOauth2InfoId) {
        if (isCache()) {
            IsvConfigBy3rdContext isvConfigContext = configContextService.getIsvConfigContext(isvNo);
            if (isvConfigContext == null) {
                return null;
            }

            // 默认oauth2配置：  逻辑无变化
            if (StringUtils.isEmpty(selectOauth2InfoId)) {
                return isvConfigContext.getOauth2ParamsByIfCode(ifCode);
            }

            // 选择的其他条目
            return isvConfigContext.queryOauth2ParamsMapByDiyList(selectOauth2InfoId, ifCode);
        }

        // selectOauth2InfoId or isvNo
        PayInterfaceConfig dbRecord = payInterfaceConfigService.getByInfoIdAndIfCode(CS.SYS_ROLE_TYPE.ISV_OAUTH2, StringUtils.defaultIfEmpty(selectOauth2InfoId, isvNo), ifCode);
        if (dbRecord == null || StringUtils.isEmpty(dbRecord.getIfParams())) {
            return null;
        }

        return Oauth2Params.factory(ifCode, dbRecord.getIfParams());
    }


    @Override
    public Oauth2Params queryNormalOauth2Params(String mchNo, String mchAppId, String ifCode) {
        if (isCache()) {
            MchAppConfigBy3rdContext mchAppConfigBy3rdContext = configContextService.getMchAppConfigContext(mchNo, mchAppId);
            return mchAppConfigBy3rdContext == null ? null : mchAppConfigBy3rdContext.getOauth2ParamsByIfCode(ifCode);
        }

        PayInterfaceConfig dbRecord = payInterfaceConfigService.getByInfoIdAndIfCode(CS.SYS_ROLE_TYPE.MCH_APP_OAUTH2, mchAppId, ifCode);
        if (dbRecord == null || StringUtils.isEmpty(dbRecord.getIfParams())) {
            return null;
        }

        return Oauth2Params.factory(ifCode, dbRecord.getIfParams());
    }

    @Override
    public <T> T queryOauth2Params(String infoType, String infoId, String ifCode, Class<T> cls) {
        PayInterfaceConfig dbRecord = payInterfaceConfigService.getByInfoIdAndIfCode(infoType, infoId, ifCode);

        if (dbRecord == null || StringUtils.isEmpty(dbRecord.getIfParams())) {
            return null;
        }
        return Oauth2Params.factory(ifCode, dbRecord.getIfParams(), cls);
    }

    public AlipayClientWrapper getAlipayClientWrapper(MchAppConfigContext mchAppConfigContext) {

        if (isCache()) {
            return
                    configContextService.getMchAppConfigContext(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId()).getAlipayClientWrapper();
        }

        if (mchAppConfigContext.isIsvsubMch()) {

            AlipayIsvParams alipayParams = (AlipayIsvParams) queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.ALIPAY);
            return AlipayClientWrapper.buildAlipayClientWrapper(
                    alipayParams,
                    this.queryOauth2Params(CS.SYS_ROLE_TYPE.ISV_OAUTH2, mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.ALIPAY, AlipayOauth2Params.class)
            );

        } else {

            AlipayNormalMchParams alipayParams = (AlipayNormalMchParams) queryNormalMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), CS.IF_CODE.ALIPAY);
            return AlipayClientWrapper.buildAlipayClientWrapper(
                    alipayParams,
                    this.queryOauth2Params(CS.SYS_ROLE_TYPE.MCH_APP_OAUTH2, mchAppConfigContext.getAppId(), CS.IF_CODE.ALIPAY, AlipayOauth2Params.class)
            );

        }

    }


    // 注意： 此函数只能获取oauth2 相关信息，  自动判断 oauth2的选择。
    public AlipayClientWrapper getAlipayClientWrapperByAutoOauth2(MchAppConfigContext mchAppConfigContext, String ifCode) {

        if (isCache()) {

            // 普通商户：  原始无变化
            if (!mchAppConfigContext.isIsvsubMch()) {
                return configContextService.getMchAppConfigContext(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId()).getAlipayClientWrapper();
            }

            MchAppConfigBy3rdContext mchAppConfigBy3rdContext = configContextService.getMchAppConfigContext(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId());

            String oauth2SelectInfoId = mchAppConfigBy3rdContext.getIsvConfigContext().getOauth2SelectedInfoId().get(ifCode);

            // 无配置 infoId ：  逻辑无变化
            if (StringUtils.isEmpty(oauth2SelectInfoId)) {
                return mchAppConfigBy3rdContext.getAlipayClientWrapper();
            }

            // 获取服务商的 其他 oauth2 对象
            IsvConfigBy3rdContext isvConfigBy3rdContext = (IsvConfigBy3rdContext) mchAppConfigBy3rdContext.getIsvConfigContext();
            return isvConfigBy3rdContext.getAlipayClientWrapperByOauth2diyMap().get(oauth2SelectInfoId);

        }

        // 以下为： 无缓存

        if (mchAppConfigContext.isIsvsubMch()) {

            // 1. 查询出该接口是否使用 默认 oauth2条目。
            String oauth2SelectedInfoId = this.queryAgentOrIsvSelectOauth2InfoId(mchAppConfigContext.getMchApplyment(), ifCode);

            // 无配置， 则使用服务商的默认参数， 否则使用配置参数。
            String selectInfoId = StringUtils.isEmpty(oauth2SelectedInfoId) ? mchAppConfigContext.getMchApplyment().getIsvNo() : oauth2SelectedInfoId;

            return AlipayClientWrapper.buildAlipayClientWrapper(
                    (AlipayIsvParams) null,
                    this.queryOauth2Params(CS.SYS_ROLE_TYPE.ISV_OAUTH2, selectInfoId, CS.IF_CODE.ALIPAY, AlipayOauth2Params.class)
            );

        } else {  // 普通商户

            AlipayNormalMchParams alipayParams = (AlipayNormalMchParams) queryNormalMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), CS.IF_CODE.ALIPAY);
            return AlipayClientWrapper.buildAlipayClientWrapper(
                    alipayParams,
                    this.queryOauth2Params(CS.SYS_ROLE_TYPE.MCH_APP_OAUTH2, mchAppConfigContext.getAppId(), CS.IF_CODE.ALIPAY, AlipayOauth2Params.class)
            );

        }
    }


    public ZftpayClientWrapper getZftpayClientWrapper(MchAppConfigContext mchAppConfigContext) {
        if (isCache()) {
            return configContextService.getIsvConfigContext(mchAppConfigContext.getMchApplyment().getIsvNo()).getZftpayClientWrapper();
        }

        ZftpayIsvParams alipayParams = (ZftpayIsvParams) queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.ZFTPAY);
        return ZftpayClientWrapper.buildZftpayClientWrapper(alipayParams);
    }

    public ZftpayClientWrapper getZftpayClientWrapper(String isvNo) {
        if (isCache()) {
            return configContextService.getIsvConfigContext(isvNo).getZftpayClientWrapper();
        }
        ZftpayIsvParams alipayParams = (ZftpayIsvParams) queryIsvParams(isvNo, CS.IF_CODE.ZFTPAY);
        return ZftpayClientWrapper.buildZftpayClientWrapper(alipayParams);
    }

    public WxServiceWrapper getWxServiceWrapper(MchAppConfigContext mchAppConfigContext) { // TODO

        if (isCache()) {
            return
                    configContextService.getMchAppConfigContext(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId()).getWxServiceWrapper();
        }

        if (mchAppConfigContext.isIsvsubMch()) {

            WxpayIsvParams wxParams = (WxpayIsvParams) queryIsvParams(mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.WXPAY);
            return WxServiceWrapper.buildWxServiceWrapper(wxParams,
                    this.queryOauth2Params(CS.SYS_ROLE_TYPE.ISV_OAUTH2, mchAppConfigContext.getMchApplyment().getIsvNo(), CS.IF_CODE.WXPAY, WxpayOauth2Params.class)
            );

        } else {

            WxpayNormalMchParams wxParams = (WxpayNormalMchParams) queryNormalMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), CS.IF_CODE.WXPAY);
            return WxServiceWrapper.buildWxServiceWrapper(wxParams,
                    this.queryOauth2Params(CS.SYS_ROLE_TYPE.MCH_APP_OAUTH2, mchAppConfigContext.getAppId(), CS.IF_CODE.WXPAY, WxpayOauth2Params.class)
            );

        }

    }

    public WxServiceWrapper getWxServiceWrapper(TransferWalletEntity walletEntity, WxpayIsvParams isvParams) {
        return WxServiceWrapper.buildWxServiceWrapper(isvParams,
                this.queryOauth2Params(CS.SYS_ROLE_TYPE.ISV_OAUTH2, walletEntity.getIsvNo(), CS.IF_CODE.WXPAY, WxpayOauth2Params.class)
        );
    }


    // 注意： 此函数只能获取oauth2 相关信息，  自动判断 oauth2的选择。
    public WxServiceWrapper getWxServiceWrapperByAutoOauth2(MchAppConfigContext mchAppConfigContext, String ifCode) {

        if (isCache()) {

            // 普通商户：  原始无变化
            if (!mchAppConfigContext.isIsvsubMch()) {
                return configContextService.getMchAppConfigContext(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId()).getWxServiceWrapper();
            }

            MchAppConfigBy3rdContext mchAppConfigBy3rdContext = configContextService.getMchAppConfigContext(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId());

            String oauth2SelectInfoId = mchAppConfigBy3rdContext.getIsvConfigContext().getOauth2SelectedInfoId().get(ifCode);

            // 无配置 infoId ：  逻辑无变化
            if (StringUtils.isEmpty(oauth2SelectInfoId)) {
                return mchAppConfigBy3rdContext.getWxServiceWrapper();
            }

            // 获取服务商的 其他 oauth2 对象
            IsvConfigBy3rdContext isvConfigBy3rdContext = (IsvConfigBy3rdContext) mchAppConfigBy3rdContext.getIsvConfigContext();
            return isvConfigBy3rdContext.getWxServiceWrapperByOauth2diyMap().get(oauth2SelectInfoId);

        }

        // 以下为： 无缓存

        // 特约商户
        if (mchAppConfigContext.isIsvsubMch()) {

            // 1. 查询出该接口是否使用 默认 oauth2条目。
            String oauth2SelectedInfoId = this.queryAgentOrIsvSelectOauth2InfoId(mchAppConfigContext.getMchApplyment(), ifCode);

            // 无配置， 则使用服务商的默认参数， 否则使用配置参数。
            String selectInfoId = StringUtils.isEmpty(oauth2SelectedInfoId) ? mchAppConfigContext.getMchApplyment().getIsvNo() : oauth2SelectedInfoId;

            return WxServiceWrapper.buildWxServiceWrapper((WxpayIsvParams) null,
                    this.queryOauth2Params(CS.SYS_ROLE_TYPE.ISV_OAUTH2, selectInfoId, CS.IF_CODE.WXPAY, WxpayOauth2Params.class)
            );

        } else { // 普通商户

            WxpayNormalMchParams wxParams = (WxpayNormalMchParams) queryNormalMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), CS.IF_CODE.WXPAY);
            return WxServiceWrapper.buildWxServiceWrapper(wxParams,
                    this.queryOauth2Params(CS.SYS_ROLE_TYPE.MCH_APP_OAUTH2, mchAppConfigContext.getAppId(), CS.IF_CODE.WXPAY, WxpayOauth2Params.class)
            );
        }
    }


    public PaypalWrapper getPaypalWrapper(MchAppConfigContext mchAppConfigContext) {

        if (isCache()) {
            return
                    configContextService.getMchAppConfigContext(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId()).getPaypalWrapper();
        }

        PpPayNormalMchParams ppPayNormalMchParams = (PpPayNormalMchParams) queryNormalMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), CS.IF_CODE.PPPAY);
        return PaypalWrapper.buildPaypalWrapper(ppPayNormalMchParams);

    }


    /**
     * 根据页面， 获取到 ifCode
     **/
    public String queryIfCodeByPageType(MchAppConfigContext mchAppConfigContext, String currentPageType) {

        if (StringUtils.isEmpty(currentPageType)) {
            return null;
        }

        String wayCode = "";
        if (currentPageType.equals(QRCodeParams.PAGE_TYPE_ALIPAY_H5)) {

            wayCode = CS.PAY_WAY_CODE.ALI_JSAPI;

        } else if (currentPageType.equals(QRCodeParams.PAGE_TYPE_ALIPAY_LITE)) {
            wayCode = CS.PAY_WAY_CODE.ALI_LITE;

        } else if (currentPageType.equals(QRCodeParams.PAGE_TYPE_WECHAT_H5)) {
            wayCode = CS.PAY_WAY_CODE.WX_JSAPI;

        } else if (currentPageType.equals(QRCodeParams.PAGE_TYPE_WECHAT_LITE)) {
            wayCode = CS.PAY_WAY_CODE.WX_LITE;
        }

        MchPayPassage payPassage = mchPayPassageService.findMchPayPassage(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), wayCode);
        return payPassage == null ? null : payPassage.getIfCode();
    }


    /***
     *
     * 查询 服务商 or 服务商配置的 auth2记录
     * 如果服务商配置了， 那么选择服务商的，
     * 如果没有配置， 那么使用服务商配置（原逻辑）
     * **/
    public String queryAgentOrIsvSelectOauth2InfoId(String agentNo, String isvNo, String ifCode) {

        // 没有服务商 （ 原逻辑 ）
        if (StringUtils.isEmpty(agentNo)) {
            return payInterfaceConfigService.queryIsvSelectOauth2InfoId(isvNo, ifCode);
        }

        String result = payInterfaceConfigService.queryAgentSelectOauth2InfoId(agentNo, ifCode);

        // 服务商配置不为空， 说明有配置（ 选择服务商默认也是有值的 ）
        if (StringUtils.isNotEmpty(result)) {
            return result;
        }

        // 使用服务商配置
        return payInterfaceConfigService.queryIsvSelectOauth2InfoId(isvNo, ifCode);
    }

    /***
     * 方法重载
     * **/
    public String queryAgentOrIsvSelectOauth2InfoId(com.jeequan.jeepay.core.entity.MchApplyment mchApplyment, String ifCode) {
        return queryAgentOrIsvSelectOauth2InfoId(mchApplyment.getAgentNo(), mchApplyment.getIsvNo(), ifCode);
    }


    /**
     * 查询服务商进件特殊参数
     */
    public JSONObject queryAgentApplymentExtParams(String agentNo, String ifCode) {

        PayInterfaceConfig payInterfaceConfig = payInterfaceConfigService.getByInfoIdAndIfCode(CS.SYS_ROLE_TYPE.AGENT, agentNo, ifCode);
        if (payInterfaceConfig == null || StringUtils.isEmpty(payInterfaceConfig.getApplymentExtParams())) {
            return new JSONObject();
        }
        return JSON.parseObject(payInterfaceConfig.getApplymentExtParams());
    }


    @Override
    public MchAppConfigContext queryMchInfoAndAppInfoV2(String mchNo, String mchAppId, String applyId) {
        MchAppConfigContext mchAppConfigContext = null;
        if (StringUtils.isNotEmpty(mchAppId)) {
            mchAppConfigContext = queryMchInfoAndAppInfo(mchNo, mchAppId);
        }
        if (StringUtils.isEmpty(applyId)) {
            MchStore mchStore = mchStoreService.queryDefaultStore(mchNo);
            //获取默认门店绑定的applyId
            applyId = mchStore.getMchApplyId();
        }
        MchApplyment mchApplyment = mchApplymentService.getById(applyId);
        if (mchAppConfigContext == null) {
            mchAppConfigContext = queryMchInfoAndAppInfo(mchNo, mchApplyment.getAutoConfigMchAppId());
        }
        mchAppConfigContext.setMchApplyment(mchInfoConverter.toModel(mchApplyment));
        mchAppConfigContext.getMchInfo().setIsvNo(mchApplyment.getIsvNo()).setMchName(mchApplyment.getMchFullName()).setMchShortName(mchApplyment.getMchShortName());
        return mchAppConfigContext;
    }


}
