From 79c4524bc2cac69ef74cbf4adc6a092e3bfa4f4b Mon Sep 17 00:00:00 2001 From: seatonwan9 Date: 星期四, 28 八月 2025 02:07:17 +0800 Subject: [PATCH] 更新代码 --- src/main/java/com/webmanage/service/impl/CartServiceImpl.java | 60 ++++++++++++++++++++++++++++++++++-------------------------- 1 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/webmanage/service/impl/CartServiceImpl.java b/src/main/java/com/webmanage/service/impl/CartServiceImpl.java index 7072069..716c8a2 100644 --- a/src/main/java/com/webmanage/service/impl/CartServiceImpl.java +++ b/src/main/java/com/webmanage/service/impl/CartServiceImpl.java @@ -18,6 +18,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; import javax.annotation.Resource; import java.math.BigDecimal; @@ -55,7 +56,7 @@ @Override @Transactional(rollbackFor = Exception.class) - public boolean addToCart(Long userId, Long unitId, CartItemDTO cartItemDTO) { + public boolean addToCart(String userId, String unitId, CartItemDTO cartItemDTO) { try { // 楠岃瘉鍟嗗搧瀹氫环鏄惁瀛樺湪 ProductPricing pricing = productPricingMapper.selectById(cartItemDTO.getPricingId()); @@ -64,7 +65,7 @@ } // 鏋勫缓璐墿杞ey - String cartKey = buildCartKey(userId, unitId); + String cartKey = buildCartKey(userId,unitId); String cartItemKey = buildCartItemKey(userId, unitId, cartItemDTO.getPricingId()); // 妫�鏌ュ晢鍝佹槸鍚﹀凡鍦ㄨ喘鐗╄溅涓� @@ -110,7 +111,7 @@ @Override @Transactional(rollbackFor = Exception.class) - public boolean removeFromCart(Long userId, Long unitId, Long pricingId) { + public boolean removeFromCart(String userId, String unitId, Long pricingId) { try { String cartItemKey = buildCartItemKey(userId, unitId, pricingId); @@ -137,7 +138,7 @@ @Override @Transactional(rollbackFor = Exception.class) - public boolean updateCartItemQuantity(Long userId, Long unitId, Long pricingId, Integer quantity) { + public boolean updateCartItemQuantity(String userId, String unitId, Long pricingId, Integer quantity) { try { if (quantity <= 0) { return removeFromCart(userId, unitId, pricingId); @@ -171,7 +172,7 @@ @Override @Transactional(rollbackFor = Exception.class) - public boolean clearCart(Long userId, Long unitId) { + public boolean clearCart(String userId, String unitId) { try { String cartKey = buildCartKey(userId, unitId); List<Long> pricingIds = getCartItemPricingIds(userId, unitId); @@ -199,12 +200,13 @@ } @Override - public CartVO getCart(Long userId, Long unitId) { + public CartVO getCart(String userId, String unitId) { try { CartVO cartVO = new CartVO(); cartVO.setUserId(userId); - cartVO.setUnitId(unitId); - + if (StringUtils.hasText(unitId)){ + cartVO.setUnitId(unitId); + } List<CartItemVO> items = getCartItems(userId, unitId); cartVO.setItems(items); @@ -226,7 +228,7 @@ } @Override - public List<CartItemVO> getCartItems(Long userId, Long unitId) { + public List<CartItemVO> getCartItems(String userId, String unitId) { try { // 浼樺厛浠嶳edis鑾峰彇 List<CartItemVO> items = getCartItemsFromRedis(userId, unitId); @@ -245,14 +247,14 @@ } @Override - public boolean checkCartItemStock(Long userId, Long unitId, Long pricingId) { + public boolean checkCartItemStock(String userId, String unitId, Long pricingId) { // TODO: 瀹炵幇搴撳瓨妫�鏌ラ�昏緫 return true; } @Override @Transactional(rollbackFor = Exception.class) - public boolean batchRemoveFromCart(Long userId, Long unitId, List<Long> pricingIds) { + public boolean batchRemoveFromCart(String userId, String unitId, List<Long> pricingIds) { try { if (CollectionUtils.isEmpty(pricingIds)) { return true; @@ -270,7 +272,7 @@ } @Override - public Integer getCartItemCount(Long userId, Long unitId) { + public Integer getCartItemCount(String userId, String unitId) { try { // 浼樺厛浠嶳edis鑾峰彇 String cartKey = buildCartKey(userId, unitId); @@ -288,7 +290,7 @@ } @Override - public boolean loadCartFromDatabase(Long userId, Long unitId) { + public boolean loadCartFromDatabase(String userId, String unitId) { try { List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId); if (CollectionUtils.isEmpty(cartItems)) { @@ -319,7 +321,7 @@ } @Override - public boolean syncCartToDatabase(Long userId, Long unitId) { + public boolean syncCartToDatabase(String userId, String unitId) { try { List<CartItemVO> redisItems = getCartItemsFromRedis(userId, unitId); if (CollectionUtils.isEmpty(redisItems)) { @@ -343,7 +345,7 @@ } @Override - public boolean checkCartConsistency(Long userId, Long unitId) { + public boolean checkCartConsistency(String userId, String unitId) { try { List<CartItemVO> redisItems = getCartItemsFromRedis(userId, unitId); List<Cart> dbItems = cartMapper.selectByUserIdAndUnitId(userId, unitId); @@ -378,15 +380,21 @@ // ==================== 绉佹湁鏂规硶 ==================== - private String buildCartKey(Long userId, Long unitId) { - return CART_KEY_PREFIX + userId + ":" + unitId; + private String buildCartKey(String userId, String unitId) { + if(StringUtils.hasText(unitId)){ + return CART_KEY_PREFIX + userId + ":" + unitId; + } + return CART_KEY_PREFIX + userId; } - private String buildCartItemKey(Long userId, Long unitId, Long pricingId) { - return CART_ITEM_KEY_PREFIX + userId + ":" + unitId + ":" + pricingId; + private String buildCartItemKey(String userId, String unitId, Long pricingId) { + if(StringUtils.hasText(unitId)){ + return CART_ITEM_KEY_PREFIX + userId + ":" + unitId + ":" + pricingId; + } + return CART_ITEM_KEY_PREFIX + userId + ":" + pricingId; } - private void updateCartItemList(Long userId, Long unitId, Long pricingId, boolean add) { + private void updateCartItemList(String userId, String unitId, Long pricingId, boolean add) { String cartKey = buildCartKey(userId, unitId); List<Long> pricingIds = (List<Long>) redisTemplate.opsForValue().get(cartKey); @@ -403,13 +411,13 @@ redisTemplate.opsForValue().set(cartKey, pricingIds, CART_EXPIRE_DAYS, TimeUnit.DAYS); } - private List<Long> getCartItemPricingIds(Long userId, Long unitId) { + private List<Long> getCartItemPricingIds(String userId, String unitId) { String cartKey = buildCartKey(userId, unitId); List<Long> pricingIds = (List<Long>) redisTemplate.opsForValue().get(cartKey); return pricingIds != null ? pricingIds : new ArrayList<>(); } - private List<CartItemVO> getCartItemsFromRedis(Long userId, Long unitId) { + private List<CartItemVO> getCartItemsFromRedis(String userId, String unitId) { try { String cartKey = buildCartKey(userId, unitId); List<Long> pricingIds = (List<Long>) redisTemplate.opsForValue().get(cartKey); @@ -434,7 +442,7 @@ } } - private List<CartItemVO> getCartItemsFromDatabase(Long userId, Long unitId) { + private List<CartItemVO> getCartItemsFromDatabase(String userId, String unitId) { try { List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId); List<CartItemVO> items = new ArrayList<>(); @@ -456,7 +464,7 @@ return itemVO; } - private void syncCartItemToDatabase(Long userId, Long unitId, CartItemVO item) { + private void syncCartItemToDatabase(String userId, String unitId, CartItemVO item) { try { Cart cart = new Cart(); BeanUtils.copyProperties(item, cart); @@ -480,7 +488,7 @@ } } - private void removeCartItemFromDatabase(Long userId, Long unitId, Long pricingId) { + private void removeCartItemFromDatabase(String userId, String unitId, Long pricingId) { try { Cart existingCart = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, pricingId); if (existingCart != null) { @@ -491,7 +499,7 @@ } } - private void clearCartFromDatabase(Long userId, Long unitId) { + private void clearCartFromDatabase(String userId, String unitId) { try { // 浣跨敤閫昏緫鍒犻櫎 List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId); -- Gitblit v1.8.0