抽奖工具类及实例

public class LotteryUtils {

    public static Lottery lottery(List<Lottery> lotteries) {
        // 总的概率区间
        float totalPro = 0f;
        // 存储每个奖品新的概率区间
        List<Float> proSection = new ArrayList<Float>();
        proSection.add(0f);
        float totalratio = 0;
        // 遍历每个奖品,设置概率区间,总的概率区间为每个概率区间的总和
        for (Lottery lottery : lotteries) {
            // 每个概率区间为奖品概率乘以1000(把三位小数转换为整)再乘以剩余奖品数量
            totalPro += lottery.getRatio() * 10*(lottery.getNum()-lottery.getLeadnum());
            proSection.add(totalPro);
            totalratio = totalratio + lottery.getRatio();
        }
        if(totalratio<100){
            totalPro = totalPro*100/totalratio;
            proSection.add(totalPro);
        }
        // 获取总的概率区间中的随机数
        Random random = new Random();
        float randomPro = (float) random.nextInt((int) totalPro);
        // 判断取到的随机数在哪个奖品的概率区间中
        for (int i = 0, size = proSection.size(); i < size; i++) {
            if (randomPro >= proSection.get(i) && randomPro < proSection.get(i + 1)) {
                if(i<=lotteries.size()-1){
                    if(lotteries.get(i).getLeadnum()<lotteries.get(i).getNum()){
                        return lotteries.get(i);
                    }
                }
            }
        }
        return null;
    }


}

  //奖品列表
        List<Lottery> lotteryList = lotteryManager.getAll();
        //抽奖
        Lottery lottery = LotteryUtils.lottery(lotteryList);
        if (lottery != null) {//0,IphoneX,1季度G家PLUS会员,2, 50元无门槛优惠券,
            // 3, 20元无门槛优惠券,4, 500积分,5, 200积分
            if (lottery.getType() == 0) {
                return new Result(Result.ERROR, "很遗憾未中奖", null);
            } else if (lottery.getType() == 1) {
                if (customer.getIdentity() == 0) {//普通
                    customer.setIdentity(1);

                    Calendar calendar = new GregorianCalendar();
                    Date date = new Date();
                    calendar.setTime(date);
                    calendar.add(calendar.DAY_OF_MONTH, 3);
                    date = calendar.getTime();
                    customer.setExpiredate(date);
                    customerManager.saveOrUpdate(customer);

                } else {
                    Calendar calendar = new GregorianCalendar();
                    Date date = customer.getExpiredate();
                    calendar.setTime(date);
                    calendar.add(calendar.DAY_OF_MONTH, 3);
                    date = calendar.getTime();
                    customer.setExpiredate(date);
                    customerManager.saveOrUpdate(customer);

                }
                String content = "恭喜你获得季度G家PLUS会员";
                addLotteryrecord(customer, lottery.getId(), lottery.getType(), content);
            } else if (lottery.getType() == 2) {
                Calendar cal = Calendar.getInstance();
                cal.add(cal.MONTH, 1);
                addCustomerCoupon(customerid, "抽奖获得50元无门槛优惠券", 50D, cal.getTime());
                String content = "恭喜你获得50元无门槛优惠券";
                addLotteryrecord(customer, lottery.getId(), lottery.getType(), content);
            } else if (lottery.getType() == 3) {
                Calendar cal = Calendar.getInstance();
                cal.add(cal.MONTH, 1);
                addCustomerCoupon(customerid, "抽奖获得20元无门槛优惠券", 20D, cal.getTime());
                String content = "恭喜你获得20元无门槛优惠券";
                addLotteryrecord(customer, lottery.getId(), lottery.getType(), content);
            } else if (lottery.getType() == 4) {
                customer.setIntegral(customer.getIntegral() + 500);
                customerManager.saveOrUpdate(customer);
                String integralremark = "抽奖获得:500积分";
                addIntegralHistory(customer.getId(), 500, integralremark, 4, null);
                String content = "恭喜你获得500积分";
                addLotteryrecord(customer, lottery.getId(), lottery.getType(), content);
            } else {
                customer.setIntegral(customer.getIntegral() + 200);
                customerManager.saveOrUpdate(customer);
                String integralremark = "抽奖获得:200积分";
                addIntegralHistory(customer.getId(), 200, integralremark, 4, null);
                String content = "恭喜你获得200积分";
                addLotteryrecord(customer, lottery.getId(), lottery.getType(), content);
            }
            //领奖数目减一
            lottery.setLeadnum(lottery.getLeadnum() + 1);
            lotteryManager.saveOrUpdate(lottery);

        } else {
            return new Result(Result.ERROR, "本次活动奖品已抽完", null);
        }
        resMap.put("lottery", lottery);
        return new Result(Result.SUCCESS, Result.SUCCESS_MSG, resMap);

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值