|
|
@ -120,7 +120,15 @@ public class ShopUserCouponController extends BaseController { |
|
|
|
final User loginUser = getLoginUser(); |
|
|
|
if (loginUser == null) return fail("请先登录"); |
|
|
|
ShopCoupon coupon = couponService.getByIdRel(userCoupon.getCouponId()); |
|
|
|
if (coupon.getTotalCount() != -1 && coupon.getReceiveNum() >= coupon.getTotalCount()) return fail("已经被领完了"); |
|
|
|
|
|
|
|
// 检查优惠券是否存在
|
|
|
|
if (coupon == null) return fail("优惠券不存在"); |
|
|
|
|
|
|
|
// 安全地检查已领取数量,避免空指针异常
|
|
|
|
Integer receiveNum = coupon.getReceiveNum(); |
|
|
|
if (receiveNum == null) receiveNum = 0; |
|
|
|
|
|
|
|
if (coupon.getTotalCount() != -1 && receiveNum >= coupon.getTotalCount()) return fail("已经被领完了"); |
|
|
|
List<ShopUserCoupon> userCouponList = shopUserCouponService.list( |
|
|
|
new LambdaQueryWrapper<ShopUserCoupon>() |
|
|
|
.eq(ShopUserCoupon::getCouponId, userCoupon.getCouponId()) |
|
|
@ -150,7 +158,11 @@ public class ShopUserCouponController extends BaseController { |
|
|
|
} |
|
|
|
userCoupon.setUserId(getLoginUserId()); |
|
|
|
shopUserCouponService.save(userCoupon); |
|
|
|
coupon.setReceiveNum(coupon.getReceiveNum() + 1); |
|
|
|
|
|
|
|
// 安全地更新已领取数量,避免空指针异常
|
|
|
|
Integer currentReceiveNum = coupon.getReceiveNum(); |
|
|
|
if (currentReceiveNum == null) currentReceiveNum = 0; |
|
|
|
coupon.setReceiveNum(currentReceiveNum + 1); |
|
|
|
couponService.updateById(coupon); |
|
|
|
return success("领取成功"); |
|
|
|
} |
|
|
|