feat(新增): 更新购物车商品选中状态

Signed-off-by: bunny <1319900154@qq.com>
This commit is contained in:
bunny 2024-03-28 14:49:53 +08:00
parent cd1b13900e
commit 1dc696935d
3 changed files with 33 additions and 3 deletions

View File

@ -43,9 +43,9 @@ public class CartController {
@Operation(summary = "更新购物车商品选中状态") @Operation(summary = "更新购物车商品选中状态")
@GetMapping("/auth/checkCart/{skuId}/{isChecked}") @GetMapping("/auth/checkCart/{skuId}/{isChecked}")
public Result checkCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable(value = "skuId") Long skuId, public Result<String> checkCart(@Parameter(name = "skuId", description = "商品skuId", required = true) @PathVariable(value = "skuId") Long skuId,
@Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable(value = "isChecked") Integer isChecked) { @Parameter(name = "isChecked", description = "是否选中 1:选中 0:取消选中", required = true) @PathVariable(value = "isChecked") Integer isChecked) {
cartService.checkCart(skuId, isChecked); cartService.checkCart(skuId, isChecked);
return Result.build(null, ResultCodeEnum.SUCCESS); return Result.success();
} }
} }

View File

@ -26,4 +26,12 @@ public interface CartService {
* @param skuId 商品的ID值 * @param skuId 商品的ID值
*/ */
void deleteCart(Long skuId); void deleteCart(Long skuId);
/**
* 更新购物车商品选中状态
*
* @param skuId 商品的ID值
* @param isChecked 是否选中
*/
void checkCart(Long skuId, Integer isChecked);
} }

View File

@ -15,6 +15,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
@ -103,4 +104,25 @@ public class CartServiceImpl implements CartService {
// 获取缓存对象 // 获取缓存对象
redisTemplate.opsForHash().delete(cartKey, String.valueOf(skuId)); redisTemplate.opsForHash().delete(cartKey, String.valueOf(skuId));
} }
/**
* 更新购物车商品选中状态
*
* @param skuId 商品的ID值
* @param isChecked 是否选中
*/
@Override
public void checkCart(Long skuId, Integer isChecked) {
// 获取当前登录的用户数据
Long userId = BaseContext.getUserInfo().getId();
String cartKey = userId.toString();
Boolean hasKey = redisTemplate.opsForHash().hasKey(cartKey, String.valueOf(skuId));
if (hasKey) {
String cartInfoJSON = Objects.requireNonNull(redisTemplate.opsForHash().get(cartKey, String.valueOf(skuId))).toString();
CartInfo cartInfo = JSON.parseObject(cartInfoJSON, CartInfo.class);
cartInfo.setIsChecked(isChecked);
redisTemplate.opsForHash().put(cartKey, String.valueOf(skuId), JSON.toJSONString(cartInfo));
}
}
} }