package ink.wgink.module.wechat.pay.startup;

import ink.wgink.module.wechat.pay.manager.v3.PayManager;
import ink.wgink.module.wechat.pay.service.v3.certificates.ICertificateService;
import ink.wgink.properties.wechat.pay.v3.PayProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @ClassName: PayStartUp
 * @Description:
 * @Author: wanggeng
 * @Date: 2022/6/22 22:55
 * @Version: 1.0
 */
@Component
public class PayStartUp implements ApplicationRunner {
    private static final Logger LOG = LoggerFactory.getLogger(PayStartUp.class);
    @Autowired
    private PayProperties payProperties;
    @Autowired
    private ICertificateService certificateService;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        PayManager payManager = PayManager.getInstance();
        payManager.setCertificateService(certificateService);
        payManager.setPayProperties(payProperties);
        refreshCertificate();
    }

    /**
     * 刷新证书，0小时开始，每6小时执行一次
     */
    @Scheduled(cron = "0 0 0/6 * * ?")
    public void refreshCertificate() throws Exception {
        if (!payProperties.getActive()) {
            return;
        }
        PayManager.getInstance().refreshPlatformCertificate();
    }
}
