package com.cm.common.wechat.startup;

import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties;
import com.cm.common.wechat.manager.officialaccount.WechatOfficialAccountManager;
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.context.annotation.Configuration;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: WechatStartUp
 * @Description: 微信启动项
 * @Author: WangGeng
 * @Date: 2020/3/1 5:05 下午
 * @Version: 1.0
 **/
@Configuration
public class WechatStartUp implements ApplicationRunner {

    private static final Logger LOG = LoggerFactory.getLogger(WechatStartUp.class);

    @Autowired
    private WechatOfficialAccountProperties wechatOfficialAccountProperties;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (wechatOfficialAccountProperties.getActivate() == null || !wechatOfficialAccountProperties.getActivate()) {
            LOG.info("微信公众号未激活");
            return;
        }
        // 初始化公众号AccessToken
        WechatOfficialAccountManager wechatOfficialAccountManager = WechatOfficialAccountManager.getInstance();
        wechatOfficialAccountManager.setWechatOfficialAccountProperties(wechatOfficialAccountProperties);
        wechatOfficialAccountManager.refreshAccessToken();
    }

}
