Bang Hu
2025-08-28 f28da0f4705921b88d94c4531fa89753c4bb9f52
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 @@
            }
            // 构建购物车key
            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 {
            // 优先从Redis获取
            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 {
            // 优先从Redis获取
            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);