package com.gxwebsoft.shop.service.impl;

import com.gxwebsoft.common.core.config.ConfigProperties;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.system.entity.Payment;
import com.gxwebsoft.shop.service.WechatConfigService;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.annotation.Annotation;

import static com.gxwebsoft.shop.consts.OrderPayType.PAY_TYPE_WECHAT;

@Service
public class WechatConfigServiceImpl implements WechatConfigService {
    private RSAAutoCertificateConfig config;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private ConfigProperties configProperties;

    @Override
    public NotificationConfig getNotificationConfig() {
        if (this.config == null) {
            final String uploadPath = configProperties.getUploadPath();
            String key2 = "Payment:".concat(PAY_TYPE_WECHAT.toString()).concat(":").concat("10559");
            final Payment payment = redisUtil.get(key2, Payment.class);
            String privateKey = uploadPath.concat("/file").concat(payment.getApiclientKey());    // 秘钥证书
            String apiClientCert = uploadPath.concat("/file").concat(payment.getApiclientCert());
            this.config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(payment.getMchId())
                    .privateKeyFromPath(privateKey)
                    .merchantSerialNumber(payment.getMerchantSerialNumber())
                    .apiV3Key(payment.getApiKey())
                    .build();
        }
        return this.config;
    }

    @Override
    public Config getConfig() {
        if (this.config == null) {
            final String uploadPath = configProperties.getUploadPath();
            String key2 = "Payment:".concat(PAY_TYPE_WECHAT.toString()).concat(":").concat("10559");
            final Payment payment = redisUtil.get(key2, Payment.class);
            String privateKey = uploadPath.concat("/file").concat(payment.getApiclientKey());    // 秘钥证书
            String apiClientCert = uploadPath.concat("/file").concat(payment.getApiclientCert());
            this.config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(payment.getMchId())
                    .privateKeyFromPath(privateKey)
                    .merchantSerialNumber(payment.getMerchantSerialNumber())
                    .apiV3Key(payment.getApiKey())
                    .build();
        }
        return this.config;
    }

    @Override
    public String value() {
        return "";
    }

    @Override
    public Class<? extends Annotation> annotationType() {
        return null;
    }
}
