Java实现抽奖模块的相关分享
阅读原文时间:2022年01月13日阅读:4

Java实现抽奖模块的相关分享

最近进行的项目中,有个抽奖的需求,今天就把相关代码给大家分享一下。

一、DAO层

/**
 * 获取奖品列表
 * @param systemVersion 手机系统版本(1-安卓 2-iOS)
 * @Date: 2021/6/7
 */
 List<LuckyPrize> getPrizeList(int systemVersion);

二、mapper.xml

<select id="getPrizeList">
&nbsp; &nbsp; &nbsp; &nbsp; SELECT *
&nbsp; &nbsp; &nbsp; &nbsp; FROM system_card sc
&nbsp; &nbsp; &nbsp; &nbsp;LEFT JOIN lucky_prize lp ON sc.system_card_id = lp.system_card_id
&nbsp; &nbsp; &nbsp; &nbsp; WHERE sc.`status` = 1
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AND sc.category = 3
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AND sc.system_version = #{systemVersion}
&nbsp; &nbsp; </select>

三、service层

//签到抽奖
LuckyPrizeVo startSignDraw(Integer systemVersion);

//积分抽奖
LuckyPrizeVo startIntegralDraw(Integer systemVersion);

其中入参systemVersion为手机系统,1是安卓 2是苹果

四、实现层

查看代码

 /**
     * 开始签到抽奖
     */
    @Override
    public LuckyPrizeVo startSignDraw(Integer systemVersion) {
        //判断用户是否有抽奖机会
        String userId = TokenUtil.getUserId();
        UserSign userSign = userSignMapper.selectOne(new QueryWrapper<UserSign>().eq("user_id", userId).eq("luck_chance", 1));
        if (null == userSign) {
            throw new BusinessException("连续成功签到7天才可参与抽奖!");
        }
        //判断上次签到时间是否在本周
        Date nowTime = new Date();
        Date lastSignTime = userSign.getLastSignTime();
        boolean flag = DateUtil.isSameWeek(nowTime, lastSignTime);
        if (!flag) {
            throw new BusinessException("连续成功签到7天才可参与转盘抽奖");
        }
        //同一周的,可以抽奖,调用定义的私有方法:luckyDraw(systemVersion)
        LuckyPrizeVo luckyPrizeVo = luckyDraw(systemVersion);

        //中奖了
        //用户签到机会 改为0
        userSign.setLuckChance(0);
        userSignMapper.updateById(userSign);

        //记录表添加一条数据
        LuckyPrizeLog luckyPrizeLog = new LuckyPrizeLog();
        luckyPrizeLog.setLogId(SnowflakeUtil.genId());
        luckyPrizeLog.setLuckyPrizeId(luckyPrizeVo.getLuckyPrizeId());
        luckyPrizeLog.setUserId(userId);
        luckyPrizeLog.setPrizeName(luckyPrizeVo.getCardName());
        luckyPrizeLog.setCreateTime(nowTime);
        luckyPrizeLogMapper.insert(luckyPrizeLog);

        //用户卡卷添加一条数据
        UserCard userCard = new UserCard();
        userCard.setUserCardId(SnowflakeUtil.genId());
        userCard.setUserId(userId);
        userCard.setSystemCardId(luckyPrizeVo.getSystemCardId());
        int systemCardType = luckyPrizeVo.getSystemCardType();
        userCard.setType(systemCardType);
        userCard.setStatus(0);
        userCard.setCreateTime(nowTime);
        if (systemCardType == 1 || systemCardType == 2) {
            //折扣卷和体验卷有效期为30天
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 30);
            Date date = calendar.getTime();
            userCard.setExpirationDate(date);
        }
        userCard.setRules(luckyPrizeVo.getSystemRules());
        userCardMapper.insert(userCard);
        return luckyPrizeVo;
    } 查看代码

 &nbsp; &nbsp;/**
     * 开始积分抽奖
     */
    @Override
    public LuckyPrizeVo startIntegralDraw(Integer systemVersion) {
        //判断用户是否有抽奖资格(100积分抽一次)
        String userId = TokenUtil.getUserId();
        UserExperience userExperience = userExperienceMapper.selectById(userId);
        if (null == userExperience) {
            throw new BusinessException("您的积分异常");
        }
        int integral = userExperience.getIntegral();
        //积分满100,抽一次
        int score = 100;
        if (integral < score) {
            throw new BusinessException("您的积分不足,无法参与抽奖");
        }

        LuckyPrizeVo luckyPrizeVo = luckyDraw(systemVersion);

        //中奖了
        //更新用户积分
        int newIntegral = integral - 100;
        userExperience.setIntegral(newIntegral);
        userExperienceMapper.updateById(userExperience);
        Date nowTime = new Date();
        //记录表添加一条数据
        LuckyPrizeLog luckyPrizeLog = new LuckyPrizeLog();
        luckyPrizeLog.setLogId(SnowflakeUtil.genId());
        luckyPrizeLog.setLuckyPrizeId(luckyPrizeVo.getLuckyPrizeId());
        luckyPrizeLog.setUserId(userId);
        luckyPrizeLog.setPrizeName(luckyPrizeVo.getCardName());
        luckyPrizeLog.setCreateTime(nowTime);
        luckyPrizeLogMapper.insert(luckyPrizeLog);

        //用户卡卷添加一条数据
        UserCard userCard = new UserCard();
        userCard.setUserCardId(SnowflakeUtil.genId());
        userCard.setUserId(userId);
        userCard.setSystemCardId(luckyPrizeVo.getSystemCardId());
        int systemCardType = luckyPrizeVo.getSystemCardType();
        userCard.setType(systemCardType);
        userCard.setStatus(0);
        userCard.setCreateTime(nowTime);
        if (systemCardType == 1 || systemCardType == 2) {
            //折扣卷和体验卷有效期为30天
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 30);
            Date date = calendar.getTime();
            userCard.setExpirationDate(date);
        }
        userCard.setRules(luckyPrizeVo.getSystemRules());
        userCardMapper.insert(userCard);
        return luckyPrizeVo;
    } 

