使用Springboot和mybatis框架开发,已有写好的抽奖工具类BaseUtils,程序可能用到的表有活动表 t_raffle_info 奖品表 t_prize_info 活动和奖品关联表 t_raffle_prize_relation 奖品库存表 t_prize_stock 抽奖表 t_lottery_info 中奖表 t_lottery_win,给出抽奖功能的Controller层,Service层,Service实现类。实现抽奖功能 :2.不在进行中的活动弹出提示 3.超过抽奖限制次数弹出提示 4限制接口频繁访问 10秒内不允许同一个用户访问2次 5抛出异常时事务需要回滚 6.需要校验用户是否存在,奖品是否存在,库存是否足够,否则给与提示
时间: 2024-03-27 17:41:33 浏览: 126
Controller层代码:
```java
@RestController
@RequestMapping("/lottery")
public class LotteryController {
@Autowired
private LotteryService lotteryService;
@PostMapping("/draw")
public Result draw(@RequestParam("userId") String userId,
@RequestParam("activityId") String activityId) {
try {
lotteryService.draw(userId, activityId);
return Result.success();
} catch (Exception e) {
e.printStackTrace();
return Result.fail(e.getMessage());
}
}
}
```
Service层代码:
```java
public interface LotteryService {
void draw(String userId, String activityId) throws Exception;
}
```
Service实现类代码:
```java
@Service
public class LotteryServiceImpl implements LotteryService {
@Autowired
private RaffleInfoMapper raffleInfoMapper;
@Autowired
private PrizeInfoMapper prizeInfoMapper;
@Autowired
private RafflePrizeRelationMapper rafflePrizeRelationMapper;
@Autowired
private PrizeStockMapper prizeStockMapper;
@Autowired
private LotteryInfoMapper lotteryInfoMapper;
@Autowired
private LotteryWinMapper lotteryWinMapper;
@Autowired
private BaseUtils baseUtils;
@Override
@Transactional(rollbackFor = Exception.class)
public void draw(String userId, String activityId) throws Exception {
// 1.判断活动状态
RaffleInfo raffleInfo = raffleInfoMapper.selectByPrimaryKey(activityId);
if (raffleInfo == null) {
throw new Exception("活动不存在");
}
if (raffleInfo.getStatus() != 1) {
throw new Exception("活动未进行中");
}
// 2.判断抽奖次数是否达到上限
if (baseUtils.isLimitDraw(userId, activityId)) {
throw new Exception("抽奖次数已达上限");
}
// 3.限制接口频繁访问
if (baseUtils.isFrequentAccess(userId, "draw", 10)) {
throw new Exception("操作过于频繁,请稍后再试");
}
// 4.校验用户是否存在,奖品是否存在,库存是否足够
PrizeStock prizeStock = prizeStockMapper.selectByActivityId(activityId);
if (prizeStock == null) {
throw new Exception("奖品库存不存在");
}
PrizeInfo prizeInfo = prizeInfoMapper.selectByPrimaryKey(prizeStock.getPrizeId());
if (prizeInfo == null) {
throw new Exception("奖品不存在");
}
if (prizeStock.getStock() <= 0) {
throw new Exception("奖品库存不足");
}
// 5.开始抽奖
PrizeInfo drawPrize = baseUtils.draw(prizeInfo, rafflePrizeRelationMapper, prizeStockMapper, activityId);
if (drawPrize == null) {
throw new Exception("很遗憾,您未中奖");
}
// 6.记录抽奖信息
LotteryInfo lotteryInfo = new LotteryInfo();
lotteryInfo.setId(UUID.randomUUID().toString());
lotteryInfo.setUserId(userId);
lotteryInfo.setActivityId(activityId);
lotteryInfo.setPrizeId(drawPrize.getId());
lotteryInfoMapper.insert(lotteryInfo);
// 7.将中奖信息记录到中奖表
LotteryWin lotteryWin = new LotteryWin();
lotteryWin.setId(UUID.randomUUID().toString());
lotteryWin.setUserId(userId);
lotteryWin.setActivityId(activityId);
lotteryWin.setPrizeId(drawPrize.getId());
lotteryWinMapper.insert(lotteryWin);
}
}
```
阅读全文