From 4e9fe4bc547fa8d8f9172b5e68b482fba74bd1c8 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 29 Oct 2022 22:23:48 +0800 Subject: [PATCH] =?UTF-8?q?price=EF=BC=9A=E5=AE=8C=E6=88=90=E9=99=90?= =?UTF-8?q?=E6=97=B6=E6=8A=98=E6=89=A3=E7=9A=84=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 --- .../discount/DiscountProductDO.java | 6 ++ .../service/price/PriceServiceImpl.java | 28 +++--- .../service/price/PriceServiceTest.java | 88 ++++++++++++++++++- 3 files changed, 107 insertions(+), 15 deletions(-) diff --git a/yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/dal/dataobject/discount/DiscountProductDO.java b/yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/dal/dataobject/discount/DiscountProductDO.java index cd73a19a5..42d4f85cb 100644 --- a/yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/dal/dataobject/discount/DiscountProductDO.java +++ b/yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/dal/dataobject/discount/DiscountProductDO.java @@ -31,6 +31,12 @@ public class DiscountProductDO extends BaseDO { * 关联 {@link DiscountActivityDO#getId()} */ private Long activityId; + /** + * 限时折扣活动的名字 + * + * 冗余 {@link DiscountActivityDO#getName()} + */ + private String activityName; /** * 商品 SPU 编号 * diff --git a/yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/service/price/PriceServiceImpl.java b/yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/service/price/PriceServiceImpl.java index 6b9d60854..7157e5044 100644 --- a/yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/service/price/PriceServiceImpl.java +++ b/yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/service/price/PriceServiceImpl.java @@ -107,33 +107,35 @@ public class PriceServiceImpl implements PriceService { Integer memberPrice = memberDiscount != null ? (int) (orderItem.getPayPrice() * memberDiscount / 100) : null; Integer promotionPrice = discountProduct != null ? discountProduct.getPromotionPrice() * orderItem.getCount() : null; if (memberPrice == null) { - + calculatePriceByDiscountActivity(priceCalculate, orderItem, discountProduct, promotionPrice); } else if (promotionPrice == null) { - calculatePriceByMemberDiscount(orderItem, memberDiscount, memberPrice, priceCalculate); + calculatePriceByMemberDiscount(priceCalculate, orderItem, memberPrice); } else if (memberPrice < promotionPrice) { - + calculatePriceByDiscountActivity(priceCalculate, orderItem, discountProduct, promotionPrice); } else { - calculatePriceByMemberDiscount(orderItem, memberDiscount, memberPrice, priceCalculate); + calculatePriceByMemberDiscount(priceCalculate, orderItem, memberPrice); } }); } - private void calculatePriceByMemberDiscount(PriceCalculateRespDTO.OrderItem orderItem, - Double memberDiscount, Integer memberPrice, - PriceCalculateRespDTO priceCalculate) { + private void calculatePriceByMemberDiscount(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem, + Integer memberPrice) { // 记录优惠明细 - addPromotion(priceCalculate, orderItem, null, - PromotionTypeEnum.MEMBER.getName(), + addPromotion(priceCalculate, orderItem, null, PromotionTypeEnum.MEMBER.getName(), PromotionTypeEnum.MEMBER.getType(), PromotionLevelEnum.SKU.getLevel(), memberPrice, true, StrUtil.format("会员折扣:省 {} 元", formatPrice(orderItem.getPayPrice() - memberPrice))); // 修改 SKU 的优惠 modifyOrderItemPayPrice(orderItem, memberPrice, priceCalculate); } - private void calculatePriceByDiscountActivity(PriceCalculateRespDTO.OrderItem orderItem, - DiscountProductDO discountProduct, Integer promotionPrice, - PriceCalculateRespDTO priceCalculate) { - + private void calculatePriceByDiscountActivity(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem, + DiscountProductDO discountProduct, Integer promotionPrice) { + // 记录优惠明细 + addPromotion(priceCalculate, orderItem, discountProduct.getActivityId(), discountProduct.getActivityName(), + PromotionTypeEnum.DISCOUNT_ACTIVITY.getType(), PromotionLevelEnum.SKU.getLevel(), promotionPrice, + true, StrUtil.format("限时折扣:省 {} 元", formatPrice(orderItem.getPayPrice() - promotionPrice))); + // 修改 SKU 的优惠 + modifyOrderItemPayPrice(orderItem, promotionPrice, priceCalculate); } private void addPromotion(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem, diff --git a/yudao-module-mall/yudao-module-market-biz/src/test/java/cn/iocoder/yudao/module/market/service/price/PriceServiceTest.java b/yudao-module-mall/yudao-module-market-biz/src/test/java/cn/iocoder/yudao/module/market/service/price/PriceServiceTest.java index b6a92bb40..5fdfe82a9 100644 --- a/yudao-module-mall/yudao-module-market-biz/src/test/java/cn/iocoder/yudao/module/market/service/price/PriceServiceTest.java +++ b/yudao-module-mall/yudao-module-market-biz/src/test/java/cn/iocoder/yudao/module/market/service/price/PriceServiceTest.java @@ -1,9 +1,11 @@ package cn.iocoder.yudao.module.market.service.price; +import cn.hutool.core.map.MapUtil; import cn.iocoder.yudao.framework.common.util.collection.SetUtils; import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateReqDTO; import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateRespDTO; +import cn.iocoder.yudao.module.market.dal.dataobject.discount.DiscountProductDO; import cn.iocoder.yudao.module.market.enums.common.PromotionLevelEnum; import cn.iocoder.yudao.module.market.enums.common.PromotionTypeEnum; import cn.iocoder.yudao.module.market.service.discount.DiscountService; @@ -13,7 +15,9 @@ import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; import org.mockito.Mock; +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; +import static java.util.Arrays.asList; import static java.util.Collections.singletonList; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.eq; @@ -55,8 +59,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest { assertEquals(order.getPayPrice(), 180); assertNull(order.getCouponId()); // 断言 OrderItem 部分 - PriceCalculateRespDTO.OrderItem orderItem = order.getItems().get(0); assertEquals(order.getItems().size(), 1); + PriceCalculateRespDTO.OrderItem orderItem = order.getItems().get(0); assertEquals(orderItem.getSkuId(), 10L); assertEquals(orderItem.getCount(), 2); assertEquals(orderItem.getOriginalPrice(), 200); @@ -66,8 +70,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest { assertEquals(orderItem.getOrderPartPrice(), 0); assertEquals(orderItem.getOrderDividePrice(), 180); // 断言 Promotion 部分 - PriceCalculateRespDTO.Promotion promotion = priceCalculate.getPromotions().get(0); assertEquals(priceCalculate.getPromotions().size(), 1); + PriceCalculateRespDTO.Promotion promotion = priceCalculate.getPromotions().get(0); assertNull(promotion.getId()); assertEquals(promotion.getName(), "会员折扣"); assertEquals(promotion.getType(), PromotionTypeEnum.MEMBER.getType()); @@ -83,4 +87,84 @@ public class PriceServiceTest extends BaseMockitoUnitTest { assertEquals(promotionItem.getDiscountPrice(), 20); } + @Test + public void testCalculatePrice_discountActivity() { + // 准备参数 + PriceCalculateReqDTO calculateReqDTO = new PriceCalculateReqDTO().setUserId(randomLongId()) + .setItems(asList(new PriceCalculateReqDTO.Item().setSkuId(10L).setCount(2), + new PriceCalculateReqDTO.Item().setSkuId(20L).setCount(3))); + // mock 方法(商品 SKU 信息) + ProductSkuRespDTO productSku01 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100)); + ProductSkuRespDTO productSku02 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(20L).setPrice(50)); + when(productSkuApi.getSkuList(eq(SetUtils.asSet(10L, 20L)))).thenReturn(asList(productSku01, productSku02)); + // mock 方法(限时折扣 DiscountActivity 信息) + DiscountProductDO discountProduct01 = randomPojo(DiscountProductDO.class, o -> o.setActivityId(1000L).setActivityName("活动 1000 号") + .setSkuId(10L).setPromotionPrice(80)); + DiscountProductDO discountProduct02 = randomPojo(DiscountProductDO.class, o -> o.setActivityId(2000L).setActivityName("活动 2000 号") + .setSkuId(20L).setPromotionPrice(40)); + when(discountService.getMatchDiscountProducts(eq(SetUtils.asSet(10L, 20L)))).thenReturn( + MapUtil.builder(10L, discountProduct01).put(20L, discountProduct02).map()); + + // 调用 + PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO); + // 断言 Order 部分 + PriceCalculateRespDTO.Order order = priceCalculate.getOrder(); + assertEquals(order.getOriginalPrice(), 350); + assertEquals(order.getDiscountPrice(), 0); + assertEquals(order.getPointPrice(), 0); + assertEquals(order.getDeliveryPrice(), 0); + assertEquals(order.getPayPrice(), 280); + assertNull(order.getCouponId()); + // 断言 OrderItem 部分 + assertEquals(order.getItems().size(), 2); + PriceCalculateRespDTO.OrderItem orderItem01 = order.getItems().get(0); + assertEquals(orderItem01.getSkuId(), 10L); + assertEquals(orderItem01.getCount(), 2); + assertEquals(orderItem01.getOriginalPrice(), 200); + assertEquals(orderItem01.getOriginalUnitPrice(), 100); + assertEquals(orderItem01.getDiscountPrice(), 40); + assertEquals(orderItem01.getPayPrice(), 160); + assertEquals(orderItem01.getOrderPartPrice(), 0); + assertEquals(orderItem01.getOrderDividePrice(), 160); + PriceCalculateRespDTO.OrderItem orderItem02 = order.getItems().get(1); + assertEquals(orderItem02.getSkuId(), 20L); + assertEquals(orderItem02.getCount(), 3); + assertEquals(orderItem02.getOriginalPrice(), 150); + assertEquals(orderItem02.getOriginalUnitPrice(), 50); + assertEquals(orderItem02.getDiscountPrice(), 30); + assertEquals(orderItem02.getPayPrice(), 120); + assertEquals(orderItem02.getOrderPartPrice(), 0); + assertEquals(orderItem02.getOrderDividePrice(), 120); + // 断言 Promotion 部分 + assertEquals(priceCalculate.getPromotions().size(), 2); + PriceCalculateRespDTO.Promotion promotion01 = priceCalculate.getPromotions().get(0); + assertEquals(promotion01.getId(), 1000L); + assertEquals(promotion01.getName(), "活动 1000 号"); + assertEquals(promotion01.getType(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType()); + assertEquals(promotion01.getLevel(), PromotionLevelEnum.SKU.getLevel()); + assertEquals(promotion01.getOriginalPrice(), 200); + assertEquals(promotion01.getDiscountPrice(), 40); + assertTrue(promotion01.getMeet()); + assertEquals(promotion01.getMeetTip(), "限时折扣:省 0.40 元"); + PriceCalculateRespDTO.PromotionItem promotionItem01 = promotion01.getItems().get(0); + assertEquals(promotion01.getItems().size(), 1); + assertEquals(promotionItem01.getSkuId(), 10L); + assertEquals(promotionItem01.getOriginalPrice(), 200); + assertEquals(promotionItem01.getDiscountPrice(), 40); + PriceCalculateRespDTO.Promotion promotion02 = priceCalculate.getPromotions().get(1); + assertEquals(promotion02.getId(), 2000L); + assertEquals(promotion02.getName(), "活动 2000 号"); + assertEquals(promotion02.getType(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType()); + assertEquals(promotion02.getLevel(), PromotionLevelEnum.SKU.getLevel()); + assertEquals(promotion02.getOriginalPrice(), 150); + assertEquals(promotion02.getDiscountPrice(), 30); + assertTrue(promotion02.getMeet()); + assertEquals(promotion02.getMeetTip(), "限时折扣:省 0.30 元"); + PriceCalculateRespDTO.PromotionItem promotionItem02 = promotion02.getItems().get(0); + assertEquals(promotion02.getItems().size(), 1); + assertEquals(promotionItem02.getSkuId(), 20L); + assertEquals(promotionItem02.getOriginalPrice(), 150); + assertEquals(promotionItem02.getDiscountPrice(), 30); + } + }