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

H
haoxr 已提交
3
import com.youlai.common.web.util.JwtUtils;
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
import com.youlai.mall.pms.api.GoodsFeignClient;
8
import com.youlai.mall.pms.pojo.dto.SkuDTO;
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
 * 购物车模块
 * <p>
 * 技术点:BoundHashOperations
 * 数据格式:
 * -- key <----> 购物车
H
haoxr 已提交
25 26 27
 * -- 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 GoodsFeignClient skuFeignService;
H
haoxr 已提交
36

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

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

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

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

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

H
haoxr 已提交
106 107 108
    /**
     * 更新购物车总商品数量、选中状态
     */
H
haoxr 已提交
109
    @Override
H
haoxr 已提交
110
    public boolean updateCartItem(CartVO.CartItem cartItem) {
H
haoxr 已提交
111
        Long memberId= JwtUtils.getUserId();
H
haoxr 已提交
112
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
113 114
        String hKey = cartItem.getSkuId() + "";
        if (cartHashOperations.get(hKey) != null) {
H
haoxr 已提交
115
            CartVO.CartItem  cacheCartItem = (CartVO.CartItem) cartHashOperations.get(hKey);
H
haoxr 已提交
116 117 118 119 120 121 122
            if(cartItem.getChecked()!=null){
                cacheCartItem.setChecked(cartItem.getChecked());
            }
            if(cartItem.getCount()!=null){
                cacheCartItem.setCount(cartItem.getCount());
            }
            cartHashOperations.put(hKey, cacheCartItem);
H
haoxr 已提交
123 124
        }
        return true;
H
haoxr 已提交
125 126
    }

H
haoxr 已提交
127 128 129
    /**
     * 移除购物车的商品
     */
H
haoxr 已提交
130
    @Override
H
haoxr 已提交
131
    public boolean removeCartItem(Long skuId) {
H
haoxr 已提交
132
        Long memberId= JwtUtils.getUserId();
H
haoxr 已提交
133
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
134 135 136
        String hKey = skuId + "";
        cartHashOperations.delete(hKey);
        return true;
H
haoxr 已提交
137 138
    }

H
haoxr 已提交
139 140 141 142

    /**
     * 设置商品全选
     */
H
haoxr 已提交
143
    @Override
H
haoxr 已提交
144
    public boolean checkAll(boolean checked) {
H
haoxr 已提交
145
        Long memberId= JwtUtils.getUserId();
H
haoxr 已提交
146
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
147 148 149 150 151 152 153
        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 已提交
154 155
    }

H
haoxr 已提交
156 157 158 159 160

    /**
     * 移除购物车选中的商品
     * — 场景:支付后删除购物车的商品
     */
H
haoxr 已提交
161
    @Override
H
haoxr 已提交
162
    public boolean removeCheckedItem() {
H
haoxr 已提交
163
        Long memberId= JwtUtils.getUserId();
H
haoxr 已提交
164
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
165 166
        for (Object value : cartHashOperations.values()) {
            CartVO.CartItem cartItem = (CartVO.CartItem) value;
H
haoxr 已提交
167
            if (cartItem.getChecked()) {
H
haoxr 已提交
168
                cartHashOperations.delete(cartItem.getSkuId()+"");
H
haoxr 已提交
169 170
            }
        }
H
haoxr 已提交
171
        return true;
H
haoxr 已提交
172 173
    }

H
haoxr 已提交
174 175 176
    /**
     * 获取第一层,即某个用户的购物车
     */
H
haoxr 已提交
177 178
    private BoundHashOperations getCartHashOperations(Long memberId) {
        String cartKey = OmsConstants.CART_PREFIX + memberId;
H
haoxr 已提交
179 180 181 182
        BoundHashOperations operations = redisTemplate.boundHashOps(cartKey);
        return operations;
    }
}