抽奖功能实现

编程入门 行业动态 更新时间:2024-10-25 20:31:22

抽奖<a href=https://www.elefans.com/category/jswz/34/1771378.html style=功能实现"/>

抽奖功能实现

1. 需求分析

抽奖可以获得积分,礼券,小样,正品等

若库存为0,则用户不能在抽中此奖

每个奖项的中奖概率

每天抽奖次数的限制

每次抽奖是否需要消耗积分的限制

有没有批量抽奖功能

 

2.表结构设计

会员表,积分表,是以前就有的,本次新增抽奖功能,需要新增一下的表:

首先要有库存表,暂且定义为BPRIZE

其次要记录用户抽中的奖项,暂且定义为HPRIZE

      

 

3. 后台代码实现

Controller 

@RestController
@RequestMapping(value = "draw")
public class DrawController extends BaseController {@AutowiredDrawService drawService;@AutowiredCouponService couponService;@AutowiredSocialProviderService socialProviderService;@ApiOperation(value = "抽奖历史记录", notes = "抽奖历史记录")@GetMapping(value = "history", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})private ResponseBean prizeHistory(@RequestParam("userId") String userId,@RequestParam("social") String social, @RequestParam("page") int page, @RequestParam("size") int size) {ResponseBean responseBean = new ResponseBean();List<HPrizerecord> hPrizerecords = drawService.getPrizeHistory(userId, social, page, size);responseBean.setData(hPrizerecords);return responseBean;}@ApiOperation(value = "抽奖榜单", notes = "抽奖榜单")@GetMapping(value = "billboard", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})private ResponseBean billboard(@RequestParam("topN") int topN, @RequestParam("levelList") String levelList, @RequestParam("social") String social) {ResponseBean responseBean = new ResponseBean();// 查找SopIdSSocialProvider socialProvider = socialProviderService.findSocialProvider(social);// 格式化等级 (str-> list(int))List<Integer> level = Arrays.stream(levelList.split(",")).map(a -> {return Integer.parseInt(a);}).collect(Collectors.toList());// 查询JSONArray billboard = drawService.getBillboard(topN, level, socialProvider.getSopId());responseBean.setData(billboard);return responseBean;}@ApiOperation(value = "抽奖", notes = "抽奖")@PostMapping(produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})private ResponseBean<BPrize> draw(@RequestParam("userId") String userId,@RequestParam("social") String social) throws Exception {ResponseBean responseBean = new ResponseBean();BPrize bPrize = drawService.draw(userId, social, super.getSid());responseBean.setData(bPrize);return responseBean;}
}

 

 

具体实现

@Service
public class DrawServiceImpl implements DrawService {@AutowiredHPrizerecordMapper hPrizerecordMapper;@AutowiredSaveDrawRecord saveDrawRecord;@AutowiredSocialService socialService;@AutowiredSocialProviderService socialProviderService;@AutowiredBCustomerService bCustomerService;@AutowiredBPrizeService bPrizeService;@AutowiredPEService peService;@AutowiredCustomerService customerService;@AutowiredSendMemberInfoToEcrmMqTask sendMemberInfoToEcrmMqTask;@Value("${sys.name}")private String sysName;int freeCount=2;int dalyMaxDrawCount=5;@Value("${cc.draw.pe.code}")String luckyDrawPeEventCode;@Value("${cc.draw.max.probability}")int maxProbability;static  Random rand = new Random();@Value("#{'${cc.draw.actual.prizeLevel}'.split(',')}")private  List<Integer>  actualPrizeLevel;@Overridepublic List<HPrizerecord> getPrizeHistory(String userId, String social, int page, int size) {int cstId = socialService.findCstIdBySocialAndSocialId(userId, social);if (size == 0 ){size = 10;}if (page<=0){page = 1;}return  hPrizerecordMapper.getPrizeHistory(cstId, page, size);}@Overridepublic BPrize draw(String userId, String social, String sid) throws Exception {int cstId = socialService.findCstIdBySocialAndSocialId(userId, social);// 今天一共抽了几次奖int todayCount = getDrawCountToday(cstId);// 如果超过最大次数,直接返回, 不进行抽奖if (todayCount >= dalyMaxDrawCount){throw new DrawCountLimitException();}// 超过规定的一个次数就要开始扣积分if (todayCount >= freeCount) {// 扣减积分, 如果扣减失败, 会抛出异常,这边不用管peService.sendEvent(cstId, sysName, luckyDrawPeEventCode);sendMemberInfoToEcrmMqTask.syncMemberInfoToECRM(cstId, luckyDrawPeEventCode);}boolean drawEnd = false;BPrize bPrize = null;while (!drawEnd) {int x = rand.nextInt(maxProbability);bPrize = bPrizeService.getPrize(x);// 如果抽中的是实物奖品if (bPrize!=null && actualPrizeLevel.indexOf(bPrize.getPrizeLevel())>=0){// 判断当日是否有中过实物奖品boolean isWinningTheActualPrizeFlag =  isWinningTheActualPrize(cstId);// 如果isWinningTheActualPrizeFlag 为false , 抽奖成功if (!isWinningTheActualPrizeFlag) {drawEnd = true;}}if (bPrize != null) {drawEnd = true;}}// 如果礼品里面配置了PEcode 调用PE积分调整String peCode = bPrize.getPointCode();if (StringUtils.isNotBlank(peCode)) {peService.sendEvent(cstId, sysName, peCode);sendMemberInfoToEcrmMqTask.syncMemberInfoToECRM(cstId, luckyDrawPeEventCode);}// 记录到抽奖历史saveDrawRecord.savePrizerecordHistory(cstId, bPrize, sid);return bPrize;}private boolean isWinningTheActualPrize(int cstId) {// 查询今天是否中过实物奖项List<HPrizerecord> hPrizerecords = hPrizerecordMapper.getWinningTheActualPrizeToday(cstId,actualPrizeLevel);if (null != hPrizerecords ||!hPrizerecords.isEmpty() || hPrizerecords.size()>0) {return true;}return false;}@Overridepublic JSONArray getBillboard(int topN, List<Integer> levelList, Integer sopId) {List<Map> records = hPrizerecordMapper.selectBillboard(topN, levelList, sopId);return (JSONArray) JSONArray.toJSON(records);}private int getDrawCountToday(int cstId) {return hPrizerecordMapper.getDrawCountToday(cstId);}}

  

 

 

4. 前端UI实现

转载于:.html

更多推荐

抽奖功能实现

本文发布于:2024-03-10 17:46:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1728616.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!