|
@@ -1,5 +1,7 @@
|
|
|
package com.caimei365.order.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.caimei365.order.components.ProductService;
|
|
|
import com.caimei365.order.mapper.BaseMapper;
|
|
|
import com.caimei365.order.mapper.CartClubMapper;
|
|
@@ -26,6 +28,9 @@ import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
+import static com.alibaba.fastjson.JSON.parseArray;
|
|
|
+import static com.alibaba.fastjson.JSON.parseObject;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Description
|
|
@@ -552,6 +557,13 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
*/
|
|
|
@Override
|
|
|
public ResponseJson<Integer> addShoppingCart(CartDto cartDto) {
|
|
|
+ saveShoppingCart(cartDto);
|
|
|
+ // 获取购物车数量(商品种类数)
|
|
|
+ int cartCount = getCartCount(cartDto.getUserId());
|
|
|
+ return ResponseJson.success("添加成功!返回购物车数量", cartCount);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveShoppingCart(CartDto cartDto) {
|
|
|
CartPo cart = cartClubMapper.getCartPo(cartDto);
|
|
|
if (cart != null) {
|
|
|
// 购物车已存在该商品,更新数量
|
|
@@ -577,6 +589,41 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
cart.setAddTime(new Date());
|
|
|
cartClubMapper.insertCart(cart);
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量添加购物车
|
|
|
+ *
|
|
|
+ * @param cartDto {
|
|
|
+ * userId 用户ID
|
|
|
+ * productInfo 商品及数量信息:[ // 商品id,数量
|
|
|
+ * {"productId": 2789, "productCount": 1},
|
|
|
+ * {"productId": 2790, "productCount": 1}
|
|
|
+ * ]
|
|
|
+ * }
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> addShoppingCartBulk(CartDto cartDto) {
|
|
|
+ JSONArray productInfo = null;
|
|
|
+ try {
|
|
|
+ productInfo = parseArray(cartDto.getProductInfo());
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("批量添加购物车参数解析异常try-catch:", e);
|
|
|
+ return ResponseJson.error("商品及数量信息参数解析异常!", null);
|
|
|
+ }
|
|
|
+ if (null == productInfo || productInfo.isEmpty()){
|
|
|
+ return ResponseJson.error("商品及数量信息参数异常!", null);
|
|
|
+ }
|
|
|
+ for (Object infoObject: productInfo) {
|
|
|
+ JSONObject tempObj = (JSONObject) infoObject;
|
|
|
+ Integer productId = (Integer) tempObj.get("productId");
|
|
|
+ Integer productCount = (Integer) tempObj.get("productCount");
|
|
|
+ CartDto cartBo = new CartDto();
|
|
|
+ cartBo.setUserId(cartDto.getUserId());
|
|
|
+ cartBo.setProductId(productId);
|
|
|
+ cartBo.setProductCount(productCount);
|
|
|
+ saveShoppingCart(cartBo);
|
|
|
+ }
|
|
|
// 获取购物车数量(商品种类数)
|
|
|
int cartCount = getCartCount(cartDto.getUserId());
|
|
|
return ResponseJson.success("添加成功!返回购物车数量", cartCount);
|