CartServiceImpl.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package com.fdkankan.ucenter.service.impl;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.fdkankan.common.exception.BusinessException;
  4. import com.fdkankan.ucenter.entity.Cart;
  5. import com.fdkankan.ucenter.entity.Goods;
  6. import com.fdkankan.ucenter.entity.User;
  7. import com.fdkankan.ucenter.mapper.ICartMapper;
  8. import com.fdkankan.ucenter.service.ICartService;
  9. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  10. import com.fdkankan.ucenter.service.IGoodsService;
  11. import com.fdkankan.ucenter.service.IUserService;
  12. import com.fdkankan.ucenter.vo.request.CartParam;
  13. import com.fdkankan.ucenter.vo.response.CartVo;
  14. import org.springframework.beans.BeanUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.stream.Collectors;
  21. /**
  22. * <p>
  23. * 购物车 服务实现类
  24. * </p>
  25. *
  26. * @author
  27. * @since 2022-07-13
  28. */
  29. @Service
  30. public class CartServiceImpl extends ServiceImpl<ICartMapper, Cart> implements ICartService {
  31. @Autowired
  32. IUserService userService;
  33. @Autowired
  34. IGoodsService goodsService;
  35. @Override
  36. public void add(CartParam param) {
  37. User user = userService.getByUserName(param.getUserName());
  38. param.setUserId(user.getId());
  39. Cart cartEntity = this.common(param);
  40. if (cartEntity == null){
  41. cartEntity = new Cart();
  42. cartEntity.setUserId(user.getId());
  43. cartEntity.setGoodsId(param.getGoodsId());
  44. cartEntity.setGoodsCount(param.getGoodsCount());
  45. cartEntity.setPayStatus(0);
  46. cartEntity.setSkuSn(param.getSkuSn());
  47. save(cartEntity);
  48. } else {
  49. cartEntity.setGoodsCount(cartEntity.getGoodsCount() + param.getGoodsCount());
  50. updateById(cartEntity);
  51. }
  52. }
  53. @Override
  54. public Cart getByUserIdAndGoodsId(Long userId, Long goodsId) {
  55. LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
  56. wrapper.eq(Cart::getUserId,userId);
  57. wrapper.eq(Cart::getGoodsId,goodsId);
  58. List<Cart> list = this.list(wrapper);
  59. if(list != null && list.size() >0){
  60. return list.get(0);
  61. }
  62. return null;
  63. }
  64. private Cart common(CartParam param){
  65. if(param.getGoodsId() == null){
  66. throw new BusinessException(-1,"商品为空");
  67. }
  68. return this.getByUserIdAndGoodsId(param.getUserId(),param.getGoodsId());
  69. }
  70. @Override
  71. public void delete(CartParam param) {
  72. User user = userService.getByUserName(param.getUserName());
  73. param.setUserId(user.getId());
  74. Cart cartEntity = common(param);
  75. if(cartEntity == null){
  76. return;
  77. }
  78. if(param.getGoodsCount() != 0){
  79. cartEntity.setGoodsCount(param.getGoodsCount());
  80. updateById(cartEntity);
  81. }else {
  82. this.removeById(cartEntity.getId());
  83. }
  84. }
  85. @Override
  86. public List<CartVo> listByUser(String username) {
  87. User user = userService.getByUserName(username);
  88. LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
  89. wrapper.eq(Cart::getUserId,user.getId());
  90. List<Cart> list = this.list(wrapper);
  91. List<Long> goodsIds = list.parallelStream().map(Cart::getGoodsId).collect(Collectors.toList());
  92. if(goodsIds.size()<=0){
  93. return new ArrayList<>();
  94. }
  95. HashMap<Long, Goods> goodsHashMap = goodsService.getByIds(goodsIds);
  96. List<CartVo> voList = new ArrayList<>();
  97. for (Cart cart : list) {
  98. Goods goods = goodsHashMap.get(cart.getGoodsId());
  99. if(goods == null){
  100. continue;
  101. }
  102. CartVo cartVo = new CartVo();
  103. BeanUtils.copyProperties(cart,cartVo);
  104. cartVo.setPrice(goods.getGoodsPrice());
  105. voList.add(cartVo);
  106. }
  107. return voList;
  108. }
  109. @Override
  110. public void deleteIds(List<String> ids) {
  111. this.removeByIds(ids);
  112. }
  113. }