p-honggang.li
9 天以前 c8a179a0bdbc7bdc987788e129efce16d467889f
修改购物车逻辑
11个文件已修改
290 ■■■■■ 已修改文件
src/main/java/com/webmanage/controller/CartController.java 66 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/webmanage/controller/ProductPricingController.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/webmanage/mapper/CartMapper.java 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/webmanage/service/CartPersistenceService.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/webmanage/service/CartService.java 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/webmanage/service/ProductPricingService.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/webmanage/service/impl/CartPersistenceServiceImpl.java 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/webmanage/service/impl/CartServiceImpl.java 148 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java 12 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/webmanage/service/impl/ProductPricingServiceImpl.java 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/mapper/CartMapper.xml 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/webmanage/controller/CartController.java
@@ -54,9 +54,10 @@
    @ApiOperation("从购物车移除商品")
    public Result<Object> removeFromCart(@RequestParam @NotNull String userId,
                                        @RequestParam  String unitId,
                                         @RequestParam @NotNull String productId,
                                        @RequestParam @NotNull Long pricingId) {
        try {
            boolean result = cartService.removeFromCart(userId, unitId, pricingId);
            boolean result = cartService.removeFromCart(userId, unitId, productId, pricingId);
            if (result) {
                return Result.success("从购物车移除商品成功");
            } else {
@@ -72,10 +73,32 @@
    @ApiOperation("更新购物车商品数量")
    public Result<Object> updateCartItemQuantity(@RequestParam @NotNull String userId,
                                               @RequestParam  String unitId,
                                               @RequestParam @NotNull String productId,
                                               @RequestParam @NotNull Long pricingId,
                                               @RequestParam @NotNull @Min(1) Integer quantity) {
        try {
            boolean result = cartService.updateCartItemQuantity(userId, unitId, pricingId, quantity);
            boolean result = cartService.updateCartItemQuantity(userId, unitId, productId, pricingId, quantity);
            if (result) {
                return Result.success("更新购物车商品数量成功");
            } else {
                return Result.error("更新购物车商品数量失败");
            }
        } catch (Exception e) {
            log.error("更新购物车商品数量失败", e);
            return Result.error("更新购物车商品数量失败:" + e.getMessage());
        }
    }
    @PutMapping("/update/duration")
    @ApiOperation("更新购物车商品数量")
    public Result<Object> updateCartItemDuration(@RequestParam @NotNull String userId,
                                                 @RequestParam  String unitId,
                                                 @RequestParam @NotNull String productId,
                                                 @RequestParam @NotNull Long pricingId,
                                                 @RequestParam @NotNull @Min(1) Integer duration) {
        try {
            boolean result = cartService.updateCartItemDuration(userId, unitId, productId, pricingId, duration);
            if (result) {
                return Result.success("更新购物车商品数量成功");
            } else {
@@ -90,9 +113,10 @@
    @DeleteMapping("/clear")
    @ApiOperation("清空购物车")
    public Result<Object> clearCart(@RequestParam @NotNull String userId,
                                   @RequestParam  String unitId) {
                                   @RequestParam  String unitId,
                                    @RequestParam @NotNull String productId) {
        try {
            boolean result = cartService.clearCart(userId, unitId);
            boolean result = cartService.clearCart(userId, unitId, productId);
            if (result) {
                return Result.success("清空购物车成功");
            } else {
@@ -107,9 +131,10 @@
    @GetMapping("/info")
    @ApiOperation("获取购物车信息")
    public Result<Object> getCart(@RequestParam @NotNull String userId,
                                 @RequestParam  String unitId) {
                                 @RequestParam  String unitId,
                                  @RequestParam @NotNull String productId ) {
        try {
            CartVO cart = cartService.getCart(userId, unitId);
            CartVO cart = cartService.getCart(userId, unitId, productId);
            return Result.success(cart);
        } catch (Exception e) {
            log.error("获取购物车信息失败", e);
@@ -120,9 +145,10 @@
    @GetMapping("/items")
    @ApiOperation("获取购物车商品列表")
    public Result<Object> getCartItems(@RequestParam @NotNull String userId,
                                      @RequestParam  String unitId) {
                                      @RequestParam  String unitId,
                                       @RequestParam @NotNull String productId) {
        try {
            List<CartItemVO> items = cartService.getCartItems(userId, unitId);
            List<CartItemVO> items = cartService.getCartItems(userId, unitId, productId);
            return Result.success(items);
        } catch (Exception e) {
            log.error("获取购物车商品列表失败", e);
@@ -134,9 +160,10 @@
    @ApiOperation("批量删除购物车商品")
    public Result<Object> batchRemoveFromCart(@RequestParam @NotNull String userId,
                                             @RequestParam  String unitId,
                                              @RequestParam @NotNull String productId,
                                             @RequestBody List<Long> pricingIds) {
        try {
            boolean result = cartService.batchRemoveFromCart(userId, unitId, pricingIds);
            boolean result = cartService.batchRemoveFromCart(userId, unitId, productId, pricingIds);
            if (result) {
                return Result.success("批量删除购物车商品成功");
            } else {
@@ -151,9 +178,10 @@
    @GetMapping("/count")
    @ApiOperation("获取购物车商品数量")
    public Result<Object> getCartItemCount(@RequestParam @NotNull String userId,
                                          @RequestParam  String unitId) {
                                          @RequestParam  String unitId,
                                           @RequestParam @NotNull String productId ) {
        try {
            Integer count = cartService.getCartItemCount(userId, unitId);
            Integer count = cartService.getCartItemCount(userId, unitId, productId);
            return Result.success(count);
        } catch (Exception e) {
            log.error("获取购物车商品数量失败", e);
@@ -164,9 +192,10 @@
    @PostMapping("/sync-to-db")
    @ApiOperation("同步Redis购物车数据到数据库")
    public Result<Object> syncCartToDatabase(@RequestParam @NotNull String userId,
                                            @RequestParam @NotNull String unitId) {
                                            @RequestParam @NotNull String unitId,
                                             @RequestParam @NotNull String productId ) {
        try {
            boolean result = cartService.syncCartToDatabase(userId, unitId);
                boolean result = cartService.syncCartToDatabase(userId, unitId, productId);
            if (result) {
                return Result.success("同步购物车数据到数据库成功");
            } else {
@@ -177,13 +206,13 @@
            return Result.error("同步购物车数据到数据库失败:" + e.getMessage());
        }
    }
    @PostMapping("/load-from-db")
    @ApiOperation("从数据库加载购物车数据到Redis")
    public Result<Object> loadCartFromDatabase(@RequestParam @NotNull String userId,
                                              @RequestParam @NotNull String unitId) {
                                              @RequestParam @NotNull String unitId,
                                              @RequestParam @NotNull String productId ) {
        try {
            boolean result = cartService.loadCartFromDatabase(userId, unitId);
            boolean result = cartService.loadCartFromDatabase(userId, unitId,productId);
            if (result) {
                return Result.success("从数据库加载购物车数据成功");
            } else {
@@ -198,9 +227,10 @@
    @GetMapping("/consistency")
    @ApiOperation("检查购物车数据一致性")
    public Result<Object> checkCartConsistency(@RequestParam @NotNull String userId,
                                              @RequestParam @NotNull String unitId) {
                                              @RequestParam @NotNull String unitId,
                                              @RequestParam @NotNull String productId) {
        try {
            boolean isConsistent = cartService.checkCartConsistency(userId, unitId);
            boolean isConsistent = cartService.checkCartConsistency(userId, unitId, productId);
            if (isConsistent) {
                return Result.success("购物车数据一致");
            } else {
src/main/java/com/webmanage/controller/ProductPricingController.java
@@ -127,13 +127,14 @@
    @GetMapping("/condition")
    @ApiOperation("根据条件查询产品定价")
    public Result<List<ProductPricing>> getPricingByCondition(
            @ApiParam("产品ID") @RequestParam(required = false) String productId,
            @ApiParam("套件名称") @RequestParam(required = false) String suiteName,
            @ApiParam("销售形式") @RequestParam(required = false) String salesForm,
            @ApiParam("客户对象") @RequestParam(required = false) String customerType,
            @ApiParam("价格类型") @RequestParam(required = false) String priceType,
            @ApiParam("启用状态") @RequestParam(required = false) Boolean isActive) {
        try {
            List<ProductPricing> result = productPricingService.getPricingByCondition(
            List<ProductPricing> result = productPricingService.getPricingByCondition(productId,
                    suiteName, salesForm, customerType, priceType, isActive);
            return Result.success("查询成功", result);
        } catch (Exception e) {
src/main/java/com/webmanage/mapper/CartMapper.java
@@ -16,17 +16,17 @@
    /**
     * 根据用户ID和单位ID查询购物车商品列表
     */
    List<Cart> selectByUserIdAndUnitId(@Param("userId") String userId, @Param("unitId") String unitId);
    List<Cart> selectByUserIdAndUnitId(@Param("userId") String userId, @Param("unitId") String unitId, @Param("productId") String productId);
    
    /**
     * 根据用户ID、单位ID和定价ID查询购物车商品
     */
    Cart selectByUserIdUnitIdAndPricingId(@Param("userId") String userId, @Param("unitId") String unitId, @Param("pricingId") Long pricingId);
    Cart selectByUserIdUnitIdAndPricingId(@Param("userId") String userId, @Param("unitId") String unitId,@Param("productId") String productId, @Param("pricingId") Long pricingId);
    
    /**
     * 根据用户ID和单位ID统计购物车商品数量
     */
    Integer countByUserIdAndUnitId(@Param("userId") String userId, @Param("unitId") String unitId);
    Integer countByUserIdAndUnitId(@Param("userId") String userId, @Param("unitId") String unitId, @Param("productId") String productId);
    
    /**
     * 根据用户ID和单位ID计算购物车总金额
src/main/java/com/webmanage/service/CartPersistenceService.java
@@ -4,8 +4,8 @@
public interface CartPersistenceService {
    void saveOrUpdate(String userId, String unitId, CartItemVO item);
    void remove(String userId, String unitId, Long pricingId);
    void clear(String userId, String unitId);
    void remove(String userId, String unitId, String productId, Long pricingId);
    void clear(String userId, String unitId, String productId);
}
src/main/java/com/webmanage/service/CartService.java
@@ -19,27 +19,33 @@
    /**
     * 从购物车移除商品(Redis + 数据库)
     */
    boolean removeFromCart(String userId, String unitId, Long pricingId);
    boolean removeFromCart(String userId, String unitId, String productId, Long pricingId);
    
    /**
     * 更新购物车商品数量(Redis + 数据库)
     */
    boolean updateCartItemQuantity(String userId, String unitId, Long pricingId, Integer quantity);
    boolean updateCartItemQuantity(String userId, String unitId,String productId, Long pricingId, Integer quantity);
    /**
     * 更新购物车商品年限(Redis + 数据库)
     */
    boolean updateCartItemDuration(String userId, String unitId,String productId, Long pricingId, Integer duration);
    
    /**
     * 清空购物车(Redis + 数据库)
     */
    boolean clearCart(String userId, String unitId);
    boolean clearCart(String userId, String unitId,String productId);
    
    /**
     * 获取购物车信息(优先Redis,失败则从数据库加载)
     */
    CartVO getCart(String userId, String unitId);
    CartVO getCart(String userId, String unitId, String productId);
    
    /**
     * 获取购物车商品列表(优先Redis,失败则从数据库加载)
     */
    List<CartItemVO> getCartItems(String userId, String unitId);
    List<CartItemVO> getCartItems(String userId, String unitId,String productId);
    
    /**
     * 检查购物车商品库存
@@ -49,25 +55,25 @@
    /**
     * 批量删除购物车商品(Redis + 数据库)
     */
    boolean batchRemoveFromCart(String userId, String unitId, List<Long> pricingIds);
    boolean batchRemoveFromCart(String userId, String unitId, String productId,List<Long> pricingIds);
    
    /**
     * 获取购物车商品数量(优先Redis,失败则从数据库加载)
     */
    Integer getCartItemCount(String userId, String unitId);
    Integer getCartItemCount(String userId, String unitId, String productId);
    
    /**
     * 从数据库加载购物车数据到Redis
     */
    boolean loadCartFromDatabase(String userId, String unitId);
    boolean loadCartFromDatabase(String userId, String unitId, String productId);
    
    /**
     * 同步Redis数据到数据库
     */
    boolean syncCartToDatabase(String userId, String unitId);
    boolean syncCartToDatabase(String userId, String unitId, String productId);
    
    /**
     * 检查购物车数据一致性
     */
    boolean checkCartConsistency(String userId, String unitId);
    boolean checkCartConsistency(String userId, String unitId, String productId);
}
src/main/java/com/webmanage/service/ProductPricingService.java
@@ -43,6 +43,6 @@
    /**
     * 根据条件查询产品定价
     */
    List<ProductPricing> getPricingByCondition(String suiteName, String salesForm,
    List<ProductPricing> getPricingByCondition(String productId, String suiteName, String salesForm,
                                             String customerType, String priceType, Boolean isActive);
}
src/main/java/com/webmanage/service/impl/CartPersistenceServiceImpl.java
@@ -30,7 +30,7 @@
                cart.setUnitId(unitId);
            }
            cart.setUpdateTime(LocalDateTime.now());
            Cart existing = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, item.getPricingId());
            Cart existing = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId,item.getProductId(), item.getPricingId());
            if (existing != null) {
                cart.setId(existing.getId());
                cartMapper.updateById(cart);
@@ -43,9 +43,9 @@
    @Override
    @Async("asyncExecutor")
    public void remove(String userId, String unitId, Long pricingId) {
    public void remove(String userId, String unitId, String productId,Long pricingId) {
        try {
            Cart existing = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, pricingId);
            Cart existing = cartMapper.selectByUserIdUnitIdAndPricingId(userId, unitId, productId,pricingId);
            if (existing != null) {
                cartMapper.deleteById(existing.getId());
            }
@@ -54,9 +54,9 @@
    @Override
    @Async("asyncExecutor")
    public void clear(String userId, String unitId) {
    public void clear(String userId, String unitId,String productId) {
        try {
            java.util.List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId);
            java.util.List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId, productId);
            for (Cart item : cartItems) {
                cartMapper.deleteById(item.getId());
            }
src/main/java/com/webmanage/service/impl/CartServiceImpl.java
@@ -65,8 +65,8 @@
            }
            // 构建购物车key
            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);
@@ -91,7 +91,7 @@
            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, (cartProperties.getExpireDays() != null ? cartProperties.getExpireDays() : CART_EXPIRE_DAYS), TimeUnit.DAYS);
@@ -111,19 +111,19 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeFromCart(String userId, String 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);
            // 从Redis中删除商品项
            Boolean removed = redisTemplate.delete(cartItemKey);
            if (Boolean.TRUE.equals(removed)) {
                // 更新购物车商品列表
                updateCartItemList(userId, unitId, pricingId, false);
                updateCartItemList(userId, unitId, productId, pricingId, false);
                // 异步从数据库中删除(根据配置)
                if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
                    cartPersistenceService.remove(userId, unitId, pricingId);
                    cartPersistenceService.remove(userId, unitId, productId,pricingId);
                }
                log.info("用户{}成功从购物车移除商品{}", userId, pricingId);
@@ -138,13 +138,13 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCartItemQuantity(String userId, String 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("购物车商品不存在");
@@ -170,16 +170,48 @@
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean clearCart(String userId, String 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());
            // 更新到Redis
            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);
            }
@@ -188,7 +220,7 @@
            // 异步清空数据库中的购物车数据(根据配置)
            if (Boolean.TRUE.equals(cartProperties.getEnablePersistence()) && "realtime".equalsIgnoreCase(cartProperties.getSyncStrategy())) {
                cartPersistenceService.clear(userId, unitId);
                cartPersistenceService.clear(userId, unitId, productId);
            }
            log.info("用户{}成功清空购物车", userId);
@@ -200,14 +232,14 @@
    }
    @Override
    public CartVO getCart(String userId, String unitId) {
    public CartVO getCart(String userId, String unitId, String productId) {
        try {
            CartVO cartVO = new CartVO();
            cartVO.setUserId(userId);
            if (StringUtils.hasText(unitId)){
                cartVO.setUnitId(unitId);
            }
            List<CartItemVO> items = getCartItems(userId, unitId);
            List<CartItemVO> items = getCartItems(userId, unitId, productId);
            cartVO.setItems(items);
            // 计算总数量和总金额
@@ -228,21 +260,21 @@
    }
    @Override
    public List<CartItemVO> getCartItems(String userId, String unitId) {
    public List<CartItemVO> getCartItems(String userId, String unitId,String productId) {
        try {
            // 优先从Redis获取
            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);
        }
    }
@@ -254,14 +286,14 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchRemoveFromCart(String userId, String 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;
@@ -272,17 +304,17 @@
    }
    @Override
    public Integer getCartItemCount(String userId, String unitId) {
    public Integer getCartItemCount(String userId, String unitId, String productId) {
        try {
            // 优先从Redis获取
            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;
@@ -290,19 +322,19 @@
    }
    @Override
    public boolean loadCartFromDatabase(String userId, String 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());
                // 保存到Redis
                redisTemplate.opsForValue().set(cartItemKey, itemVO, CART_EXPIRE_DAYS, TimeUnit.DAYS);
@@ -321,19 +353,19 @@
    }
    @Override
    public boolean syncCartToDatabase(String userId, String 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);
@@ -345,10 +377,10 @@
    }
    @Override
    public boolean checkCartConsistency(String userId, String 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());
@@ -380,22 +412,22 @@
    // ==================== 私有方法 ====================
    private String buildCartKey(String userId, String unitId) {
    private String buildCartKey(String userId, String unitId,String productId) {
        if(StringUtils.hasText(unitId)){
            return CART_KEY_PREFIX + userId + ":" + unitId;
            return CART_KEY_PREFIX + userId + ":" + unitId + ":" + productId;
        }
        return CART_KEY_PREFIX + userId;
        return CART_KEY_PREFIX + userId + ":" + productId;
    }
    private String buildCartItemKey(String userId, String unitId, Long pricingId) {
    private String buildCartItemKey(String userId, String unitId,String productId, Long pricingId) {
        if(StringUtils.hasText(unitId)){
            return CART_ITEM_KEY_PREFIX + userId + ":" + unitId + ":" + pricingId;
            return CART_ITEM_KEY_PREFIX + userId + ":" + unitId + ":" + productId + ":" + pricingId;
        }
        return CART_ITEM_KEY_PREFIX + userId + ":" + pricingId;
        return CART_ITEM_KEY_PREFIX + userId + ":" + productId + ":" + pricingId;
    }
    private void updateCartItemList(String userId, String 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) {
@@ -411,15 +443,15 @@
        redisTemplate.opsForValue().set(cartKey, pricingIds, CART_EXPIRE_DAYS, TimeUnit.DAYS);
    }
    private List<Long> getCartItemPricingIds(String userId, String 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(String userId, String 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)) {
@@ -428,7 +460,7 @@
            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);
@@ -442,9 +474,9 @@
        }
    }
    
    private List<CartItemVO> getCartItemsFromDatabase(String userId, String 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) {
@@ -464,7 +496,7 @@
        return itemVO;
    }
    
    private void syncCartItemToDatabase(String userId, String unitId, CartItemVO item) {
    private void syncCartItemToDatabase(String userId, String unitId, String productId,CartItemVO item) {
        try {
            Cart cart = new Cart();
            BeanUtils.copyProperties(item, cart);
@@ -473,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());
@@ -488,9 +520,9 @@
        }
    }
    
    private void removeCartItemFromDatabase(String userId, String 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.deleteByCustomerCondition(existingCart.getId());
            }
@@ -499,10 +531,10 @@
        }
    }
    
    private void clearCartFromDatabase(String userId, String 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());
            }
src/main/java/com/webmanage/service/impl/PointsFlowServiceImpl.java
@@ -221,7 +221,7 @@
        if (applyProvider && providerPointsChange != 0) {
            PointsFlow providerFlow = new PointsFlow();
            providerFlow.setUserId(providerId);
            providerFlow.setUnitId(providerId);
            providerFlow.setUnitId(null);
            providerFlow.setDataType(ruleType);
            providerFlow.setDataCategory(addPointsFlowDTO.getCategory());
            providerFlow.setPoints(providerPointsChange);
@@ -233,8 +233,8 @@
            if (!providerSaved) {
                throw new BusinessException("保存提供者积分流水失败");
            }
            updateProviderUnitPoints(providerId, providerPointsChange);
            updateUserPointsByRule(providerId, null, userPointsChange);
            // updateProviderUnitPoints(providerId, providerPointsChange);
        }
        return true;
@@ -492,7 +492,9 @@
            
            userPoints = new UserPoints();
            userPoints.setUserId(userId);
            if(StringUtils.hasText(unitId)){
            userPoints.setUnitId(unitId);
            }
            userPoints.setBalance(pointsValue);
            userPoints.setTotalEarned(pointsValue > 0 ? pointsValue : 0);
            userPoints.setTotalConsumed(pointsValue < 0 ? Math.abs(pointsValue) : 0);
@@ -522,7 +524,7 @@
        }
        // 更新单位积分账户
        QueryWrapper<UserPoints> unitWrapper = new QueryWrapper<>();
         /* QueryWrapper<UserPoints> unitWrapper = new QueryWrapper<>();
        unitWrapper.eq("deleted", 0)
                  .eq("unit_id", unitId);
        
@@ -562,7 +564,7 @@
            
            unitPoints.setUpdateTime(LocalDateTime.now());
            userPointsMapper.updateById(unitPoints);
        }
        } */
    }
    /**
src/main/java/com/webmanage/service/impl/ProductPricingServiceImpl.java
@@ -172,11 +172,15 @@
    }
    @Override
    public List<ProductPricing> getPricingByCondition(String suiteName, String salesForm,
    public List<ProductPricing> getPricingByCondition(String productId, String suiteName, String salesForm,
                                                    String customerType, String priceType, Boolean isActive) {
        try {
            QueryWrapper<ProductPricing> queryWrapper = new QueryWrapper<>();
            
            if (StringUtils.hasText(productId)) {
                queryWrapper.like("product_id", productId);
            }
            if (StringUtils.hasText(suiteName)) {
                queryWrapper.like("suite_name", suiteName);
            }
src/main/resources/mapper/CartMapper.xml
@@ -43,6 +43,9 @@
        <if test="unitId !=null and unitId != ''">
            AND unit_id = #{unitId}
        </if>
        <if test="productId !=null and productId != ''">
            AND product_id = #{productId}
        </if>
        ORDER BY add_time DESC
    </select>
    
@@ -55,6 +58,7 @@
            AND unit_id = #{unitId}
        </if>
        AND pricing_id = #{pricingId}
        AND product_id = #{productId}
        LIMIT 1
    </select>
    
@@ -66,6 +70,9 @@
        <if test="unitId != '' and unitId != null">
            AND unit_id = #{unitId}
        </if>
        <if test="productId != '' and productId != null">
            AND product_id = #{productId}
        </if>
    </select>
    
    <select id="sumTotalAmountByUserIdAndUnitId" resultType="java.math.BigDecimal">