提示:

1.可能我的需求与大家不一样,具体还是要看各位的需求要有所改动,但大体的思路可以参考我的代码; 2.项目中,继承了mabytis-plus,还封装了一些工具类,以及结果的统一处理。

五、Controller层

    @PostMapping("/startSignDraw")
    @ApiOperation(value = "签到抽奖")
    @Log(title = "签到抽奖")
    public String startSignDraw(@RequestBody SystemVersionDto dto) {
        LuckyPrizeVo luckyPrizeVo = luckyPrizeService.startSignDraw(Integer.valueOf(dto.getSystemVersion()));
        return Result.success(luckyPrizeVo);
    }

    @PostMapping("/startIntegralDraw")
    @ApiOperation(value = "积分抽奖")
    @Log(title = "积分抽奖")
    public String startIntegralDraw(@RequestBody SystemVersionDto dto) {
        LuckyPrizeVo luckyPrizeVo = luckyPrizeService.startIntegralDraw(Integer.valueOf(dto.getSystemVersion()));
        return Result.success(luckyPrizeVo);
    }

最后,附上建表sql:
六、建表SQL语句

CREATE TABLE `lucky_prize` (
  `lucky_prize_id` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '奖品id',
  `system_card_id` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '系统卡卷id',
  `system_version` tinyint(1) DEFAULT NULL COMMENT '手机系统版本(1-安卓 2-iOS)',
  `remark` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
  `probability` double(3,2) NOT NULL COMMENT '抽奖概率',
  `num` int DEFAULT NULL COMMENT '奖品数量',
  PRIMARY KEY (`lucky_prize_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='奖品表';


CREATE TABLE `lucky_prize_log` (
  `log_id` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '记录id',
  `lucky_prize_id` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '奖品id',
  `user_id` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '抽奖用户id',
  `prize_name` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '奖品名称',
  `create_time` datetime DEFAULT NULL COMMENT '抽奖时间',
  PRIMARY KEY (`log_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='抽奖记录表';


CREATE TABLE `system_card` (
  `system_card_id` char(18) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '系统卡卷id',
  `card_name` char(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '卡卷名称',
  `type` tinyint(1) NOT NULL COMMENT '卡卷类型(0谢谢参与卷 1折扣卷 2vip体验卷 3经验卷)',
  `count` int DEFAULT NULL COMMENT '卡卷数量(个)',
  `is_count` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否有数量(0否 1是)默认为0',
  `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '状态(0未启用,1已启用,2已下线) 默认为1',
  `failure_time` tinyint(1) NOT NULL COMMENT '卡卷时效时间(天)',
  `is_failure_time` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否有时效时间(0否 1是)默认为0',
  `create_time` datetime NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` datetime DEFAULT NULL COMMENT '修改时间',
  `remark` char(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
  `rules` double(5,2) NOT NULL COMMENT '卡卷规则',
  `category` tinyint(1) DEFAULT NULL COMMENT '卡卷种类(1-普通类 2-积分兑换类 3-转盘类)',
  `system_version` tinyint(1) DEFAULT NULL COMMENT '手机系统版本(1-安卓 2-iOS)',
  `required_points` int DEFAULT NULL COMMENT '积分卷所需积分',
  PRIMARY KEY (`system_card_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='系统卡卷';


CREATE TABLE `user_sign` (
  `user_id` char(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '用户id',
  `is_monday` tinyint(1) DEFAULT '0' COMMENT '周一是否签到(0无 1有)',
  `is_tuesday` tinyint(1) DEFAULT '0' COMMENT '周二是否签到(0无 1有)',
  `is_wednesday` tinyint(1) DEFAULT '0' COMMENT '周三是否签到(0无 1有)',
  `is_thursday` tinyint(1) DEFAULT '0' COMMENT '周四是否签到(0无 1有)',
  `is_friday` tinyint(1) DEFAULT '0' COMMENT '周五是否签到(0无 1有)',
  `is_saturday` tinyint(1) DEFAULT '0' COMMENT '周六是否签到(0无 1有)',
  `is_sunday` tinyint(1) DEFAULT '0' COMMENT '周末是否签到(0无 1有)',
  `last_sign_time` datetime DEFAULT NULL COMMENT '最近一次签到时间',
  `luck_chance` tinyint(1) DEFAULT '0' COMMENT '抽奖机会(0无 1有)',
  PRIMARY KEY (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='用户签到记录表';