大家好,欢迎来到IT知识分享网。
因为我们添加成功后会跳转到下面这个页面,所以我们需要获取sku的信息和商品数量
所以当我们点击添加商品时就需要发送请求,请求如下:
1、controller层
2、serviceImpl层(将数据存储到redis中)
package com.gulimall.cart.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gulimall.cart.feign.ProductFeignService;
import com.gulimall.cart.interceptor.CartInterceptor;
import com.gulimall.cart.service.CartService;
import com.gulimall.cart.to.UserInfoTo;
import com.gulimall.cart.vo.CartItem;
import com.gulimall.cart.vo.SkuInfoVo;
import com.gulimall.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
@Slf4j
@Service
public class CartServiceImpl implements CartService {
@Autowired
ProductFeignService productFeignService;
@Autowired
StringRedisTemplate redisTemplate;
@Autowired
ThreadPoolExecutor executor;
private final String CART_PREFIX = "gulimall:cart";
@Override
public CartItem addToCart(Long skuId, Integer num) throws ExecutionException, InterruptedException {
BoundHashOperations<String, Object, Object> cartOps = this.getCartOps();
//2、商品添加到购物车
CartItem cartItem = new CartItem();
//异步
CompletableFuture<Void> getSkuInfo = CompletableFuture.runAsync(() -> {
//1、远程查询当前要添加的商品的信息
R skuInfo = productFeignService.info(skuId);
SkuInfoVo data = skuInfo.getData("skuInfo", new TypeReference<SkuInfoVo>() {
});
cartItem.setCheck(true);
cartItem.setCount(num);
cartItem.setImage(data.getSkuDefaultImg());
cartItem.setTitle(data.getSkuTitle());
cartItem.setSkuId(skuId);
cartItem.setPrice(data.getPrice());
}, executor);
//3、远程查询sku的组合信息
//异步
CompletableFuture<Void> getSkuSaleAttrValues = CompletableFuture.runAsync(() -> {
List<String> skuSaleAttrValue = productFeignService.getSkuSaleAttrValue(skuId);
cartItem.setSkuAttr(skuSaleAttrValue);
}, executor);
CompletableFuture.allOf(getSkuInfo, getSkuSaleAttrValues).get();//等待异步请求都完成
String s = JSON.toJSONString(cartItem);
cartOps.put(skuId.toString(), s);
return cartItem;
}
/**
* 获取到我们要操作的购物车
* @return
*/
private BoundHashOperations<String, Object, Object> getCartOps() {
//先得到当前用户信息
UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
String cartKey = "";
if (userInfoTo.getUserId() != null) { //已登录
//gulimall:cart:1
cartKey = CART_PREFIX + userInfoTo.getUserId();
} else {
cartKey = CART_PREFIX + userInfoTo.getUserKey();
}
//绑定指定的key操作Redis
BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(cartKey);
return operations;
}
}
之所以选用redis的hash数据结构,是因为我们要存储成如下结构:
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/32623.html