package com.cm.common.wechat.task;

import com.cm.common.wechat.config.pojo.WechatOfficialAccountProperties;
import com.cm.common.wechat.manager.miniapp.WechatMiniAppManager;
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.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: WechatTask
 * @Description: 微信定时任务
 * @Author: WangGeng
 * @Date: 2020/3/1 2:55 下午
 * @Version: 1.0
 **/
@Configuration
@EnableScheduling
public class WechatTask {

    private static final Logger LOG = LoggerFactory.getLogger(WechatTask.class);
    @Autowired
    private WechatOfficialAccountProperties wechatOfficialAccountProperties;

    /**
     * 刷新公众号AccessToken，每10分钟刷新一次
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void refreshOfficialAccountAccessToken() {
        if (wechatOfficialAccountProperties.getActivate() == null || !wechatOfficialAccountProperties.getActivate()) {
            LOG.info("微信公众号未激活，取消刷新");
            return;
        }
        WechatOfficialAccountManager.getInstance().refreshAccessToken();
    }

    /**
     * 删除小程序超时用户，每10分钟执行一次
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void removeTimeoutMiniAppUser() {
        WechatMiniAppManager.getInstance().removeTimeoutUser();
    }

}
