From 5b6a1a910c47ad647a7584e39af9c9704bfdf743 Mon Sep 17 00:00:00 2001 From: bunny <1319900154@qq.com> Date: Wed, 10 Apr 2024 13:06:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=96=B0=E5=A2=9E):=20=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E8=B4=AD=E7=89=A9=E8=BD=A6=E5=86=85=E5=AE=B9,=E6=B8=85?= =?UTF-8?q?=E7=A9=BA=E8=B4=AD=E7=89=A9=E8=BD=A6,=E6=89=B9=E9=87=8F?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=B4=AD=E7=89=A9=E8=BD=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cart/controller/CartApiController.java | 7 ++--- .../ssyx/cart/service/CartInfoService.java | 7 +++-- .../service/impl/CartInfoServiceImpl.java | 29 +++++++++++++++---- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/controller/CartApiController.java b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/controller/CartApiController.java index 43cf144..f9baac0 100644 --- a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/controller/CartApiController.java +++ b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/controller/CartApiController.java @@ -9,7 +9,6 @@ import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import javax.servlet.http.HttpServletRequest; import java.util.List; @Api(tags = "购物车相关接口") @@ -37,7 +36,7 @@ public class CartApiController { @ApiOperation(value = "清空购物车") @DeleteMapping("deleteAllCart") - public Result deleteAllCart(HttpServletRequest request) { + public Result deleteAllCart() { Long userId = BaseContext.getUserId(); cartInfoService.deleteAllCart(userId); return Result.success(); @@ -45,9 +44,9 @@ public class CartApiController { @ApiOperation(value = "批量删除购物车") @PostMapping("batchDeleteCart") - public Result batchDeleteCart(@RequestBody List skuIdList, HttpServletRequest request) { + public Result batchDeleteCart(@RequestBody List skuIdList) { Long userId = BaseContext.getUserId(); - cartInfoService.batchDeleteCart(userId); + cartInfoService.batchDeleteCart(userId, skuIdList); return Result.success(); } } diff --git a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/CartInfoService.java b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/CartInfoService.java index cb7f479..172f3b9 100644 --- a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/CartInfoService.java +++ b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/CartInfoService.java @@ -1,5 +1,7 @@ package com.atguigu.ssyx.cart.service; +import java.util.List; + public interface CartInfoService { /** @@ -29,7 +31,8 @@ public interface CartInfoService { /** * * 批量删除购物车 * - * @param userId 用户id + * @param userId 用户id + * @param skuIdList 商品的id */ - void batchDeleteCart(Long userId); + void batchDeleteCart(Long userId, List skuIdList); } diff --git a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/impl/CartInfoServiceImpl.java b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/impl/CartInfoServiceImpl.java index df9bf0a..d757e59 100644 --- a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/impl/CartInfoServiceImpl.java +++ b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/impl/CartInfoServiceImpl.java @@ -15,6 +15,7 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import java.util.Date; +import java.util.List; @Service public class CartInfoServiceImpl implements CartInfoService { @@ -90,7 +91,10 @@ public class CartInfoServiceImpl implements CartInfoService { */ @Override public void deleteCart(Long skuId, Long userId) { - + BoundHashOperations hashOperations = redisTemplate.boundHashOps(module.getCartKey(skuId)); + if (Boolean.TRUE.equals(hashOperations.hasKey(skuId.toString()))) { + hashOperations.delete(skuId.toString()); + } } /** @@ -100,16 +104,31 @@ public class CartInfoServiceImpl implements CartInfoService { */ @Override public void deleteAllCart(Long userId) { - + String cartKey = module.getCartKey(userId); + BoundHashOperations hashOperations = redisTemplate.boundHashOps(cartKey); + List cartInfoList = hashOperations.values(); + // cartInfoList 不为空 + if (cartInfoList != null) { + cartInfoList.forEach(cartInfo -> { + Long skuId = cartInfo.getSkuId(); + hashOperations.delete(skuId); + }); + } } /** * * 批量删除购物车 * - * @param userId 用户id + * @param userId 用户id + * @param skuIdList 商品的id */ @Override - public void batchDeleteCart(Long userId) { - + public void batchDeleteCart(Long userId, List skuIdList) { + String cartKey = module.getCartKey(userId); + BoundHashOperations hashOperations = redisTemplate.boundHashOps(cartKey); + // 如果购物车中列表不为空 + if (skuIdList != null) { + skuIdList.forEach(skuId -> hashOperations.delete(skuId.toString())); + } } }