package com.yiqi.init;

import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import com.yiqi.common.config.*;
import com.yiqi.common.enums.SystemEnv;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.io.*;
import java.security.PrivateKey;

/**
 * @author xiaohei
 * @date 2024/1/28
 */
@Component
public class ApplicationListenerImpl implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private RuoYiConfig ruoYiConfig;
    @Autowired
    private WxMaProperties wxMaProperties;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        SystemEnvConfig.env = ruoYiConfig.getEnv();
        SystemEnvConfig.baseUrl = ruoYiConfig.getBaseUrl();
        AlipayConfigs.init();
        WxPayConfigs.init();


    }

    /**
     * 获取商户的私钥文件
     *
     * @param keyPemPath
     * @return
     */
    private PrivateKey getPrivateKey(String keyPemPath) {
        File file = new File(keyPemPath);
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return PemUtil.loadPrivateKey(inputStream);
    }
}