|
@@ -1,5 +1,6 @@
|
|
|
package com.caimei365.order.service.impl;
|
|
|
|
|
|
+import com.caimei365.order.components.ProductService;
|
|
|
import com.caimei365.order.mapper.BaseMapper;
|
|
|
import com.caimei365.order.mapper.CartClubMapper;
|
|
|
import com.caimei365.order.model.ResponseJson;
|
|
@@ -8,19 +9,16 @@ import com.caimei365.order.model.po.CartPo;
|
|
|
import com.caimei365.order.model.vo.*;
|
|
|
import com.caimei365.order.service.CartClubService;
|
|
|
import com.caimei365.order.utils.MathUtil;
|
|
|
-import com.caimei365.order.utils.ProductUtil;
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.google.common.util.concurrent.AtomicDouble;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
-import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -38,6 +36,8 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
private BaseMapper baseMapper;
|
|
|
@Resource
|
|
|
private CartClubMapper cartClubMapper;
|
|
|
+ @Resource
|
|
|
+ private ProductService productService;
|
|
|
|
|
|
/**
|
|
|
* 购物车列表详细数据
|
|
@@ -67,7 +67,7 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
* 处理数据
|
|
|
*/
|
|
|
// 促销活动(总)
|
|
|
- List<CartPromotionsVo> totalPromotions = new ArrayList<>();
|
|
|
+ List<PromotionsVo> totalPromotions = new ArrayList<>();
|
|
|
List<Integer> promotionsIds = new ArrayList<>();
|
|
|
// 用户身份
|
|
|
Integer userIdentity = baseMapper.getIdentityByUserId(userId);
|
|
@@ -81,7 +81,7 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
// 该供应商满减金额(供应商满减,单品满减)
|
|
|
AtomicDouble shopReducedPrice = new AtomicDouble(0);
|
|
|
// 供应商促销优惠活动
|
|
|
- CartPromotionsVo shopPromotion = cartClubMapper.getPromotionByShopId(shop.getShopId());
|
|
|
+ PromotionsVo shopPromotion = baseMapper.getPromotionByShopId(shop.getShopId());
|
|
|
// 供应商下商品列表
|
|
|
List<CartItemVo> productList = cartClubMapper.getCartProductsByShopId(shop.getShopId(), userId);
|
|
|
|
|
@@ -90,30 +90,32 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
while (productIterator.hasNext()) {
|
|
|
CartItemVo cartItemVo = productIterator.next();
|
|
|
// 设置商品图片及税费
|
|
|
- boolean taxFlag = setCartItemImgAndTax(cartItemVo);
|
|
|
+ boolean taxFlag = productService.setCartItemImgAndTax(cartItemVo);
|
|
|
// 已上架商品
|
|
|
if (cartItemVo.getValidFlag() == 2) {
|
|
|
// 设置商品有效
|
|
|
cartItemVo.setStatus(0);
|
|
|
+ // 默认所有商品选中状态
|
|
|
+ cartItemVo.setIsChecked(true);
|
|
|
// 价格是否可见
|
|
|
boolean priceVisible = (cartItemVo.getPriceFlag() == 0 || (cartItemVo.getPriceFlag() == 2 && userIdentity == 2));
|
|
|
// 是否库存充足
|
|
|
boolean isStocked = (cartItemVo.getStock() != null && cartItemVo.getStock() > 0 && cartItemVo.getStock() >= cartItemVo.getMin() && cartItemVo.getStock() >= cartItemVo.getNumber());
|
|
|
if (priceVisible && isStocked) {
|
|
|
// 获取商品促销信息
|
|
|
- CartPromotionsVo promotions = null;
|
|
|
+ PromotionsVo promotions = null;
|
|
|
// 没有店铺促销时,商品促销才有效
|
|
|
if (null == shopPromotion) {
|
|
|
// 获取商品促销信息
|
|
|
- promotions = cartClubMapper.getPromotionByProductId(cartItemVo.getProductId());
|
|
|
+ promotions = baseMapper.getPromotionByProductId(cartItemVo.getProductId());
|
|
|
/*
|
|
|
* 设置商品促销优惠
|
|
|
*/
|
|
|
if (null != promotions) {
|
|
|
// 当前促销活动的价格计算列表
|
|
|
- List<CartPromotionPriceVo> promotionPriceList = getPromotionProducts(promotions, cartItemVo, taxFlag);
|
|
|
+ List<PromotionPriceVo> promotionPriceList = productService.getPromotionProducts(promotions, cartItemVo, taxFlag);
|
|
|
// 更新到总促销列表
|
|
|
- updateTotalPromotions(totalPromotions, promotionsIds, promotions, promotionPriceList);
|
|
|
+ productService.updateTotalPromotions(totalPromotions, promotionsIds, promotions, promotionPriceList);
|
|
|
//单品满减-计算供应商总价/满减金额
|
|
|
if (promotions.getType() == 1 && promotions.getMode() == 2) {
|
|
|
BigDecimal totalAmount = MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice());
|
|
@@ -134,7 +136,7 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
} else {
|
|
|
if (cartItemVo.getLadderFlag() == 1) {
|
|
|
// 设置阶梯价
|
|
|
- setCartLadderPrices(cartItemVo, taxFlag);
|
|
|
+ productService.setCartLadderPrices(cartItemVo, taxFlag);
|
|
|
} else {
|
|
|
// 复购价
|
|
|
Double repurchase = baseMapper.getRepurchasePrice(cartItemVo.getProductId(), userId);
|
|
@@ -194,13 +196,13 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
// 店铺满赠
|
|
|
if (shopPromotion.getMode() == 3) {
|
|
|
// 获取赠品
|
|
|
- List<CartItemVo> giftList = cartClubMapper.getPromotionGifts(shopPromotion.getId());
|
|
|
+ List<CartItemVo> giftList = baseMapper.getPromotionGifts(shopPromotion.getId());
|
|
|
shopPromotion.setGiftList(giftList);
|
|
|
}
|
|
|
// 设置该优惠下的商品列表
|
|
|
- List<CartPromotionPriceVo> promotionPriceList = new ArrayList<>();
|
|
|
+ List<PromotionPriceVo> promotionPriceList = new ArrayList<>();
|
|
|
productList.forEach(item -> {
|
|
|
- CartPromotionPriceVo promotionPrice = new CartPromotionPriceVo();
|
|
|
+ PromotionPriceVo promotionPrice = new PromotionPriceVo();
|
|
|
promotionPrice.setProductId(item.getProductId());
|
|
|
promotionPrice.setNumber(item.getNumber());
|
|
|
promotionPrice.setPrice(item.getPrice());
|
|
@@ -218,6 +220,7 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ // 供应商商品
|
|
|
shop.setCartList(productList);
|
|
|
// 供应商总价
|
|
|
shop.setTotalPrice(shopPrice.get());
|
|
@@ -237,7 +240,7 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
kindCount.updateAndGet(v -> v + shopKindCount.get());
|
|
|
});
|
|
|
// 删除空数据
|
|
|
- shopInfoList.removeIf(shop -> (null != shop && shop.getCount() == 0));
|
|
|
+ shopInfoList.removeIf(shop -> (null == shop || shop.getCount() == 0));
|
|
|
// 总促销计算
|
|
|
totalPromotions.forEach(promotions -> {
|
|
|
// 凑单满减
|
|
@@ -291,7 +294,7 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
* 处理数据
|
|
|
*/
|
|
|
// 促销活动(总)
|
|
|
- List<CartPromotionsVo> totalPromotions = new ArrayList<>();
|
|
|
+ List<PromotionsVo> totalPromotions = new ArrayList<>();
|
|
|
List<Integer> promotionsIds = new ArrayList<>();
|
|
|
// 用户身份
|
|
|
Integer userIdentity = baseMapper.getIdentityByUserId(userId);
|
|
@@ -302,9 +305,9 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
cartList.removeIf(cartItemVo -> !(cartItemVo.getPriceFlag() == 0 || (cartItemVo.getPriceFlag() == 2 && userIdentity == 2)));
|
|
|
cartList.forEach(cartItemVo -> {
|
|
|
// 设置商品图片及税费
|
|
|
- boolean taxFlag = setCartItemImgAndTax(cartItemVo);
|
|
|
+ boolean taxFlag = productService.setCartItemImgAndTax(cartItemVo);
|
|
|
// 获取商品促销信息
|
|
|
- CartPromotionsVo promotions = cartClubMapper.getPromotionByProductId(cartItemVo.getProductId());
|
|
|
+ PromotionsVo promotions = baseMapper.getPromotionByProductId(cartItemVo.getProductId());
|
|
|
/*
|
|
|
* 设置商品促销优惠
|
|
|
*/
|
|
@@ -314,13 +317,13 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
// 商品处于活动状态
|
|
|
cartItemVo.setActStatus(1);
|
|
|
// 当前促销活动的价格计算列表
|
|
|
- List<CartPromotionPriceVo> promotionPriceList = getPromotionProducts(promotions, cartItemVo, taxFlag);
|
|
|
+ List<PromotionPriceVo> promotionPriceList = productService.getPromotionProducts(promotions, cartItemVo, taxFlag);
|
|
|
// 更新到总促销列表
|
|
|
- updateTotalPromotions(totalPromotions, promotionsIds, promotions, promotionPriceList);
|
|
|
+ productService.updateTotalPromotions(totalPromotions, promotionsIds, promotions, promotionPriceList);
|
|
|
} else {
|
|
|
if (cartItemVo.getLadderFlag() == 1) {
|
|
|
// 设置阶梯价
|
|
|
- setCartLadderPrices(cartItemVo, taxFlag);
|
|
|
+ productService.setCartLadderPrices(cartItemVo, taxFlag);
|
|
|
// 头部只展示简略信息,不显示详细阶梯价格
|
|
|
cartItemVo.setLadderPrices(null);
|
|
|
} else {
|
|
@@ -450,7 +453,7 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取购物车数量(商品种类数)
|
|
|
+ * 获取机构购物车数量(商品种类数)
|
|
|
* @param userId 用户ID
|
|
|
* @return int
|
|
|
*/
|
|
@@ -471,122 +474,8 @@ public class CartClubServiceImpl implements CartClubService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 设置商品图片及税费
|
|
|
- * @param cartItemVo 购物车商品
|
|
|
- * @return taxFlag 是否需要设置税费
|
|
|
- */
|
|
|
- private boolean setCartItemImgAndTax(CartItemVo cartItemVo) {
|
|
|
- // 图片路径
|
|
|
- String image = ProductUtil.getImageUrl("product", cartItemVo.getImage(), domain);
|
|
|
- cartItemVo.setImage(image);
|
|
|
- // 是否添加税费,不含税商品 开票需添加税费
|
|
|
- boolean taxFlag = "0".equals(cartItemVo.getIncludedTax()) && ("1".equals(cartItemVo.getInvoiceType()) || "2".equals(cartItemVo.getInvoiceType()));
|
|
|
- if (taxFlag) {
|
|
|
- BigDecimal cartItemTax = MathUtil.div(MathUtil.mul(cartItemVo.getPrice(), cartItemVo.getTaxRate()), 100, 2);
|
|
|
- cartItemVo.setPrice(MathUtil.add(cartItemVo.getPrice(), cartItemTax).doubleValue());
|
|
|
- }
|
|
|
- return taxFlag;
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 设置购物车阶梯价
|
|
|
- * @param cartItemVo
|
|
|
- * @param taxFlag
|
|
|
- */
|
|
|
- private void setCartLadderPrices(CartItemVo cartItemVo, boolean taxFlag) {
|
|
|
- // 阶梯价列表
|
|
|
- List<LadderPriceVo> ladderPrices = baseMapper.getLadderPriceList(cartItemVo.getProductId());
|
|
|
- if (!CollectionUtils.isEmpty(ladderPrices)) {
|
|
|
- IntStream.range(0, ladderPrices.size()).forEach(i -> {
|
|
|
- boolean isThisLadder;
|
|
|
- // 添加税费
|
|
|
- if (taxFlag) {
|
|
|
- BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(ladderPrices.get(i).getBuyPrice(), cartItemVo.getTaxRate()), 100, 2);
|
|
|
- ladderPrices.get(i).setBuyPrice(MathUtil.add(ladderPrices.get(i).getBuyPrice(), addedValueTax).doubleValue());
|
|
|
- }
|
|
|
- if (i == ladderPrices.size() - 1) {
|
|
|
- ladderPrices.get(i).setMaxNum(0);
|
|
|
- ladderPrices.get(i).setNumRange("≥" + ladderPrices.get(i).getBuyNum());
|
|
|
- isThisLadder = (cartItemVo.getNumber() >= ladderPrices.get(i).getBuyNum());
|
|
|
- } else {
|
|
|
- ladderPrices.get(i).setMaxNum(ladderPrices.get(i + 1).getBuyNum());
|
|
|
- String buyNumRangeShow = ladderPrices.get(i).getBuyNum() + "~" + (ladderPrices.get(i + 1).getBuyNum() - 1);
|
|
|
- ladderPrices.get(i).setNumRange(buyNumRangeShow);
|
|
|
- isThisLadder = (cartItemVo.getNumber() >= ladderPrices.get(i).getBuyNum() && cartItemVo.getNumber() <= ladderPrices.get(i).getMaxNum());
|
|
|
- }
|
|
|
- if (isThisLadder) {
|
|
|
- cartItemVo.setPrice(ladderPrices.get(i).getBuyPrice());
|
|
|
- cartItemVo.setOriginalPrice(ladderPrices.get(i).getBuyPrice());
|
|
|
- }
|
|
|
- });
|
|
|
- cartItemVo.setMin(ladderPrices.get(0).getBuyNum());
|
|
|
- cartItemVo.setLadderPrices(ladderPrices);
|
|
|
- } else {
|
|
|
- cartItemVo.setLadderFlag(0);
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- /**
|
|
|
- * 当前促销活动的价格计算列表
|
|
|
- *
|
|
|
- * @param promotions 促销活动
|
|
|
- * @param cartItemVo 当前购物车商品
|
|
|
- * @param taxFlag 计算税费标志
|
|
|
- * @return 价格计算列表
|
|
|
- */
|
|
|
- private List<CartPromotionPriceVo> getPromotionProducts(CartPromotionsVo promotions, CartItemVo cartItemVo, boolean taxFlag) {
|
|
|
- List<CartPromotionPriceVo> promotionPriceList = new ArrayList<>();
|
|
|
- CartPromotionPriceVo promotionPrice = new CartPromotionPriceVo();
|
|
|
- promotionPrice.setProductId(cartItemVo.getProductId());
|
|
|
- promotionPrice.setNumber(cartItemVo.getNumber());
|
|
|
- // 设置价格
|
|
|
- if (promotions.getType() == 1 && promotions.getMode() == 1) {
|
|
|
- // 单品优惠价添加税费
|
|
|
- if (taxFlag) {
|
|
|
- BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(promotions.getTouchPrice(), cartItemVo.getTaxRate()), BigDecimal.valueOf(100), 2);
|
|
|
- promotions.setTouchPrice(MathUtil.add(promotions.getTouchPrice(), addedValueTax).doubleValue());
|
|
|
- }
|
|
|
- promotionPrice.setPrice(promotions.getTouchPrice());
|
|
|
- promotionPrice.setOriginalPrice(cartItemVo.getOriginalPrice());
|
|
|
- // 设置商品活动价
|
|
|
- cartItemVo.setPrice(promotions.getTouchPrice());
|
|
|
- } else {
|
|
|
- promotionPrice.setPrice(cartItemVo.getPrice());
|
|
|
- }
|
|
|
- promotionPriceList.add(promotionPrice);
|
|
|
- return promotionPriceList;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 把当前促销更新购物车总促销
|
|
|
- * @param totalPromotions 总促销列表
|
|
|
- * @param promotionsIds 总促销Id集合
|
|
|
- * @param promotions 当前促销
|
|
|
- * @param promotionPriceList 当前促销商品价格计算列表
|
|
|
- */
|
|
|
- private void updateTotalPromotions(List<CartPromotionsVo> totalPromotions, List<Integer> promotionsIds, CartPromotionsVo promotions, List<CartPromotionPriceVo> promotionPriceList) {
|
|
|
- if (promotionsIds.contains(promotions.getId())) {
|
|
|
- // 列表已有该促销活动
|
|
|
- totalPromotions.forEach(item -> {
|
|
|
- if (item.getId().equals(promotions.getId())) {
|
|
|
- promotionPriceList.addAll(item.getProductList());
|
|
|
- item.setProductList(promotionPriceList);
|
|
|
- }
|
|
|
- });
|
|
|
- } else {
|
|
|
- // 新的促销活动
|
|
|
- promotionsIds.add(promotions.getId());
|
|
|
- if (promotions.getMode() == 3) {
|
|
|
- // 获取赠品
|
|
|
- List<CartItemVo> giftList = cartClubMapper.getPromotionGifts(promotions.getId());
|
|
|
- promotions.setGiftList(giftList);
|
|
|
- }
|
|
|
- promotions.setProductList(promotionPriceList);
|
|
|
- // 添加到总促销
|
|
|
- totalPromotions.add(promotions);
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
|
|
|
}
|