| | |
| | | shoppingCartItem.setNum(num); |
| | | Long userId = tokenService.getLoginUser().getUserId(); |
| | | List<ShoppingCartItem> cacheList = redisCache.getCacheList("shopCart:"+userId); |
| | | if (cacheList==null){ |
| | | cacheList = new ArrayList<>(); |
| | | } |
| | | redisCache.deleteObject("shopCart:"+userId); |
| | | if (cacheList != null) { |
| | | if (!cacheList.isEmpty()) { |
| | | if (num==0){ |
| | | // 移除 |
| | | List<ShoppingCartItem> res = new ArrayList<>(); |
| | | for (ShoppingCartItem cartItem : cacheList) { |
| | | if (!cartItem.getId().equals( id)){ |
| | | res.add(cartItem); |
| | | } |
| | | } |
| | | redisCache.setCacheList("shopCart:"+userId, res); |
| | | }else{ |
| | | ShoppingCartItem temp = cacheList.stream().filter(e -> e.getId().equals(id)).findFirst().orElse(null); |
| | | if (temp != null){ |
| | | // 修改数量 |
| | | for (ShoppingCartItem cartItem : cacheList) { |
| | | if (cartItem.getId().equals(id)) { |
| | | cartItem.setNum(num); |
| | | if (num==0){ |
| | | // 移除 |
| | | List<ShoppingCartItem> res = new ArrayList<>(); |
| | | for (ShoppingCartItem c : cacheList) { |
| | | if (c.getNum()==0){ |
| | | continue; |
| | | } |
| | | res.add(c); |
| | | } |
| | | redisCache.setCacheList("shopCart:"+userId, res); |
| | | return R.ok(); |
| | | } |
| | | redisCache.setCacheList("shopCart:"+userId, cacheList); |
| | | return R.ok(); |
| | | }else{ |
| | | cacheList.add(cartItem); |
| | | break; |
| | | } |
| | | } |
| | | } else { |
| | | if (num!=0){ |
| | | cacheList = new ArrayList<>(); |
| | | cacheList.add(shoppingCartItem); |
| | | } |
| | | redisCache.setCacheList("shopCart:"+userId, cacheList); |
| | | }else{ |
| | | // 新增 |
| | | cacheList.add(shoppingCartItem); |
| | | redisCache.setCacheList("shopCart:"+userId, cacheList); |
| | | |
| | | } |
| | | } else { |
| | | cacheList = new ArrayList<>(); |
| | | if (num!=0){ |
| | | cacheList.add(shoppingCartItem); |
| | | } |
| | | |
| | | } |
| | | List<ShoppingCartItem> res = new ArrayList<>(); |
| | | for (ShoppingCartItem cartItem : cacheList) { |
| | | if (cartItem.getNum()==0){ |
| | | continue; |
| | | } |
| | | res.add(cartItem); |
| | | } |
| | | redisCache.setCacheList("shopCart:"+userId, res); |
| | | return R.ok(); |
| | | } |
| | | |