package com.jeesharp.business.modules.pay.paychannelconfig.support;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.pay.paychannelconfig.entity.PayChannelConfig;
import com.jeesharp.business.modules.pay.payalipayconfig.entity.PayAlipayConfig;
import com.jeesharp.business.modules.pay.paypufaconfig.entity.PayPufaConfig;
import com.jeesharp.business.modules.pay.paywechatconfig.entity.PayWechatConfig;

/**
 * 将渠道公共配置中的回调基地址等合并到微信子配置，供运行时读取。
 *
 * @author jeesharp
 * @since 2026-05-25
 */
public final class PayChannelConfigMergeSupport {

    private PayChannelConfigMergeSupport() {
    }

    /**
     * 以渠道主表为准覆盖回调相关字段（子表未单独维护时）。
     */
    public static PayWechatConfig mergeWechatNotify(PayWechatConfig wechat, PayChannelConfig channel) {
        if (wechat == null) {
            return null;
        }
        if (channel == null) {
            return wechat;
        }
        PayWechatConfig merged = BeanUtil.copyProperties(wechat, PayWechatConfig.class);
        if (StrUtil.isNotBlank(channel.getNotifyBaseUrl())) {
            merged.setNotifyBaseUrl(channel.getNotifyBaseUrl());
        }
        if (StrUtil.isNotBlank(channel.getPayCenterNotifyPath())) {
            merged.setPayCenterNotifyPath(channel.getPayCenterNotifyPath());
        }
        return merged;
    }

    public static PayAlipayConfig mergeAlipayNotify(PayAlipayConfig alipay, PayChannelConfig channel) {
        if (alipay == null) {
            return null;
        }
        if (channel == null) {
            return alipay;
        }
        PayAlipayConfig merged = BeanUtil.copyProperties(alipay, PayAlipayConfig.class);
        if (StrUtil.isNotBlank(channel.getNotifyBaseUrl())) {
            merged.setNotifyBaseUrl(channel.getNotifyBaseUrl());
        }
        if (StrUtil.isNotBlank(channel.getPayCenterNotifyPath())) {
            merged.setPayCenterNotifyPath(channel.getPayCenterNotifyPath());
        }
        return merged;
    }

    public static PayPufaConfig mergePufaNotify(PayPufaConfig pufa, PayChannelConfig channel) {
        if (pufa == null) {
            return null;
        }
        if (channel == null) {
            return pufa;
        }
        PayPufaConfig merged = BeanUtil.copyProperties(pufa, PayPufaConfig.class);
        if (StrUtil.isNotBlank(channel.getNotifyBaseUrl())) {
            merged.setNotifyBaseUrl(channel.getNotifyBaseUrl());
        }
        if (StrUtil.isNotBlank(channel.getPayCenterNotifyPath())) {
            merged.setPayCenterNotifyPath(channel.getPayCenterNotifyPath());
        }
        return merged;
    }
}
