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

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

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

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

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

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

郝先瑞 已提交
96
        Assert.isTrue(cartItem.getSkuId() != null,"商品不存在");
97
        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
        Long memberId;
        try {
108
            memberId = MemberUtils.getMemberId();
109 110 111
        } 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
        Long memberId;
        try {
134
            memberId = MemberUtils.getMemberId();
135 136 137
        } 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
        Long memberId;
        try {
152
            memberId = MemberUtils.getMemberId();
153 154 155
        } 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
        Long memberId;
        try {
175
            memberId = MemberUtils.getMemberId();
176 177 178
        } 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;
    }
}