|
@@ -42,7 +42,6 @@ import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* StoreCartServiceImpl 接口实现
|
|
|
-
|
|
|
*/
|
|
|
@Service
|
|
|
public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartService {
|
|
@@ -66,6 +65,8 @@ public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartS
|
|
|
private TransactionTemplate transactionTemplate;
|
|
|
@Autowired
|
|
|
private ProductRelationService productRelationService;
|
|
|
+ @Autowired
|
|
|
+ private CartShareService cartShareService;
|
|
|
|
|
|
/**
|
|
|
* 列表
|
|
@@ -185,6 +186,7 @@ public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartS
|
|
|
});
|
|
|
return responseList;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 购物车数量
|
|
|
*
|
|
@@ -231,22 +233,38 @@ public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartS
|
|
|
// 是否已经有同类型商品在购物车,有则添加数量没有则新增
|
|
|
User user = userService.getInfo();
|
|
|
Cart forUpdateStoreCart = getByUniqueAndUid(request.getProductAttrUnique(), user.getId());
|
|
|
+ CartShare forUpdateStoreCartShare = getCartShareByUniqueAndUid(request.getProductAttrUnique(), user.getId());
|
|
|
Boolean execute = transactionTemplate.execute(e -> {
|
|
|
- if (ObjectUtil.isNotNull(forUpdateStoreCart)) { // 购物车添加数量
|
|
|
+ if (ObjectUtil.isNotNull(forUpdateStoreCart) && ObjectUtil.isNotNull(forUpdateStoreCartShare)) { // 购物车添加数量
|
|
|
forUpdateStoreCart.setCartNum(forUpdateStoreCart.getCartNum() + request.getCartNum());
|
|
|
boolean updateResult = updateById(forUpdateStoreCart);
|
|
|
if (!updateResult) throw new CrmebException("添加购物车失败");
|
|
|
+
|
|
|
+ forUpdateStoreCartShare.setCartNum(forUpdateStoreCartShare.getCartNum() + request.getCartNum());
|
|
|
+ boolean updateResult1 = cartShareService.updateById(forUpdateStoreCartShare);
|
|
|
+ if (!updateResult1) throw new CrmebException("添加购物车失败");
|
|
|
+
|
|
|
+
|
|
|
} else {// 新增购物车数据
|
|
|
Cart storeCart = new Cart();
|
|
|
BeanUtils.copyProperties(request, storeCart);
|
|
|
storeCart.setUid(user.getId());
|
|
|
storeCart.setMerId(product.getMerId());
|
|
|
if (dao.insert(storeCart) <= 0) throw new CrmebException("添加购物车失败");
|
|
|
+
|
|
|
+ CartShare storeCartShare = new CartShare();
|
|
|
+ BeanUtils.copyProperties(storeCart, storeCartShare);
|
|
|
+ storeCartShare.setCartNum(storeCart.getCartNum());
|
|
|
+ if (cartShareDao.insert(storeCartShare) <= 0) throw new CrmebException("添加购物车分享失败");
|
|
|
}
|
|
|
+
|
|
|
return Boolean.TRUE;
|
|
|
});
|
|
|
+
|
|
|
+
|
|
|
if (execute) {
|
|
|
purchaseStatistics(request.getProductId(), request.getCartNum());
|
|
|
+
|
|
|
}
|
|
|
return execute;
|
|
|
}
|
|
@@ -264,6 +282,9 @@ public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartS
|
|
|
}
|
|
|
List<Cart> updateCartList = new ArrayList<>();
|
|
|
List<Cart> addCartList = new ArrayList<>();
|
|
|
+ //购物车分享
|
|
|
+ List<CartShare> updateCartShareList = new ArrayList<>();
|
|
|
+ List<CartShare> addCartShareList = new ArrayList<>();
|
|
|
User currentUser = userService.getInfo();
|
|
|
cartListRequest.forEach(cartRequest -> {
|
|
|
// 判断商品正常
|
|
@@ -282,21 +303,36 @@ public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartS
|
|
|
// 普通商品部分(只有普通商品才能添加购物车)
|
|
|
// 是否已经有同类型商品在购物车,有则添加数量没有则新增
|
|
|
Cart forUpdateStoreCart = getByUniqueAndUid(cartRequest.getProductAttrUnique(), currentUser.getId());
|
|
|
- if (ObjectUtil.isNotNull(forUpdateStoreCart)) { // 购物车添加数量
|
|
|
+ CartShare forUpdateStoreCartShare = getCartShareByUniqueAndUid(cartRequest.getProductAttrUnique(), currentUser.getId());
|
|
|
+
|
|
|
+ if (ObjectUtil.isNotNull(forUpdateStoreCart)&&ObjectUtil.isNotNull(forUpdateStoreCartShare)) { // 购物车添加数量
|
|
|
forUpdateStoreCart.setCartNum(forUpdateStoreCart.getCartNum() + cartRequest.getCartNum());
|
|
|
updateCartList.add(forUpdateStoreCart);
|
|
|
+
|
|
|
+ forUpdateStoreCartShare.setCartNum(forUpdateStoreCart.getCartNum() + cartRequest.getCartNum());
|
|
|
+ updateCartShareList.add(forUpdateStoreCartShare);
|
|
|
} else {// 新增购物车数据
|
|
|
Cart cart = new Cart();
|
|
|
BeanUtils.copyProperties(cartRequest, cart);
|
|
|
cart.setUid(currentUser.getId());
|
|
|
cart.setMerId(product.getMerId());
|
|
|
addCartList.add(cart);
|
|
|
+
|
|
|
+ CartShare cartShare = new CartShare();
|
|
|
+ BeanUtils.copyProperties(cart, cartShare);
|
|
|
+ cartShare.setUid(currentUser.getId());
|
|
|
+ cartShare.setMerId(product.getMerId());
|
|
|
+ cartShare.setCartNum(cart.getCartNum());
|
|
|
+ addCartShareList.add(cartShare);
|
|
|
+
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
Boolean execute = transactionTemplate.execute(exec -> {
|
|
|
saveBatch(addCartList);
|
|
|
updateBatchById(updateCartList);
|
|
|
+ //分享表数据
|
|
|
+ cartShareService.saveBatch(addCartShareList);
|
|
|
+ cartShareService.updateBatchById(updateCartShareList);
|
|
|
return Boolean.TRUE;
|
|
|
});
|
|
|
if (execute) {
|
|
@@ -309,7 +345,7 @@ public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartS
|
|
|
* 加购商品统计
|
|
|
*
|
|
|
* @param productId 商品id
|
|
|
- * @param num 加购数量
|
|
|
+ * @param num 加购数量
|
|
|
*/
|
|
|
private void purchaseStatistics(Integer productId, Integer num) {
|
|
|
String todayStr = DateUtil.date().toString(DateConstants.DATE_FORMAT_DATE);
|
|
@@ -335,6 +371,21 @@ public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartS
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 获取购物车分享表信息
|
|
|
+ *
|
|
|
+ * @param productAttrUnique 商品规制值
|
|
|
+ * @param uid uid
|
|
|
+ * @return StoreCart
|
|
|
+ */
|
|
|
+ private CartShare getCartShareByUniqueAndUid(Integer productAttrUnique, Integer uid) {
|
|
|
+ LambdaQueryWrapper<CartShare> lqw = Wrappers.lambdaQuery();
|
|
|
+ lqw.eq(CartShare::getProductAttrUnique, productAttrUnique);
|
|
|
+ lqw.eq(CartShare::getUid, uid);
|
|
|
+ lqw.last(" limit 1");
|
|
|
+ return cartShareDao.selectOne(lqw);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 删除购物车信息
|
|
|
*
|
|
|
* @param ids 待删除id
|
|
@@ -465,6 +516,7 @@ public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartS
|
|
|
|
|
|
/**
|
|
|
* 购物车移入收藏
|
|
|
+ *
|
|
|
* @param ids 购物车id列表
|
|
|
* @return Boolean
|
|
|
*/
|
|
@@ -489,6 +541,7 @@ public class CartServiceImpl extends ServiceImpl<CartDao, Cart> implements CartS
|
|
|
|
|
|
/**
|
|
|
* 通过用户id删除
|
|
|
+ *
|
|
|
* @param uid 用户ID
|
|
|
*/
|
|
|
@Override
|