From 35601b1455ec05ece1414bd288fa38944c03bfdd Mon Sep 17 00:00:00 2001 From: Pu Zhibing <393733352@qq.com> Date: 星期四, 26 九月 2024 20:15:17 +0800 Subject: [PATCH] 修改bug --- ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java | 89 +++++++++++++++++++++++++++++++++++++------- 1 files changed, 74 insertions(+), 15 deletions(-) diff --git a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java index 75e28c4..858885d 100644 --- a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java +++ b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java @@ -25,6 +25,7 @@ import com.ruoyi.order.mapper.TShoppingOrderMapper; import com.ruoyi.order.service.TShoppingOrderRefundService; import com.ruoyi.order.service.TShoppingOrderService; +import com.ruoyi.order.util.RedisLock; import com.ruoyi.other.api.domain.TCoupon; import com.ruoyi.other.api.domain.TGoods; import com.ruoyi.other.api.feignClient.CouponClient; @@ -37,6 +38,8 @@ import com.ruoyi.payment.api.vo.AliQueryOrder; import com.ruoyi.payment.api.vo.NotifyV3PayDecodeRespBody; import io.seata.spring.annotation.GlobalTransactional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -85,6 +88,9 @@ @Resource private TShoppingOrderRefundService shoppingOrderRefundService; + + @Autowired + public RedisTemplate redisTemplate; @@ -309,7 +315,7 @@ //退款金额 BigDecimal refundAmount = shoppingOrder.getPaymentAmount().subtract(bigDecimal); - + //先查询第三方订单状态订单是否退款 //支付方式(1=微信,2=支付宝) Integer paymentType = shoppingOrder.getPaymentType(); @@ -387,6 +393,44 @@ AjaxResult success = cancelShoppingOrderWxRefund(resp.getOutTradeNo(), resp.getTradeNo(), "SUCCESS", sdf1.format(new Date())); if(success.isSuccess()){ shoppingOrderRefundService.save(shoppingOrderRefund); + //商品 + if(shoppingOrder.getOrderType() == 1){ + //redis锁 和支付使用同一个锁 + RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_GOODS_LOCK", 5, 30000); + try { + redisLock.lock(); + TGoods goods = goodsClient.getGoodsById(shoppingOrder.getGoodsId()).getData(); + Integer inventory = goods.getInventory(); + if(-1 != inventory){ + goods.setInventory(inventory + shoppingOrder.getPurchaseQuantity()); + goodsClient.updateGoods(goods); + } + }catch (Exception e){ + e.printStackTrace(); + }finally { + //解锁 + redisLock.unlock(); + } + } + //优惠券 + if(shoppingOrder.getOrderType() == 2){ + //redis锁 和支付使用同一个锁 + RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_COUPON_LOCK", 5, 30000); + try { + redisLock.lock(); + TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getCouponId()).getData(); + Integer inventory = coupon.getInventoryQuantity(); + if(-1 != inventory){ + coupon.setInventoryQuantity(inventory + shoppingOrder.getPurchaseQuantity()); + couponClient.updateCoupon(coupon); + } + }catch (Exception e){ + e.printStackTrace(); + }finally { + //解锁 + redisLock.unlock(); + } + } } } } @@ -415,26 +459,41 @@ TShoppingOrder shoppingOrder = this.getById(one.getShoppingOrderId()); //商品 if(shoppingOrder.getOrderType() == 1){ - // todo 需完善redis锁 //redis锁 和支付使用同一个锁 - TGoods goods = goodsClient.getGoodsById(shoppingOrder.getGoodsId()).getData(); - Integer inventory = goods.getInventory(); - if(-1 != inventory){ - goods.setInventory(inventory + shoppingOrder.getPurchaseQuantity()); - goodsClient.updateGoods(goods); + RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_GOODS_LOCK", 5, 30000); + try { + redisLock.lock(); + TGoods goods = goodsClient.getGoodsById(shoppingOrder.getGoodsId()).getData(); + Integer inventory = goods.getInventory(); + if(-1 != inventory){ + goods.setInventory(inventory + shoppingOrder.getPurchaseQuantity()); + goodsClient.updateGoods(goods); + } + }catch (Exception e){ + e.printStackTrace(); + }finally { + //解锁 + redisLock.unlock(); } - //解锁 } //优惠券 if(shoppingOrder.getOrderType() == 2){ - //redis锁 - TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getCouponId()).getData(); - Integer inventory = coupon.getInventoryQuantity(); - if(-1 != inventory){ - coupon.setInventoryQuantity(inventory + shoppingOrder.getPurchaseQuantity()); - couponClient.updateCoupon(coupon); + //redis锁 和支付使用同一个锁 + RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_COUPON_LOCK", 5, 30000); + try { + redisLock.lock(); + TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getCouponId()).getData(); + Integer inventory = coupon.getInventoryQuantity(); + if(-1 != inventory){ + coupon.setInventoryQuantity(inventory + shoppingOrder.getPurchaseQuantity()); + couponClient.updateCoupon(coupon); + } + }catch (Exception e){ + e.printStackTrace(); + }finally { + //解锁 + redisLock.unlock(); } - //解锁 } } return AjaxResult.success(); -- Gitblit v1.7.1