From c8a179a0bdbc7bdc987788e129efce16d467889f Mon Sep 17 00:00:00 2001
From: p-honggang.li <p-honggang.li@pcitc.com>
Date: 星期四, 04 九月 2025 20:44:57 +0800
Subject: [PATCH] 修改购物车逻辑

---
 src/main/java/com/webmanage/service/impl/CartServiceImpl.java |  319 +++++++++++++++++++++++++++++++---------------------
 1 files changed, 188 insertions(+), 131 deletions(-)

diff --git a/src/main/java/com/webmanage/service/impl/CartServiceImpl.java b/src/main/java/com/webmanage/service/impl/CartServiceImpl.java
index 304a9fe..258e3bd 100644
--- a/src/main/java/com/webmanage/service/impl/CartServiceImpl.java
+++ b/src/main/java/com/webmanage/service/impl/CartServiceImpl.java
@@ -7,14 +7,18 @@
 import com.webmanage.mapper.CartMapper;
 import com.webmanage.mapper.ProductPricingMapper;
 import com.webmanage.service.CartService;
+import com.webmanage.service.CartPersistenceService;
 import com.webmanage.vo.CartItemVO;
 import com.webmanage.vo.CartVO;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
+import com.webmanage.config.CartProperties;
 import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.format.datetime.DateFormatter;
 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;
@@ -39,25 +43,31 @@
     @Resource
     private ProductPricingMapper productPricingMapper;
     
+    @Resource
+    private CartPersistenceService cartPersistenceService;
+    
+    @Resource
+    private CartProperties cartProperties;
+    
     // Redis key鍓嶇紑
     private static final String CART_KEY_PREFIX = "cart:";
     private static final String CART_ITEM_KEY_PREFIX = "cart_item:";
     private static final int CART_EXPIRE_DAYS = 30; // 璐墿杞﹁繃鏈熸椂闂�30澶�
-    
+
     @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());
             if (pricing == null) {
                 throw new BusinessException("鍟嗗搧瀹氫环涓嶅瓨鍦�");
             }
-            
+
             // 鏋勫缓璐墿杞ey
-            String cartKey = buildCartKey(userId, unitId);
-            String cartItemKey = buildCartItemKey(userId, unitId, cartItemDTO.getPricingId());
-            
+            String cartKey = buildCartKey(userId,unitId, cartItemDTO.getProductId());
+            String cartItemKey = buildCartItemKey(userId, unitId, cartItemDTO.getProductId(),cartItemDTO.getPricingId());
+
             // 妫�鏌ュ晢鍝佹槸鍚﹀凡鍦ㄨ喘鐗╄溅涓�
             CartItemVO existingItem = (CartItemVO) redisTemplate.opsForValue().get(cartItemKey);
             if (existingItem != null) {
@@ -76,19 +86,21 @@
                     existingItem.setTotalPrice(existingItem.getUnitPrice().multiply(BigDecimal.valueOf(existingItem.getQuantity())));
                 }
             }
-            
+
             // 淇濆瓨鍒癛edis
-            redisTemplate.opsForValue().set(cartItemKey, existingItem, CART_EXPIRE_DAYS, TimeUnit.DAYS);
-            
+            redisTemplate.opsForValue().set(cartItemKey, existingItem, (cartProperties.getExpireDays() != null ? cartProperties.getExpireDays() : CART_EXPIRE_DAYS), TimeUnit.DAYS);
+
             // 鏇存柊璐墿杞﹀晢鍝佸垪琛�
-            updateCartItemList(userId, unitId, cartItemDTO.getPricingId(), true);
-            
+            updateCartItemList(userId, unitId, cartItemDTO.getProductId(), cartItemDTO.getPricingId(), true);
+
             // 璁剧疆璐墿杞﹁繃鏈熸椂闂�
-            redisTemplate.expire(cartKey, CART_EXPIRE_DAYS, TimeUnit.DAYS);
-            
-            // 鍚屾鍒版暟鎹簱
-            syncCartItemToDatabase(userId, unitId, existingItem);
-            
+            redisTemplate.expire(cartKey, (cartProperties.getExpireDays() != null ? cartProperties.getExpireDays() : CART_EXPIRE_DAYS), TimeUnit.DAYS);
+
+            // 寮傛鎸佷箙鍖栧埌鏁版嵁搴擄紙鏍规嵁閰嶇疆锛�
+            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
+                cartPersistenceService.saveOrUpdate(userId, unitId, existingItem);
+            }
+
             log.info("鐢ㄦ埛{}鎴愬姛娣诲姞鍟嗗搧{}鍒拌喘鐗╄溅", userId, cartItemDTO.getProductName());
             return true;
         } catch (Exception e) {
@@ -96,22 +108,24 @@
             throw new BusinessException("娣诲姞鍟嗗搧鍒拌喘鐗╄溅澶辫触锛�" + e.getMessage());
         }
     }
