package com.jeesharp.business.modules.pay.paytype.pufapay.config;

import cn.hutool.core.util.StrUtil;
import com.jeesharp.business.modules.pay.paypufaconfig.entity.PayPufaConfig;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import jakarta.annotation.PostConstruct;

/**
 * 浦发支付接口参数
 *
 * @author wangmeng
 * @date 2023/5/18 14:59
 */
@Component
@ConfigurationProperties
@PropertySource(value = "classpath:pufa-pay.properties", encoding = "UTF-8")
@Data
public class PuFaPayConfig {

    /**
     * 商户号
     */
    @Value("${spdbMrcNO}")
    private String spdbMrcNO;
    /**
     * 终端号
     */
    @Value("${terminalNo}")
    private String terminalNo;
    /**
     * ClientID
     */
    @Value("${clientId}")
    private String clientId;
    /**
     * 商户秘钥
     */
    @Value("${secret}")
    private String secret;
    /**
     * 商户私钥
     */
    @Value("${privateKey}")
    private String privateKey;
    /**
     * 浦发公钥
     */
    @Value("${spdbPublicKey}")
    private String spdbPublicKey;

    /**
     * 系统回调地址
     */
    @Value("${notifyUrl}")
    private String notifyUrl;

    /**
     * APP支付交易查证
     */
    @Value("${appPayStatus}")
    private String appPayStatus;
    /**
     * APP支付退货交易
     */
    @Value("${appPayReturn}")
    private String appPayReturn;
    /**
     * APP支付撤销交易
     */
    @Value("${appPayCancel}")
    private String appPayCancel;
    /**
     * APP支付交易
     */
    @Value("${appPayTrade}")
    private String appPayTrade;

    public static String SPDB_MRC_NO;
    public static String TERMINAL_NO;
    public static String CLIENT_ID;
    public static String SECRET;
    public static String PRIVATE_KEY;
    public static String SPDB_PUBLIC_KEY;
    public static String NOTIFY_URL;
    public static String APP_PAY_STATUS;
    public static String APP_PAY_RETURN;
    public static String APP_PAY_CANCEL;
    public static String APP_PAY_TRADE;

    @PostConstruct
    public void init() {
        applyFromProperties();
    }

    private void applyFromProperties() {
        SPDB_MRC_NO = this.spdbMrcNO;
        TERMINAL_NO = this.terminalNo;
        CLIENT_ID = this.clientId;
        SECRET = this.secret;
        PRIVATE_KEY = this.privateKey;
        SPDB_PUBLIC_KEY = this.spdbPublicKey;
        APP_PAY_STATUS = this.appPayStatus;
        APP_PAY_RETURN = this.appPayReturn;
        APP_PAY_CANCEL = this.appPayCancel;
        APP_PAY_TRADE = this.appPayTrade;
        NOTIFY_URL = this.notifyUrl;
    }

    /**
     * 将库表配置写入静态字段，供 {@code PfPayUtils} 使用。
     */
    public static void applyFrom(PayPufaConfig config, String notifyUrl) {
        if (config == null) {
            return;
        }
        if (StrUtil.isNotBlank(config.getSpdbMrcNo())) {
            SPDB_MRC_NO = config.getSpdbMrcNo();
        }
        if (StrUtil.isNotBlank(config.getTerminalNo())) {
            TERMINAL_NO = config.getTerminalNo();
        }
        if (StrUtil.isNotBlank(config.getClientId())) {
            CLIENT_ID = config.getClientId();
        }
        if (StrUtil.isNotBlank(config.getSecret())) {
            SECRET = config.getSecret();
        }
        if (StrUtil.isNotBlank(config.getPrivateKey())) {
            PRIVATE_KEY = config.getPrivateKey();
        }
        if (StrUtil.isNotBlank(config.getSpdbPublicKey())) {
            SPDB_PUBLIC_KEY = config.getSpdbPublicKey();
        }
        if (StrUtil.isNotBlank(config.getAppPayTrade())) {
            APP_PAY_TRADE = config.getAppPayTrade();
        }
        if (StrUtil.isNotBlank(config.getAppPayStatus())) {
            APP_PAY_STATUS = config.getAppPayStatus();
        }
        if (StrUtil.isNotBlank(config.getAppPayReturn())) {
            APP_PAY_RETURN = config.getAppPayReturn();
        }
        if (StrUtil.isNotBlank(config.getAppPayCancel())) {
            APP_PAY_CANCEL = config.getAppPayCancel();
        }
        if (StrUtil.isNotBlank(notifyUrl)) {
            NOTIFY_URL = notifyUrl;
        }
    }
}
