package com.seamew.lottery.domain.strategy.service.algorithm.impl;

import com.seamew.lottery.common.Constants;
import com.seamew.lottery.domain.strategy.annotation.StrategyMode;
import com.seamew.lottery.domain.strategy.service.algorithm.BaseAlgorithm;
import org.springframework.stereotype.Component;

import java.security.SecureRandom;
import java.util.List;

/**
 * @Author: seamew
 * @Title: SingleRateRandomDrawAlgorithm
 * @CreateTime: 2023年02月13日 17:03:00
 * @Description: 单项随机概率抽奖，抽到一个已经排掉的奖品则未中奖
 * @Version: 1.0
 */
@Component
@StrategyMode(strategyMode = Constants.StrategyMode.SINGLE)
public class SingleRateRandomDrawAlgorithm extends BaseAlgorithm {
    @Override
    public String randomDraw(Long strategyId, List<String> excludeAwardIds) {

        // 获取策略对应的元祖
        String[] rateTuple = rateTupleMap.get(strategyId);

        // 随机索引
        int randomVal = generateSecureRandomIntCode(100);
        int index = hashIdx(randomVal);

        // 返回结果
        String awardId = rateTuple[index];

        // 如果中奖ID命中排除奖品列表，则返回NULL
        if (excludeAwardIds.contains(awardId)) {
            return null;
        }

        return awardId;
    }

}
