p-honggang.li
2025-08-20 fefed50bf97060a82aabd09e090fa08ce532532f
src/main/java/com/webmanage/service/impl/CartServiceImpl.java
@@ -7,11 +7,14 @@
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;
@@ -38,6 +41,12 @@
    
    @Resource
    private ProductPricingMapper productPricingMapper;
    @Resource
    private CartPersistenceService cartPersistenceService;
    @Resource
    private CartProperties cartProperties;
    
    // Redis key前缀
    private static final String CART_KEY_PREFIX = "cart:";
@@ -78,16 +87,18 @@
            }
            
            // 保存到Redis
            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);
            
            // 设置购物车过期时间
            redisTemplate.expire(cartKey, CART_EXPIRE_DAYS, TimeUnit.DAYS);
            redisTemplate.expire(cartKey, (cartProperties.getExpireDays() != null ? cartProperties.getExpireDays() : CART_EXPIRE_DAYS), TimeUnit.DAYS);
            
            // 同步到数据库
            syncCartItemToDatabase(userId, unitId, existingItem);
            // 异步持久化到数据库(根据配置)
            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
                cartPersistenceService.saveOrUpdate(userId, unitId, existingItem);
            }
            
            log.info("用户{}成功添加商品{}到购物车", userId, cartItemDTO.getProductName());
            return true;
@@ -109,8 +120,10 @@
                // 更新购物车商品列表
                updateCartItemList(userId, unitId, pricingId, false);
                
                // 从数据库中删除
                removeCartItemFromDatabase(userId, unitId, pricingId);
                // 异步从数据库中删除(根据配置)
                if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
                    cartPersistenceService.remove(userId, unitId, pricingId);
                }
                
                log.info("用户{}成功从购物车移除商品{}", userId, pricingId);
                return true;
@@ -141,10 +154,12 @@
            cartItem.setUpdateTime(LocalDateTime.now());
            
            // 更新到Redis
            redisTemplate.opsForValue().set(cartItemKey, cartItem, CART_EXPIRE_DAYS, TimeUnit.DAYS);
            redisTemplate.opsForValue().set(cartItemKey, cartItem, (cartProperties.getExpireDays() != null ? cartProperties.getExpireDays() : CART_EXPIRE_DAYS), TimeUnit.DAYS);
            
            // 同步到数据库
            syncCartItemToDatabase(userId, unitId, cartItem);
            // 异步持久化到数据库(根据配置)
            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
                cartPersistenceService.saveOrUpdate(userId, unitId, cartItem);
            }
            
            log.info("用户{}成功更新购物车商品{}数量为{}", userId, pricingId, quantity);
            return true;
@@ -170,8 +185,10 @@
            // 删除购物车列表
            redisTemplate.delete(cartKey);
            
            // 清空数据库中的购物车数据
            clearCartFromDatabase(userId, unitId);
            // 异步清空数据库中的购物车数据(根据配置)
            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
                cartPersistenceService.clear(userId, unitId);
            }
            
            log.info("用户{}成功清空购物车", userId);
            return true;
@@ -467,7 +484,7 @@
        try {
            Cart existingCart = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, pricingId);
            if (existingCart != null) {
                cartMapper.deleteById(existingCart.getId());
                cartMapper.deleteByCustomerCondition(existingCart.getId());
            }
        } catch (Exception e) {
            log.error("从数据库删除购物车商品失败", e);