-    
+
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public boolean removeFromCart(Long userId, Long unitId, Long pricingId) {
+    public boolean removeFromCart(String userId, String unitId, String productId, Long pricingId) {
         try {
-            String cartItemKey = buildCartItemKey(userId, unitId, pricingId);
-            
+            String cartItemKey = buildCartItemKey(userId, unitId, productId, pricingId);
+
             // 浠嶳edis涓垹闄ゅ晢鍝侀」
             Boolean removed = redisTemplate.delete(cartItemKey);
             if (Boolean.TRUE.equals(removed)) {
                 // 鏇存柊璐墿杞﹀晢鍝佸垪琛�
-                updateCartItemList(userId, unitId, pricingId, false);
-                
-                // 浠庢暟鎹簱涓垹闄�
-                removeCartItemFromDatabase(userId, unitId, pricingId);
-                
+                updateCartItemList(userId, unitId, productId, pricingId, false);
+
+                // 寮傛浠庢暟鎹簱涓垹闄わ紙鏍规嵁閰嶇疆锛�
+                if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
+                    cartPersistenceService.remove(userId, unitId, productId,pricingId);
+                }
+
                 log.info("鐢ㄦ埛{}鎴愬姛浠庤喘鐗╄溅绉婚櫎鍟嗗搧{}", userId, pricingId);
                 return true;
             }
@@ -121,31 +135,33 @@
             throw new BusinessException("浠庤喘鐗╄溅绉婚櫎鍟嗗搧澶辫触锛�" + e.getMessage());
         }
     }
-    
+
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public boolean updateCartItemQuantity(Long userId, Long unitId, Long pricingId, Integer quantity) {
+    public boolean updateCartItemQuantity(String userId, String unitId, String productId, Long pricingId, Integer quantity) {
         try {
             if (quantity <= 0) {
-                return removeFromCart(userId, unitId, pricingId);
+                return removeFromCart(userId, unitId, productId, pricingId);
             }
-            
-            String cartItemKey = buildCartItemKey(userId, unitId, pricingId);
+
+            String cartItemKey = buildCartItemKey(userId, unitId, productId, pricingId);
             CartItemVO cartItem = (CartItemVO) redisTemplate.opsForValue().get(cartItemKey);
             if (cartItem == null) {
                 throw new BusinessException("璐墿杞﹀晢鍝佷笉瀛樺湪");
             }
-            
+
             cartItem.setQuantity(quantity);
             cartItem.setTotalPrice(cartItem.getUnitPrice().multiply(BigDecimal.valueOf(quantity)));
             cartItem.setUpdateTime(LocalDateTime.now());
-            
+
             // 鏇存柊鍒癛edis
-            redisTemplate.opsForValue().set(cartItemKey, cartItem, CART_EXPIRE_DAYS, TimeUnit.DAYS);
-            
-            // 鍚屾鍒版暟鎹簱
-            syncCartItemToDatabase(userId, unitId, cartItem);
-            
+            redisTemplate.opsForValue().set(cartItemKey, cartItem, (cartProperties.getExpireDays() != null ? cartProperties.getExpireDays() : CART_EXPIRE_DAYS), TimeUnit.DAYS);
+
+            // 寮傛鎸佷箙鍖栧埌鏁版嵁搴擄紙鏍规嵁閰嶇疆锛�
+            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
+                cartPersistenceService.saveOrUpdate(userId, unitId, cartItem);
+            }
+
             log.info("鐢ㄦ埛{}鎴愬姛鏇存柊璐墿杞﹀晢鍝亄}鏁伴噺涓簕}", userId, pricingId, quantity);
             return true;
         } catch (Exception e) {
@@ -153,26 +169,60 @@
             throw new BusinessException("鏇存柊璐墿杞﹀晢鍝佹暟閲忓け璐ワ細" + e.getMessage());
         }
     }
