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

3
import cn.hutool.core.lang.Assert;
4 5
import com.youlai.common.result.ResultCode;
import com.youlai.common.web.exception.BizException;
H
haoxr 已提交
6
import com.youlai.common.web.util.JwtUtils;
H
haoxr 已提交
7
import com.youlai.mall.oms.constant.OmsConstants;
有来技术 已提交
8
import com.youlai.mall.oms.pojo.dto.CartItemDTO;
H
haoxr 已提交
9
import com.youlai.mall.oms.service.ICartService;
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 61 62 63 64 65
        Long memberId;
        try {
            memberId = JwtUtils.getUserId();
        } catch (Exception e) {
            throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
        }
H
haoxr 已提交
66
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
67 68
        String hKey = skuId + "";

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

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

H
haoxr 已提交
101 102 103
    /**
     * 更新购物车总商品数量、选中状态
     */
H
haoxr 已提交
104
    @Override
有来技术 已提交
105
    public boolean updateCartItem(CartItemDTO cartItem) {
106 107 108 109 110 111
        Long memberId;
        try {
            memberId = JwtUtils.getUserId();
        } catch (Exception e) {
            throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
        }
H
haoxr 已提交
112
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
113 114
        String hKey = cartItem.getSkuId() + "";
        if (cartHashOperations.get(hKey) != null) {
有来技术 已提交
115
            CartItemDTO cacheCartItem = (CartItemDTO) cartHashOperations.get(hKey);
116
            if (cartItem.getChecked() != null) {
H
haoxr 已提交
117 118
                cacheCartItem.setChecked(cartItem.getChecked());
            }
119
            if (cartItem.getCount() != null) {
H
haoxr 已提交
120 121 122
                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) {
132 133 134 135 136 137
        Long memberId;
        try {
            memberId = JwtUtils.getUserId();
        } catch (Exception e) {
            throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
        }
H
haoxr 已提交
138
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
139 140 141
        String hKey = skuId + "";
        cartHashOperations.delete(hKey);
        return true;
H
haoxr 已提交
142 143
    }

H
haoxr 已提交
144 145 146 147

    /**
     * 设置商品全选
     */
H
haoxr 已提交
148
    @Override
H
haoxr 已提交
149
    public boolean checkAll(boolean checked) {
150 151 152 153 154 155
        Long memberId;
        try {
            memberId = JwtUtils.getUserId();
        } catch (Exception e) {
            throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
        }
H
haoxr 已提交
156
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
157
        for (Object value : cartHashOperations.values()) {
有来技术 已提交
158
            CartItemDTO cartItem = (CartItemDTO) value;
H
haoxr 已提交
159 160 161 162 163
            cartItem.setChecked(checked);
            String hKey = cartItem.getSkuId() + "";
            cartHashOperations.put(hKey, cartItem);
        }
        return true;
H
haoxr 已提交
164 165
    }

H
haoxr 已提交
166 167 168 169 170

    /**
     * 移除购物车选中的商品
     * — 场景:支付后删除购物车的商品
     */
H
haoxr 已提交
171
    @Override
H
haoxr 已提交
172
    public boolean removeCheckedItem() {
173 174 175 176 177 178
        Long memberId;
        try {
            memberId = JwtUtils.getUserId();
        } catch (Exception e) {
            throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
        }
H
haoxr 已提交
179
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
180
        for (Object value : cartHashOperations.values()) {
有来技术 已提交
181
            CartItemDTO cartItem = (CartItemDTO) value;
H
haoxr 已提交
182
            if (cartItem.getChecked()) {
183
                cartHashOperations.delete(cartItem.getSkuId() + "");
H
haoxr 已提交
184 185
            }
        }
H
haoxr 已提交
186
        return true;
H
haoxr 已提交
187 188
    }

H
haoxr 已提交
189 190 191
    /**
     * 获取第一层,即某个用户的购物车
     */
H
haoxr 已提交
192 193
    private BoundHashOperations getCartHashOperations(Long memberId) {
        String cartKey = OmsConstants.CART_PREFIX + memberId;
H
haoxr 已提交
194 195 196 197
        BoundHashOperations operations = redisTemplate.boundHashOps(cartKey);
        return operations;
    }
}