CartServiceImpl.java 5.6 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;
有来技术 已提交
5
import com.youlai.mall.oms.pojo.dto.CartItemDTO;
H
haoxr 已提交
6
import com.youlai.mall.oms.service.ICartService;
H
.  
haoxr 已提交
7
import com.youlai.mall.pms.api.GoodsFeignClient;
H
.  
haoxr 已提交
8
import com.youlai.mall.pms.pojo.dto.app.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 37

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

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

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

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

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

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

H
haoxr 已提交
126 127 128 129

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

H
haoxr 已提交
143 144 145 146 147

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

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