package com.jeequan.jeepay.thirdparty.channel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.MchApplyment;
import com.jeequan.jeepay.core.interfaces.paychannel.IIsvmchWrapper;
import com.jeequan.jeepay.core.model.applyment.ApplymentBasicInfo;
import com.jeequan.jeepay.core.model.applyment.PaywayFee;
import com.jeequan.jeepay.db.entity.MchAppEntity;
import com.jeequan.jeepay.db.entity.RateConfig;
import com.jeequan.jeepay.service.impl.MchAppService;
import com.jeequan.jeepay.service.impl.RateConfigService;
import org.springframework.beans.factory.annotation.Autowired;

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

public abstract class AbsIsvmchWrapper<T extends ApplymentBasicInfo> implements IIsvmchWrapper {

    @Autowired
    private RateConfigService rateConfigService;

    @Autowired
    private MchAppService mchAppService;

    /**
     * 根据商户的上下级设置其缺省的费率值
     *
     * @param mchApplyment 商户信息
     */
    @Override
    public final void rateWrap(MchApplyment mchApplyment) {
        JSONObject applyDetailInfo = JSON.parseObject(mchApplyment.getApplyDetailInfo());

        Map<String, PaywayFee> mchApplyRateConfigMap = new HashMap<>();
        Optional.ofNullable(applyDetailInfo.getJSONArray("paywayFeeList"))
                .map(t -> t.toJavaList(PaywayFee.class))
                .map(t -> {
                    t.forEach(t2 -> mchApplyRateConfigMap.put(t2.getWayCode(), t2));
                    return t;
                });

        MchAppEntity mchApp = mchAppService.getById(mchApplyment.getAutoConfigMchAppId());

        String agentNo = mchApplyment.getAgentNo();
        String isvNo = mchApplyment.getIsvNo();

        // 获取商户费率, 一般来说这一步是没必要的，这里单纯做数据初始化吧
        Map<String, PaywayFee> mchRateConfigMap = rateConfigService.queryPaywayFeeMap(agentNo + "_" + RateConfig.MCHRATE, CS.SYS_ROLE_TYPE.MCH_APP, mchApplyment.getIfCode()
                , mchApp.getRange(), mchApp.getMccCode(), mchApplyment.getIsvNo());
        setIfEmpty(mchApplyRateConfigMap, mchRateConfigMap);

        // 获取直属上级费率
        Map<String, PaywayFee> agentMchDefRateConfigMap = rateConfigService.queryPaywayFeeMap(agentNo + "_" + RateConfig.MCHAPPLYDEF, CS.SYS_ROLE_TYPE.AGENT, mchApplyment.getIfCode()
                , mchApp.getRange(), mchApp.getMccCode(), mchApplyment.getIsvNo());
        setIfEmpty(mchRateConfigMap, agentMchDefRateConfigMap);

        // 获取服务商费率信息
        Map<String, PaywayFee> isvRateConfigMap = rateConfigService.queryPaywayFeeMap(isvNo + "_" + RateConfig.MCHAPPLYDEF, CS.SYS_ROLE_TYPE.ISV, mchApplyment.getIfCode()
                , mchApp.getRange(), mchApp.getMccCode(), mchApplyment.getIsvNo());
        setIfEmpty(mchRateConfigMap, isvRateConfigMap);

        mchRateConfigMap = channelInit(mchRateConfigMap, mchApplyment);

        applyDetailInfo.put("paywayFeeList", mchRateConfigMap.values());

        mchApplyment.setApplyDetailInfo(JSON.toJSONString(applyDetailInfo));
    }

    private void setIfEmpty(Map<String, PaywayFee> mchRate, Map<String, PaywayFee> targetConfigMap) {
        targetConfigMap.forEach((wayCode, rateConfig) -> mchRate.putIfAbsent(wayCode, targetConfigMap.get(wayCode)));
    }

    public Map<String, PaywayFee> channelInit(Map<String, PaywayFee> mchRateConfigMap, MchApplyment mchApplyment) {
        return mchRateConfigMap;
    }
}
