package com.zanxiang.game.module.manage.task;

import com.zanxiang.game.module.manage.constant.RedisKeyConstant;
import com.zanxiang.game.module.manage.service.IAppletCheckService;
import com.zanxiang.module.redis.service.IDistributedLockComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;

/**
 * @author : lingfeng
 * @time : 2024-01-20
 * @description : 支付应用任务
 */
@Slf4j
@Component
public class PayApplicationTask {

    @Autowired
    private IDistributedLockComponent distributedLockComponent;

    @Autowired
    private IAppletCheckService appletCheckService;

    /**
     * 服务器域名
     */
    @Value("${taskRun}")
    private Boolean taskRun;

    /**
     * 小程序每10分钟检查
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void payApplicationCheck() {
        if (!taskRun) {
            return;
        }
        //上锁
        if (!distributedLockComponent.doLock(RedisKeyConstant.PAY_APP_CHECK_LOCK, 0L, 15L, TimeUnit.MINUTES)) {
            return;
        }
        log.error("小程序每10分钟封停监控定时器开始执行, startTime : {}", LocalDateTime.now().toString());
        try {
            //执行检测
            appletCheckService.payApplicationCheck();
        } catch (Exception e) {
            log.error("小程序每10分钟封停监控异常, e : {}", e.getMessage());
        } finally {
            //释放锁
            distributedLockComponent.unlock(RedisKeyConstant.PAY_APP_CHECK_LOCK);
        }
        log.error("小程序每10分钟封停监控定时器执行结束 , endTime  : {}", LocalDateTime.now().toString());
    }

}
