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 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 getCartQuantity(Integer userId) { Integer cartQuantity = shoppingCartMapper.getCartQuantity(userId); return ResponseJson.success(cartQuantity); } @Override public ResponseJson> shoppingInfo(Integer userId) { Map map = new HashMap<>(3); List shopList = shoppingCartMapper.findCartShop(userId); shopList.forEach(shop -> { shop.setLogo(ProductUtils.getImageURL("shopLogo", shop.getLogo(), 0, domain)); BigDecimal shopTotalPrice = BigDecimal.ZERO; List 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 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 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 buyAgainAddCart(Integer orderId) { OrderVo order = orderMapper.findOrder(orderId); List 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 deleteCart(String cartIds) { String[] split = cartIds.split(","); for (String cartId : split) { if (!StringUtils.isEmpty(cartId)) { shoppingCartMapper.deleteCart(cartId); } } return ResponseJson.success("删除购物车成功"); } }