CartServiceImpl.java 5.1 KB
Newer Older
H
haoxr 已提交
1 2 3
package com.youlai.mall.oms.service.impl;

import com.youlai.common.web.util.RequestUtils;
H
haoxr 已提交
4
import com.youlai.mall.oms.constant.OmsConstants;
H
haoxr 已提交
5
import com.youlai.mall.oms.pojo.vo.CartVO;
H
haoxr 已提交
6
import com.youlai.mall.oms.service.ICartService;
H
haoxr 已提交
7 8
import com.youlai.mall.pms.api.app.PmsSkuFeignService;
import com.youlai.mall.pms.pojo.domain.PmsSku;
H
haoxr 已提交
9 10 11 12 13 14 15 16 17 18 19
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;


/**
H
haoxr 已提交
20 21 22 23 24 25 26 27
 * 购物车模块
 * <p>
 * 技术点:BoundHashOperations
 * 数据格式:
 * -- key <----> 购物车
 * -- hKey:value <----> 购物车商品1
 * -- hKey:value <----> 购物车商品2
 * -- hKey:value <----> 购物车商品3
H
haoxr 已提交
28 29 30 31
 */
@Service
@Slf4j
@AllArgsConstructor
H
haoxr 已提交
32
public class CartServiceImpl implements ICartService {
H
haoxr 已提交
33 34

    private RedisTemplate redisTemplate;
H
haoxr 已提交
35
    private PmsSkuFeignService skuFeignService;
H
haoxr 已提交
36

H
haoxr 已提交
37 38 39 40 41 42 43 44 45 46 47
    /**
     * 获取用户购物车
     */
    @Override
    public CartVO getCart() {
        CartVO cart = new CartVO();
        BoundHashOperations cartHashOperations = getCartHashOperations();
        List<CartVO.CartItem> cartItems = cartHashOperations.values();
        cart.setItems(cartItems);
        return cart;
    }
H
haoxr 已提交
48 49

    @Override
H
haoxr 已提交
50 51 52 53
    public List<CartVO.CartItem> getCartItems() {
        BoundHashOperations cartHashOperations = getCartHashOperations();
        List<CartVO.CartItem> cartItems = cartHashOperations.values();
        return cartItems;
H
haoxr 已提交
54 55
    }

H
haoxr 已提交
56 57 58 59

    /**
     * 删除用户购物车(清空购物车)
     */
H
haoxr 已提交
60
    @Override
H
haoxr 已提交
61 62 63 64
    public boolean deleteCart() {
        String key = OmsConstants.CART_PREFIX + RequestUtils.getUserId();
        redisTemplate.delete(key);
        return true;
H
haoxr 已提交
65 66
    }

H
haoxr 已提交
67 68 69
    /**
     * 添加商品至购物车
     */
H
haoxr 已提交
70
    @Override
H
haoxr 已提交
71 72 73 74 75 76 77 78 79 80 81
    public boolean addCartItem(Long skuId) {
        BoundHashOperations cartHashOperations = getCartHashOperations();
        String hKey = skuId + "";

        CartVO.CartItem cartItem;
        // 购物车已存在该商品,更新商品数量
        if (cartHashOperations.get(hKey) != null) {
            cartItem = (CartVO.CartItem) cartHashOperations.get(hKey);
            cartItem.setCount(cartItem.getCount() + 1); // 点击一次“加入购物车”,数量+1
            cartHashOperations.put(hKey, cartItem);
            return true;
H
haoxr 已提交
82
        }
H
haoxr 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        // 购物车不存在该商品,添加商品至购物车
        cartItem = new CartVO.CartItem();
        CompletableFuture<Void> cartItemCompletableFuture = CompletableFuture.runAsync(() -> {
            PmsSku sku = skuFeignService.getSkuById(skuId).getData();
            if (sku != null) {
                cartItem.setSkuId(sku.getId());
                cartItem.setCount(1);
                cartItem.setPrice(sku.getPrice());
                cartItem.setPic(sku.getPic());
                cartItem.setSkuId(sku.getId());
                cartItem.setTitle(sku.getTitle());
            }
        });
        CompletableFuture.allOf(cartItemCompletableFuture).join();
        return true;
H
haoxr 已提交
98 99
    }

H
haoxr 已提交
100 101 102
    /**
     * 更新购物车总商品数量、选中状态
     */
H
haoxr 已提交
103
    @Override
H
haoxr 已提交
104 105 106 107 108 109 110
    public boolean updateCartItem(CartVO.CartItem cartItem) {
        BoundHashOperations cartHashOperations = getCartHashOperations();
        String hKey = cartItem.getSkuId() + "";
        if (cartHashOperations.get(hKey) != null) {
            cartHashOperations.put(hKey, cartItem);
        }
        return true;
H
haoxr 已提交
111 112
    }

H
haoxr 已提交
113 114 115
    /**
     * 移除购物车的商品
     */
H
haoxr 已提交
116
    @Override
H
haoxr 已提交
117 118 119 120 121
    public boolean removeCartItem(Long skuId) {
        BoundHashOperations cartHashOperations = getCartHashOperations();
        String hKey = skuId + "";
        cartHashOperations.delete(hKey);
        return true;
H
haoxr 已提交
122 123
    }

H
haoxr 已提交
124 125 126 127

    /**
     * 设置商品全选
     */
H
haoxr 已提交
128
    @Override
H
haoxr 已提交
129 130 131 132 133 134 135 136 137
    public boolean checkAll(boolean checked) {
        BoundHashOperations cartHashOperations = getCartHashOperations();
        for (Object value : cartHashOperations.values()) {
            CartVO.CartItem cartItem = (CartVO.CartItem) value;
            cartItem.setChecked(checked);
            String hKey = cartItem.getSkuId() + "";
            cartHashOperations.put(hKey, cartItem);
        }
        return true;
H
haoxr 已提交
138 139
    }

H
haoxr 已提交
140 141 142 143 144

    /**
     * 移除购物车选中的商品
     * — 场景:支付后删除购物车的商品
     */
H
haoxr 已提交
145
    @Override
H
haoxr 已提交
146 147 148 149
    public boolean removeCheckedItem() {
        BoundHashOperations cartHashOperations = getCartHashOperations();
        for (Object value : cartHashOperations.values()) {
            CartVO.CartItem cartItem = (CartVO.CartItem) value;
H
haoxr 已提交
150
            if (cartItem.isChecked()) {
H
haoxr 已提交
151
                cartHashOperations.delete(cartItem.getSkuId()+"");
H
haoxr 已提交
152 153
            }
        }
H
haoxr 已提交
154
        return true;
H
haoxr 已提交
155 156
    }

H
haoxr 已提交
157 158 159 160
    /**
     * 获取第一层,即某个用户的购物车
     */
    private BoundHashOperations getCartHashOperations() {
H
haoxr 已提交
161
        Long userId = RequestUtils.getUserId();
H
haoxr 已提交
162
        String cartKey = OmsConstants.CART_PREFIX + userId;
H
haoxr 已提交
163 164 165 166
        BoundHashOperations operations = redisTemplate.boundHashOps(cartKey);
        return operations;
    }
}