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

3
import cn.hutool.core.lang.Assert;
H
haoxr 已提交
4
import com.youlai.common.web.util.JwtUtils;
H
haoxr 已提交
5
import com.youlai.mall.oms.constant.OmsConstants;
有来技术 已提交
6
import com.youlai.mall.oms.pojo.dto.CartItemDTO;
H
haoxr 已提交
7
import com.youlai.mall.oms.service.ICartService;
H
.  
haoxr 已提交
8
import com.youlai.mall.pms.api.GoodsFeignClient;
H
.  
haoxr 已提交
9
import com.youlai.mall.pms.pojo.dto.app.SkuDTO;
H
haoxr 已提交
10 11 12 13 14 15 16 17 18 19 20
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 已提交
21 22 23 24 25
 * 购物车模块
 * <p>
 * 技术点:BoundHashOperations
 * 数据格式:
 * -- key <----> 购物车
H
haoxr 已提交
26 27 28
 * -- hKey:value <----> 商品1
 * -- hKey:value <----> 商品2
 * -- hKey:value <----> 商品3
H
haoxr 已提交
29 30 31 32
 */
@Service
@Slf4j
@AllArgsConstructor
H
haoxr 已提交
33
public class CartServiceImpl implements ICartService {
H
haoxr 已提交
34 35

    private RedisTemplate redisTemplate;
H
.  
haoxr 已提交
36
    private GoodsFeignClient skuFeignService;
H
haoxr 已提交
37 38

    @Override
有来技术 已提交
39
    public List<CartItemDTO> listCartItemByMemberId(Long memberId) {
H
haoxr 已提交
40
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
有来技术 已提交
41
        List<CartItemDTO> cartItems = cartHashOperations.values();
H
haoxr 已提交
42
        return cartItems;
H
haoxr 已提交
43 44
    }

H
haoxr 已提交
45 46 47
    /**
     * 删除用户购物车(清空购物车)
     */
H
haoxr 已提交
48
    @Override
H
haoxr 已提交
49
    public boolean deleteCart() {
H
haoxr 已提交
50
        String key = OmsConstants.CART_PREFIX + JwtUtils.getUserId();
H
haoxr 已提交
51 52
        redisTemplate.delete(key);
        return true;
H
haoxr 已提交
53 54
    }

H
haoxr 已提交
55 56 57
    /**
     * 添加商品至购物车
     */
H
haoxr 已提交
58
    @Override
H
haoxr 已提交
59
    public boolean addCartItem(Long skuId) {
60
        Long memberId = JwtUtils.getUserId();
H
haoxr 已提交
61
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
62 63
        String hKey = skuId + "";

有来技术 已提交
64
        CartItemDTO cartItem;
H
haoxr 已提交
65 66
        // 购物车已存在该商品,更新商品数量
        if (cartHashOperations.get(hKey) != null) {
有来技术 已提交
67
            cartItem = (CartItemDTO) cartHashOperations.get(hKey);
H
haoxr 已提交
68
            cartItem.setCount(cartItem.getCount() + 1); // 点击一次“加入购物车”,数量+1
H
haoxr 已提交
69
            cartItem.setChecked(true);
H
haoxr 已提交
70 71
            cartHashOperations.put(hKey, cartItem);
            return true;
H
haoxr 已提交
72
        }
H
haoxr 已提交
73
        // 购物车不存在该商品,添加商品至购物车
有来技术 已提交
74
        cartItem = new CartItemDTO();
H
haoxr 已提交
75
        CompletableFuture<Void> cartItemCompletableFuture = CompletableFuture.runAsync(() -> {
76
            SkuDTO sku = skuFeignService.getSkuById(skuId).getData();
H
haoxr 已提交
77 78 79 80
            if (sku != null) {
                cartItem.setSkuId(sku.getId());
                cartItem.setCount(1);
                cartItem.setPrice(sku.getPrice());
有来技术 已提交
81
                cartItem.setPicUrl(sku.getPicUrl());
H
haoxr 已提交
82
                cartItem.setSkuName(sku.getName());
H
haoxr 已提交
83
                cartItem.setStock(sku.getStock());
有来技术 已提交
84 85
                cartItem.setSkuSn(sku.getSn());
                cartItem.setGoodsName(sku.getGoodsName());
H
haoxr 已提交
86
                cartItem.setChecked(true);
H
haoxr 已提交
87 88 89
            }
        });
        CompletableFuture.allOf(cartItemCompletableFuture).join();
90 91 92

        Assert.isTrue(cartItem.getSkuId() != null && cartItem.getSkuId() > 0,"商品不存在");
        cartHashOperations.put(hKey, cartItem);
H
haoxr 已提交
93
        return true;
H
haoxr 已提交
94 95
    }

H
haoxr 已提交
96 97 98
    /**
     * 更新购物车总商品数量、选中状态
     */
H
haoxr 已提交
99
    @Override
有来技术 已提交
100
    public boolean updateCartItem(CartItemDTO cartItem) {
101
        Long memberId = JwtUtils.getUserId();
H
haoxr 已提交
102
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
103 104
        String hKey = cartItem.getSkuId() + "";
        if (cartHashOperations.get(hKey) != null) {
有来技术 已提交
105
            CartItemDTO cacheCartItem = (CartItemDTO) cartHashOperations.get(hKey);
106
            if (cartItem.getChecked() != null) {
H
haoxr 已提交
107 108
                cacheCartItem.setChecked(cartItem.getChecked());
            }
109
            if (cartItem.getCount() != null) {
H
haoxr 已提交
110 111 112
                cacheCartItem.setCount(cartItem.getCount());
            }
            cartHashOperations.put(hKey, cacheCartItem);
H
haoxr 已提交
113 114
        }
        return true;
H
haoxr 已提交
115 116
    }

H
haoxr 已提交
117 118 119
    /**
     * 移除购物车的商品
     */
H
haoxr 已提交
120
    @Override
H
haoxr 已提交
121
    public boolean removeCartItem(Long skuId) {
122
        Long memberId = JwtUtils.getUserId();
H
haoxr 已提交
123
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
124 125 126
        String hKey = skuId + "";
        cartHashOperations.delete(hKey);
        return true;
H
haoxr 已提交
127 128
    }

H
haoxr 已提交
129 130 131 132

