Bläddra i källkod

积分库存的缓存

xw 3 dagar sedan
förälder
incheckning
e855b8b77e

+ 24 - 1
fs-service/src/main/java/com/fs/his/service/impl/FsIntegralOrderServiceImpl.java

@@ -161,6 +161,9 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
 
     @Autowired
     private IFsUserAddressService fsUserAddressService;
+
+    @Autowired
+    private com.fs.his.utils.RedisCacheUtil redisCacheUtil;
     /**
      * 查询积分商品订单
      *
@@ -312,6 +315,10 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
             throw new CustomException("库存不足");
         }
 
+        // 清除商品缓存
+        redisCacheUtil.delSpringCacheKey("getIntegralGoodsById", integralGoods.getGoodsId());
+        redisCacheUtil.delRedisKey("getIntegralGoodsList");
+
         integralGoods.setNum(1);
         List<FsIntegralGoods> goodsItem = new ArrayList<>();
         goodsItem.add(integralGoods);
@@ -467,6 +474,9 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
                 throw new CustomException("库存不足");
             }
 
+            // 清除商品缓存
+            redisCacheUtil.delSpringCacheKey("getIntegralGoodsById", integralGoods.getGoodsId());
+
             totalIntegral += integralGoods.getIntegral() * cart.getCartNum();
             totalCash = totalCash.add(integralGoods.getCash().multiply(BigDecimal.valueOf(cart.getCartNum())));
 
@@ -474,6 +484,9 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
             goodsItem.add(integralGoods);
         }
 
+        // 清除商品列表缓存
+        redisCacheUtil.delRedisKey("getIntegralGoodsList");
+
         return createOrder(user, address, totalIntegral, totalCash, goodsItem, null);
     }
 
@@ -576,11 +589,21 @@ public class FsIntegralOrderServiceImpl implements IFsIntegralOrderService
         // 还原库存
         if (order.getItemJson().startsWith("[") && order.getItemJson().endsWith("]")){
             List<FsIntegralGoods> goodsItem = JSONUtil.toBean(order.getItemJson(), new TypeReference<List<FsIntegralGoods>>(){}, true);
-            goodsItem.forEach(goods -> fsIntegralGoodsMapper.addStock(goods.getGoodsId(), Objects.isNull(goods.getNum()) ? 1 : goods.getNum()));
+            goodsItem.forEach(goods -> {
+                fsIntegralGoodsMapper.addStock(goods.getGoodsId(), Objects.isNull(goods.getNum()) ? 1 : goods.getNum());
+                // 清除商品缓存
+                redisCacheUtil.delSpringCacheKey("getIntegralGoodsById", goods.getGoodsId());
+                log.info("取消订单还原库存,清除商品缓存, goodsId: {}", goods.getGoodsId());
+            });
         } else {
             FsIntegralGoods integralGoods = JSONUtil.toBean(order.getItemJson(), FsIntegralGoods.class);
             fsIntegralGoodsMapper.addStock(integralGoods.getGoodsId(), Objects.isNull(integralGoods.getNum()) ? 1 : integralGoods.getNum());
+            // 清除商品缓存
+            redisCacheUtil.delSpringCacheKey("getIntegralGoodsById", integralGoods.getGoodsId());
+            log.info("取消订单还原库存,清除商品缓存, goodsId: {}", integralGoods.getGoodsId());
         }
+        // 清除商品列表缓存
+        redisCacheUtil.delRedisKey("getIntegralGoodsList");
 
         // 还原积分
         FsUser user=fsUserMapper.selectFsUserByUserId(order.getUserId());