|
@@ -5,13 +5,10 @@ import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
|
|
|
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
|
|
|
import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi;
|
|
|
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
|
|
|
-import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartAddReqVO;
|
|
|
-import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartListRespVO;
|
|
|
-import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartResetReqVO;
|
|
|
-import cn.iocoder.yudao.module.trade.controller.app.cart.vo.AppTradeCartUpdateReqVO;
|
|
|
+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.TradeCartDO;
|
|
|
-import cn.iocoder.yudao.module.trade.dal.mysql.cart.TradeCartMapper;
|
|
|
+import cn.iocoder.yudao.module.trade.dal.dataobject.cart.CartDO;
|
|
|
+import cn.iocoder.yudao.module.trade.dal.mysql.cart.CartMapper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
@@ -29,17 +26,16 @@ import static java.util.Collections.emptyList;
|
|
|
/**
|
|
|
* 购物车 Service 实现类
|
|
|
*
|
|
|
- * // TODO 芋艿:秒杀、拼团、砍价对购物车的影响
|
|
|
- * // TODO 芋艿:未来优化:购物车的价格计算,支持营销信息
|
|
|
+ * // TODO 芋艿:未来优化:购物车的价格计算,支持营销信息;目前不支持的原因,前端界面需要前端 pr 支持下;
|
|
|
*
|
|
|
* @author 芋道源码
|
|
|
*/
|
|
|
@Service
|
|
|
@Validated
|
|
|
-public class TradeCartServiceImpl implements TradeCartService {
|
|
|
+public class CartServiceImpl implements CartService {
|
|
|
|
|
|
@Resource
|
|
|
- private TradeCartMapper cartMapper;
|
|
|
+ private CartMapper cartMapper;
|
|
|
|
|
|
@Resource
|
|
|
private ProductSpuApi productSpuApi;
|
|
@@ -47,13 +43,11 @@ public class TradeCartServiceImpl implements TradeCartService {
|
|
|
private ProductSkuApi productSkuApi;
|
|
|
|
|
|
@Override
|
|
|
- public Long addCart(Long userId, AppTradeCartAddReqVO addReqVO) {
|
|
|
+ public Long addCart(Long userId, AppCartAddReqVO addReqVO) {
|
|
|
// 查询 TradeCartDO
|
|
|
- TradeCartDO cart = cartMapper.selectByUserIdAndSkuId(userId, addReqVO.getSkuId(),
|
|
|
- addReqVO.getAddStatus(), false);
|
|
|
+ CartDO cart = cartMapper.selectByUserIdAndSkuId(userId, addReqVO.getSkuId());
|
|
|
// 校验 SKU
|
|
|
- Integer count = cart != null && addReqVO.getAddStatus() ?
|
|
|
- cart.getCount() + addReqVO.getCount() : addReqVO.getCount();
|
|
|
+ Integer count = addReqVO.getCount();
|
|
|
ProductSkuRespDTO sku = checkProductSku(addReqVO.getSkuId(), count);
|
|
|
|
|
|
// 情况零:特殊,count 小于等于 0,说明前端项目删除
|
|
@@ -63,23 +57,22 @@ public class TradeCartServiceImpl implements TradeCartService {
|
|
|
if (count <= 0) {
|
|
|
cartMapper.deleteById(cart.getId());
|
|
|
} else {
|
|
|
- cartMapper.updateById(new TradeCartDO().setId(cart.getId()).setCount(count));
|
|
|
+ cartMapper.updateById(new CartDO().setId(cart.getId()).setCount(count));
|
|
|
}
|
|
|
return cart.getId();
|
|
|
// 情况二:不存在,则进行插入
|
|
|
} else {
|
|
|
- cart = new TradeCartDO().setUserId(userId)
|
|
|
- .setSpuId(sku.getSpuId()).setSkuId(sku.getId()).setCount(count)
|
|
|
- .setAddStatus(addReqVO.getAddStatus()).setOrderStatus(false);
|
|
|
+ cart = new CartDO().setUserId(userId)
|
|
|
+ .setSpuId(sku.getSpuId()).setSkuId(sku.getId()).setCount(count);
|
|
|
cartMapper.insert(cart);
|
|
|
}
|
|
|
return cart.getId();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void updateCart(Long userId, AppTradeCartUpdateReqVO updateReqVO) {
|
|
|
+ public void updateCartCount(Long userId, AppCartUpdateCountReqVO updateReqVO) {
|
|
|
// 校验 TradeCartDO 存在
|
|
|
- TradeCartDO cart = cartMapper.selectById(updateReqVO.getId(), userId);
|
|
|
+ CartDO cart = cartMapper.selectById(updateReqVO.getId(), userId);
|
|
|
if (cart == null) {
|
|
|
throw exception(CARD_ITEM_NOT_FOUND);
|
|
|
}
|
|
@@ -87,29 +80,35 @@ public class TradeCartServiceImpl implements TradeCartService {
|
|
|
checkProductSku(cart.getSkuId(), updateReqVO.getCount());
|
|
|
|
|
|
// 更新数量
|
|
|
- cartMapper.updateById(new TradeCartDO().setId(cart.getId())
|
|
|
+ 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, AppTradeCartResetReqVO resetReqVO) {
|
|
|
+ public void resetCart(Long userId, AppCartResetReqVO resetReqVO) {
|
|
|
// 第一步:删除原本的购物项
|
|
|
- TradeCartDO oldCart = cartMapper.selectById(resetReqVO.getId(), userId);
|
|
|
+ CartDO oldCart = cartMapper.selectById(resetReqVO.getId(), userId);
|
|
|
if (oldCart == null) {
|
|
|
throw exception(CARD_ITEM_NOT_FOUND);
|
|
|
}
|
|
|
cartMapper.deleteById(oldCart.getId());
|
|
|
|
|
|
// 第二步:添加新的购物项
|
|
|
- TradeCartDO newCart = cartMapper.selectByUserIdAndSkuId(userId, resetReqVO.getSkuId(),
|
|
|
- true, false);
|
|
|
+ // TODO 芋艿:直接改成 addCart 貌似就行
|
|
|
+ CartDO newCart = cartMapper.selectByUserIdAndSkuId(userId, resetReqVO.getSkuId());
|
|
|
if (newCart != null) {
|
|
|
- updateCart(userId, new AppTradeCartUpdateReqVO()
|
|
|
+ updateCartCount(userId, new AppCartUpdateCountReqVO()
|
|
|
.setId(newCart.getId()).setCount(resetReqVO.getCount()));
|
|
|
} else {
|
|
|
- addCart(userId, new AppTradeCartAddReqVO().setAddStatus(true)
|
|
|
- .setSkuId(resetReqVO.getSkuId()).setCount(resetReqVO.getCount()));
|
|
|
+ addCart(userId, new AppCartAddReqVO().setSkuId(resetReqVO.getSkuId())
|
|
|
+ .setCount(resetReqVO.getCount()));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -122,7 +121,7 @@ public class TradeCartServiceImpl implements TradeCartService {
|
|
|
@Override
|
|
|
public void deleteCart(Long userId, Collection<Long> ids) {
|
|
|
// 查询 TradeCartDO 列表
|
|
|
- List<TradeCartDO> carts = cartMapper.selectListByIds(ids, userId);
|
|
|
+ List<CartDO> carts = cartMapper.selectListByIds(ids, userId);
|
|
|
if (CollUtil.isEmpty(carts)) {
|
|
|
return;
|
|
|
}
|
|
@@ -133,30 +132,33 @@ public class TradeCartServiceImpl implements TradeCartService {
|
|
|
|
|
|
@Override
|
|
|
public Integer getCartCount(Long userId) {
|
|
|
+ // TODO 芋艿:需要算上 selected
|
|
|
return cartMapper.selectSumByUserId(userId);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Map<Long, Integer> getCartCountMap(Long userId) {
|
|
|
+ // TODO 芋艿:需要算上 selected
|
|
|
return cartMapper.selectSumMapByUserId(userId);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public AppTradeCartListRespVO getCartList(Long userId) {
|
|
|
- // 获得购物车的商品,只查询未下单的
|
|
|
- List<TradeCartDO> carts = cartMapper.selectListByUserId(userId, true, false);
|
|
|
- carts.sort(Comparator.comparing(TradeCartDO::getId).reversed());
|
|
|
+ public AppCartListRespVO getCartList(Long userId) {
|
|
|
+ // 获得购物车的商品
|
|
|
+ List<CartDO> carts = cartMapper.selectListByUserId(userId);
|
|
|
+ carts.sort(Comparator.comparing(CartDO::getId).reversed());
|
|
|
// 如果未空,则返回空结果
|
|
|
if (CollUtil.isEmpty(carts)) {
|
|
|
- return new AppTradeCartListRespVO().setValidList(emptyList())
|
|
|
+ return new AppCartListRespVO().setValidList(emptyList())
|
|
|
.setInvalidList(emptyList());
|
|
|
}
|
|
|
|
|
|
// 查询 SPU、SKU 列表
|
|
|
- List<ProductSpuRespDTO> spus = productSpuApi.getSpuList(convertSet(carts, TradeCartDO::getSpuId));
|
|
|
- List<ProductSkuRespDTO> skus = productSkuApi.getSkuList(convertSet(carts, TradeCartDO::getSkuId));
|
|
|
+ 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);
|
|
|
|
|
|
// 拼接数据
|
|
@@ -164,14 +166,14 @@ public class TradeCartServiceImpl implements TradeCartService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<TradeCartDO> getCartList(Long userId, Set<Long> ids) {
|
|
|
+ public List<CartDO> getCartList(Long userId, Set<Long> ids) {
|
|
|
if (CollUtil.isEmpty(ids)) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
return cartMapper.selectListByUserId(userId, ids);
|
|
|
}
|
|
|
|
|
|
- private void deleteCartIfSpuDeleted(List<TradeCartDO> carts, List<ProductSpuRespDTO> spus) {
|
|
|
+ private void deleteCartIfSpuDeleted(List<CartDO> carts, List<ProductSpuRespDTO> spus) {
|
|
|
// 如果 SPU 被删除,则删除购物车对应的商品。延迟删除
|
|
|
carts.removeIf(cart -> {
|
|
|
if (spus.stream().noneMatch(spu -> spu.getId().equals(cart.getSpuId()))) {
|