package com.zbkj.common.utils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

/**
 * @Description: 类描述
 * @date: 2024/11/13 0:09
 * @author: tangz
 * @since JDK 1.8
 **/
public class PrizeDrawUtil {

    public static Integer draw(List<BigDecimal> items) {
        BigDecimal sum = BigDecimal.ZERO;
        for (BigDecimal item : items) {
            sum = sum.add(item);
        }
        Random random = new Random();
        BigDecimal randomValue = new BigDecimal(random.nextDouble()).multiply(sum);
        for (int i = 0; i < items.size(); i++) {
            randomValue = randomValue.subtract(items.get(i));
            if (randomValue.compareTo(BigDecimal.ZERO) <= 0) {
                return i;
            }
        }
        return null; // 应该不会执行到这里，除非items为空
    }
}
