Commit d26ef6b89e2c3748360a92255ffbee61822c6429
1 parent
806c828b
【代码优化】商城: TradeRewardActivityPriceCalculatorTest
Showing
3 changed files
with
23 additions
and
11 deletions
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeRewardActivityPriceCalculator.java
| ... | ... | @@ -93,7 +93,7 @@ public class TradeRewardActivityPriceCalculator implements TradePriceCalculator |
| 93 | 93 | TradePriceCalculatorHelper.recountAllPrice(result); |
| 94 | 94 | |
| 95 | 95 | // 4.1 记录赠送的积分 |
| 96 | - if (rule.getGivePoint()) { | |
| 96 | + if (Boolean.TRUE.equals(rule.getGivePoint())) { | |
| 97 | 97 | List<Integer> dividePoints = TradePriceCalculatorHelper.dividePrice(orderItems, rule.getPoint()); |
| 98 | 98 | for (int i = 0; i < orderItems.size(); i++) { |
| 99 | 99 | // 商品可能赠送了积分,所以这里要加上 |
| ... | ... | @@ -102,12 +102,12 @@ public class TradeRewardActivityPriceCalculator implements TradePriceCalculator |
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | 104 | // 4.2 记录订单是否包邮 |
| 105 | - if (rule.getFreeDelivery()) { | |
| 105 | + if (Boolean.TRUE.equals(rule.getFreeDelivery())) { | |
| 106 | 106 | // 只要满足一个活动包邮那么这单就包邮 |
| 107 | 107 | result.setFreeDelivery(true); |
| 108 | 108 | } |
| 109 | 109 | // 4.3 记录赠送的优惠券 |
| 110 | - if (rule.getGiveCoupon()) { | |
| 110 | + if (Boolean.TRUE.equals(rule.getGiveCoupon())) { | |
| 111 | 111 | for (Map.Entry<Long, Integer> entry : rule.getGiveCouponsMap().entrySet()) { |
| 112 | 112 | Map<Long, Integer> giveCouponsMap = result.getGiveCouponsMap(); |
| 113 | 113 | if (giveCouponsMap.get(entry.getKey()) == null) { // 情况一:还没有赠送的优惠券 | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeRewardActivityPriceCalculatorTest.java
| ... | ... | @@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 4 | 4 | import cn.iocoder.yudao.module.promotion.api.reward.RewardActivityApi; |
| 5 | 5 | import cn.iocoder.yudao.module.promotion.api.reward.dto.RewardActivityMatchRespDTO; |
| 6 | 6 | import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum; |
| 7 | +import cn.iocoder.yudao.module.promotion.enums.common.PromotionProductScopeEnum; | |
| 7 | 8 | import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum; |
| 8 | 9 | import cn.iocoder.yudao.module.trade.enums.order.TradeOrderTypeEnum; |
| 9 | 10 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; |
| ... | ... | @@ -13,6 +14,7 @@ import org.mockito.InjectMocks; |
| 13 | 14 | import org.mockito.Mock; |
| 14 | 15 | |
| 15 | 16 | import java.util.ArrayList; |
| 17 | +import java.util.LinkedHashMap; | |
| 16 | 18 | |
| 17 | 19 | import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; |
| 18 | 20 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| ... | ... | @@ -47,7 +49,7 @@ public class TradeRewardActivityPriceCalculatorTest extends BaseMockitoUnitTest |
| 47 | 49 | TradePriceCalculateRespBO result = new TradePriceCalculateRespBO() |
| 48 | 50 | .setType(TradeOrderTypeEnum.NORMAL.getType()) |
| 49 | 51 | .setPrice(new TradePriceCalculateRespBO.Price()) |
| 50 | - .setPromotions(new ArrayList<>()) | |
| 52 | + .setPromotions(new ArrayList<>()).setGiveCouponsMap(new LinkedHashMap<>()) | |
| 51 | 53 | .setItems(asList( |
| 52 | 54 | new TradePriceCalculateRespBO.OrderItem().setSkuId(10L).setCount(2).setSelected(true) |
| 53 | 55 | .setPrice(100).setSpuId(1L), |
| ... | ... | @@ -60,16 +62,22 @@ public class TradeRewardActivityPriceCalculatorTest extends BaseMockitoUnitTest |
| 60 | 62 | TradePriceCalculatorHelper.recountPayPrice(result.getItems()); |
| 61 | 63 | TradePriceCalculatorHelper.recountAllPrice(result); |
| 62 | 64 | |
| 63 | - // mock 方法(限时折扣 DiscountActivity 信息) | |
| 65 | + // mock 方法(满减送 RewardActivity 信息) | |
| 64 | 66 | when(rewardActivityApi.getMatchRewardActivityList(eq(asSet(1L, 2L, 3L)))).thenReturn(asList( |
| 65 | 67 | randomPojo(RewardActivityMatchRespDTO.class, o -> o.setId(1000L).setName("活动 1000 号") |
| 66 | - .setProductScopeValues(asList(1L, 2L)).setConditionType(PromotionConditionTypeEnum.PRICE.getType()) | |
| 67 | - .setRules(singletonList(new RewardActivityMatchRespDTO.Rule().setLimit(200).setDiscountPrice(70)))), | |
| 68 | + .setConditionType(PromotionConditionTypeEnum.PRICE.getType()) | |
| 69 | + .setProductScope(PromotionProductScopeEnum.SPU.getScope()).setProductScopeValues(asList(1L, 2L)) | |
| 70 | + .setRules(singletonList(new RewardActivityMatchRespDTO.Rule().setLimit(20).setDiscountPrice(70) | |
| 71 | + .setGivePoint(false).setFreeDelivery(false)))), | |
| 68 | 72 | randomPojo(RewardActivityMatchRespDTO.class, o -> o.setId(2000L).setName("活动 2000 号") |
| 69 | - .setProductScopeValues(singletonList(3L)).setConditionType(PromotionConditionTypeEnum.COUNT.getType()) | |
| 70 | - .setRules(asList(new RewardActivityMatchRespDTO.Rule().setLimit(1).setDiscountPrice(10), | |
| 71 | - new RewardActivityMatchRespDTO.Rule().setLimit(2).setDiscountPrice(60), // 最大可满足,因为是 4 个 | |
| 72 | - new RewardActivityMatchRespDTO.Rule().setLimit(10).setDiscountPrice(100)))) | |
| 73 | + .setConditionType(PromotionConditionTypeEnum.COUNT.getType()) | |
| 74 | + .setProductScope(PromotionProductScopeEnum.SPU.getScope()).setProductScopeValues(singletonList(3L)) | |
| 75 | + .setRules(asList(new RewardActivityMatchRespDTO.Rule().setLimit(1).setDiscountPrice(10) | |
| 76 | + .setGivePoint(true).setPoint(50).setFreeDelivery(false), | |
| 77 | + new RewardActivityMatchRespDTO.Rule().setLimit(2).setDiscountPrice(60).setGivePoint(true) | |
| 78 | + .setPoint(100).setFreeDelivery(false), // 最大可满足,因为是 4 个 | |
| 79 | + new RewardActivityMatchRespDTO.Rule().setLimit(10).setDiscountPrice(100) | |
| 80 | + .setGivePoint(false).setFreeDelivery(false)))) | |
| 73 | 81 | )); |
| 74 | 82 | |
| 75 | 83 | // 调用 |
| ... | ... | @@ -94,6 +102,7 @@ public class TradeRewardActivityPriceCalculatorTest extends BaseMockitoUnitTest |
| 94 | 102 | assertEquals(orderItem01.getCouponPrice(), 0); |
| 95 | 103 | assertEquals(orderItem01.getPointPrice(), 0); |
| 96 | 104 | assertEquals(orderItem01.getPayPrice(), 160); |
| 105 | + assertEquals(orderItem01.getGivePoint(), 0); | |
| 97 | 106 | // 断言:SKU 2 |
| 98 | 107 | TradePriceCalculateRespBO.OrderItem orderItem02 = result.getItems().get(1); |
| 99 | 108 | assertEquals(orderItem02.getSkuId(), 20L); |
| ... | ... | @@ -104,6 +113,7 @@ public class TradeRewardActivityPriceCalculatorTest extends BaseMockitoUnitTest |
| 104 | 113 | assertEquals(orderItem02.getCouponPrice(), 0); |
| 105 | 114 | assertEquals(orderItem02.getPointPrice(), 0); |
| 106 | 115 | assertEquals(orderItem02.getPayPrice(), 120); |
| 116 | + assertEquals(orderItem02.getGivePoint(), 0); | |
| 107 | 117 | // 断言:SKU 3 |
| 108 | 118 | TradePriceCalculateRespBO.OrderItem orderItem03 = result.getItems().get(2); |
| 109 | 119 | assertEquals(orderItem03.getSkuId(), 30L); |
| ... | ... | @@ -114,6 +124,7 @@ public class TradeRewardActivityPriceCalculatorTest extends BaseMockitoUnitTest |
| 114 | 124 | assertEquals(orderItem03.getCouponPrice(), 0); |
| 115 | 125 | assertEquals(orderItem03.getPointPrice(), 0); |
| 116 | 126 | assertEquals(orderItem03.getPayPrice(), 60); |
| 127 | + assertEquals(orderItem03.getGivePoint(), 100); | |
| 117 | 128 | // 断言:Promotion 部分(第一个) |
| 118 | 129 | assertEquals(result.getPromotions().size(), 2); |
| 119 | 130 | TradePriceCalculateRespBO.Promotion promotion01 = result.getPromotions().get(0); | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/test/resources/sql/create_tables.sql
| ... | ... | @@ -48,6 +48,7 @@ CREATE TABLE IF NOT EXISTS "trade_order" |
| 48 | 48 | "give_point" int NULL, |
| 49 | 49 | "refund_point" int NULL, |
| 50 | 50 | "vip_price" int NULL, |
| 51 | + "give_coupons_map" varchar NULL, | |
| 51 | 52 | "seckill_activity_id" long NULL, |
| 52 | 53 | "bargain_activity_id" long NULL, |
| 53 | 54 | "bargain_record_id" long NULL, | ... | ... |