package com.fs.qw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.RedisUtil;
import com.fs.qw.domain.QwGroupActual;
import com.fs.qw.mapper.QwGroupActualMapper;
import com.fs.qw.service.IQwGroupActualService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 群活码实际二维码表 服务实现类
 * </p>
 *
 * @author zhangqin
 * @since 2025-11-28
 */
@Service
public class QwGroupActualServiceImpl extends ServiceImpl<QwGroupActualMapper, QwGroupActual> implements IQwGroupActualService {

    @Autowired
    private RedisUtil redisUtil;
    // 轮询机计数
    private final static String MY_ITEM_CURSOR = "new-adv:cursor:";

    @Override
    public QwGroupActual getGroupActualByAllocationRuleId(Long allocationRuleId) {
        List<QwGroupActual> list = list(new LambdaQueryWrapper<QwGroupActual>()
                .eq(QwGroupActual::getLiveCodeId, allocationRuleId));
        int total = list.size();
        long cursor = redisUtil.increment(MY_ITEM_CURSOR);
        int index = (int) ((cursor - 1) % total);
        return list.get(index);
    }
}
