|
@@ -0,0 +1,345 @@
|
|
|
+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.vo.*;
|
|
|
+import com.caimei365.order.service.CartService;
|
|
|
+import com.caimei365.order.utils.MathUtil;
|
|
|
+import com.caimei365.order.utils.ProductUtil;
|
|
|
+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;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Description
|
|
|
+ *
|
|
|
+ * @author : Charles
|
|
|
+ * @date : 2021/6/25
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class CartServiceImpl implements CartService {
|
|
|
+ @Value("${caimei.wwwDomain}")
|
|
|
+ private String domain;
|
|
|
+ @Resource
|
|
|
+ private BaseMapper baseMapper;
|
|
|
+ @Resource
|
|
|
+ private CartMapper cartMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 购物车列表详细数据
|
|
|
+ *
|
|
|
+ * @param userId 用户Id
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public ResponseJson<Map<String, Object>> getShoppingCartList(Integer userId) {
|
|
|
+ /*
|
|
|
+ * 初始化返回数据
|
|
|
+ */
|
|
|
+ // 购物车供应商列表
|
|
|
+ List<CartShopVo> shopInfoList = cartMapper.getCartShops(userId);
|
|
|
+ // 失效商品列表
|
|
|
+ List<CartItemVo> invalidList = new ArrayList<>();
|
|
|
+ // 商品总数量
|
|
|
+ AtomicInteger totalCount = new AtomicInteger(0);
|
|
|
+ // 商品种类
|
|
|
+ AtomicInteger kindCount = new AtomicInteger(0);
|
|
|
+ // 统计商品总金额
|
|
|
+ AtomicDouble totalPrice = new AtomicDouble(0);
|
|
|
+ // 统计总促销满减
|
|
|
+ AtomicDouble reducedPrice = new AtomicDouble(0);
|
|
|
+ // 统计总划线价
|
|
|
+ AtomicDouble totalOriginalPrice = new AtomicDouble(0);
|
|
|
+ /*
|
|
|
+ * 处理数据
|
|
|
+ */
|
|
|
+ // 促销活动(总)
|
|
|
+ List<CartPromotionsVo> totalPromotions = new ArrayList<>();
|
|
|
+ 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);
|
|
|
+
|
|
|
+ // 迭代器设置商品信息
|
|
|
+ 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) {
|
|
|
+ // 获取商品促销信息
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } 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);
|
|
|
+ }
|
|
|
+ //单品满减-计算供应商总价/满减金额
|
|
|
+ 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) {
|
|
|
+ // 阶梯价
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 该供应商下价格累加
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ invalidList.add(cartItemVo);
|
|
|
+ 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) {
|
|
|
+ // 该供应商总价 - 满减金额
|
|
|
+ shopPrice.set(MathUtil.sub(shopPrice.get(), shopPromotion.getReducedPrice()).doubleValue());
|
|
|
+ // 该供应商优惠总额 + 满减金额
|
|
|
+ shopReducedPrice.set(MathUtil.add(shopReducedPrice.get(), shopPromotion.getReducedPrice()).doubleValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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) {
|
|
|
+ // 凑单满减
|
|
|
+ if (promotions.getType() == 2 && promotions.getMode() == 2) {
|
|
|
+ // 总价 - 满减金额
|
|
|
+ totalPrice.set(MathUtil.sub(totalPrice, promotions.getReducedPrice()).doubleValue());
|
|
|
+ // 优惠总额 + 满减金额
|
|
|
+ reducedPrice.set(MathUtil.add(reducedPrice, promotions.getReducedPrice()).doubleValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ /*
|
|
|
+ * 返回结果
|
|
|
+ */
|
|
|
+ Map<String, Object> resultMap = new HashMap<>(8);
|
|
|
+ resultMap.put("list", shopInfoList);
|
|
|
+ resultMap.put("invalid", invalidList);
|
|
|
+ resultMap.put("kindCount", kindCount);
|
|
|
+ resultMap.put("totalCount", totalCount);
|
|
|
+ resultMap.put("totalPrice", totalPrice);
|
|
|
+ resultMap.put("reducedPrice", reducedPrice);
|
|
|
+ resultMap.put("totalOriginalPrice", totalOriginalPrice);
|
|
|
+ resultMap.put("promotions", totalPromotions);
|
|
|
+ // 返回数据
|
|
|
+ return ResponseJson.success(resultMap);
|
|
|
+ }
|
|
|
+}
|