package com.fzm.mall.server.front;

import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author lyz
 * @mail lyz@disanbo.com
 * @create 2022/2/10 14:20
 * @description
 */
public class BlindBoxTest {

    public static void main(String[] args) {

        ProdItem prod1 = ProdItem.builder().prodId("111111").isHidden(false).build();
        ProdItem prod2 = ProdItem.builder().prodId("222222").isHidden(false).build();
        ProdItem prod3 = ProdItem.builder().prodId("333333").isHidden(false).build();
        ProdItem prod4 = ProdItem.builder().prodId("444444").isHidden(false).build();
        ProdItem prod5 = ProdItem.builder().prodId("555555").isHidden(true).build();
        ProdItem prod6 = ProdItem.builder().prodId("666666").isHidden(false).build();

        List<ProdItem> prodList = new ArrayList<>();
        prodList.add(prod1);
        prodList.add(prod6);
        prodList.add(prod3);
        prodList.add(prod2);
        prodList.add(prod5);
        prodList.add(prod4);

        System.out.println(genRandomProd(prodList, 1));

    }

    /**
     * @descprition 随机获得产品
     * @author lyz
     * @create 2022/2/14 9:42
     * @param prodList 从数据库获取的库存大于0的盲盒产品列表
     * @param difficulty 正整数[1,10]
     * @return 随机产品编号
     */
    public static String genRandomProd(List<ProdItem> prodList, int difficulty){
        int hiddenWeight = difficulty == 10 ? 1 : (10 - difficulty);

        List<String> baseList = new ArrayList<>();
        for(ProdItem prodItem : prodList){
            if(prodItem.isHidden){
                for(int i=0;i<hiddenWeight;i++){
                    baseList.add(prodItem.prodId);
                }
            }else{
                for(int i=0;i<difficulty;i++){
                    baseList.add(prodItem.prodId);
                }
            }
        }

        //为了增加随机度，可以打开
        //Collections.shuffle(baseList);

        int base = baseList.size();
        int randomNum = ThreadLocalRandom.current().nextInt(base);
        return baseList.get(randomNum);
    }

    @Data
    @Builder
    static class ProdItem {

        private String prodId;

        private boolean isHidden;

    }

}
