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

郝先瑞 已提交
3
import cn.hutool.core.bean.BeanUtil;
4
import cn.hutool.core.lang.Assert;
5 6
import com.youlai.common.result.ResultCode;
import com.youlai.common.web.exception.BizException;
H
haoxr 已提交
7
import com.youlai.common.web.util.JwtUtils;
H
haoxr 已提交
8
import com.youlai.mall.oms.constant.OmsConstants;
有来技术 已提交
9
import com.youlai.mall.oms.pojo.dto.CartItemDTO;
H
haoxr 已提交
10
import com.youlai.mall.oms.service.ICartService;
郝先瑞 已提交
11 12
import com.youlai.mall.pms.api.SkuFeignClient;
import com.youlai.mall.pms.pojo.dto.SkuInfoDTO;
H
haoxr 已提交
13 14
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
郝先瑞 已提交
15 16
import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.beans.factory.BeanFactory;
H
haoxr 已提交
17 18 19 20 21 22 23 24 25
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 已提交
26 27 28 29 30
 * 购物车模块
 * <p>
 * 技术点:BoundHashOperations
 * 数据格式:
 * -- key <----> 购物车
H
haoxr 已提交
31 32 33
 * -- hKey:value <----> 商品1
 * -- hKey:value <----> 商品2
 * -- hKey:value <----> 商品3
H
haoxr 已提交
34 35 36 37
 */
@Service
@Slf4j
@AllArgsConstructor
H
haoxr 已提交
38
public class CartServiceImpl implements ICartService {
H
haoxr 已提交
39 40

    private RedisTemplate redisTemplate;
郝先瑞 已提交
41
    private SkuFeignClient skuFeignService;
H
haoxr 已提交
42 43

    @Override
有来技术 已提交
44
    public List<CartItemDTO> listCartItemByMemberId(Long memberId) {
H
haoxr 已提交
45
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
有来技术 已提交
46
        List<CartItemDTO> cartItems = cartHashOperations.values();
H
haoxr 已提交
47
        return cartItems;
H
haoxr 已提交
48 49
    }

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

H
haoxr 已提交
60 61 62
    /**
     * 添加商品至购物车
     */
H
haoxr 已提交
63
    @Override
H
haoxr 已提交
64
    public boolean addCartItem(Long skuId) {
65 66 67 68 69 70
        Long memberId;
        try {
            memberId = JwtUtils.getUserId();
        } catch (Exception e) {
            throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
        }
H
haoxr 已提交
71
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
72 73
        String hKey = skuId + "";

有来技术 已提交
74
        CartItemDTO cartItem;
H
haoxr 已提交
75 76
        // 购物车已存在该商品,更新商品数量
        if (cartHashOperations.get(hKey) != null) {
有来技术 已提交
77
            cartItem = (CartItemDTO) cartHashOperations.get(hKey);
H
haoxr 已提交
78
            cartItem.setCount(cartItem.getCount() + 1); // 点击一次“加入购物车”,数量+1
H
haoxr 已提交
79
            cartItem.setChecked(true);
H
haoxr 已提交
80 81
            cartHashOperations.put(hKey, cartItem);
            return true;
H
haoxr 已提交
82
        }
H
haoxr 已提交
83
        // 购物车不存在该商品,添加商品至购物车
有来技术 已提交
84
        cartItem = new CartItemDTO();
H
haoxr 已提交
85
        CompletableFuture<Void> cartItemCompletableFuture = CompletableFuture.runAsync(() -> {
郝先瑞 已提交
86 87 88
            SkuInfoDTO skuInfo = skuFeignService.getSkuInfo(skuId).getData();
            if (skuInfo != null) {
                BeanUtil.copyProperties(skuInfo,cartItem);
H
haoxr 已提交
89
                cartItem.setCount(1);
H
haoxr 已提交
90
                cartItem.setChecked(true);
H
haoxr 已提交
91 92 93
            }
        });
        CompletableFuture.allOf(cartItemCompletableFuture).join();
94

郝先瑞 已提交
95
        Assert.isTrue(cartItem.getSkuId() != null,"商品不存在");
96
        cartHashOperations.put(hKey, cartItem);
H
haoxr 已提交
97
        return true;
H
haoxr 已提交
98 99
    }

H
haoxr 已提交
100 101 102
    /**
     * 更新购物车总商品数量、选中状态
     */
H
haoxr 已提交
103
    @Override
有来技术 已提交
104
    public boolean updateCartItem(CartItemDTO cartItem) {
105 106 107 108 109 110
        Long memberId;
        try {
            memberId = JwtUtils.getUserId();
        } catch (Exception e) {
            throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
        }
H
haoxr 已提交
111
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
112 113
        String hKey = cartItem.getSkuId() + "";
        if (cartHashOperations.get(hKey) != null) {
有来技术 已提交
114
            CartItemDTO cacheCartItem = (CartItemDTO) cartHashOperations.get(hKey);
115
            if (cartItem.getChecked() != null) {
H
haoxr 已提交
116 117
                cacheCartItem.setChecked(cartItem.getChecked());
            }
118
            if (cartItem.getCount() != null) {
H
haoxr 已提交
119 120 121
                cacheCartItem.setCount(cartItem.getCount());
            }
            cartHashOperations.put(hKey, cacheCartItem);
H
haoxr 已提交
122 123
        }
        return true;
H
haoxr 已提交
124 125
    }

H
haoxr 已提交
126 127 128
    /**
     * 移除购物车的商品
     */
H
haoxr 已提交
129
    @Override
H
haoxr 已提交
130
    public boolean removeCartItem(Long skuId) {
131 132 133 134 135 136
        Long memberId;
        try {
            memberId = JwtUtils.getUserId();
        } catch (Exception e) {
            throw new BizException(ResultCode.TOKEN_INVALID_OR_EXPIRED);
        }
H
haoxr 已提交
137
        BoundHashOperations cartHashOperations = getCartHashOperations(memberId);
H
haoxr 已提交
138 139 140
        String hKey = skuId + "";
        cartHashOperations.delete(hKey);
        return true;
H
haoxr 已提交
141 142
    }

H
haoxr 已提交
143 144 145 146

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

H
haoxr 已提交
165 166 167 168 169

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

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