-    
+
+
     @Override
     @Transactional(rollbackFor = Exception.class)
-    public boolean clearCart(Long userId, Long unitId) {
+    public boolean updateCartItemDuration(String userId, String unitId, String productId, Long pricingId, Integer duration) {
         try {
-            String cartKey = buildCartKey(userId, unitId);
-            List<Long> pricingIds = getCartItemPricingIds(userId, unitId);
-            
+
+            String cartItemKey = buildCartItemKey(userId, unitId, productId, pricingId);
+            CartItemVO cartItem = (CartItemVO) redisTemplate.opsForValue().get(cartItemKey);
+            if (cartItem == null) {
+                throw new BusinessException("璐墿杞﹀晢鍝佷笉瀛樺湪");
+            }
+
+            cartItem.setDuration(duration);
+            cartItem.setUpdateTime(LocalDateTime.now());
+
+            // 鏇存柊鍒癛edis
+            redisTemplate.opsForValue().set(cartItemKey, cartItem, (cartProperties.getExpireDays() != null ? cartProperties.getExpireDays() : CART_EXPIRE_DAYS), TimeUnit.DAYS);
+
+            // 寮傛鎸佷箙鍖栧埌鏁版嵁搴擄紙鏍规嵁閰嶇疆锛�
+            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
+                cartPersistenceService.saveOrUpdate(userId, unitId, cartItem);
+            }
+
+            log.info("鐢ㄦ埛{}鎴愬姛鏇存柊璐墿杞﹀晢鍝亄}骞撮檺涓簕}", userId, pricingId, duration);
+            return true;
+        } catch (Exception e) {
+            log.error("鏇存柊璐墿杞﹀晢鍝佹暟閲忓け璐�", e);
+            throw new BusinessException("鏇存柊璐墿杞﹀晢鍝佹暟閲忓け璐ワ細" + e.getMessage());
+        }
+    }
+
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public boolean clearCart(String userId, String unitId, String productId) {
+        try {
+            String cartKey = buildCartKey(userId, unitId, productId);
+            List<Long> pricingIds = getCartItemPricingIds(userId, unitId, productId);
+
             // 鍒犻櫎鎵�鏈夊晢鍝侀」
             for (Long pricingId : pricingIds) {
-                String cartItemKey = buildCartItemKey(userId, unitId, pricingId);
+                String cartItemKey = buildCartItemKey(userId, unitId, productId,pricingId);
                 redisTemplate.delete(cartItemKey);
             }
-            
+
             // 鍒犻櫎璐墿杞﹀垪琛�
             redisTemplate.delete(cartKey);
-            
-            // 娓呯┖鏁版嵁搴撲腑鐨勮喘鐗╄溅鏁版嵁
-            clearCartFromDatabase(userId, unitId);
-            
+
+            // 寮傛娓呯┖鏁版嵁搴撲腑鐨勮喘鐗╄溅鏁版嵁锛堟牴鎹厤缃級
+            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
+                cartPersistenceService.clear(userId, unitId, productId);
+            }
+
             log.info("鐢ㄦ埛{}鎴愬姛娓呯┖璐墿杞�", userId);
             return true;
         } catch (Exception e) {
@@ -180,119 +230,120 @@
             throw new BusinessException("娓呯┖璐墿杞﹀け璐ワ細" + e.getMessage());
         }
     }
-    
+
     @Override
