ShoppingCartServiceImpl.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package com.caimei.service.impl;
  2. import com.caimei.mapper.OrderMapper;
  3. import com.caimei.mapper.ProductMapper;
  4. import com.caimei.mapper.ShoppingCartMapper;
  5. import com.caimei.model.ResponseJson;
  6. import com.caimei.model.dto.CartDto;
  7. import com.caimei.model.po.CmCartPo;
  8. import com.caimei.model.vo.*;
  9. import com.caimei.service.ShoppingCartService;
  10. import com.caimei.util.MathUtil;
  11. import com.caimei.util.ProductUtils;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Service;
  14. import org.springframework.util.StringUtils;
  15. import javax.annotation.Resource;
  16. import java.math.BigDecimal;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. /**
  21. * Description
  22. *
  23. * @author : plf
  24. * @date : 2021/3/23
  25. */
  26. @Service
  27. public class ShoppingCartServiceImpl implements ShoppingCartService {
  28. @Resource
  29. private ShoppingCartMapper shoppingCartMapper;
  30. @Resource
  31. private ProductMapper productMapper;
  32. @Resource
  33. private OrderMapper orderMapper;
  34. @Value("${caimei.oldapi}")
  35. private String domain;
  36. @Override
  37. public ResponseJson<Integer> addShoppingCart(CartDto cart) {
  38. Integer heUserId = cart.getHeUserId();
  39. heUserId = heUserId == null ? 0 : heUserId;
  40. CmCartPo cartPo = shoppingCartMapper.findCartProduct(cart.getUserId(), cart.getProductId(), heUserId);
  41. if (cartPo == null) {
  42. shoppingCartMapper.insertCart(cart);
  43. } else {
  44. int productCount = cartPo.getProductCount() + cart.getProductCount();
  45. shoppingCartMapper.updateByProductCount(cartPo.getCartId(), productCount);
  46. }
  47. Integer cartQuantity = shoppingCartMapper.getCartQuantity(cart.getUserId());
  48. return ResponseJson.success(cartQuantity);
  49. }
  50. @Override
  51. public ResponseJson<Integer> getCartQuantity(Integer userId) {
  52. Integer cartQuantity = shoppingCartMapper.getCartQuantity(userId);
  53. return ResponseJson.success(cartQuantity);
  54. }
  55. @Override
  56. public ResponseJson<Map<String, Object>> shoppingInfo(Integer userId) {
  57. Map<String, Object> map = new HashMap<>(3);
  58. List<ShopVo> shopList = shoppingCartMapper.findCartShop(userId);
  59. shopList.forEach(shop -> {
  60. shop.setLogo(ProductUtils.getImageURL("shopLogo", shop.getLogo(), 0, domain));
  61. BigDecimal shopTotalPrice = BigDecimal.ZERO;
  62. List<CartProductVo> productList = shoppingCartMapper.findByShopCartProduct(shop.getShopId(), userId);
  63. for (CartProductVo product : productList) {
  64. setPrice(product);
  65. shopTotalPrice = MathUtil.add(shopTotalPrice, MathUtil.mul(product.getProductCount(), product.getPrice()));
  66. }
  67. shop.setShopTotalPrice(shopTotalPrice);
  68. shop.setProductList(productList);
  69. });
  70. map.put("shopList", shopList);
  71. //失效商品
  72. List<CartProductVo> products = shoppingCartMapper.findExpiredGoods(userId);
  73. map.put("products", products);
  74. //商品数量
  75. Integer cartQuantity = shoppingCartMapper.getCartQuantity(userId);
  76. map.put("cartQuantity", cartQuantity);
  77. return ResponseJson.success(map);
  78. }
  79. /**
  80. * 设置活动价格阶梯
  81. */
  82. @Override
  83. public void setPrice(CartProductVo product) {
  84. //税费
  85. boolean addTaxFlag = ("0".equals(product.getIncludedTax()) && ("1".equals(product.getInvoiceType()) || "2".equals(product.getInvoiceType())));
  86. Integer activityId = productMapper.findActivityByProductId(product.getProductId());
  87. if (activityId != null && activityId > 0) {
  88. //活动阶梯
  89. List<ActivityLadderVo> ladderList = productMapper.findActivityLadder(activityId, product.getProductId());
  90. if (ladderList != null && ladderList.size() > 0) {
  91. product.setActiveStatus(1);
  92. for (ActivityLadderVo ladder : ladderList) {
  93. if (addTaxFlag) {
  94. BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(ladder.getBuyPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
  95. ladder.setBuyPrice(MathUtil.add(addedValueTax, ladder.getBuyPrice()));
  96. }
  97. if (product.getProductCount() >= ladder.getBuyNum()) {
  98. product.setPrice(ladder.getBuyPrice());
  99. }
  100. }
  101. } else if (addTaxFlag) {
  102. BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(product.getPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
  103. product.setPrice(MathUtil.add(addedValueTax, product.getPrice()));
  104. }
  105. product.setLadderList(ladderList);
  106. } else {
  107. if (addTaxFlag) {
  108. BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(product.getPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
  109. product.setPrice(MathUtil.add(addedValueTax, product.getPrice()));
  110. }
  111. }
  112. }
  113. @Override
  114. public ResponseJson<String> buyAgainAddCart(Integer orderId) {
  115. OrderVo order = orderMapper.findOrder(orderId);
  116. List<OrderProductVo> orderProductList = orderMapper.findOrderProductAll(orderId);
  117. CartDto cart = new CartDto();
  118. cart.setUserId(order.getUserId());
  119. //商品是否全部下架
  120. orderProductList.forEach(orderProduct -> {
  121. Integer validFlag = orderMapper.findProductStatus(orderProduct.getProductId());
  122. if (validFlag == 1) {
  123. cart.setProductId(orderProduct.getProductId());
  124. cart.setProductCount(orderProduct.getNum());
  125. Integer heUserId = orderProduct.getHeUserId();
  126. if (heUserId != null && heUserId > 0) {
  127. Integer activityId = productMapper.findActivityByProductId(orderProduct.getProductId());
  128. if (activityId != null && activityId > 0) {
  129. cart.setHeUserId(heUserId);
  130. } else {
  131. cart.setHeUserId(0);
  132. }
  133. } else {
  134. cart.setHeUserId(0);
  135. }
  136. addShoppingCart(cart);
  137. }
  138. });
  139. return ResponseJson.success(null);
  140. }
  141. @Override
  142. public void updateNumber(Integer cartId, Integer productCount) {
  143. shoppingCartMapper.updateByProductCount(cartId, productCount);
  144. }
  145. @Override
  146. public ResponseJson<String> deleteCart(String cartIds) {
  147. String[] split = cartIds.split(",");
  148. for (String cartId : split) {
  149. if (!StringUtils.isEmpty(cartId)) {
  150. shoppingCartMapper.deleteCart(cartId);
  151. }
  152. }
  153. return ResponseJson.success("删除购物车成功");
  154. }
  155. }