You've already forked FrameTour-BE
feat(voucher): 优化券码领取接口返回结果
- 添加success和message字段到VoucherCodeResp响应类 - 修改券码领取逻辑,失败时不抛出异常而是返回失败结果 - 实现券码状态、用户权限和批次验证的统一响应格式 - 成功领取时设置success为true并返回成功消息 - 失败时设置success为false并返回具体失败原因 - 保持券码基础信息在失败情况下仍然返回给前端
This commit is contained in:
@@ -7,6 +7,15 @@ import java.util.Date;
|
||||
|
||||
@Data
|
||||
public class VoucherCodeResp {
|
||||
/**
|
||||
* 领取是否成功
|
||||
*/
|
||||
private Boolean success;
|
||||
/**
|
||||
* 结果描述(失败时为原因说明)
|
||||
*/
|
||||
private String message;
|
||||
|
||||
private Long id;
|
||||
private Long batchId;
|
||||
private String batchName;
|
||||
|
||||
@@ -80,7 +80,7 @@ public VoucherCodeResp claimVoucher(VoucherClaimReq req) {
|
||||
|
||||
Long userId = Long.valueOf(BaseContextHandler.getUserId());
|
||||
|
||||
// 验证券码是否存在且未被领取
|
||||
// 查询券码
|
||||
LambdaQueryWrapper<PriceVoucherCode> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(PriceVoucherCode::getCode, req.getCode())
|
||||
.eq(PriceVoucherCode::getDeleted, 0);
|
||||
@@ -90,18 +90,29 @@ public VoucherCodeResp claimVoucher(VoucherClaimReq req) {
|
||||
throw new BizException(400, "券码不存在");
|
||||
}
|
||||
|
||||
// 查询批次信息,用于构建响应
|
||||
PriceVoucherBatchConfig batch = voucherBatchMapper.selectById(voucherCode.getBatchId());
|
||||
|
||||
// 券码已找到,后续校验失败时仍返回 scenicId 等信息
|
||||
if (!Objects.equals(voucherCode.getStatus(), VoucherCodeStatus.UNCLAIMED.getCode())) {
|
||||
throw new BizException(400, "券码已被领取或已使用");
|
||||
VoucherCodeResp resp = convertToResp(voucherCode, batch);
|
||||
resp.setSuccess(false);
|
||||
resp.setMessage("券码已被领取或已使用");
|
||||
return resp;
|
||||
}
|
||||
|
||||
if (!canClaimVoucher(userId, voucherCode.getScenicId())) {
|
||||
throw new BizException(400, "该用户在此景区已领取过券码");
|
||||
VoucherCodeResp resp = convertToResp(voucherCode, batch);
|
||||
resp.setSuccess(false);
|
||||
resp.setMessage("该用户在此景区已领取过券码");
|
||||
return resp;
|
||||
}
|
||||
|
||||
// 获取券码所属批次
|
||||
PriceVoucherBatchConfig batch = voucherBatchMapper.selectById(voucherCode.getBatchId());
|
||||
if (batch == null || batch.getDeleted() == 1) {
|
||||
throw new BizException(400, "券码批次不存在");
|
||||
VoucherCodeResp resp = convertToResp(voucherCode, batch);
|
||||
resp.setSuccess(false);
|
||||
resp.setMessage("券码批次不存在");
|
||||
return resp;
|
||||
}
|
||||
|
||||
// 更新券码状态
|
||||
@@ -117,7 +128,10 @@ public VoucherCodeResp claimVoucher(VoucherClaimReq req) {
|
||||
|
||||
voucherBatchService.updateBatchClaimedCount(batch.getId());
|
||||
|
||||
return convertToResp(voucherCode, batch);
|
||||
VoucherCodeResp resp = convertToResp(voucherCode, batch);
|
||||
resp.setSuccess(true);
|
||||
resp.setMessage("领取成功");
|
||||
return resp;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user