From dd213e7e33108c66b47ec628b284b5acc0d105d7 Mon Sep 17 00:00:00 2001 From: lifenlong Date: Sun, 18 Jul 2021 18:30:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A0=8D=E4=BB=B7=E3=80=81=E7=A7=AF=E5=88=86?= =?UTF-8?q?=E5=95=86=E5=93=81=E8=AE=A2=E5=8D=95=E4=BB=B7=E6=A0=BC=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/cart/entity/vo/CartSkuVO.java | 3 +- .../cart/render/impl/CheckDataRender.java | 20 ++ .../cart/render/impl/SkuPromotionRender.java | 126 +++++------- .../entity/dto/GoodsSkuPromotionPriceDTO.java | 7 +- .../promotion/service/PointsGoodsService.java | 6 +- .../service/PromotionPriceService.java | 4 +- .../KanjiaActivityGoodsServiceImpl.java | 18 +- .../serviceimpl/PointsGoodsServiceImpl.java | 20 +- .../PromotionPriceServiceImpl.java | 191 +++++++++++++++--- 9 files changed, 259 insertions(+), 136 deletions(-) diff --git a/framework/src/main/java/cn/lili/modules/order/cart/entity/vo/CartSkuVO.java b/framework/src/main/java/cn/lili/modules/order/cart/entity/vo/CartSkuVO.java index 7671d446..8c185030 100644 --- a/framework/src/main/java/cn/lili/modules/order/cart/entity/vo/CartSkuVO.java +++ b/framework/src/main/java/cn/lili/modules/order/cart/entity/vo/CartSkuVO.java @@ -66,8 +66,7 @@ public class CartSkuVO extends CartBase implements Serializable { @ApiModelProperty(value = "是否可配送") private Boolean isShip; - @ApiModelProperty(value = - "拼团id 如果是拼团购买 此值为拼团活动id," + + @ApiModelProperty(value = "拼团id 如果是拼团购买 此值为拼团活动id," + "当pintuanId为空,则表示普通购买(或者拼团商品,单独购买)") private String pintuanId; diff --git a/framework/src/main/java/cn/lili/modules/order/cart/render/impl/CheckDataRender.java b/framework/src/main/java/cn/lili/modules/order/cart/render/impl/CheckDataRender.java index 618737eb..ad21ff1a 100644 --- a/framework/src/main/java/cn/lili/modules/order/cart/render/impl/CheckDataRender.java +++ b/framework/src/main/java/cn/lili/modules/order/cart/render/impl/CheckDataRender.java @@ -16,6 +16,10 @@ import cn.lili.modules.order.cart.entity.vo.CartSkuVO; import cn.lili.modules.order.cart.entity.vo.CartVO; import cn.lili.modules.order.cart.render.CartRenderStep; import cn.lili.modules.order.order.service.OrderService; +import cn.lili.modules.promotion.entity.dos.Pintuan; +import cn.lili.modules.promotion.entity.dos.PromotionGoods; +import cn.lili.modules.promotion.entity.enums.PromotionTypeEnum; +import cn.lili.modules.promotion.service.PintuanService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Service; @@ -23,6 +27,7 @@ import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; /** @@ -41,6 +46,9 @@ public class CheckDataRender implements CartRenderStep { @Autowired private OrderService orderService; + @Autowired + private PintuanService pintuanService; + @Override public void render(TradeDTO tradeDTO) { //预校验 @@ -140,6 +148,18 @@ public class CheckDataRender implements CartRenderStep { throw new ServiceException(ResultCode.PINTUAN_JOIN_ERROR); } } + //判断拼团商品的限购数量 + Optional pintuanId = tradeDTO.getSkuList().get(0).getPromotions().stream().filter(i -> i.getPromotionType().equals(PromotionTypeEnum.PINTUAN.name())).map(PromotionGoods::getPromotionId).findFirst(); + if (pintuanId.isPresent()) { + Pintuan pintuan = pintuanService.getPintuanById(pintuanId.get()); + Integer limitNum = pintuan.getLimitNum(); + for (CartSkuVO cartSkuVO : tradeDTO.getSkuList()) { + if (limitNum != 0 && cartSkuVO.getNum() > limitNum) { + throw new ServiceException(ResultCode.PINTUAN_LIMIT_NUM_ERROR); + } + } + } + }else if(tradeDTO.getCartTypeEnum().equals(CartTypeEnum.KANJIA)){ } diff --git a/framework/src/main/java/cn/lili/modules/order/cart/render/impl/SkuPromotionRender.java b/framework/src/main/java/cn/lili/modules/order/cart/render/impl/SkuPromotionRender.java index ab9bc9a0..d08c3005 100644 --- a/framework/src/main/java/cn/lili/modules/order/cart/render/impl/SkuPromotionRender.java +++ b/framework/src/main/java/cn/lili/modules/order/cart/render/impl/SkuPromotionRender.java @@ -1,8 +1,6 @@ package cn.lili.modules.order.cart.render.impl; import cn.hutool.core.date.DateUtil; -import cn.lili.common.enums.ResultCode; -import cn.lili.common.exception.ServiceException; import cn.lili.common.utils.CurrencyUtil; import cn.lili.modules.order.cart.entity.dto.MemberCouponDTO; import cn.lili.modules.order.cart.entity.dto.TradeDTO; @@ -14,14 +12,10 @@ import cn.lili.modules.order.cart.entity.vo.PriceDetailVO; import cn.lili.modules.order.cart.render.CartRenderStep; import cn.lili.modules.order.order.entity.dto.PriceDetailDTO; import cn.lili.modules.promotion.entity.dos.MemberCoupon; -import cn.lili.modules.promotion.entity.dos.Pintuan; -import cn.lili.modules.promotion.entity.dos.PromotionGoods; import cn.lili.modules.promotion.entity.dto.GoodsSkuPromotionPriceDTO; import cn.lili.modules.promotion.entity.dto.PromotionPriceDTO; import cn.lili.modules.promotion.entity.dto.PromotionPriceParamDTO; import cn.lili.modules.promotion.entity.dto.StorePromotionPriceDTO; -import cn.lili.modules.promotion.entity.enums.PromotionTypeEnum; -import cn.lili.modules.promotion.service.PintuanService; import cn.lili.modules.promotion.service.PromotionGoodsService; import cn.lili.modules.promotion.service.PromotionPriceService; import org.springframework.beans.factory.annotation.Autowired; @@ -53,19 +47,15 @@ public class SkuPromotionRender implements CartRenderStep { */ @Autowired private PromotionGoodsService promotionGoodsService; - /** - * 拼团 - */ - @Autowired - private PintuanService pintuanService; @Override public void render(TradeDTO tradeDTO) { - //主要渲染各个优惠的价格 - this.renderSkuPromotion(tradeDTO); + //渲染促销价格 + this.renderPromotionPrice(tradeDTO); - this.checkPromotionLimit(tradeDTO); + //获取商品促销 + renderSkuPromotion(tradeDTO); } @@ -76,23 +66,12 @@ public class SkuPromotionRender implements CartRenderStep { */ private void renderSkuPromotion(TradeDTO tradeDTO) { - //渲染促销价格 - this.renderPromotionPrice(tradeDTO); - - //拼团和积分购买需要特殊处理,这里优先特殊处理 - if (tradeDTO.getCartTypeEnum().equals(CartTypeEnum.POINTS)) { - List cartSkuVOList = tradeDTO.getSkuList(); - for (CartSkuVO cartSku : cartSkuVOList) { - PriceDetailDTO priceDetailDTO = cartSku.getPriceDetailDTO(); - //需要支付的积分 - priceDetailDTO.setPayPoint( - CurrencyUtil.mul( - cartSku.getPoint(), cartSku.getNum()).intValue()); - } - } else { - //这里普通购物车也只渲染满优惠,其他优惠都是商品级别的,都写在商品属性里 + //非积分商品、拼团、砍价商品可渲染满优惠活动 + //这里普通购物车也只渲染满优惠,其他优惠都是商品级别的,都写在商品属性里 + if (!tradeDTO.getCartTypeEnum().equals(CartTypeEnum.POINTS) + && !tradeDTO.getCartTypeEnum().equals(CartTypeEnum.PINTUAN) + && !tradeDTO.getCartTypeEnum().equals(CartTypeEnum.KANJIA)) { List cartVOS = tradeDTO.getCartList(); - for (CartVO cartVO : cartVOS) { if (isFull(cartVO)) { this.renderFullMinus(cartVO); @@ -101,16 +80,61 @@ public class SkuPromotionRender implements CartRenderStep { promotionGoodsService.getCartSkuPromotion(cartSkuVO); } } - } } /** * 渲染购物车视图的促销价格 + * 1.获取购物车商品列表 + * 2.获取用户的优惠券 + * 3.调用价格计算模块,返回价格计算结果 + * 4.分配计算后的促销 * * @param tradeDTO 购物车视图 */ private void renderPromotionPrice(TradeDTO tradeDTO) { + + //获取购物车商品列表 + List promotionPriceParamList = this.getPromotionPriceParamList(tradeDTO); + //店铺优惠券集合 + List memberCoupons = this.getMemberCoupons(tradeDTO); + //调用价格计算模块,返回价格计算结果 + PromotionPriceDTO promotionPrice = promotionPriceService.calculationPromotionPrice(promotionPriceParamList, memberCoupons,tradeDTO.getCartTypeEnum()); + // 分配计算后的促销 + this.distributionPromotionPrice(tradeDTO, promotionPrice); + } + + /** + * 获取用户优惠券列表 + * + * @param tradeDTO 交易DTO + * @return 用户优惠券列表 + */ + private List getMemberCoupons(TradeDTO tradeDTO) { + //店铺优惠券集合 + List memberCoupons = new ArrayList<>(); + if (tradeDTO.getStoreCoupons() != null) { + memberCoupons.addAll(tradeDTO.getStoreCoupons().values().parallelStream().map(MemberCouponDTO::getMemberCoupon).collect(Collectors.toList())); + } + + //平台优惠券 + if (tradeDTO.getPlatformCoupon() != null && tradeDTO.getPlatformCoupon().getMemberCoupon() != null) { + memberCoupons.add(tradeDTO.getPlatformCoupon().getMemberCoupon()); + } + + //清除过期优惠券 + long now = DateUtil.date().getTime(); + memberCoupons.removeIf(memberCoupon -> memberCoupon.getEndTime().getTime() < now); + return memberCoupons; + } + + /** + * 获取促销价格DTO列表 + * + * @param tradeDTO 交易DTO + * @return 促销价格DTO列表 + */ + private List getPromotionPriceParamList(TradeDTO tradeDTO) { List promotionPriceParamList = new ArrayList<>(); List cartList = tradeDTO.getCartList(); for (CartVO cartVO : cartList) { @@ -130,30 +154,7 @@ public class SkuPromotionRender implements CartRenderStep { } } } - //如果包含促销规则 - if (!promotionPriceParamList.isEmpty()) { - //店铺优惠券集合 - List memberCoupons = new ArrayList<>(); - if (tradeDTO.getStoreCoupons() != null) { - memberCoupons.addAll(tradeDTO.getStoreCoupons().values().parallelStream().map(MemberCouponDTO::getMemberCoupon).collect(Collectors.toList())); - } - - //平台优惠券 - if (tradeDTO.getPlatformCoupon() != null && tradeDTO.getPlatformCoupon().getMemberCoupon() != null) { - memberCoupons.add(tradeDTO.getPlatformCoupon().getMemberCoupon()); - } - //检查优惠券集合中是否存在过期优惠券 - this.checkMemberCoupons(memberCoupons); - //调用价格计算模块,返回价格计算结果 - PromotionPriceDTO promotionPrice = promotionPriceService.calculationPromotionPrice(promotionPriceParamList, memberCoupons); - // 分配计算后的促销 - this.distributionPromotionPrice(tradeDTO, promotionPrice); - } - } - - private void checkMemberCoupons(List memberCoupons) { - long now = DateUtil.date().getTime(); - memberCoupons.removeIf(memberCoupon -> memberCoupon.getEndTime().getTime() < now); + return promotionPriceParamList; } /** @@ -284,19 +285,4 @@ public class SkuPromotionRender implements CartRenderStep { return false; } - private void checkPromotionLimit(TradeDTO tradeDTO) { - if (tradeDTO.getCartTypeEnum().equals(CartTypeEnum.PINTUAN)) { - //如果为拼团订单,则获取拼团活动ID - Optional pintuanId = tradeDTO.getSkuList().get(0).getPromotions().stream().filter(i -> i.getPromotionType().equals(PromotionTypeEnum.PINTUAN.name())).map(PromotionGoods::getPromotionId).findFirst(); - if (pintuanId.isPresent()) { - Pintuan pintuan = pintuanService.getPintuanById(pintuanId.get()); - Integer limitNum = pintuan.getLimitNum(); - for (CartSkuVO cartSkuVO : tradeDTO.getSkuList()) { - if (limitNum != 0 && cartSkuVO.getNum() > limitNum) { - throw new ServiceException(ResultCode.PINTUAN_LIMIT_NUM_ERROR); - } - } - } - } - } } diff --git a/framework/src/main/java/cn/lili/modules/promotion/entity/dto/GoodsSkuPromotionPriceDTO.java b/framework/src/main/java/cn/lili/modules/promotion/entity/dto/GoodsSkuPromotionPriceDTO.java index a299ec8c..ffe0a971 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/entity/dto/GoodsSkuPromotionPriceDTO.java +++ b/framework/src/main/java/cn/lili/modules/promotion/entity/dto/GoodsSkuPromotionPriceDTO.java @@ -52,8 +52,8 @@ public class GoodsSkuPromotionPriceDTO implements Serializable { @ApiModelProperty(value = "单个商品积分购买数量") private Double points; - @ApiModelProperty(value = "商品购买总数量 = 单个商品积分购买数量 * 数量") - private Double totalPoints; + @ApiModelProperty(value = "商品购买积分总数量 = 单个商品积分购买数量 * 数量") + private Long totalPoints; @ApiModelProperty(value = "单个优惠的所占总优惠金额比例") private Double discountPriceRate; @@ -101,8 +101,9 @@ public class GoodsSkuPromotionPriceDTO implements Serializable { this.setOriginalPrice(sku.getPrice()); this.setCouponPrice(0D); this.setPoints(0d); - this.setTotalPoints(0d); + this.setTotalPoints(0L); this.setFinalePrice(sku.getPrice()); this.setJoinPromotion(new ArrayList<>()); + } } diff --git a/framework/src/main/java/cn/lili/modules/promotion/service/PointsGoodsService.java b/framework/src/main/java/cn/lili/modules/promotion/service/PointsGoodsService.java index 122d6142..1217efbf 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/service/PointsGoodsService.java +++ b/framework/src/main/java/cn/lili/modules/promotion/service/PointsGoodsService.java @@ -59,12 +59,12 @@ public interface PointsGoodsService extends IService { PointsGoodsVO getPointsGoodsDetail(String id); /** - * 根据SkuID获取积分商品信息 + * 根据ID获取积分详情 * - * @param skuId 商品skuId + * @param skuId 商品SkuId * @return 积分详情 */ - PointsGoods getPointsGoodsDetailBySkuId(String skuId); + PointsGoodsVO getPointsGoodsVOByMongo(String skuId); /** * 根据条件查询积分商品 diff --git a/framework/src/main/java/cn/lili/modules/promotion/service/PromotionPriceService.java b/framework/src/main/java/cn/lili/modules/promotion/service/PromotionPriceService.java index 0b934850..c50948a1 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/service/PromotionPriceService.java +++ b/framework/src/main/java/cn/lili/modules/promotion/service/PromotionPriceService.java @@ -1,5 +1,6 @@ package cn.lili.modules.promotion.service; +import cn.lili.modules.order.cart.entity.enums.CartTypeEnum; import cn.lili.modules.promotion.entity.dos.MemberCoupon; import cn.lili.modules.promotion.entity.dto.PromotionPriceDTO; import cn.lili.modules.promotion.entity.dto.PromotionPriceParamDTO; @@ -19,8 +20,9 @@ public interface PromotionPriceService { * * @param tradeSkuList 促销计算参数 * @param memberCouponList 使用的优惠券 + * @param cartTypeEnum 购物车类型 * @return 促销计算结果 */ - PromotionPriceDTO calculationPromotionPrice(List tradeSkuList, List memberCouponList); + PromotionPriceDTO calculationPromotionPrice(List tradeSkuList, List memberCouponList, CartTypeEnum cartTypeEnum); } diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/KanjiaActivityGoodsServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/KanjiaActivityGoodsServiceImpl.java index 6cb48438..1982b9ce 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/KanjiaActivityGoodsServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/KanjiaActivityGoodsServiceImpl.java @@ -315,17 +315,11 @@ public class KanjiaActivityGoodsServiceImpl extends ServiceImpl kanJiaActivityGoodsDTOS = this.mongoTemplate.find(query, KanjiaActivityGoodsDTO.class); - // 为了担心会查到多条数据 所以查询集合 正常情况下只会查询到一条 - if (kanJiaActivityGoodsDTOS.size() > 0) { - return kanJiaActivityGoodsDTOS.get(0); - } - return null; + //mongo查询条件 + Query query = new Query(); + query.addCriteria(Criteria.where("skuId").ne(skuId)) + .addCriteria(Criteria.where("promotionStatus").ne(PromotionStatusEnum.START.name())); + List kanjiaActivityGoodsDTOList=this.mongoTemplate.find(query, KanjiaActivityGoodsDTO.class); + return kanjiaActivityGoodsDTOList.get(0); } } \ No newline at end of file diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PointsGoodsServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PointsGoodsServiceImpl.java index b3a10458..38a4a78b 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PointsGoodsServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PointsGoodsServiceImpl.java @@ -203,20 +203,14 @@ public class PointsGoodsServiceImpl extends ServiceImpl queryWrapper = new LambdaQueryWrapper<>(); - queryWrapper.eq(PointsGoods::getSkuId, skuId).eq(PointsGoods::getPromotionStatus, PromotionStatusEnum.START.name()); - List list = this.list(queryWrapper); - if (list.size() == 1) { - return list.get(0); - } - return null; + public PointsGoodsVO getPointsGoodsVOByMongo(String skuId) { + //mongo查询条件 + Query query = new Query(); + query.addCriteria(Criteria.where("skuId").ne(skuId)) + .addCriteria(Criteria.where("promotionStatus").ne(PromotionStatusEnum.START.name())); + List pointsGoodsVO=this.mongoTemplate.find(query, PointsGoodsVO.class); + return pointsGoodsVO.get(0); } /** diff --git a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PromotionPriceServiceImpl.java b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PromotionPriceServiceImpl.java index f747e050..581ba6e7 100644 --- a/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PromotionPriceServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/promotion/serviceimpl/PromotionPriceServiceImpl.java @@ -1,15 +1,16 @@ package cn.lili.modules.promotion.serviceimpl; +import cn.hutool.core.convert.Convert; import cn.hutool.core.text.CharSequenceUtil; import cn.lili.common.utils.CurrencyUtil; import cn.lili.modules.goods.entity.dos.GoodsSku; import cn.lili.modules.goods.service.GoodsSkuService; +import cn.lili.modules.order.cart.entity.enums.CartTypeEnum; import cn.lili.modules.promotion.entity.dos.*; import cn.lili.modules.promotion.entity.dto.*; import cn.lili.modules.promotion.entity.enums.*; -import cn.lili.modules.promotion.service.PromotionGoodsService; -import cn.lili.modules.promotion.service.PromotionPriceService; -import cn.lili.modules.promotion.service.SeckillApplyService; +import cn.lili.modules.promotion.entity.vos.PointsGoodsVO; +import cn.lili.modules.promotion.service.*; import cn.lili.modules.search.entity.dos.EsGoodsIndex; import cn.lili.modules.search.service.EsGoodsSearchService; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; @@ -50,27 +51,114 @@ public class PromotionPriceServiceImpl implements PromotionPriceService { */ @Autowired private GoodsSkuService goodsSkuService; + /** + * 积分商品 + */ + @Autowired + private PointsGoodsService pointsGoodsService; + /** + * 积分商品 + */ + @Autowired + private KanjiaActivityGoodsService kanjiaActivityGoodsService; + @Override - public PromotionPriceDTO calculationPromotionPrice(List tradeSkuList, List memberCouponList) { + public PromotionPriceDTO calculationPromotionPrice(List tradeSkuList, List memberCouponList, CartTypeEnum cartTypeEnum) { + + List priceDTOList=new ArrayList<>(); - PromotionPriceDTO promotionPrice = new PromotionPriceDTO(); //拆分出SkuId列表 List skuIds = tradeSkuList.parallelStream().map(PromotionPriceParamDTO::getSkuId).collect(Collectors.toList()); //参与计算的ES商品SKU及其促销信息列表 List esGoodsSkus = goodsSearchService.getEsGoodsBySkuIds(skuIds); - //参与计算的缓存中的商品SKU列表 - List goodsSkus = goodsSkuService.getGoodsSkuByIdFromCache(skuIds); - //单品商品SKU促销计算结果列表 - List priceDTOList = this.packageGoodsSkuPromotionPrice(tradeSkuList, esGoodsSkus, goodsSkus); + + //判断交易类型,进行不同的处理 + if (cartTypeEnum.equals(CartTypeEnum.POINTS)) { + this.pointGoodsPromotion(tradeSkuList); + } else if (cartTypeEnum.equals(CartTypeEnum.KANJIA)) { + this.kanjiaPromotion(tradeSkuList); + } else { + //参与计算的缓存中的商品SKU列表 + List goodsSkus = goodsSkuService.getGoodsSkuByIdFromCache(skuIds); + + //单品商品SKU促销计算结果列表 + priceDTOList = this.packageGoodsSkuPromotionPrice(tradeSkuList, esGoodsSkus, goodsSkus); + } + + + //获取商品促销价格DTO列表 + List storePromotionPriceList = new ArrayList<>(); + + //计算优惠券金额 + Double couponTotalPrice = 0.0; //将使用的优惠券根据店铺分类 Map> couponCollect = memberCouponList.parallelStream().collect(Collectors.groupingBy(MemberCoupon::getStoreId)); + //获取店铺优惠券金额 + this.getStorePromotionPrice(storePromotionPriceList, priceDTOList, esGoodsSkus, couponCollect, couponTotalPrice); + //获取平台优惠金额 + this.getPlatformPromotionPrice(couponCollect, storePromotionPriceList, priceDTOList, couponTotalPrice); - double couponTotalPrice = 0; + //计算最终结算金额返回数据 + return this.getPromotionPriceDTO(storePromotionPriceList, couponTotalPrice); + } + /** + * 获取促销金额DTO + * + * @param storePromotionPriceList 店铺促销列表 + * @param couponTotalPrice 优惠券优惠金额 + * @return 促销金额DTO + */ + private PromotionPriceDTO getPromotionPriceDTO(List storePromotionPriceList, Double couponTotalPrice) { + + PromotionPriceDTO promotionPrice = new PromotionPriceDTO(); + promotionPrice.setStorePromotionPriceList(storePromotionPriceList); + promotionPrice.setTotalCouponPrice(couponTotalPrice); + promotionPrice.setTotalOriginPrice(storePromotionPriceList.parallelStream().mapToDouble(StorePromotionPriceDTO::getTotalOriginPrice).sum()); + promotionPrice.setTotalPoints(storePromotionPriceList.parallelStream().mapToDouble(StorePromotionPriceDTO::getTotalPoints).sum()); + promotionPrice.setTotalDiscountPrice(storePromotionPriceList.parallelStream().mapToDouble(StorePromotionPriceDTO::getTotalDiscountPrice).sum()); + //最终结算金额 = 商品原价格合计 - 总优惠价格合计 - 优惠券合计 + double totalFinalePrice = CurrencyUtil.sub(CurrencyUtil.sub(promotionPrice.getTotalOriginPrice(), promotionPrice.getTotalDiscountPrice()), promotionPrice.getTotalCouponPrice()); + promotionPrice.setTotalFinalePrice(totalFinalePrice); + + return promotionPrice; + } + + /** + * 获取平台优惠券促销金额 + * + * @param couponCollect 优惠券列表 + * @param storePromotionPriceList 店铺促销列表 + * @param priceDTOList 商品促销DTO列表 + * @param couponTotalPrice 优惠券金额合计 + */ + private void getPlatformPromotionPrice(Map> couponCollect, List storePromotionPriceList, List priceDTOList, Double couponTotalPrice) { + //获取平台优惠券 + List platformCoupons = couponCollect.get("platform"); + if (platformCoupons != null && !platformCoupons.isEmpty()) { + //计算平台优惠券活动 + couponTotalPrice = CurrencyUtil.add(couponTotalPrice, this.calculationCoupon(platformCoupons, priceDTOList)); + for (StorePromotionPriceDTO storePromotionPriceDTO : storePromotionPriceList) { + Double couponPrice = storePromotionPriceDTO.getGoodsSkuPromotionPriceList().parallelStream().mapToDouble(GoodsSkuPromotionPriceDTO::getCouponPrice).sum(); + storePromotionPriceDTO.setTotalCouponPrice(couponPrice); + } + } + } + + /** + * 获取店铺优惠券促销金额 + * + * @param storePromotionPriceList 店铺促销金额列表 + * @param priceDTOList 商品促销金额DTO列表 + * @param esGoodsSkus 商品索引SKU + * @param couponCollect 会员优惠券列表 + * @param couponTotalPrice 优惠券金额合计 + */ + private void getStorePromotionPrice(List storePromotionPriceList, List priceDTOList, + List esGoodsSkus, Map> couponCollect, Double couponTotalPrice) { //根据卖家分组商品信息 Map> storeCollect = priceDTOList.parallelStream().collect(Collectors.groupingBy(GoodsSkuPromotionPriceDTO::getStoreId)); - List storePromotionPriceList = new ArrayList<>(); for (Map.Entry> entry : storeCollect.entrySet()) { StorePromotionPriceDTO storePromotionPrice = new StorePromotionPriceDTO(); storePromotionPrice.setStoreId(entry.getKey()); @@ -110,25 +198,6 @@ public class PromotionPriceServiceImpl implements PromotionPriceService { storePromotionPriceList.add(storePromotionPrice); } - //获取平台优惠券 - List platformCoupons = couponCollect.get("platform"); - if (platformCoupons != null && !platformCoupons.isEmpty()) { - //计算平台优惠券活动 - couponTotalPrice = CurrencyUtil.add(couponTotalPrice, this.calculationCoupon(platformCoupons, priceDTOList)); - for (StorePromotionPriceDTO storePromotionPriceDTO : storePromotionPriceList) { - Double couponPrice = storePromotionPriceDTO.getGoodsSkuPromotionPriceList().parallelStream().mapToDouble(GoodsSkuPromotionPriceDTO::getCouponPrice).sum(); - storePromotionPriceDTO.setTotalCouponPrice(couponPrice); - } - } - promotionPrice.setStorePromotionPriceList(storePromotionPriceList); - promotionPrice.setTotalCouponPrice(couponTotalPrice); - promotionPrice.setTotalOriginPrice(storePromotionPriceList.parallelStream().mapToDouble(StorePromotionPriceDTO::getTotalOriginPrice).sum()); - promotionPrice.setTotalPoints(storePromotionPriceList.parallelStream().mapToDouble(StorePromotionPriceDTO::getTotalPoints).sum()); - promotionPrice.setTotalDiscountPrice(storePromotionPriceList.parallelStream().mapToDouble(StorePromotionPriceDTO::getTotalDiscountPrice).sum()); - //最终结算金额 = 商品原价格合计 - 总优惠价格合计 - 优惠券合计 - double totalFinalePrice = CurrencyUtil.sub(CurrencyUtil.sub(promotionPrice.getTotalOriginPrice(), promotionPrice.getTotalDiscountPrice()), promotionPrice.getTotalCouponPrice()); - promotionPrice.setTotalFinalePrice(totalFinalePrice); - return promotionPrice; } /** @@ -156,7 +225,6 @@ public class PromotionPriceServiceImpl implements PromotionPriceService { this.calculationPromotionMap(promotionMap, goodsSkuPromotionPrice, esGoodsIndex, promotionPriceParamDTO); goodsSkuPromotionPrice.setTotalOriginalPrice(CurrencyUtil.mul(goodsSkuPromotionPrice.getOriginalPrice(), goodsSkuPromotionPrice.getNumber())); - goodsSkuPromotionPrice.setTotalPoints(CurrencyUtil.mul(goodsSkuPromotionPrice.getPoints(), goodsSkuPromotionPrice.getNumber())); goodsSkuPromotionPrice.setTotalDiscountPrice(CurrencyUtil.mul(goodsSkuPromotionPrice.getDiscountPrice(), goodsSkuPromotionPrice.getNumber())); goodsSkuPromotionPrice.setTotalFinalePrice(CurrencyUtil.mul(goodsSkuPromotionPrice.getFinalePrice(), goodsSkuPromotionPrice.getNumber())); priceDTOList.add(goodsSkuPromotionPrice); @@ -165,13 +233,72 @@ public class PromotionPriceServiceImpl implements PromotionPriceService { return priceDTOList; } + + /** + * 计算积分商品 + * 积分商品的购买金额是:0 + * 1.根据SkuId去查询积分商品(Mongo) + * 2.计算积分商品的优惠信息 + * + * @param tradeSkuList 交易商品促销金额列表 + * @return 计算结果 + */ + private List pointGoodsPromotion(List tradeSkuList) { + List priceDTOList = new ArrayList<>(); + //获取积分商品SkuId + String skuId = tradeSkuList.get(0).getSkuId(); + //获取积分商品VO + PointsGoodsVO pointsGoodsVO = pointsGoodsService.getPointsGoodsVOByMongo(skuId); + //参与计算的缓存中的商品SKU列表 + GoodsSku goodsSku = pointsGoodsVO.getGoodsSku(); + //获取商品促销金额 + GoodsSkuPromotionPriceDTO goodsSkuPromotionPrice = new GoodsSkuPromotionPriceDTO(goodsSku, tradeSkuList.get(0).getNum()); + //计算商品原价=原价*数量 + goodsSkuPromotionPrice.setTotalOriginalPrice(CurrencyUtil.mul(goodsSkuPromotionPrice.getOriginalPrice(), goodsSkuPromotionPrice.getNumber())); + //计算商品积分数量=兑换积分*数量 + goodsSkuPromotionPrice.setTotalPoints(pointsGoodsVO.getPoints() * Convert.toLong(goodsSkuPromotionPrice.getNumber())); + //优惠金额=商品原价*数量 + goodsSkuPromotionPrice.setTotalDiscountPrice(CurrencyUtil.mul(goodsSkuPromotionPrice.getOriginalPrice(), goodsSkuPromotionPrice.getNumber())); + //购买价格=积分商品价格为 0 + goodsSkuPromotionPrice.setTotalFinalePrice(0.0); + priceDTOList.add(goodsSkuPromotionPrice); + return priceDTOList; + } + + /** + * 计算砍价商品 + * 砍价商品只能购买一件 + * 1.根据SkuId去查询积分商品(Mongo) + * 2.计算积分商品的优惠信息 + * + * @param tradeSkuList 交易商品促销金额列表 + * @return 计算结果 + */ + private List kanjiaPromotion(List tradeSkuList) { + List priceDTOList = new ArrayList<>(); + //获取积分商品SkuId + String skuId = tradeSkuList.get(0).getSkuId(); + //获取积分商品VO + KanjiaActivityGoodsDTO kanjiaActivityGoodsDTO = kanjiaActivityGoodsService.getKanJiaGoodsBySku(skuId); + + //参与计算的缓存中的商品SKU列表 + GoodsSku goodsSku = kanjiaActivityGoodsDTO.getGoodsSku(); + GoodsSkuPromotionPriceDTO goodsSkuPromotionPrice = new GoodsSkuPromotionPriceDTO(goodsSku, tradeSkuList.get(0).getNum()); + //优惠金额=商品原价-购买价格 + goodsSkuPromotionPrice.setTotalDiscountPrice(CurrencyUtil.sub(goodsSkuPromotionPrice.getOriginalPrice(), kanjiaActivityGoodsDTO.getPurchasePrice())); + //购买价格=砍价成交金额 + goodsSkuPromotionPrice.setTotalFinalePrice(kanjiaActivityGoodsDTO.getPurchasePrice()); + priceDTOList.add(goodsSkuPromotionPrice); + return priceDTOList; + } + /** * 促销计算(秒杀活动、拼团、满优惠活动、砍价) * * @param promotionMap 当前商品所有参加的促销活动 * @param goodsSkuPromotionPrice 商品SKU促销计算信息 * @param esGoodsIndex ES商品信息 - * @param promotionPriceParamDTO 拼团id,标示是否计算拼团活动 + * @param promotionPriceParamDTO 拼团id,标示是否计算拼团活动 */ private void calculationPromotionMap(Map promotionMap, GoodsSkuPromotionPriceDTO goodsSkuPromotionPrice, EsGoodsIndex esGoodsIndex, PromotionPriceParamDTO promotionPriceParamDTO) { //未参加促销则直接返回