1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| @Service @Slf4j public class CartServiceImpl implements CartService {
@Autowired private ProductService productService;
@Autowired private RedisTemplate redisTemplate;
@Override public void addToCart(CartItemRequest cartItemRequest) {
long productId = cartItemRequest.getProductId(); int buyNum = cartItemRequest.getBuyNum();
BoundHashOperations<String,Object,Object> myCart = getMyCartOps();
Object cacheObj = myCart.get(productId); String result = "";
if(cacheObj!=null){ result = (String)cacheObj; }
if(StringUtils.isBlank(result)){ CartItemVO cartItemVO = new CartItemVO();
ProductVO productVO = productService.findDetailById(productId); if(productVO == null){throw new BizException(BizCodeEnum.CART_FAIL);}
cartItemVO.setAmount(productVO.getAmount()); cartItemVO.setBuyNum(buyNum); cartItemVO.setProductId(productId); cartItemVO.setProductImg(productVO.getCoverImg()); cartItemVO.setProductTitle(productVO.getTitle()); myCart.put(productId,JSON.toJSONString(cartItemVO));
}else { CartItemVO cartItem = JSON.parseObject(result,CartItemVO.class); cartItem.setBuyNum(cartItem.getBuyNum()+buyNum); myCart.put(productId,JSON.toJSONString(cartItem)); }
}
@Override public void clear() { String cartKey = getCartKey(); redisTemplate.delete(cartKey);
}
@Override public void deleteItem(long productId) {
BoundHashOperations<String,Object,Object> mycart = getMyCartOps();
mycart.delete(productId);
}
@Override public void changeItemNum(CartItemRequest cartItemRequest) { BoundHashOperations<String,Object,Object> mycart = getMyCartOps();
Object cacheObj = mycart.get(cartItemRequest.getProductId());
if(cacheObj==null){throw new BizException(BizCodeEnum.CART_FAIL);}
String obj = (String)cacheObj;
CartItemVO cartItemVO = JSON.parseObject(obj,CartItemVO.class); cartItemVO.setBuyNum(cartItemRequest.getBuyNum()); mycart.put(cartItemRequest.getProductId(),JSON.toJSONString(cartItemVO)); }
@Override public CartVO getMyCart() {
List<CartItemVO> cartItemVOList = buildCartItem(false); CartVO cartVO = new CartVO(); cartVO.setCartItems(cartItemVOList);
return cartVO; }
private List<CartItemVO> buildCartItem(boolean latestPrice) {
BoundHashOperations<String,Object,Object> myCart = getMyCartOps();
List<Object> itemList = myCart.values();
List<CartItemVO> cartItemVOList = new ArrayList<>();
List<Long> productIdList = new ArrayList<>();
for(Object item: itemList){ CartItemVO cartItemVO = JSON.parseObject((String)item,CartItemVO.class); cartItemVOList.add(cartItemVO);
productIdList.add(cartItemVO.getProductId()); }
if(latestPrice){
setProductLatestPrice(cartItemVOList,productIdList); }
return cartItemVOList;
}
private void setProductLatestPrice(List<CartItemVO> cartItemVOList, List<Long> productIdList) {
List<ProductVO> productVOList = productService.findProductsByIdBatch(productIdList);
Map<Long,ProductVO> maps = productVOList.stream().collect(Collectors.toMap(ProductVO::getId,Function.identity()));
cartItemVOList.stream().forEach(item->{
ProductVO productVO = maps.get(item.getProductId()); item.setProductTitle(productVO.getTitle()); item.setProductImg(productVO.getCoverImg()); item.setAmount(productVO.getAmount());
});
}
private BoundHashOperations<String,Object,Object> getMyCartOps(){ String cartKey = getCartKey(); return redisTemplate.boundHashOps(cartKey); }
private String getCartKey(){ LoginUser loginUser = LoginInterceptor.threadLocal.get(); String cartKey = String.format(CacheKey.CART_KEY,loginUser.getId()); return cartKey;
} }
|