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