Compare commits

...

2 Commits

Author SHA1 Message Date
科技小王子 1d5a775ded fix(shop): 修复优惠券领取功能的空指针异常问题 3 days ago
科技小王子 a38256f9ca fix(shop): 修复未登录用户可领取优惠券的bug- 在领取优惠券接口中增加了登录验证逻辑 3 days ago
  1. 20
      src/main/java/com/gxwebsoft/shop/controller/ShopUserCouponController.java
  2. 3
      src/main/java/com/gxwebsoft/shop/mapper/xml/ShopCouponMapper.xml

20
src/main/java/com/gxwebsoft/shop/controller/ShopUserCouponController.java

@ -117,8 +117,18 @@ public class ShopUserCouponController extends BaseController {
@Operation(summary = "领取优惠券")
@PostMapping("/take")
public ApiResult<?> take(@RequestBody ShopUserCoupon userCoupon) {
ShopCoupon coupon = couponService.getByIdRel(userCoupon.getCouponId());
if (coupon.getTotalCount() != -1 && coupon.getReceiveNum() >= coupon.getTotalCount()) return fail("已经被领完了");
final User loginUser = getLoginUser();
if (loginUser == null) return fail("请先登录");
ShopCoupon coupon = couponService.getByIdRel(userCoupon.getCouponId());
// 检查优惠券是否存在
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())
@ -148,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("领取成功");
}

3
src/main/java/com/gxwebsoft/shop/mapper/xml/ShopCouponMapper.xml

@ -4,7 +4,8 @@
<!-- 关联查询sql -->
<sql id="selectSql">
SELECT a.*
SELECT a.*,
COALESCE((SELECT COUNT(*) FROM shop_user_coupon suc WHERE suc.coupon_id = a.id AND suc.deleted = 0), 0) AS receive_num
FROM shop_coupon a
<where>
<if test="param.id != null">

Loading…
Cancel
Save