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

import com.zanxiang.game.module.manage.constant.RedisKeyConstant;
import com.zanxiang.game.module.manage.service.IIpDataAssayService;
import com.zanxiang.game.module.manage.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
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.List;

/**
 * @author : lingfeng
 * @time : 2025-01-15
 * @description : ip解析定时器
 */
@Slf4j
@Component
@RefreshScope
public class IpDataAssayTask {

    @Autowired
    private RedisUtil<String> redisUtil;

    @Autowired
    private IIpDataAssayService ipDataAssayService;

    /**
     * IP解析每分钟检测
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void ipDataAssayCheck() {
        String queueKey = RedisKeyConstant.IP_DATA_ASSAY_QUEUE;
        //redis队列不存在, 或者长度为 0 , 不执行
        if (!redisUtil.hasKey(queueKey) || redisUtil.getSetSize(queueKey) <= 0) {
            return;
        }
        //随机移除取出5千条数据
        List<String> msgList = redisUtil.popOfSet(queueKey, 5000);
        //判断是否获取到数据, 没拿到数据, 结束
        if (CollectionUtils.isEmpty(msgList)) {
            return;
        }
        log.error("IP解析从队列中获取到数据长度 : {}", msgList.size());
        //数据处理执行
        ipDataAssayService.ipDataHandle(msgList);
        log.error("IP解析队列消息消费完成 : {}", msgList.size());
    }
}
