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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zanxiang.game.module.mybatis.entity.CpPushErrorLog;
import com.zanxiang.game.module.sdk.constant.RedisKeyConstant;
import com.zanxiang.game.module.sdk.service.ICpPushErrorLogService;
import com.zanxiang.module.redis.service.IDistributedLockComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author : lingfeng
 * @time : 2024-07-02
 * @description : CP推送数据
 */
@Slf4j
@Component
@RefreshScope
public class CpPushDataTask {

    @Autowired
    private ICpPushErrorLogService cpPushErrorLogService;

    @Autowired
    private IDistributedLockComponent distributedLockComponent;

    /**
     * 无效错误日志定时删除, 1小时执行一次
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void cpPushErrorLogDel() {
        //上锁
        if (!distributedLockComponent.doLock(RedisKeyConstant.CP_PUSH_ERROR_LOG_LOCK, 0L, 30L, TimeUnit.MINUTES)) {
            return;
        }
        log.error("[CP推送数据]无效错误日志定时删除");
        //删除无效的日志
        cpPushErrorLogService.remove(new LambdaQueryWrapper<CpPushErrorLog>()
                .eq(CpPushErrorLog::getStatus, CpPushErrorLog.STATUS_INVALID)
        );
    }
}
