|
@@ -3,10 +3,13 @@ package com.caimei365.order.service.impl;
|
|
|
import com.caimei365.order.mapper.BaseMapper;
|
|
|
import com.caimei365.order.mapper.CartMapper;
|
|
|
import com.caimei365.order.model.ResponseJson;
|
|
|
+import com.caimei365.order.model.dto.CartDto;
|
|
|
+import com.caimei365.order.model.po.CartPo;
|
|
|
import com.caimei365.order.model.vo.*;
|
|
|
import com.caimei365.order.service.CartService;
|
|
|
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;
|
|
@@ -68,223 +71,146 @@ public class CartServiceImpl implements CartService {
|
|
|
List<Integer> promotionsIds = new ArrayList<>();
|
|
|
// 用户身份
|
|
|
Integer userIdentity = baseMapper.getIdentityByUserId(userId);
|
|
|
- // 遍历供应商列表
|
|
|
- shopInfoList.forEach(shop -> {
|
|
|
- // 该供应商下商品种类
|
|
|
- AtomicInteger shopKindCount = new AtomicInteger(0);
|
|
|
- // 该供应商总价
|
|
|
- AtomicDouble shopPrice = new AtomicDouble(0);
|
|
|
- // 该供应商满减金额(供应商满减,单品满减)
|
|
|
- AtomicDouble shopReducedPrice = new AtomicDouble(0);
|
|
|
- // 供应商促销优惠活动
|
|
|
- CartPromotionsVo shopPromotion = cartMapper.getPromotionByShopId(shop.getShopId());
|
|
|
- // 供应商下商品列表
|
|
|
- List<CartItemVo> productList = cartMapper.getCartProductsByShopId(shop.getShopId(), userId);
|
|
|
+ if (null != shopInfoList && shopInfoList.size()>0) {
|
|
|
+ // 遍历供应商列表
|
|
|
+ shopInfoList.forEach(shop -> {
|
|
|
+ // 该供应商下商品种类
|
|
|
+ AtomicInteger shopKindCount = new AtomicInteger(0);
|
|
|
+ // 该供应商总价
|
|
|
+ AtomicDouble shopPrice = new AtomicDouble(0);
|
|
|
+ // 该供应商满减金额(供应商满减,单品满减)
|
|
|
+ AtomicDouble shopReducedPrice = new AtomicDouble(0);
|
|
|
+ // 供应商促销优惠活动
|
|
|
+ CartPromotionsVo shopPromotion = cartMapper.getPromotionByShopId(shop.getShopId());
|
|
|
+ // 供应商下商品列表
|
|
|
+ List<CartItemVo> productList = cartMapper.getCartProductsByShopId(shop.getShopId(), userId);
|
|
|
|
|
|
- // 迭代器设置商品信息
|
|
|
- Iterator<CartItemVo> productIterator = productList.iterator();
|
|
|
- while (productIterator.hasNext()) {
|
|
|
- CartItemVo cartItemVo = productIterator.next();
|
|
|
- // 图片路径
|
|
|
- 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()));
|
|
|
- BigDecimal cartItemTax = MathUtil.div(MathUtil.mul(cartItemVo.getPrice(), cartItemVo.getTaxRate()), 100, 2);
|
|
|
- cartItemVo.setPrice(MathUtil.add(cartItemVo.getPrice(), cartItemTax).doubleValue());
|
|
|
- // 已上架商品
|
|
|
- if (cartItemVo.getValidFlag() == 2) {
|
|
|
- // 设置商品有效
|
|
|
- cartItemVo.setStatus(0);
|
|
|
- // 价格是否可见
|
|
|
- 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;
|
|
|
- // 没有店铺促销时,商品促销才有效
|
|
|
- if (null == shopPromotion) {
|
|
|
+ // 迭代器设置商品信息
|
|
|
+ Iterator<CartItemVo> productIterator = productList.iterator();
|
|
|
+ while (productIterator.hasNext()) {
|
|
|
+ CartItemVo cartItemVo = productIterator.next();
|
|
|
+ // 设置商品图片及税费
|
|
|
+ boolean taxFlag = setCartItemImgAndTax(cartItemVo);
|
|
|
+ // 已上架商品
|
|
|
+ if (cartItemVo.getValidFlag() == 2) {
|
|
|
+ // 设置商品有效
|
|
|
+ cartItemVo.setStatus(0);
|
|
|
+ // 价格是否可见
|
|
|
+ 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) {
|
|
|
// 获取商品促销信息
|
|
|
- promotions = cartMapper.getPromotionByProductId(cartItemVo.getProductId());
|
|
|
- /*
|
|
|
- * 设置商品促销优惠
|
|
|
- */
|
|
|
- if (null != promotions) {
|
|
|
- cartItemVo.setPromotions(promotions);
|
|
|
- Integer promotionsId = promotions.getId();
|
|
|
- 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 {
|
|
|
- // 其他优惠商品添加税费
|
|
|
- if (taxFlag) {
|
|
|
- BigDecimal addedValueTax = MathUtil.div(MathUtil.mul(cartItemVo.getPrice(), cartItemVo.getTaxRate()), 100, 2);
|
|
|
- promotionPrice.setPrice(MathUtil.add(cartItemVo.getPrice(), addedValueTax).doubleValue());
|
|
|
- } else {
|
|
|
- promotionPrice.setPrice(cartItemVo.getPrice());
|
|
|
- }
|
|
|
- }
|
|
|
- promotionPriceList.add(promotionPrice);
|
|
|
- if (promotionsIds.contains(promotionsId)) {
|
|
|
- // 列表已有该促销活动
|
|
|
- totalPromotions.forEach(item -> {
|
|
|
- if (item.getId().equals(promotionsId)) {
|
|
|
- promotionPriceList.addAll(item.getProductList());
|
|
|
- item.setProductList(promotionPriceList);
|
|
|
+ CartPromotionsVo promotions = null;
|
|
|
+ // 没有店铺促销时,商品促销才有效
|
|
|
+ if (null == shopPromotion) {
|
|
|
+ // 获取商品促销信息
|
|
|
+ promotions = cartMapper.getPromotionByProductId(cartItemVo.getProductId());
|
|
|
+ /*
|
|
|
+ * 设置商品促销优惠
|
|
|
+ */
|
|
|
+ if (null != promotions) {
|
|
|
+ // 当前促销活动的价格计算列表
|
|
|
+ List<CartPromotionPriceVo> promotionPriceList = getPromotionProducts(promotions, cartItemVo, taxFlag);
|
|
|
+ // 更新到总促销列表
|
|
|
+ updateTotalPromotions(totalPromotions, promotionsIds, promotions, promotionPriceList);
|
|
|
+ //单品满减-计算供应商总价/满减金额
|
|
|
+ if (promotions.getType() == 1 && promotions.getMode() == 2) {
|
|
|
+ BigDecimal totalAmount = MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice());
|
|
|
+ if (MathUtil.compare(totalAmount, promotions.getTouchPrice()) > -1) {
|
|
|
+ // 如果满足促销条件,设置供应商价格-满减金额,满减总额 + 当前促销满减金额
|
|
|
+ shopPrice.set(MathUtil.sub(shopPrice.get(), promotions.getReducedPrice()).doubleValue());
|
|
|
+ shopReducedPrice.set(MathUtil.add(shopReducedPrice, promotions.getReducedPrice()).doubleValue());
|
|
|
}
|
|
|
- });
|
|
|
- } else {
|
|
|
- // 新的促销活动
|
|
|
- promotionsIds.add(promotions.getId());
|
|
|
- if (promotions.getMode() == 3) {
|
|
|
- // 获取赠品
|
|
|
- List<CartItemVo> giftList = cartMapper.getPromotionGifts(promotions.getId());
|
|
|
- promotions.setGiftList(giftList);
|
|
|
}
|
|
|
- promotions.setProductList(promotionPriceList);
|
|
|
- // 添加到总促销
|
|
|
- totalPromotions.add(promotions);
|
|
|
+ cartItemVo.setPromotions(promotions);
|
|
|
}
|
|
|
- //单品满减-计算供应商总价/满减金额
|
|
|
- if (promotions.getType() == 1 && promotions.getMode() == 2) {
|
|
|
- BigDecimal totalAmount = MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice());
|
|
|
- if (MathUtil.compare(totalAmount, promotions.getTouchPrice()) > -1) {
|
|
|
- // 如果满足促销条件,设置供应商价格-满减金额,满减总额 + 当前促销满减金额
|
|
|
- shopPrice.set(MathUtil.sub(shopPrice.get(), promotions.getReducedPrice()).doubleValue());
|
|
|
- shopReducedPrice.set(MathUtil.add(shopReducedPrice, promotions.getReducedPrice()).doubleValue());
|
|
|
+ }
|
|
|
+ if (null != promotions || null != shopPromotion) {
|
|
|
+ // 商品处于活动状态
|
|
|
+ cartItemVo.setActStatus(1);
|
|
|
+ // 关闭阶梯价格,活动优先
|
|
|
+ cartItemVo.setLadderFlag(0);
|
|
|
+ } else {
|
|
|
+ if (cartItemVo.getLadderFlag() == 1) {
|
|
|
+ // 设置阶梯价
|
|
|
+ setCartLadderPrices(cartItemVo, taxFlag);
|
|
|
+ } else {
|
|
|
+ // 复购价
|
|
|
+ Double repurchase = baseMapper.getRepurchasePrice(cartItemVo.getProductId(), userId);
|
|
|
+ if (null != repurchase && repurchase > 0) {
|
|
|
+ cartItemVo.setPrice(repurchase);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- if (null != promotions || null != shopPromotion) {
|
|
|
- // 商品处于活动状态
|
|
|
- cartItemVo.setActStatus(1);
|
|
|
- // 关闭阶梯价格,活动优先
|
|
|
- cartItemVo.setLadderFlag(0);
|
|
|
+ // 该供应商下价格累加
|
|
|
+ shopPrice.set(MathUtil.add(shopPrice, MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice())).doubleValue());
|
|
|
+ // 该供应商下商品种类 +1
|
|
|
+ shopKindCount.incrementAndGet();
|
|
|
+ // 购物车总数量 + 当前商品购买数量
|
|
|
+ totalCount.updateAndGet(v -> v + cartItemVo.getNumber());
|
|
|
} else {
|
|
|
- /*
|
|
|
- * 设置阶梯价
|
|
|
- */
|
|
|
- if (cartItemVo.getLadderFlag() == 1) {
|
|
|
- // 阶梯价
|
|
|
- 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);
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 复购价
|
|
|
- Double repurchase = baseMapper.getRepurchasePrice(cartItemVo.getProductId(), userId);
|
|
|
- if (null != repurchase && repurchase > 0) {
|
|
|
- cartItemVo.setPrice(repurchase);
|
|
|
- }
|
|
|
+ // 失效商品
|
|
|
+ if (cartItemVo.getPriceFlag() == 1) {
|
|
|
+ // 未公开价格
|
|
|
+ cartItemVo.setStatus(6);
|
|
|
+ } else if (cartItemVo.getPriceFlag() == 2 && userIdentity == 4) {
|
|
|
+ // 价格仅会员可见
|
|
|
+ cartItemVo.setStatus(5);
|
|
|
+ } else if (cartItemVo.getStock() == null || cartItemVo.getStock() == 0) {
|
|
|
+ // 售罄
|
|
|
+ cartItemVo.setStatus(4);
|
|
|
+ } else if (cartItemVo.getStock() != null && (cartItemVo.getStock() < cartItemVo.getMin() || cartItemVo.getStock() < cartItemVo.getNumber())) {
|
|
|
+ // 库存不足
|
|
|
+ cartItemVo.setStatus(7);
|
|
|
}
|
|
|
+ invalidList.add(cartItemVo);
|
|
|
+ productIterator.remove();
|
|
|
}
|
|
|
- // 该供应商下价格累加
|
|
|
- shopPrice.set(MathUtil.add(shopPrice, MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice())).doubleValue());
|
|
|
- // 该供应商下商品种类 +1
|
|
|
- shopKindCount.incrementAndGet();
|
|
|
- // 购物车总数量 + 当前商品购买数量
|
|
|
- totalCount.updateAndGet(v -> v + cartItemVo.getNumber());
|
|
|
} else {
|
|
|
// 失效商品
|
|
|
- if (cartItemVo.getPriceFlag() == 1) {
|
|
|
- // 未公开价格
|
|
|
- cartItemVo.setStatus(6);
|
|
|
- } else if (cartItemVo.getPriceFlag() == 2 && userIdentity == 4) {
|
|
|
- // 价格仅会员可见
|
|
|
- cartItemVo.setStatus(5);
|
|
|
- } else if (cartItemVo.getStock() == null || cartItemVo.getStock() == 0) {
|
|
|
- // 售罄
|
|
|
- cartItemVo.setStatus(4);
|
|
|
- } else if (cartItemVo.getStock() != null && (cartItemVo.getStock() < cartItemVo.getMin() || cartItemVo.getStock() < cartItemVo.getNumber())) {
|
|
|
- // 库存不足
|
|
|
- cartItemVo.setStatus(7);
|
|
|
+ if (cartItemVo.getValidFlag() == 0) {
|
|
|
+ // 后台逻辑删除,已停售
|
|
|
+ cartItemVo.setStatus(1);
|
|
|
+ invalidList.add(cartItemVo);
|
|
|
+ } else if (cartItemVo.getValidFlag() == 10) {
|
|
|
+ // 已冻结,已丢失
|
|
|
+ cartItemVo.setStatus(2);
|
|
|
+ invalidList.add(cartItemVo);
|
|
|
+ } else if (cartItemVo.getValidFlag() == 3) {
|
|
|
+ // 已下架
|
|
|
+ cartItemVo.setStatus(3);
|
|
|
+ invalidList.add(cartItemVo);
|
|
|
}
|
|
|
- invalidList.add(cartItemVo);
|
|
|
+ //隐身商品validFlag = 9 不加入失效商品,直接去除
|
|
|
productIterator.remove();
|
|
|
}
|
|
|
- } else {
|
|
|
- // 失效商品
|
|
|
- if (cartItemVo.getValidFlag() == 0) {
|
|
|
- // 后台逻辑删除,已停售
|
|
|
- cartItemVo.setStatus(1);
|
|
|
- invalidList.add(cartItemVo);
|
|
|
- } else if (cartItemVo.getValidFlag() == 10) {
|
|
|
- // 已冻结,已丢失
|
|
|
- cartItemVo.setStatus(2);
|
|
|
- invalidList.add(cartItemVo);
|
|
|
- } else if (cartItemVo.getValidFlag() == 3) {
|
|
|
- // 已下架
|
|
|
- cartItemVo.setStatus(3);
|
|
|
- invalidList.add(cartItemVo);
|
|
|
- }
|
|
|
- //隐身商品validFlag = 9 不加入失效商品,直接去除
|
|
|
- productIterator.remove();
|
|
|
}
|
|
|
- }
|
|
|
- // 店铺促销
|
|
|
- if (null != shopPromotion) {
|
|
|
- shop.setPromotions(shopPromotion);
|
|
|
- if (!promotionsIds.contains(shopPromotion.getId())) {
|
|
|
- promotionsIds.add(shopPromotion.getId());
|
|
|
- // 店铺满赠
|
|
|
- if (shopPromotion.getMode() == 3) {
|
|
|
- // 获取赠品
|
|
|
- List<CartItemVo> giftList = cartMapper.getPromotionGifts(shopPromotion.getId());
|
|
|
- shopPromotion.setGiftList(giftList);
|
|
|
- }
|
|
|
- // 设置该优惠下的商品列表
|
|
|
- List<CartPromotionPriceVo> promotionPriceList = new ArrayList<>();
|
|
|
- productList.forEach(item -> {
|
|
|
- CartPromotionPriceVo promotionPrice = new CartPromotionPriceVo();
|
|
|
- promotionPrice.setProductId(item.getProductId());
|
|
|
- promotionPrice.setNumber(item.getNumber());
|
|
|
- promotionPrice.setPrice(item.getPrice());
|
|
|
- promotionPriceList.add(promotionPrice);
|
|
|
- });
|
|
|
- shopPromotion.setProductList(promotionPriceList);
|
|
|
- // 添加到总促销
|
|
|
- totalPromotions.add(shopPromotion);
|
|
|
- // 店铺满减-计算供应商总价/满减金额
|
|
|
- if (shopPromotion.getMode() == 2) {
|
|
|
- if (MathUtil.compare(shopPrice, shopPromotion.getTouchPrice()) > -1) {
|
|
|
+ // 店铺促销
|
|
|
+ if (null != shopPromotion) {
|
|
|
+ shop.setPromotions(shopPromotion);
|
|
|
+ if (!promotionsIds.contains(shopPromotion.getId())) {
|
|
|
+ promotionsIds.add(shopPromotion.getId());
|
|
|
+ // 店铺满赠
|
|
|
+ if (shopPromotion.getMode() == 3) {
|
|
|
+ // 获取赠品
|
|
|
+ List<CartItemVo> giftList = cartMapper.getPromotionGifts(shopPromotion.getId());
|
|
|
+ shopPromotion.setGiftList(giftList);
|
|
|
+ }
|
|
|
+ // 设置该优惠下的商品列表
|
|
|
+ List<CartPromotionPriceVo> promotionPriceList = new ArrayList<>();
|
|
|
+ productList.forEach(item -> {
|
|
|
+ CartPromotionPriceVo promotionPrice = new CartPromotionPriceVo();
|
|
|
+ promotionPrice.setProductId(item.getProductId());
|
|
|
+ promotionPrice.setNumber(item.getNumber());
|
|
|
+ promotionPrice.setPrice(item.getPrice());
|
|
|
+ promotionPriceList.add(promotionPrice);
|
|
|
+ });
|
|
|
+ shopPromotion.setProductList(promotionPriceList);
|
|
|
+ // 添加到总促销
|
|
|
+ totalPromotions.add(shopPromotion);
|
|
|
+ // 店铺满减-计算供应商总价/满减金额
|
|
|
+ if (shopPromotion.getMode() == 2 && MathUtil.compare(shopPrice, shopPromotion.getTouchPrice()) > -1) {
|
|
|
// 该供应商总价 - 满减金额
|
|
|
shopPrice.set(MathUtil.sub(shopPrice.get(), shopPromotion.getReducedPrice()).doubleValue());
|
|
|
// 该供应商优惠总额 + 满减金额
|
|
@@ -292,41 +218,41 @@ public class CartServiceImpl implements CartService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- shop.setCartList(productList);
|
|
|
- // 供应商总价
|
|
|
- shop.setTotalPrice(shopPrice.get());
|
|
|
- // 供应商总优惠
|
|
|
- shop.setReducedPrice(shopReducedPrice.get());
|
|
|
- // 供应商划线价
|
|
|
- shop.setOriginalPrice(MathUtil.add(shopPrice.get(), shopReducedPrice.get()).doubleValue());
|
|
|
- // 供应商下商品种类
|
|
|
- shop.setCount(shopKindCount.get());
|
|
|
- // 计算总价
|
|
|
- totalPrice.set(MathUtil.add(totalPrice, shop.getTotalPrice()).doubleValue());
|
|
|
- // 优惠总额
|
|
|
- reducedPrice.set(MathUtil.add(reducedPrice, shop.getReducedPrice()).doubleValue());
|
|
|
- // 总划线价
|
|
|
- totalOriginalPrice.set(MathUtil.add(totalOriginalPrice, shop.getOriginalPrice()).doubleValue());
|
|
|
- // 商品种类
|
|
|
- kindCount.updateAndGet(v -> v + shopKindCount.get());
|
|
|
- });
|
|
|
- // 删除空数据
|
|
|
- shopInfoList.removeIf(shop -> (null != shop && shop.getCount() == 0));
|
|
|
- // 总促销计算
|
|
|
- totalPromotions.forEach(promotions -> {
|
|
|
- // 该促销内商品总价
|
|
|
- double touchPrice = promotions.getProductList().stream().mapToDouble(product -> product.getNumber() * product.getPrice()).sum();
|
|
|
- if (MathUtil.compare(touchPrice, promotions.getTouchPrice()) > -1) {
|
|
|
+ shop.setCartList(productList);
|
|
|
+ // 供应商总价
|
|
|
+ shop.setTotalPrice(shopPrice.get());
|
|
|
+ // 供应商总优惠
|
|
|
+ shop.setReducedPrice(shopReducedPrice.get());
|
|
|
+ // 供应商划线价
|
|
|
+ shop.setOriginalPrice(MathUtil.add(shopPrice.get(), shopReducedPrice.get()).doubleValue());
|
|
|
+ // 供应商下商品种类
|
|
|
+ shop.setCount(shopKindCount.get());
|
|
|
+ // 计算总价
|
|
|
+ totalPrice.set(MathUtil.add(totalPrice, shop.getTotalPrice()).doubleValue());
|
|
|
+ // 优惠总额
|
|
|
+ reducedPrice.set(MathUtil.add(reducedPrice, shop.getReducedPrice()).doubleValue());
|
|
|
+ // 总划线价
|
|
|
+ totalOriginalPrice.set(MathUtil.add(totalOriginalPrice, shop.getOriginalPrice()).doubleValue());
|
|
|
+ // 商品种类
|
|
|
+ kindCount.updateAndGet(v -> v + shopKindCount.get());
|
|
|
+ });
|
|
|
+ // 删除空数据
|
|
|
+ shopInfoList.removeIf(shop -> (null != shop && shop.getCount() == 0));
|
|
|
+ // 总促销计算
|
|
|
+ totalPromotions.forEach(promotions -> {
|
|
|
// 凑单满减
|
|
|
if (promotions.getType() == 2 && promotions.getMode() == 2) {
|
|
|
- // 总价 - 满减金额
|
|
|
- totalPrice.set(MathUtil.sub(totalPrice, promotions.getReducedPrice()).doubleValue());
|
|
|
- // 优惠总额 + 满减金额
|
|
|
- reducedPrice.set(MathUtil.add(reducedPrice, promotions.getReducedPrice()).doubleValue());
|
|
|
+ // 该促销内商品总价
|
|
|
+ double touchPrice = promotions.getProductList().stream().mapToDouble(product -> product.getNumber() * product.getPrice()).sum();
|
|
|
+ if (MathUtil.compare(touchPrice, promotions.getTouchPrice()) > -1) {
|
|
|
+ // 总价 - 满减金额
|
|
|
+ totalPrice.set(MathUtil.sub(totalPrice, promotions.getReducedPrice()).doubleValue());
|
|
|
+ // 优惠总额 + 满减金额
|
|
|
+ reducedPrice.set(MathUtil.add(reducedPrice, promotions.getReducedPrice()).doubleValue());
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- });
|
|
|
+ });
|
|
|
+ }
|
|
|
/*
|
|
|
* 返回结果
|
|
|
*/
|
|
@@ -342,4 +268,325 @@ public class CartServiceImpl implements CartService {
|
|
|
// 返回数据
|
|
|
return ResponseJson.success(resultMap);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 网站顶部购物车数据
|
|
|
+ *
|
|
|
+ * @param userId 用户Id
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Map<String, Object>> getShoppingCartHead(Integer userId) {
|
|
|
+ /*
|
|
|
+ * 初始化返回数据
|
|
|
+ */
|
|
|
+ // 商品总数量
|
|
|
+ AtomicInteger totalCount = new AtomicInteger(0);
|
|
|
+ // 商品种类
|
|
|
+ AtomicInteger kindCount = new AtomicInteger(0);
|
|
|
+ // 统计商品总金额
|
|
|
+ AtomicDouble totalPrice = new AtomicDouble(0);
|
|
|
+ // 购物车商品列表
|
|
|
+ List<CartItemVo> cartList = null;
|
|
|
+ /*
|
|
|
+ * 处理数据
|
|
|
+ */
|
|
|
+ // 促销活动(总)
|
|
|
+ List<CartPromotionsVo> totalPromotions = new ArrayList<>();
|
|
|
+ List<Integer> promotionsIds = new ArrayList<>();
|
|
|
+ // 用户身份
|
|
|
+ Integer userIdentity = baseMapper.getIdentityByUserId(userId);
|
|
|
+ // 获取购物车商品列表(不区分供应商)
|
|
|
+ cartList = cartMapper.getCartProductList(userId);
|
|
|
+ if (null != cartList && cartList.size()>0) {
|
|
|
+ // 移除价格不可见商品
|
|
|
+ cartList.removeIf(cartItemVo -> !(cartItemVo.getPriceFlag() == 0 || (cartItemVo.getPriceFlag() == 2 && userIdentity == 2)));
|
|
|
+ cartList.forEach(cartItemVo -> {
|
|
|
+ // 设置商品图片及税费
|
|
|
+ boolean taxFlag = setCartItemImgAndTax(cartItemVo);
|
|
|
+ // 获取商品促销信息
|
|
|
+ CartPromotionsVo promotions = cartMapper.getPromotionByProductId(cartItemVo.getProductId());
|
|
|
+ /*
|
|
|
+ * 设置商品促销优惠
|
|
|
+ */
|
|
|
+ if (null != promotions) {
|
|
|
+ // 关闭阶梯价格,活动优先
|
|
|
+ cartItemVo.setLadderFlag(0);
|
|
|
+ // 商品处于活动状态
|
|
|
+ cartItemVo.setActStatus(1);
|
|
|
+ // 当前促销活动的价格计算列表
|
|
|
+ List<CartPromotionPriceVo> promotionPriceList = getPromotionProducts(promotions, cartItemVo, taxFlag);
|
|
|
+ // 更新到总促销列表
|
|
|
+ updateTotalPromotions(totalPromotions, promotionsIds, promotions, promotionPriceList);
|
|
|
+ } else {
|
|
|
+ if (cartItemVo.getLadderFlag() == 1) {
|
|
|
+ // 设置阶梯价
|
|
|
+ setCartLadderPrices(cartItemVo, taxFlag);
|
|
|
+ // 头部只展示简略信息,不显示详细阶梯价格
|
|
|
+ cartItemVo.setLadderPrices(null);
|
|
|
+ } else {
|
|
|
+ // 复购价
|
|
|
+ Double repurchase = baseMapper.getRepurchasePrice(cartItemVo.getProductId(), userId);
|
|
|
+ if (null != repurchase && repurchase > 0) {
|
|
|
+ cartItemVo.setPrice(repurchase);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 商品总金额累加
|
|
|
+ totalPrice.set(MathUtil.add(totalPrice, MathUtil.mul(cartItemVo.getNumber(), cartItemVo.getPrice())).doubleValue());
|
|
|
+ // 商品种类 +1
|
|
|
+ kindCount.incrementAndGet();
|
|
|
+ // 购物车总数量 + 当前商品购买数量
|
|
|
+ totalCount.updateAndGet(v -> v + cartItemVo.getNumber());
|
|
|
+ });
|
|
|
+ // 总促销-满减计算
|
|
|
+ totalPromotions.forEach(promotions -> {
|
|
|
+ // 满减
|
|
|
+ if (promotions.getMode() == 2) {
|
|
|
+ // 该促销内商品总价
|
|
|
+ double touchPrice = promotions.getProductList().stream().mapToDouble(product -> product.getNumber() * product.getPrice()).sum();
|
|
|
+ if (MathUtil.compare(touchPrice, promotions.getTouchPrice()) > -1) {
|
|
|
+ // 总价 - 满减金额
|
|
|
+ totalPrice.set(MathUtil.sub(totalPrice, promotions.getReducedPrice()).doubleValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ * 返回结果
|
|
|
+ */
|
|
|
+ Map<String, Object> resultMap = new HashMap<>(4);
|
|
|
+ resultMap.put("list", cartList);
|
|
|
+ resultMap.put("kindCount", kindCount);
|
|
|
+ resultMap.put("totalCount", totalCount);
|
|
|
+ resultMap.put("totalPrice", totalPrice);
|
|
|
+ // 返回数据
|
|
|
+ return ResponseJson.success(resultMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加购物车
|
|
|
+ *
|
|
|
+ * @param cartDto {
|
|
|
+ * userId 用户ID
|
|
|
+ * productId 商品id
|
|
|
+ * productCount 商品数量
|
|
|
+ * }
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> addShoppingCart(CartDto cartDto) {
|
|
|
+ CartPo cart = cartMapper.getCartPo(cartDto);
|
|
|
+ if (cart != null) {
|
|
|
+ // 购物车已存在该商品,更新数量
|
|
|
+ cart.setProductCount(cart.getProductCount() + cartDto.getProductCount());
|
|
|
+ cart.setAddTime(new Date());
|
|
|
+ cartMapper.updateCart(cart);
|
|
|
+ } else {
|
|
|
+ // 添加新购物车
|
|
|
+ cart = new CartPo();
|
|
|
+ cart.setUserId(cartDto.getUserId());
|
|
|
+ cart.setProductId(cartDto.getProductId());
|
|
|
+ cart.setProductCount(cartDto.getProductCount());
|
|
|
+ //判断是否是再次购买
|
|
|
+ Double repurchase = baseMapper.getRepurchasePrice(cartDto.getProductId(), cartDto.getUserId());
|
|
|
+ if (null != repurchase && repurchase > 0) {
|
|
|
+ // 再次购买
|
|
|
+ cart.setReBuyFlag(1);
|
|
|
+ } else {
|
|
|
+ cart.setReBuyFlag(0);
|
|
|
+ }
|
|
|
+ cart.setAddTime(new Date());
|
|
|
+ cartMapper.insertCart(cart);
|
|
|
+ }
|
|
|
+ // 获取购物车数量(商品种类数)
|
|
|
+ int cartCount = getCartCount(cartDto.getUserId());
|
|
|
+ return ResponseJson.success("添加成功!返回购物车数量", cartCount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新购物车
|
|
|
+ *
|
|
|
+ * @param cartDto {
|
|
|
+ * userId 用户ID
|
|
|
+ * productId 商品id
|
|
|
+ * productCount 商品数量
|
|
|
+ * }
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> updateShoppingCart(CartDto cartDto) {
|
|
|
+ CartPo cart = new CartPo();
|
|
|
+ cart.setUserId(cartDto.getUserId());
|
|
|
+ cart.setProductId(cartDto.getProductId());
|
|
|
+ cart.setProductCount(cartDto.getProductCount());
|
|
|
+ cart.setAddTime(new Date());
|
|
|
+ cartMapper.updateCart(cart);
|
|
|
+ // 获取购物车数量(商品种类数)
|
|
|
+ int cartCount = getCartCount(cartDto.getUserId());
|
|
|
+ return ResponseJson.success("更新成功!返回购物车数量", cartCount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除购物车
|
|
|
+ *
|
|
|
+ * @param cartDto {
|
|
|
+ * userId 用户ID
|
|
|
+ * productIds 商品ids,逗号隔开
|
|
|
+ * }
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Integer> deleteShoppingCart(CartDto cartDto) {
|
|
|
+ List<Integer> productIdList = Lists.newArrayList();
|
|
|
+ if (cartDto.getProductIds().contains(",")) {
|
|
|
+ String[] productArr = cartDto.getProductIds().split(",");
|
|
|
+ for (String id : productArr) {
|
|
|
+ productIdList.add(Integer.parseInt(id));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ productIdList.add(Integer.parseInt(cartDto.getProductIds()));
|
|
|
+ }
|
|
|
+ cartMapper.deleteCartByProductIds(cartDto.getUserId(), productIdList);
|
|
|
+ // 获取购物车数量(商品种类数)
|
|
|
+ int cartCount = getCartCount(cartDto.getUserId());
|
|
|
+ return ResponseJson.success("删除成功!返回购物车数量", cartCount);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取购物车数量(商品种类数)
|
|
|
+ * @param userId 用户ID
|
|
|
+ * @return int
|
|
|
+ */
|
|
|
+ private int getCartCount(Integer userId){
|
|
|
+ if (null == userId) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ // 获取购物车商品列表(不区分供应商)
|
|
|
+ List<CartItemVo> cartList = cartMapper.getCartProductList(userId);
|
|
|
+ if (null != cartList && cartList.size()>0) {
|
|
|
+ // 用户身份
|
|
|
+ Integer userIdentity = baseMapper.getIdentityByUserId(userId);
|
|
|
+ // 移除价格不可见商品
|
|
|
+ cartList.removeIf(cartItemVo -> !(cartItemVo.getPriceFlag() == 0 || (cartItemVo.getPriceFlag() == 2 && userIdentity == 2)));
|
|
|
+ return cartList.size();
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置商品图片及税费
|
|
|
+ * @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 = cartMapper.getPromotionGifts(promotions.getId());
|
|
|
+ promotions.setGiftList(giftList);
|
|
|
+ }
|
|
|
+ promotions.setProductList(promotionPriceList);
|
|
|
+ // 添加到总促销
|
|
|
+ totalPromotions.add(promotions);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|