-    public CartVO getCart(Long userId, Long unitId) {
+    public CartVO getCart(String userId, String unitId, String productId) {
         try {
             CartVO cartVO = new CartVO();
             cartVO.setUserId(userId);
-            cartVO.setUnitId(unitId);
-            
-            List<CartItemVO> items = getCartItems(userId, unitId);
+            if (StringUtils.hasText(unitId)){
+                cartVO.setUnitId(unitId);
+            }
+            List<CartItemVO> items = getCartItems(userId, unitId, productId);
             cartVO.setItems(items);
-            
+
             // 璁$畻鎬绘暟閲忓拰鎬婚噾棰�
             int totalQuantity = items.stream().mapToInt(item -> item.getQuantity()).sum();
             BigDecimal totalAmount = items.stream()
                     .map(item -> item.getTotalPrice())
                     .reduce(BigDecimal.ZERO, BigDecimal::add);
-            
+
             cartVO.setTotalQuantity(totalQuantity);
             cartVO.setTotalAmount(totalAmount);
             cartVO.setLastUpdateTime(LocalDateTime.now());
-            
+
             return cartVO;
         } catch (Exception e) {
             log.error("鑾峰彇璐墿杞︿俊鎭け璐�", e);
             throw new BusinessException("鑾峰彇璐墿杞︿俊鎭け璐ワ細" + e.getMessage());
         }
     }
-    
+
     @Override
-    public List<CartItemVO> getCartItems(Long userId, Long unitId) {
+    public List<CartItemVO> getCartItems(String userId, String unitId,String productId) {
         try {
             // 浼樺厛浠嶳edis鑾峰彇
-            List<CartItemVO> items = getCartItemsFromRedis(userId, unitId);
+            List<CartItemVO> items = getCartItemsFromRedis(userId, unitId,productId);
             if (items != null && !items.isEmpty()) {
                 return items;
             }
-            
+
             // Redis涓病鏈夋暟鎹紝浠庢暟鎹簱鍔犺浇
             log.info("Redis涓棤璐墿杞︽暟鎹紝浠庢暟鎹簱鍔犺浇鐢ㄦ埛{}鐨勮喘鐗╄溅", userId);
-            return loadCartFromDatabase(userId, unitId) ? getCartItemsFromRedis(userId, unitId) : new ArrayList<>();
+            return loadCartFromDatabase(userId, unitId, productId) ? getCartItemsFromRedis(userId, unitId, productId) : new ArrayList<>();
         } catch (Exception e) {
             log.error("鑾峰彇璐墿杞﹀晢鍝佸垪琛ㄥけ璐�", e);
             // 闄嶇骇鍒版暟鎹簱鏌ヨ
-            return getCartItemsFromDatabase(userId, unitId);
+            return getCartItemsFromDatabase(userId, unitId, productId);
         }
     }
-    
+
     @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, String productId,List<Long> pricingIds) {
         try {
             if (CollectionUtils.isEmpty(pricingIds)) {
                 return true;
             }
-            
+
             for (Long pricingId : pricingIds) {
-                removeFromCart(userId, unitId, pricingId);
+                removeFromCart(userId, unitId, productId,pricingId);
             }
-            
+
             return true;
         } catch (Exception e) {
             log.error("鎵归噺鍒犻櫎璐墿杞﹀晢鍝佸け璐�", e);
             throw new BusinessException("鎵归噺鍒犻櫎璐墿杞﹀晢鍝佸け璐ワ細" + e.getMessage());
         }
     }
-    
+
     @Override
-    public Integer getCartItemCount(Long userId, Long unitId) {
+    public Integer getCartItemCount(String userId, String unitId, String productId) {
         try {
             // 浼樺厛浠嶳edis鑾峰彇
-            String cartKey = buildCartKey(userId, unitId);
+            String cartKey = buildCartKey(userId, unitId, productId);
             List<Long> pricingIds = (List<Long>) redisTemplate.opsForValue().get(cartKey);
             if (pricingIds != null) {
                 return pricingIds.size();
             }
-            
+
             // 浠庢暟鎹簱鑾峰彇
-            return cartMapper.countByUserIdAndUnitId(userId, unitId);
+            return cartMapper.countByUserIdAndUnitId(userId, unitId, productId);
         } catch (Exception e) {
             log.error("鑾峰彇璐墿杞﹀晢鍝佹暟閲忓け璐�", e);
             return 0;
         }
     }
-    
+
     @Override
-    public boolean loadCartFromDatabase(Long userId, Long unitId) {
+    public boolean loadCartFromDatabase(String userId, String unitId, String productId) {
         try {
-            List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
+            List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId, productId);
             if (CollectionUtils.isEmpty(cartItems)) {
                 return false;
             }
-            
-            String cartKey = buildCartKey(userId, unitId);
+
+            String cartKey = buildCartKey(userId, unitId, productId);
             List<Long> pricingIds = new ArrayList<>();
-            
+
             for (Cart cartItem : cartItems) {
                 CartItemVO itemVO = convertCartToCartItemVO(cartItem);
-                String cartItemKey = buildCartItemKey(userId, unitId, cartItem.getPricingId());
-                
+                String cartItemKey = buildCartItemKey(userId, unitId,cartItem.getProductId(), cartItem.getPricingId());
+
                 // 淇濆瓨鍒癛edis
                 redisTemplate.opsForValue().set(cartItemKey, itemVO, CART_EXPIRE_DAYS, TimeUnit.DAYS);
                 pricingIds.add(cartItem.getPricingId());
             }
-            
+
             // 淇濆瓨璐墿杞﹀垪琛ㄥ埌Redis
             redisTemplate.opsForValue().set(cartKey, pricingIds, CART_EXPIRE_DAYS, TimeUnit.DAYS);
-            
+
             log.info("鎴愬姛浠庢暟鎹簱鍔犺浇鐢ㄦ埛{}鐨勮喘鐗╄溅鏁版嵁鍒癛edis", userId);
             return true;
         } catch (Exception e) {
@@ -300,23 +351,23 @@
             return false;
         }
     }
-    
+
     @Override
-    public boolean syncCartToDatabase(Long userId, Long unitId) {
+    public boolean syncCartToDatabase(String userId, String unitId, String productId) {
         try {
-            List<CartItemVO> redisItems = getCartItemsFromRedis(userId, unitId);
+            List<CartItemVO> redisItems = getCartItemsFromRedis(userId, unitId, productId);
             if (CollectionUtils.isEmpty(redisItems)) {
                 return true;
             }
-            
+
             // 娓呯┖鏁版嵁搴撲腑鐨勮喘鐗╄溅鏁版嵁
-            clearCartFromDatabase(userId, unitId);
-            
+            clearCartFromDatabase(userId, unitId, productId);
+
             // 鍚屾Redis鏁版嵁鍒版暟鎹簱
             for (CartItemVO item : redisItems) {
-                syncCartItemToDatabase(userId, unitId, item);
+                syncCartItemToDatabase(userId, unitId,productId, item);
             }
-            
+
             log.info("鎴愬姛鍚屾Redis璐墿杞︽暟鎹埌鏁版嵁搴擄紝鐢ㄦ埛{}", userId);
             return true;
         } catch (Exception e) {
@@ -324,18 +375,18 @@
             return false;
         }
     }
-    
+
     @Override
-    public boolean checkCartConsistency(Long userId, Long unitId) {
+    public boolean checkCartConsistency(String userId, String unitId, String productId) {
         try {
-            List<CartItemVO> redisItems = getCartItemsFromRedis(userId, unitId);
-            List<Cart> dbItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
-            
+            List<CartItemVO> redisItems = getCartItemsFromRedis(userId, unitId, productId);
+            List<Cart> dbItems = cartMapper.selectByUserIdAndUnitId(userId, unitId, productId);
+
             if (redisItems.size() != dbItems.size()) {
                 log.warn("璐墿杞︽暟鎹笉涓�鑷达細Redis鏁伴噺{}锛屾暟鎹簱鏁伴噺{}", redisItems.size(), dbItems.size());
                 return false;
             }
-            
+
             // 妫�鏌ユ瘡涓晢鍝侀」鏄惁涓�鑷�
             for (CartItemVO redisItem : redisItems) {
                 boolean found = false;
@@ -351,65 +402,71 @@
                     return false;
                 }
             }
-            
+
             return true;
         } catch (Exception e) {
             log.error("妫�鏌ヨ喘鐗╄溅鏁版嵁涓�鑷存�уけ璐�", e);
             return false;
         }
     }
-    
+
     // ==================== 绉佹湁鏂规硶 ====================
-    
-    private String buildCartKey(Long userId, Long unitId) {
-        return CART_KEY_PREFIX + userId + ":" + unitId;
+
+    private String buildCartKey(String userId, String unitId,String productId) {
+        if(StringUtils.hasText(unitId)){
+            return CART_KEY_PREFIX + userId + ":" + unitId + ":" + productId;
+        }
+        return CART_KEY_PREFIX + userId + ":" + productId;
     }
-    
-    private String buildCartItemKey(Long userId, Long unitId, Long pricingId) {
-        return CART_ITEM_KEY_PREFIX + userId + ":" + unitId + ":" + pricingId;
+
+    private String buildCartItemKey(String userId, String unitId,String productId, Long pricingId) {
+        if(StringUtils.hasText(unitId)){
+            return CART_ITEM_KEY_PREFIX + userId + ":" + unitId + ":" + productId + ":" + pricingId;
+        }
+        return CART_ITEM_KEY_PREFIX + userId + ":" + productId + ":" + pricingId;
     }
-    
-    private void updateCartItemList(Long userId, Long unitId, Long pricingId, boolean add) {
-        String cartKey = buildCartKey(userId, unitId);
+
+    private void updateCartItemList(String userId, String unitId,String productId ,Long pricingId, boolean add) {
+        String cartKey = buildCartKey(userId, unitId, productId);
         List<Long> pricingIds = (List<Long>) redisTemplate.opsForValue().get(cartKey);
-        
+
         if (pricingIds == null) {
             pricingIds = new ArrayList<>();
         }
-        
+
         if (add && !pricingIds.contains(pricingId)) {
             pricingIds.add(pricingId);
         } else if (!add) {
             pricingIds.remove(pricingId);
         }
-        
+
         redisTemplate.opsForValue().set(cartKey, pricingIds, CART_EXPIRE_DAYS, TimeUnit.DAYS);
     }
-    
-    private List<Long> getCartItemPricingIds(Long userId, Long unitId) {
-        String cartKey = buildCartKey(userId, unitId);
+
+    private List<Long> getCartItemPricingIds(String userId, String unitId, String productId) {
+        String cartKey = buildCartKey(userId, unitId, productId);
         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, String productId) {
         try {
-            String cartKey = buildCartKey(userId, unitId);
+            String cartKey = buildCartKey(userId, unitId, productId);
             List<Long> pricingIds = (List<Long>) redisTemplate.opsForValue().get(cartKey);
-            
+
             if (CollectionUtils.isEmpty(pricingIds)) {
                 return new ArrayList<>();
             }
-            
+
             List<CartItemVO> items = new ArrayList<>();
             for (Long pricingId : pricingIds) {
-                String cartItemKey = buildCartItemKey(userId, unitId, pricingId);
+                String cartItemKey = buildCartItemKey(userId, unitId, productId,pricingId);
                 CartItemVO item = (CartItemVO) redisTemplate.opsForValue().get(cartItemKey);
                 if (item != null) {
                     items.add(item);
                 }
             }
-            
+
             return items;
         } catch (Exception e) {
             log.error("浠嶳edis鑾峰彇璐墿杞﹀晢鍝佸垪琛ㄥけ璐�", e);
@@ -417,9 +474,9 @@
         }
     }
     
-    private List<CartItemVO> getCartItemsFromDatabase(Long userId, Long unitId) {
+    private List<CartItemVO> getCartItemsFromDatabase(String userId, String unitId, String productId) {
         try {
-            List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
+            List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId, productId);
             List<CartItemVO> items = new ArrayList<>();
             
             for (Cart cartItem : cartItems) {
@@ -439,7 +496,7 @@
         return itemVO;
     }
     
-    private void syncCartItemToDatabase(Long userId, Long unitId, CartItemVO item) {
+    private void syncCartItemToDatabase(String userId, String unitId, String productId,CartItemVO item) {
         try {
             Cart cart = new Cart();
             BeanUtils.copyProperties(item, cart);
@@ -448,7 +505,7 @@
             cart.setUpdateTime(LocalDateTime.now());
             
             // 妫�鏌ユ槸鍚﹀凡瀛樺湪
-            Cart existingCart = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, item.getPricingId());
+            Cart existingCart = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, productId,item.getPricingId());
             if (existingCart != null) {
                 // 鏇存柊
                 cart.setId(existingCart.getId());
@@ -463,21 +520,21 @@
         }
     }
     
-    private void removeCartItemFromDatabase(Long userId, Long unitId, Long pricingId) {
+    private void removeCartItemFromDatabase(String userId, String unitId, String productId, Long pricingId) {
         try {
-            Cart existingCart = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, pricingId);
+            Cart existingCart = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, productId,pricingId);
             if (existingCart != null) {
-                cartMapper.deleteById(existingCart.getId());
+                cartMapper.deleteByCustomerCondition(existingCart.getId());
             }
         } catch (Exception e) {
             log.error("浠庢暟鎹簱鍒犻櫎璐墿杞﹀晢鍝佸け璐�", e);
         }
     }
     
-    private void clearCartFromDatabase(Long userId, Long unitId) {
+    private void clearCartFromDatabase(String userId, String unitId, String productId) {
         try {
             // 浣跨敤閫昏緫鍒犻櫎
-            List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
+            List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId, productId);
             for (Cart item : cartItems) {
                 cartMapper.deleteById(item.getId());
             }

--
Gitblit v1.8.0