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

import ink.wgink.module.wechat.officialaccount.manager.OfficialAccountAccessTokenManager;
import ink.wgink.properties.wechat.official.account.OfficialAccountProperties;
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: OfficialAccountStartup
 * @Description:
 * @Author: wanggeng
 * @Date: 2022/6/22 22:38
 * @Version: 1.0
 */
@Component
public class OfficialAccountStartup implements ApplicationRunner {

    private static final Logger LOG = LoggerFactory.getLogger(OfficialAccountStartup.class);
    @Autowired
    private OfficialAccountProperties officialAccountProperties;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        refreshOfficialAccountAccessToken();
    }

    @Scheduled(cron = "0 0/5 * * * ?")
    public void refreshOfficialAccountAccessToken() {
        if (!officialAccountProperties.getActivate()) {
            return;
        }
        OfficialAccountAccessTokenManager.getInstance().refreshAccessToken(officialAccountProperties);
    }
}
