java实现大转盘抽奖

    /**
     * 大转盘抽奖
     *
     * @return 中奖信息
     */
    @ResponseBody
    @Transactional
    @RequestMapping(value = "/drawBigWheel", method = RequestMethod.POST)
    public JSONObject drawBigWheel(String eventId, String userId) {
        JSONObject rtn = new JSONObject();
        try {
            logger.debug("drawBigWheel inputData[eventId:{},userId:{}]", eventId, userId);
            CinemaWelfare cinemaWelfare = cinemaWelfareService.getWelfareDetail(eventId);

            if (cinemaWelfare == null || "2".equals(cinemaWelfare.getStatus())) {
                rtn.put("code", "01");
                rtn.put("data", "该活动已被删除或已下线!");
                return rtn;
            }

            if (new Date().compareTo(cinemaWelfare.getStart_time()) < 0) {
                rtn.put("code", "02");
                rtn.put("data", "该活动还未开始!");
                rtn.put("start_time", cinemaWelfare.getDis_start_time());
                return rtn;
            }

            if (new Date().compareTo(cinemaWelfare.getEnd_time()) >= 0) {
                rtn.put("code", "03");
                rtn.put("data", "该活动已结束!");
                return rtn;
            }


            if (!CinemaConstants.USER_ID_MAP.contains(userId)) {
                CinemaConstants.USER_ID_MAP.put(userId, userId);
                // 获取我的积分
                Account myInfo = accountService.getAccount(Integer.parseInt(userId));
                Integer myIntegral = myInfo.getIntegral();
                // 抽奖次数
                int drawCount = 0;
                BigWheelRecord bigWheelRecord = bigWheelRecordService.getTodayRecord(userId, eventId);
                if (bigWheelRecord == null) {
                    drawCount = myIntegral / cinemaWelfare.getIntegral_cost() + cinemaWelfare.getDefault_count();
                    if (drawCount > 0) {
                        if (cinemaWelfare.getDefault_count() <= 0) {
                            // 扣除积分消费
                            deductIntegral(userId, cinemaWelfare);
                            // 是否扣分标志
                            cinemaWelfare.setIsDeduct("1");
                        }
                        // 添加一条抽奖记录
                        BigWheelRecord bigWheelRecordNew = new BigWheelRecord();
                        bigWheelRecordNew.setDraw_count(1);
                        bigWheelRecordNew.setUser_id(Integer.parseInt(userId));
                        bigWheelRecordNew.setCinema_welfare_id(cinemaWelfare.getCinema_welfare_id());
                        bigWheelRecordService.createNewRecord(bigWheelRecordNew);
                        // 剩余免费次数
                        cinemaWelfare.setOddDrawCount(String.valueOf(cinemaWelfare.getDefault_count() - 1));
                    } else {
                        CinemaConstants.USER_ID_MAP.remove(userId);
                        rtn.put("code", "05");
                        rtn.put("data", "积分不够抽奖啦!去赚点积分再来吧~");
                        return rtn;
                    }
                } else {
                    if (bigWheelRecord.getDraw_count() >= cinemaWelfare.getDefault_count()) {
                        drawCount = myIntegral / cinemaWelfare.getIntegral_cost();
                        if (drawCount > 0) {
                            // 扣除积分消费
                            deductIntegral(userId, cinemaWelfare);
                            // 剩余免费次数
                            cinemaWelfare.setOddDrawCount("0");
                            // 是否扣分标志
                            cinemaWelfare.setIsDeduct("1");
                        } else {
                            CinemaConstants.USER_ID_MAP.remove(userId);
                            rtn.put("code", "05");
                            rtn.put("data", "积分不够抽奖啦!去赚点积分再来吧~");
                            return rtn;
                        }
                    } else {
                        drawCount = myIntegral / cinemaWelfare.getIntegral_cost() + cinemaWelfare.getDefault_count() - bigWheelRecord.getDraw_count();
                        // 剩余免费次数
                        cinemaWelfare.setOddDrawCount(String.valueOf(cinemaWelfare.getDefault_count() - bigWheelRecord.getDraw_count() - 1));
                    }
                    if (drawCount > 0) {
                        // 增加一条抽奖次数
                        BigWheelRecord bigWheelRecordNew = new BigWheelRecord();
                        bigWheelRecordNew.setBig_wheel_record_id(bigWheelRecord.getBig_wheel_record_id());
                        bigWheelRecordNew.setDraw_count(bigWheelRecord.getDraw_count() + 1);
                        bigWheelRecordNew.setUser_id(Integer.parseInt(userId));
                        bigWheelRecordNew.setUpdate_time(new Date());
                        bigWheelRecordNew.setCinema_welfare_id(cinemaWelfare.getCinema_welfare_id());
                        bigWheelRecordService.update(bigWheelRecordNew);
                    } else {
                        CinemaConstants.USER_ID_MAP.remove(userId);
                        rtn.put("code", "05");
                        rtn.put("data", "积分不够抽奖啦!去赚点积分再来吧~");
                        return rtn;
                    }
                }
                // 奖品配置
                List<AwardsConfig> awardsConfigList = awardsConfigService.getAwardsConfigList(eventId);
                // 100%中奖奖项
                List<AwardsConfig> hundredsAwardsList = new ArrayList<>();
                // 概率中奖奖项
                List<AwardsConfig> probabilityAwardsList = new ArrayList<>();
                // 构造条形图
                List<Integer> winningList = new ArrayList<>();
                winningList.add(0);
                int end = 0;
                int start = 0;
                synchronized (this) {
                    if (awardsConfigList != null) {
                        for (AwardsConfig config : awardsConfigList) {
                            boolean addFlag = false;
                            // 是否超过中奖数量,没超过则加入奖项条形图
                            if (config.getAwards_count() == 0 || winningManagementService.getEventAwardsBingoCount(eventId, config.getAwards_config_id().toString(), "0") < config.getAwards_count()) {
                                // 不能重复中奖的情况
                                if ("1".equals(config.getIs_repeated())) {
                                    boolean isRepeated = winningManagementService.isRepeated(eventId, userId, config.getAwards_config_id().toString(), "0");
                                    if (!isRepeated) {
                                        addFlag = true;
                                    }
                                } else {
                                    addFlag = true;
                                }
                            }
                            if (addFlag) {
                                // 100%中奖奖项
                                if (new BigDecimal("100").compareTo(config.getProbability()) == 0) {
                                    hundredsAwardsList.add(config);
                                } else {
                                    BigDecimal probability = config.getProbability();
                                    start = start + probability.multiply(new BigDecimal(Math.pow(10, 4))).intValue();
                                    winningList.add(start);
                                    end = end + (int) Math.pow(10, 6);
                                    probabilityAwardsList.add(config);
                                }
                            }
                        }
                    }
                    winningList.add(end);
                    int radom = new Random().nextInt(end + 1);
                    logger.debug("随机数radom====================" + radom);
                    logger.debug("条形图====================" + winningList.toString());
//                int radom = 222222;
                    AwardsConfig bingo = null;
                    for (int i = 0; i < winningList.size() && i + 2 < winningList.size(); i++) {
                        if (winningList.get(i) <= radom && radom <= winningList.get(i + 1)) {
                            bingo = probabilityAwardsList.get(i);
                            insertWinningManagement(myInfo, cinemaWelfare, bingo);
                            break;
                        }
                    }
                    // 没有抽中概率奖,则随机抽一个100%中奖奖项
                    if (bingo == null) {
                        int sysRadom = new Random().nextInt(hundredsAwardsList.size());
                        bingo = hundredsAwardsList.get(sysRadom);
                        insertWinningManagement(myInfo, cinemaWelfare, bingo);
                    }

                    cinemaWelfare.setAwardsConfig(bingo);
                }
                // 获取我的积分
                myInfo = accountService.getAccount(Integer.parseInt(userId));
                myIntegral = myInfo.getIntegral();
                cinemaWelfare.setMyIntegral(myIntegral.toString());
                // 抽奖次数
                drawCount = 0;
                bigWheelRecord = bigWheelRecordService.getTodayRecord(userId, eventId);
                if (bigWheelRecord == null) {
                    drawCount = myIntegral / cinemaWelfare.getIntegral_cost() + cinemaWelfare.getDefault_count();
                } else {
                    if (bigWheelRecord.getDraw_count() >= cinemaWelfare.getDefault_count()) {
                        drawCount = myIntegral / cinemaWelfare.getIntegral_cost();

                    } else {
                        drawCount = myIntegral / cinemaWelfare.getIntegral_cost() + cinemaWelfare.getDefault_count() - bigWheelRecord.getDraw_count();
                    }
                }
                if (drawCount < 0) {
                    drawCount = 0;
                }
                cinemaWelfare.setDrawCount(String.valueOf(drawCount));
                // 我的最近2条奖品记录
                List<WinningManagement> myWinningManagementList = winningManagementService.getMyLatestTwoRecords(eventId, userId);
                cinemaWelfare.setMyWinningManagementList(myWinningManagementList);

                // 近12条中奖记录
                List<WinningManagement> winningManagementList = getWinningManagementList(eventId);
                cinemaWelfare.setWinningManagementList(winningManagementList);
                rtn.put("code", "00");
                rtn.put("data", cinemaWelfare);
                CinemaConstants.USER_ID_MAP.remove(userId);
            } else {
                rtn.put("code", "04");
                rtn.put("data", "抽奖进行中,请稍等!");
            }
        } catch (Exception e) {
            CinemaConstants.USER_ID_MAP.remove(userId);
            logger.error("drawBigWheel 异常:", e);
            rtn.put("code", "-1");
            rtn.put("data", "抽奖失败!");
        }
        return rtn;
    }
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

haibo8023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值