| | |
| | | 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; |
| | |
| | | |
| | | @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()); |
| | |
| | | } |
| | | |
| | | // 构建购物车key |
| | | String cartKey = buildCartKey(userId, unitId); |
| | | String cartKey = buildCartKey(userId,unitId); |
| | | String cartItemKey = buildCartItemKey(userId, unitId, cartItemDTO.getPricingId()); |
| | | |
| | | // 检查商品是否已在购物车中 |
| | |
| | | |
| | | @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); |
| | | |
| | |
| | | |
| | | @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); |
| | |
| | | |
| | | @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); |
| | |
| | | } |
| | | |
| | | @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); |
| | | |
| | |
| | | } |
| | | |
| | | @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); |
| | |
| | | } |
| | | |
| | | @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; |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Integer getCartItemCount(Long userId, Long unitId) { |
| | | public Integer getCartItemCount(String userId, String unitId) { |
| | | try { |
| | | // 优先从Redis获取 |
| | | String cartKey = buildCartKey(userId, unitId); |
| | |
| | | } |
| | | |
| | | @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)) { |
| | |
| | | } |
| | | |
| | | @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)) { |
| | |
| | | } |
| | | |
| | | @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); |
| | |
| | | |
| | | // ==================== 私有方法 ==================== |
| | | |
| | | 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); |
| | | |
| | |
| | | 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); |
| | |
| | | } |
| | | } |
| | | |
| | | 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<>(); |
| | |
| | | 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); |
| | |
| | | } |
| | | } |
| | | |
| | | 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) { |
| | |
| | | } |
| | | } |
| | | |
| | | private void clearCartFromDatabase(Long userId, Long unitId) { |
| | | private void clearCartFromDatabase(String userId, String unitId) { |
| | | try { |
| | | // 使用逻辑删除 |
| | | List<Cart> cartItems = cartMapper.selectByUserIdAndUnitId(userId, unitId); |