66 lines
1.9 KiB
Java
66 lines
1.9 KiB
Java
package com.tailbet.controller;
|
|
|
|
import com.tailbet.model.dto.ReceiveRpDTO;
|
|
import com.tailbet.model.dto.SendRpDTO;
|
|
import com.tailbet.model.entity.RedPacket;
|
|
import com.tailbet.model.entity.RedPacketRecv;
|
|
import com.tailbet.model.vo.R;
|
|
import com.tailbet.model.vo.UserContext;
|
|
import com.tailbet.service.IGameService;
|
|
import com.tailbet.service.IRedPacketService;
|
|
import com.tailbet.util.RedisLockUtil;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
/**
|
|
* 红包Controller
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/api/rp")
|
|
@RequiredArgsConstructor
|
|
public class RedPacketController {
|
|
|
|
private final IRedPacketService redPacketService;
|
|
private final IGameService gameService;
|
|
|
|
/**
|
|
* 发红包
|
|
*/
|
|
@PostMapping("/send")
|
|
public R<RedPacket> sendRp(@RequestBody SendRpDTO dto) {
|
|
Long userId = UserContext.getUserId();
|
|
// roundId从当前group的RP_SENDING状态局获取
|
|
var sendingRound = gameService.getRpSendingRound(dto.getGroupId());
|
|
Long roundId = sendingRound == null ? null : sendingRound.getId();
|
|
var rp = redPacketService.sendRp(userId, dto.getGroupId(),
|
|
dto.getTotalAmount(), dto.getTotalCount(), roundId);
|
|
return R.ok(rp);
|
|
|
|
}
|
|
|
|
/**
|
|
* 领红包
|
|
*/
|
|
@PostMapping("/receive")
|
|
public R<RedPacketRecv> receiveRp(@RequestBody ReceiveRpDTO dto) {
|
|
Long userId = UserContext.getUserId();
|
|
// 使用分布式锁防止多领
|
|
var recv = RedisLockUtil.lock("rp:receive:" + dto.getRpId(),
|
|
() -> redPacketService.receiveRp(dto.getRpId(), userId));
|
|
if (recv == null) {
|
|
return R.fail("领取失败,请重试");
|
|
}
|
|
return R.ok(recv);
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取红包详情
|
|
*/
|
|
@GetMapping("/detail")
|
|
public R<RedPacket> detail(Long rpId) {
|
|
var rp = redPacketService.getById(rpId);
|
|
return R.ok(rp);
|
|
}
|
|
}
|