    /**
     * 设置商品全选
     */
H
haoxr 已提交
133
    @Override
H
haoxr 已提交
134
    public boolean checkAll(boolean checked) {
135
        Long memberId = JwtUtils.getUserId();
H
haoxr 已提交
136
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
137
        for (Object value : cartHashOperations.values()) {
有来技术 已提交
138
            CartItemDTO cartItem = (CartItemDTO) value;
H
haoxr 已提交
139 140 141 142 143
            cartItem.setChecked(checked);
            String hKey = cartItem.getSkuId() + "";
            cartHashOperations.put(hKey, cartItem);
        }
        return true;
H
haoxr 已提交
144 145
    }

H
haoxr 已提交
146 147 148 149 150

    /**
     * 移除购物车选中的商品
     * — 场景:支付后删除购物车的商品
     */
H
haoxr 已提交
151
    @Override
H
haoxr 已提交
152
    public boolean removeCheckedItem() {
153
        Long memberId = JwtUtils.getUserId();
H
haoxr 已提交
154
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
155
        for (Object value : cartHashOperations.values()) {
有来技术 已提交
156
            CartItemDTO cartItem = (CartItemDTO) value;
H
haoxr 已提交
157
            if (cartItem.getChecked()) {
158
                cartHashOperations.delete(cartItem.getSkuId() + "");
H
haoxr 已提交
159 160
            }
        }
H
haoxr 已提交
161
        return true;
H
haoxr 已提交
162 163
    }

H
haoxr 已提交
164 165 166
    /**
     * 获取第一层,即某个用户的购物车
     */
H
haoxr 已提交
167 168
    private BoundHashOperations getCartHashOperations(Long memberId) {
        String cartKey = OmsConstants.CART_PREFIX + memberId;
H
haoxr 已提交
169 170 171 172
        BoundHashOperations operations = redisTemplate.boundHashOps(cartKey);
        return operations;
    }
}