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);
}