|
@@ -9,11 +9,11 @@ import cn.iocoder.yudao.module.trade.controller.app.cart.vo.*;
|
|
|
import cn.iocoder.yudao.module.trade.convert.cart.TradeCartConvert;
|
|
|
import cn.iocoder.yudao.module.trade.dal.dataobject.cart.CartDO;
|
|
|
import cn.iocoder.yudao.module.trade.dal.mysql.cart.CartMapper;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
-import jakarta.annotation.Resource;
|
|
|
import java.util.*;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
@@ -33,15 +33,15 @@ import static java.util.Collections.emptyList;
|
|
|
@Service
|
|
|
@Validated
|
|
|
public class CartServiceImpl implements CartService {
|
|
|
-
|
|
|
+
|
|
|
@Resource
|
|
|
private CartMapper cartMapper;
|
|
|
-
|
|
|
+
|
|
|
@Resource
|
|
|
private ProductSpuApi productSpuApi;
|
|
|
@Resource
|
|
|
private ProductSkuApi productSkuApi;
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public Long addCart(Long userId, AppCartAddReqVO addReqVO) {
|
|
|
// 查询 TradeCartDO
|
|
@@ -49,13 +49,13 @@ public class CartServiceImpl implements CartService {
|
|
|
// 校验 SKU
|
|
|
Integer count = addReqVO.getCount();
|
|
|
ProductSkuRespDTO sku = checkProductSku(addReqVO.getSkuId(), count);
|
|
|
-
|
|
|
+
|
|
|
// 情况一:存在,则进行数量更新
|
|
|
if (cart != null) {
|
|
|
cartMapper.updateById(new CartDO().setId(cart.getId()).setSelected(true)
|
|
|
.setCount(cart.getCount() + count));
|
|
|
return cart.getId();
|
|
|
- // 情况二:不存在,则进行插入
|
|
|
+ // 情况二:不存在,则进行插入
|
|
|
} else {
|
|
|
cart = new CartDO().setUserId(userId).setSelected(true)
|
|
|
.setSpuId(sku.getSpuId()).setSkuId(sku.getId()).setCount(count);
|
|
@@ -63,7 +63,7 @@ public class CartServiceImpl implements CartService {
|
|
|
}
|
|
|
return cart.getId();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public void updateCartCount(Long userId, AppCartUpdateCountReqVO updateReqVO) {
|
|
|
// 校验 TradeCartDO 存在
|
|
@@ -73,18 +73,18 @@ public class CartServiceImpl implements CartService {
|
|
|
}
|
|
|
// 校验商品 SKU
|
|
|
checkProductSku(cart.getSkuId(), updateReqVO.getCount());
|
|
|
-
|
|
|
+
|
|
|
// 更新数量
|
|
|
cartMapper.updateById(new CartDO().setId(cart.getId())
|
|
|
.setCount(updateReqVO.getCount()));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public void updateCartSelected(Long userId, AppCartUpdateSelectedReqVO updateSelectedReqVO) {
|
|
|
cartMapper.updateByIds(updateSelectedReqVO.getIds(), userId,
|
|
|
new CartDO().setSelected(updateSelectedReqVO.getSelected()));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void resetCart(Long userId, AppCartResetReqVO resetReqVO) {
|
|
@@ -94,7 +94,7 @@ public class CartServiceImpl implements CartService {
|
|
|
throw exception(CARD_ITEM_NOT_FOUND);
|
|
|
}
|
|
|
cartMapper.deleteById(oldCart.getId());
|
|
|
-
|
|
|
+
|
|
|
// 第二步:添加新的购物项
|
|
|
CartDO newCart = cartMapper.selectByUserIdAndSkuId(userId, resetReqVO.getSkuId());
|
|
|
if (newCart != null) {
|
|
@@ -105,12 +105,12 @@ public class CartServiceImpl implements CartService {
|
|
|
.setCount(resetReqVO.getCount()));
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 购物车删除商品
|
|
|
*
|
|
|
* @param userId 用户编号
|
|
|
- * @param ids 商品 SKU 编号的数组
|
|
|
+ * @param ids 商品 SKU 编号的数组
|
|
|
*/
|
|
|
@Override
|
|
|
public void deleteCart(Long userId, Collection<Long> ids) {
|
|
@@ -119,17 +119,17 @@ public class CartServiceImpl implements CartService {
|
|
|
if (CollUtil.isEmpty(carts)) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 批量标记删除
|
|
|
cartMapper.deleteByIds(convertSet(carts, CartDO::getId));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public Integer getCartCount(Long userId) {
|
|
|
// TODO 芋艿:需要算上 selected
|
|
|
return cartMapper.selectSumByUserId(userId);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public AppCartListRespVO getCartList(Long userId) {
|
|
|
// 获得购物车的商品
|
|
@@ -140,11 +140,33 @@ public class CartServiceImpl implements CartService {
|
|
|
return new AppCartListRespVO().setValidList(emptyList())
|
|
|
.setInvalidList(emptyList());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 查询 SPU、SKU 列表
|
|
|
List<ProductSpuRespDTO> spus = productSpuApi.getSpuList(convertSet(carts, CartDO::getSpuId));
|
|
|
List<ProductSkuRespDTO> skus = productSkuApi.getSkuList(convertSet(carts, CartDO::getSkuId));
|
|
|
-
|
|
|
+
|
|
|
+ // 如果 SPU 被删除,则删除购物车对应的商品。延迟删除
|
|
|
+ // 为什么不是 SKU 被删除呢?因为 SKU 被删除时,还可以通过 SPU 选择其它 SKU
|
|
|
+ deleteCartIfSpuDeleted(carts, spus);
|
|
|
+ return TradeCartConvert.INSTANCE.convertList(carts, spus, skus);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 拼接数据@Override
|
|
|
+ @Override
|
|
|
+ public AppCartListRespVO getCartListAll() {
|
|
|
+ // 获得购物车的商品
|
|
|
+ List<CartDO> carts = cartMapper.selectListAll();
|
|
|
+ carts.sort(Comparator.comparing(CartDO::getId).reversed());
|
|
|
+ // 如果未空,则返回空结果
|
|
|
+ if (CollUtil.isEmpty(carts)) {
|
|
|
+ return new AppCartListRespVO().setValidList(emptyList())
|
|
|
+ .setInvalidList(emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询 SPU、SKU 列表
|
|
|
+ List<ProductSpuRespDTO> spus = productSpuApi.getSpuList(convertSet(carts, CartDO::getSpuId));
|
|
|
+ List<ProductSkuRespDTO> skus = productSkuApi.getSkuList(convertSet(carts, CartDO::getSkuId));
|
|
|
+
|
|
|
// 如果 SPU 被删除,则删除购物车对应的商品。延迟删除
|
|
|
// 为什么不是 SKU 被删除呢?因为 SKU 被删除时,还可以通过 SPU 选择其它 SKU
|
|
|
deleteCartIfSpuDeleted(carts, spus);
|