随机抽奖代码片段--Java

	public String turntable(int customerId){
		//查询用户积分
		CustomerScore userscore = customerScoreDao.findByCustomerId(customerId);
		//积分不足
		if(null==userscore)
			return ActivityResponse.backFail(ActivityBackType.FAIL);
		if(userscore.getAvailableScore()<200)
			return ActivityResponse.backFail(ActivityBackType.NO_SCORE);
		//扣除积分
		scoreRecordService.addScoreRecord(customerId, 200, ScoreOperType.TURN_TABLE_SUB, null);
		//开始抽奖
		List<Integer> amtList = Lists.newArrayList();
		int chance = 0;
		int max = 0;
		int min = 1;
		int pre = 0;
		int next = 0;
			
		// 遍历分类产品
		List<ActivityConst> lists = activityConstDao.listByTag("turnTable");
		
		for(ActivityConst c : lists)
			amtList.add(c.getCountAll().intValue());
		
		max = activityConstDao.sumCountAllByTag("turnTable");
		
		Random random = new Random();
		chance = random.nextInt(max) % (max - min + 1) + min;
		for (int i = 0; i < lists.size(); i++) {
			// special case
			if (i == 0) {
				if (1 <= chance && chance <= amtList.get(i)) {
					return updateTurnTableDb(lists.get(i), customerId);
				}
				next = amtList.get(i);
				continue;
			}
			// normal case
			pre = next + 1;
			next = pre + amtList.get(i) - 1;
			if (pre <= chance && chance <= next) {
				return updateTurnTableDb(lists.get(i), customerId);
			}
		}		
		
		
		
		return ActivityResponse.backFail(ActivityBackType.FAIL);
	}
	

	private String updateTurnTableDb(ActivityConst ac, int customerId){
		ActivityLottery lottery = new ActivityLottery();
		lottery.setActivityTag(ac.getActivityTag());
		lottery.setCustomerId(customerId);
		lottery.setLotteryName(ac.getLotteryName());
		lottery.setSupportExchange(ac.isSupportExchange());
		lottery.setUseScore(200l);
		
		if(ac.getLotteryName().indexOf("积分")>=0){
			scoreRecordService.addScoreRecord(customerId, ac.getPrice().intValue(), ScoreOperType.TURN_TABLE_PLUS, null);
		}
		else if(ac.getLotteryName().indexOf("现金红包")>=0){
			if(productSubAccountDao.countCustomerInvestTimes(customerId)==0){
				scoreRecordService.addScoreRecord(customerId, 200, ScoreOperType.TURN_TABLE_PLUS, null);
				return turntable(customerId);
			}
			else{
	            userTicketService.autoGive(TriggerPoints.fromString(ac.getTriggerPoints()),customerId);
	            userTicketService.autoEffective(TriggerPoints.fromString(ac.getTriggerPoints()),customerId);
			}
		}
		else if(ac.getLotteryName().indexOf("代金券")>=0){
            userTicketService.autoGive(TriggerPoints.fromString(ac.getTriggerPoints()),customerId);
            userTicketService.autoEffective(TriggerPoints.fromString(ac.getTriggerPoints()),customerId);
		}
		else if(ac.getLotteryName().indexOf("蓝牙耳机")>=0){
			if(productSubAccountDao.countCustomerInvestTimes(customerId)==0){
				scoreRecordService.addScoreRecord(customerId, 200, ScoreOperType.TURN_TABLE_PLUS, null);
				return turntable(customerId);
			}
		}
		else if(ac.getLotteryName().indexOf("小米平衡车")>=0){
			if(productSubAccountDao.countCustomerInvestTimes(customerId)==0){
				scoreRecordService.addScoreRecord(customerId, 200, ScoreOperType.TURN_TABLE_PLUS, null);
				return turntable(customerId);
			}
			if(activityLotteryDao.countTimesByName(ac.getActivityTag())<10000){
				scoreRecordService.addScoreRecord(customerId, 200, ScoreOperType.TURN_TABLE_PLUS, null);
				return turntable(customerId);
			}
		}
		
		activityLotteryDao.save(lottery);
		
		return ActivityResponse.backSuc(ac.getLotteryName());
	}
	


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值