123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- package com.caimei.service.impl;
- import com.caimei.mapper.OrderMapper;
- import com.caimei.mapper.ProductMapper;
- import com.caimei.mapper.ShoppingCartMapper;
- import com.caimei.model.ResponseJson;
- import com.caimei.model.dto.CartDto;
- import com.caimei.model.po.CmCartPo;
- import com.caimei.model.vo.*;
- import com.caimei.service.ShoppingCartService;
- import com.caimei.util.MathUtil;
- import com.caimei.util.ProductUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- import org.springframework.util.StringUtils;
- import javax.annotation.Resource;
- import java.math.BigDecimal;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * Description
- *
- * @author : plf
- * @date : 2021/3/23
- */
- @Service
- public class ShoppingCartServiceImpl implements ShoppingCartService {
- @Resource
- private ShoppingCartMapper shoppingCartMapper;
- @Resource
- private ProductMapper productMapper;
- @Resource
- private OrderMapper orderMapper;
- @Value("${caimei.oldapi}")
- private String domain;
- @Override
- public ResponseJson<Integer> addShoppingCart(CartDto cart) {
- Integer heUserId = cart.getHeUserId();
- heUserId = heUserId == null ? 0 : heUserId;
- CmCartPo cartPo = shoppingCartMapper.findCartProduct(cart.getUserId(), cart.getProductId(), heUserId);
- if (cartPo == null) {
- shoppingCartMapper.insertCart(cart);
- } else {
- int productCount = cartPo.getProductCount() + cart.getProductCount();
- shoppingCartMapper.updateByProductCount(cartPo.getCartId(), productCount);
- }
- Integer cartQuantity = shoppingCartMapper.getCartQuantity(cart.getUserId());
- return ResponseJson.success(cartQuantity);
- }
- @Override
- public ResponseJson<Integer> getCartQuantity(Integer userId) {
- Integer cartQuantity = shoppingCartMapper.getCartQuantity(userId);
- return ResponseJson.success(cartQuantity);
- }
- @Override
- public ResponseJson<Map<String, Object>> shoppingInfo(Integer userId) {
- Map<String, Object> map = new HashMap<>(3);
- List<ShopVo> shopList = shoppingCartMapper.findCartShop(userId);
- shopList.forEach(shop -> {
- shop.setLogo(ProductUtils.getImageURL("shopLogo", shop.getLogo(), 0, domain));
- BigDecimal shopTotalPrice = BigDecimal.ZERO;
- List<CartProductVo> productList = shoppingCartMapper.findByShopCartProduct(shop.getShopId(), userId);
- for (CartProductVo product : productList) {
- setPrice(product);
- shopTotalPrice = MathUtil.add(shopTotalPrice, MathUtil.mul(product.getProductCount(), product.getPrice()));
- }
- shop.setShopTotalPrice(shopTotalPrice);
- shop.setProductList(productList);
- });
- map.put("shopList", shopList);
- //失效商品
- List<CartProductVo> products = shoppingCartMapper.findExpiredGoods(userId);
- map.put("products", products);
- //商品数量
- Integer cartQuantity = shoppingCartMapper.getCartQuantity(userId);
- map.put("cartQuantity", cartQuantity);
- return ResponseJson.success(map);
- }
- /**
- * 设置活动价格阶梯
- */
- @Override
- public void setPrice(CartProductVo product) {
- //税费
- boolean addTaxFlag = ("0".equals(product.getIncludedTax()) && ("1".equals(product.getInvoiceType()) || "2".equals(product.getInvoiceType())));
- Integer activityId = productMapper.findActivityByProductId(product.getProductId());
- if (activityId != null && activityId > 0) {
- //活动阶梯
- List<ActivityLadderVo> ladderList = productMapper.findActivityLadder(activityId, product.getProductId());
- if (ladderList != null && ladderList.size() > 0) {
- product.setActiveStatus(1);
- for (ActivityLadderVo ladder : ladderList) {
- if (addTaxFlag) {
- BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(ladder.getBuyPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
- ladder.setBuyPrice(MathUtil.add(addedValueTax, ladder.getBuyPrice()));
- }
- if (product.getProductCount() >= ladder.getBuyNum()) {
- product.setPrice(ladder.getBuyPrice());
- }
- }
- } else if (addTaxFlag) {
- BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(product.getPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
- product.setPrice(MathUtil.add(addedValueTax, product.getPrice()));
- }
- product.setLadderList(ladderList);
- } else {
- if (addTaxFlag) {
- BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(product.getPrice(), product.getClubTaxPoint()), BigDecimal.valueOf(100), 2);
- product.setPrice(MathUtil.add(addedValueTax, product.getPrice()));
- }
- }
- }
- @Override
- public ResponseJson<String> buyAgainAddCart(Integer orderId) {
- OrderVo order = orderMapper.findOrder(orderId);
- List<OrderProductVo> orderProductList = orderMapper.findOrderProductAll(orderId);
- CartDto cart = new CartDto();
- cart.setUserId(order.getUserId());
- //商品是否全部下架
- orderProductList.forEach(orderProduct -> {
- Integer validFlag = orderMapper.findProductStatus(orderProduct.getProductId());
- if (validFlag == 1) {
- cart.setProductId(orderProduct.getProductId());
- cart.setProductCount(orderProduct.getNum());
- Integer heUserId = orderProduct.getHeUserId();
- if (heUserId != null && heUserId > 0) {
- Integer activityId = productMapper.findActivityByProductId(orderProduct.getProductId());
- if (activityId != null && activityId > 0) {
- cart.setHeUserId(heUserId);
- } else {
- cart.setHeUserId(0);
- }
- } else {
- cart.setHeUserId(0);
- }
- addShoppingCart(cart);
- }
- });
- return ResponseJson.success(null);
- }
- @Override
- public void updateNumber(Integer cartId, Integer productCount) {
- shoppingCartMapper.updateByProductCount(cartId, productCount);
- }
- @Override
- public ResponseJson<String> deleteCart(String cartIds) {
- String[] split = cartIds.split(",");
- for (String cartId : split) {
- if (!StringUtils.isEmpty(cartId)) {
- shoppingCartMapper.deleteCart(cartId);
- }
- }
- return ResponseJson.success("删除购物车成功");
- }
- }
|