Commit 160d619d5902d1ce482eff5fa0db639213095137
1 parent
4e9fe4bc
price:完成满减送的价格计算~
Showing
7 changed files
with
478 additions
and
21 deletions
yudao-module-mall/yudao-module-market-api/src/main/java/cn/iocoder/yudao/module/market/api/price/dto/PriceCalculateRespDTO.java
| @@ -104,6 +104,10 @@ public class PriceCalculateRespDTO { | @@ -104,6 +104,10 @@ public class PriceCalculateRespDTO { | ||
| 104 | public static class OrderItem { | 104 | public static class OrderItem { |
| 105 | 105 | ||
| 106 | /** | 106 | /** |
| 107 | + * SPU 编号 | ||
| 108 | + */ | ||
| 109 | + private Long spuId; | ||
| 110 | + /** | ||
| 107 | * SKU 编号 | 111 | * SKU 编号 |
| 108 | */ | 112 | */ |
| 109 | private Long skuId; | 113 | private Long skuId; |
yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/convert/price/PriceConvert.java
| @@ -30,9 +30,10 @@ public interface PriceConvert { | @@ -30,9 +30,10 @@ public interface PriceConvert { | ||
| 30 | skuList.forEach(sku -> { | 30 | skuList.forEach(sku -> { |
| 31 | Integer count = skuIdCountMap.get(sku.getId()); | 31 | Integer count = skuIdCountMap.get(sku.getId()); |
| 32 | PriceCalculateRespDTO.OrderItem orderItem = new PriceCalculateRespDTO.OrderItem() | 32 | PriceCalculateRespDTO.OrderItem orderItem = new PriceCalculateRespDTO.OrderItem() |
| 33 | - .setSkuId(sku.getId()).setCount(count).setOriginalUnitPrice(sku.getPrice()) | ||
| 34 | - .setOriginalPrice(sku.getPrice() * count).setDiscountPrice(0).setOrderPartPrice(0); | ||
| 35 | - orderItem.setPayPrice(orderItem.getOriginalPrice()).setOrderDividePrice(orderItem.getOrderDividePrice()); | 33 | + .setSpuId(sku.getSpuId()).setSkuId(sku.getId()).setCount(count) |
| 34 | + .setOriginalUnitPrice(sku.getPrice()).setOriginalPrice(sku.getPrice() * count) | ||
| 35 | + .setDiscountPrice(0).setOrderPartPrice(0); | ||
| 36 | + orderItem.setPayPrice(orderItem.getOriginalPrice()).setOrderDividePrice(orderItem.getOriginalPrice()); | ||
| 36 | priceCalculate.getOrder().getItems().add(orderItem); | 37 | priceCalculate.getOrder().getItems().add(orderItem); |
| 37 | // 补充价格信息到 Order 中 | 38 | // 补充价格信息到 Order 中 |
| 38 | order.setOriginalPrice(order.getOriginalPrice() + orderItem.getOriginalPrice()).setPayPrice(order.getOriginalPrice()); | 39 | order.setOriginalPrice(order.getOriginalPrice() + orderItem.getOriginalPrice()).setPayPrice(order.getOriginalPrice()); |
yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/dal/dataobject/reward/RewardActivityDO.java
| @@ -92,7 +92,7 @@ public class RewardActivityDO extends BaseDO { | @@ -92,7 +92,7 @@ public class RewardActivityDO extends BaseDO { | ||
| 92 | /** | 92 | /** |
| 93 | * 优惠价格,单位:分 | 93 | * 优惠价格,单位:分 |
| 94 | */ | 94 | */ |
| 95 | - private Integer promotionPrice; | 95 | + private Integer discountPrice; |
| 96 | /** | 96 | /** |
| 97 | * 是否包邮 | 97 | * 是否包邮 |
| 98 | */ | 98 | */ |
| @@ -100,7 +100,7 @@ public class RewardActivityDO extends BaseDO { | @@ -100,7 +100,7 @@ public class RewardActivityDO extends BaseDO { | ||
| 100 | /** | 100 | /** |
| 101 | * 赠送的积分 | 101 | * 赠送的积分 |
| 102 | */ | 102 | */ |
| 103 | - private Integer integral; | 103 | + private Integer point; |
| 104 | /** | 104 | /** |
| 105 | * 赠送的优惠劵编号的数组 | 105 | * 赠送的优惠劵编号的数组 |
| 106 | */ | 106 | */ |
yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/service/price/PriceServiceImpl.java
| 1 | package cn.iocoder.yudao.module.market.service.price; | 1 | package cn.iocoder.yudao.module.market.service.price; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 4 | +import cn.hutool.core.lang.Assert; | ||
| 3 | import cn.hutool.core.util.StrUtil; | 5 | import cn.hutool.core.util.StrUtil; |
| 4 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 6 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 5 | import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateReqDTO; | 7 | import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateReqDTO; |
| 6 | import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateRespDTO; | 8 | import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateRespDTO; |
| 7 | import cn.iocoder.yudao.module.market.convert.price.PriceConvert; | 9 | import cn.iocoder.yudao.module.market.convert.price.PriceConvert; |
| 8 | import cn.iocoder.yudao.module.market.dal.dataobject.discount.DiscountProductDO; | 10 | import cn.iocoder.yudao.module.market.dal.dataobject.discount.DiscountProductDO; |
| 11 | +import cn.iocoder.yudao.module.market.dal.dataobject.reward.RewardActivityDO; | ||
| 12 | +import cn.iocoder.yudao.module.market.enums.common.PromotionConditionTypeEnum; | ||
| 9 | import cn.iocoder.yudao.module.market.enums.common.PromotionLevelEnum; | 13 | import cn.iocoder.yudao.module.market.enums.common.PromotionLevelEnum; |
| 10 | import cn.iocoder.yudao.module.market.enums.common.PromotionTypeEnum; | 14 | import cn.iocoder.yudao.module.market.enums.common.PromotionTypeEnum; |
| 11 | import cn.iocoder.yudao.module.market.service.discount.DiscountService; | 15 | import cn.iocoder.yudao.module.market.service.discount.DiscountService; |
| 16 | +import cn.iocoder.yudao.module.market.service.reward.RewardService; | ||
| 12 | import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; | 17 | import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; |
| 13 | import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; | 18 | import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; |
| 14 | import com.google.common.base.Suppliers; | 19 | import com.google.common.base.Suppliers; |
| @@ -16,12 +21,15 @@ import org.springframework.stereotype.Service; | @@ -16,12 +21,15 @@ import org.springframework.stereotype.Service; | ||
| 16 | import org.springframework.validation.annotation.Validated; | 21 | import org.springframework.validation.annotation.Validated; |
| 17 | 22 | ||
| 18 | import javax.annotation.Resource; | 23 | import javax.annotation.Resource; |
| 24 | +import java.util.ArrayList; | ||
| 19 | import java.util.List; | 25 | import java.util.List; |
| 20 | import java.util.Map; | 26 | import java.util.Map; |
| 27 | +import java.util.Set; | ||
| 21 | import java.util.function.Supplier; | 28 | import java.util.function.Supplier; |
| 22 | 29 | ||
| 23 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 30 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 24 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; | 31 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; |
| 32 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getSumValue; | ||
| 25 | import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_NOT_EXISTS; | 33 | import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_NOT_EXISTS; |
| 26 | import static java.util.Collections.singletonList; | 34 | import static java.util.Collections.singletonList; |
| 27 | 35 | ||
| @@ -32,6 +40,11 @@ import static java.util.Collections.singletonList; | @@ -32,6 +40,11 @@ import static java.util.Collections.singletonList; | ||
| 32 | * 参考文档: | 40 | * 参考文档: |
| 33 | * 1. <a href="https://help.youzan.com/displaylist/detail_4_4-1-60384">有赞文档:限时折扣、满减送、优惠券哪个优先计算?</a> | 41 | * 1. <a href="https://help.youzan.com/displaylist/detail_4_4-1-60384">有赞文档:限时折扣、满减送、优惠券哪个优先计算?</a> |
| 34 | * | 42 | * |
| 43 | + * TODO 芋艿:进一步完善 | ||
| 44 | + * 1. 限时折扣:指定金额、减免金额、折扣 | ||
| 45 | + * 2. 满减送:循环、折扣 | ||
| 46 | + * 3. | ||
| 47 | + * | ||
| 35 | * @author 芋道源码 | 48 | * @author 芋道源码 |
| 36 | */ | 49 | */ |
| 37 | @Service | 50 | @Service |
| @@ -40,6 +53,8 @@ public class PriceServiceImpl implements PriceService { | @@ -40,6 +53,8 @@ public class PriceServiceImpl implements PriceService { | ||
| 40 | 53 | ||
| 41 | @Resource | 54 | @Resource |
| 42 | private DiscountService discountService; | 55 | private DiscountService discountService; |
| 56 | + @Resource | ||
| 57 | + private RewardService rewardService; | ||
| 43 | 58 | ||
| 44 | @Resource | 59 | @Resource |
| 45 | private ProductSkuApi productSkuApi; | 60 | private ProductSkuApi productSkuApi; |
| @@ -53,7 +68,8 @@ public class PriceServiceImpl implements PriceService { | @@ -53,7 +68,8 @@ public class PriceServiceImpl implements PriceService { | ||
| 53 | 68 | ||
| 54 | // 计算商品级别的价格 | 69 | // 计算商品级别的价格 |
| 55 | calculatePriceForSkuLevel(calculateReqDTO.getUserId(), priceCalculate); | 70 | calculatePriceForSkuLevel(calculateReqDTO.getUserId(), priceCalculate); |
| 56 | - // 计算【满减送】促销 TODO 待实现 | 71 | + // 计算订单级别的价格 |
| 72 | + calculatePriceForOrderLevel(calculateReqDTO.getUserId(), priceCalculate); | ||
| 57 | // 计算【优惠劵】促销 TODO 待实现 | 73 | // 计算【优惠劵】促销 TODO 待实现 |
| 58 | return priceCalculate; | 74 | return priceCalculate; |
| 59 | } | 75 | } |
| @@ -75,10 +91,12 @@ public class PriceServiceImpl implements PriceService { | @@ -75,10 +91,12 @@ public class PriceServiceImpl implements PriceService { | ||
| 75 | return skus; | 91 | return skus; |
| 76 | } | 92 | } |
| 77 | 93 | ||
| 94 | + // ========== 计算商品级别的价格 ========== | ||
| 95 | + | ||
| 78 | /** | 96 | /** |
| 79 | * 计算商品级别的价格,例如说: | 97 | * 计算商品级别的价格,例如说: |
| 80 | * 1. 会员折扣 | 98 | * 1. 会员折扣 |
| 81 | - * 2. 限时折扣 | 99 | + * 2. 限时折扣 {@link cn.iocoder.yudao.module.market.dal.dataobject.discount.DiscountActivityDO} |
| 82 | * | 100 | * |
| 83 | * 其中,会员折扣、限时折扣取最低价 | 101 | * 其中,会员折扣、限时折扣取最低价 |
| 84 | * | 102 | * |
| @@ -138,10 +156,126 @@ public class PriceServiceImpl implements PriceService { | @@ -138,10 +156,126 @@ public class PriceServiceImpl implements PriceService { | ||
| 138 | modifyOrderItemPayPrice(orderItem, promotionPrice, priceCalculate); | 156 | modifyOrderItemPayPrice(orderItem, promotionPrice, priceCalculate); |
| 139 | } | 157 | } |
| 140 | 158 | ||
| 159 | + // TODO 芋艿:提前实现 | ||
| 160 | + private Supplier<Double> getMemberDiscountSupplier(Long userId) { | ||
| 161 | + return Suppliers.memoize(() -> { | ||
| 162 | + if (userId == 1) { | ||
| 163 | + return 90d; | ||
| 164 | + } | ||
| 165 | + if (userId == 2) { | ||
| 166 | + return 80d; | ||
| 167 | + } | ||
| 168 | + return null; // 无优惠 | ||
| 169 | + }); | ||
| 170 | + } | ||
| 171 | + | ||
| 172 | + // ========== 计算商品级别的价格 ========== | ||
| 173 | + | ||
| 174 | + /** | ||
| 175 | + * 计算订单级别的价格,例如说: | ||
| 176 | + * 1. 满减送 {@link cn.iocoder.yudao.module.market.dal.dataobject.reward.RewardActivityDO} | ||
| 177 | + * | ||
| 178 | + * @param userId 用户编号 | ||
| 179 | + * @param priceCalculate 价格计算的结果 | ||
| 180 | + */ | ||
| 181 | + @SuppressWarnings("unused") | ||
| 182 | + private void calculatePriceForOrderLevel(Long userId, PriceCalculateRespDTO priceCalculate) { | ||
| 183 | + // 获取 SKU 级别的所有优惠信息 | ||
| 184 | + Set<Long> spuIds = convertSet(priceCalculate.getOrder().getItems(), PriceCalculateRespDTO.OrderItem::getSpuId); | ||
| 185 | + Map<RewardActivityDO, Set<Long>> rewardActivities = rewardService.getMatchRewardActivities(spuIds); | ||
| 186 | + | ||
| 187 | + // 处理满减送活动 | ||
| 188 | + if (CollUtil.isNotEmpty(rewardActivities)) { | ||
| 189 | + rewardActivities.forEach((rewardActivity, activitySpuIds) -> { | ||
| 190 | + List<PriceCalculateRespDTO.OrderItem> orderItems = CollectionUtils.filterList(priceCalculate.getOrder().getItems(), | ||
| 191 | + orderItem -> CollUtil.contains(activitySpuIds, orderItem.getSpuId())); | ||
| 192 | + calculatePriceByRewardActivity(priceCalculate, orderItems, rewardActivity); | ||
| 193 | + }); | ||
| 194 | + } | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + private void calculatePriceByRewardActivity(PriceCalculateRespDTO priceCalculate, List<PriceCalculateRespDTO.OrderItem> orderItems, | ||
| 198 | + RewardActivityDO rewardActivity) { | ||
| 199 | + // 获得最大匹配的满减送活动的规格 | ||
| 200 | + RewardActivityDO.Rule rule = getLastMatchRewardActivityRule(rewardActivity, orderItems); | ||
| 201 | + if (rule == null) { | ||
| 202 | + // 获取不到的情况下,记录不满足的优惠明细 | ||
| 203 | + addNotMeetPromotion(priceCalculate, orderItems, rewardActivity.getId(), rewardActivity.getName(), | ||
| 204 | + PromotionTypeEnum.REWARD_ACTIVITY.getType(), PromotionLevelEnum.ORDER.getLevel(), | ||
| 205 | + getRewardActivityNotMeetTip(rewardActivity)); | ||
| 206 | + return; | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + // 分摊金额 | ||
| 210 | + // TODO 芋艿:limit 不能超过最大价格 | ||
| 211 | + List<Integer> discountPartPrices = dividePrice(orderItems, rule.getDiscountPrice()); | ||
| 212 | + // 记录优惠明细 | ||
| 213 | + addPromotion(priceCalculate, orderItems, rewardActivity.getId(), rewardActivity.getName(), | ||
| 214 | + PromotionTypeEnum.REWARD_ACTIVITY.getType(), PromotionLevelEnum.ORDER.getLevel(), discountPartPrices, | ||
| 215 | + true, StrUtil.format("满减送:省 {} 元", formatPrice(rule.getDiscountPrice()))); | ||
| 216 | + // 修改 SKU 的分摊 | ||
| 217 | + for (int i = 0; i < orderItems.size(); i++) { | ||
| 218 | + modifyOrderItemOrderPartPriceFromDiscountPrice(orderItems.get(i), discountPartPrices.get(i), priceCalculate); | ||
| 219 | + } | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + /** | ||
| 223 | + * 获得最大匹配的满减送活动的规格 | ||
| 224 | + * | ||
| 225 | + * @param rewardActivity 满减送活动 | ||
| 226 | + * @param orderItems 商品项 | ||
| 227 | + * @return 匹配的活动规格 | ||
| 228 | + */ | ||
| 229 | + private RewardActivityDO.Rule getLastMatchRewardActivityRule(RewardActivityDO rewardActivity, | ||
| 230 | + List<PriceCalculateRespDTO.OrderItem> orderItems) { | ||
| 231 | + Integer count = CollectionUtils.getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getCount, Integer::sum); | ||
| 232 | + // price 的计算逻辑,使用 orderDividePrice 的原因,主要考虑分摊后,这个才是该 SKU 当前真实的支付总价 | ||
| 233 | + Integer price = CollectionUtils.getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getOrderDividePrice, Integer::sum); | ||
| 234 | + assert count != null && price != null; | ||
| 235 | + for (int i = rewardActivity.getRules().size() - 1; i >= 0; i--) { | ||
| 236 | + RewardActivityDO.Rule rule = rewardActivity.getRules().get(i); | ||
| 237 | + if (PromotionConditionTypeEnum.PRICE.getType().equals(rewardActivity.getConditionType()) | ||
| 238 | + && price >= rule.getLimit()) { | ||
| 239 | + return rule; | ||
| 240 | + } | ||
| 241 | + if (PromotionConditionTypeEnum.COUNT.getType().equals(rewardActivity.getConditionType()) | ||
| 242 | + && count >= rule.getLimit()) { | ||
| 243 | + return rule; | ||
| 244 | + } | ||
| 245 | + } | ||
| 246 | + return null; | ||
| 247 | + } | ||
| 248 | + | ||
| 249 | + /** | ||
| 250 | + * 获得满减送活动部匹配时的提示 | ||
| 251 | + * | ||
| 252 | + * @param rewardActivity 满减送活动 | ||
| 253 | + * @return 提示 | ||
| 254 | + */ | ||
| 255 | + private String getRewardActivityNotMeetTip(RewardActivityDO rewardActivity) { | ||
| 256 | + return "TODO"; // TODO 芋艿:后面再想想 | ||
| 257 | + } | ||
| 258 | + | ||
| 259 | + // ========== 其它相对通用的方法 ========== | ||
| 260 | + | ||
| 261 | + /** | ||
| 262 | + * 添加单个 OrderItem 的营销明细 | ||
| 263 | + * | ||
| 264 | + * @param priceCalculate 价格计算结果 | ||
| 265 | + * @param orderItem 单个订单商品 SKU | ||
| 266 | + * @param id 营销编号 | ||
| 267 | + * @param name 营销名字 | ||
| 268 | + * @param type 营销类型 | ||
| 269 | + * @param level 营销级别 | ||
| 270 | + * @param newPayPrice 新的单实付金额(总) | ||
| 271 | + * @param meet 是否满足优惠条件 | ||
| 272 | + * @param meetTip 满足条件的提示 | ||
| 273 | + */ | ||
| 141 | private void addPromotion(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem, | 274 | private void addPromotion(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem, |
| 142 | Long id, String name, Integer type, Integer level, | 275 | Long id, String name, Integer type, Integer level, |
| 143 | Integer newPayPrice, Boolean meet, String meetTip) { | 276 | Integer newPayPrice, Boolean meet, String meetTip) { |
| 144 | // 创建营销明细 Item | 277 | // 创建营销明细 Item |
| 278 | + // TODO 芋艿:orderItem.getPayPrice() 要不要改成 orderDividePrice;同时,newPayPrice 要不要改成直接传递 discountPrice | ||
| 145 | PriceCalculateRespDTO.PromotionItem promotionItem = new PriceCalculateRespDTO.PromotionItem().setSkuId(orderItem.getSkuId()) | 279 | PriceCalculateRespDTO.PromotionItem promotionItem = new PriceCalculateRespDTO.PromotionItem().setSkuId(orderItem.getSkuId()) |
| 146 | .setOriginalPrice(orderItem.getPayPrice()).setDiscountPrice(orderItem.getPayPrice() - newPayPrice); | 280 | .setOriginalPrice(orderItem.getPayPrice()).setDiscountPrice(orderItem.getPayPrice() - newPayPrice); |
| 147 | // 创建营销明细 | 281 | // 创建营销明细 |
| @@ -152,6 +286,60 @@ public class PriceServiceImpl implements PriceService { | @@ -152,6 +286,60 @@ public class PriceServiceImpl implements PriceService { | ||
| 152 | priceCalculate.getPromotions().add(promotion); | 286 | priceCalculate.getPromotions().add(promotion); |
| 153 | } | 287 | } |
| 154 | 288 | ||
| 289 | + /** | ||
| 290 | + * 添加多个 OrderItem 的营销明细 | ||
| 291 | + * | ||
| 292 | + * @param priceCalculate 价格计算结果 | ||
| 293 | + * @param orderItems 多个订单商品 SKU | ||
| 294 | + * @param id 营销编号 | ||
| 295 | + * @param name 营销名字 | ||
| 296 | + * @param type 营销类型 | ||
| 297 | + * @param level 营销级别 | ||
| 298 | + * @param discountPrices 多个订单商品 SKU 的优惠价格(总),和 orderItems 一一对应 | ||
| 299 | + * @param meet 是否满足优惠条件 | ||
| 300 | + * @param meetTip 满足条件的提示 | ||
| 301 | + */ | ||
| 302 | + private void addPromotion(PriceCalculateRespDTO priceCalculate, List<PriceCalculateRespDTO.OrderItem> orderItems, | ||
| 303 | + Long id, String name, Integer type, Integer level, | ||
| 304 | + List<Integer> discountPrices, Boolean meet, String meetTip) { | ||
| 305 | + // 创建营销明细 Item | ||
| 306 | + List<PriceCalculateRespDTO.PromotionItem> promotionItems = new ArrayList<>(discountPrices.size()); | ||
| 307 | + for (int i = 0; i < orderItems.size(); i++) { | ||
| 308 | + PriceCalculateRespDTO.OrderItem orderItem = orderItems.get(i); | ||
| 309 | + promotionItems.add(new PriceCalculateRespDTO.PromotionItem().setSkuId(orderItem.getSkuId()) | ||
| 310 | + .setOriginalPrice(orderItem.getPayPrice()).setDiscountPrice(discountPrices.get(i))); | ||
| 311 | + } | ||
| 312 | + // 创建营销明细 | ||
| 313 | + PriceCalculateRespDTO.Promotion promotion = new PriceCalculateRespDTO.Promotion() | ||
| 314 | + .setId(id).setName(name).setType(type).setLevel(level) | ||
| 315 | + .setOriginalPrice(getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getOrderDividePrice, Integer::sum)) | ||
| 316 | + .setDiscountPrice(getSumValue(discountPrices, value -> value, Integer::sum)) | ||
| 317 | + .setItems(promotionItems).setMeet(meet).setMeetTip(meetTip); | ||
| 318 | + priceCalculate.getPromotions().add(promotion); | ||
| 319 | + } | ||
| 320 | + | ||
| 321 | + private void addNotMeetPromotion(PriceCalculateRespDTO priceCalculate, List<PriceCalculateRespDTO.OrderItem> orderItems, | ||
| 322 | + Long id, String name, Integer type, Integer level, String meetTip) { | ||
| 323 | + // 创建营销明细 Item | ||
| 324 | + List<PriceCalculateRespDTO.PromotionItem> promotionItems = CollectionUtils.convertList(orderItems, | ||
| 325 | + orderItem -> new PriceCalculateRespDTO.PromotionItem().setSkuId(orderItem.getSkuId()) | ||
| 326 | + .setOriginalPrice(orderItem.getOrderDividePrice()).setDiscountPrice(0)); | ||
| 327 | + // 创建营销明细 | ||
| 328 | + Integer originalPrice = CollectionUtils.getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getOrderDividePrice, Integer::sum); | ||
| 329 | + PriceCalculateRespDTO.Promotion promotion = new PriceCalculateRespDTO.Promotion() | ||
| 330 | + .setId(id).setName(name).setType(type).setLevel(level) | ||
| 331 | + .setOriginalPrice(originalPrice).setDiscountPrice(0) | ||
| 332 | + .setItems(promotionItems).setMeet(false).setMeetTip(meetTip); | ||
| 333 | + priceCalculate.getPromotions().add(promotion); | ||
| 334 | + } | ||
| 335 | + | ||
| 336 | + /** | ||
| 337 | + * 修改 OrderItem 的 payPrice 价格,同时会修改 Order 的 payPrice 价格 | ||
| 338 | + * | ||
| 339 | + * @param orderItem 订单商品 SKU | ||
| 340 | + * @param newPayPrice 新的 payPrice 价格 | ||
| 341 | + * @param priceCalculate 价格计算结果 | ||
| 342 | + */ | ||
| 155 | private void modifyOrderItemPayPrice(PriceCalculateRespDTO.OrderItem orderItem, Integer newPayPrice, | 343 | private void modifyOrderItemPayPrice(PriceCalculateRespDTO.OrderItem orderItem, Integer newPayPrice, |
| 156 | PriceCalculateRespDTO priceCalculate) { | 344 | PriceCalculateRespDTO priceCalculate) { |
| 157 | int diffPayPrice = orderItem.getPayPrice() - newPayPrice; | 345 | int diffPayPrice = orderItem.getPayPrice() - newPayPrice; |
| @@ -163,17 +351,45 @@ public class PriceServiceImpl implements PriceService { | @@ -163,17 +351,45 @@ public class PriceServiceImpl implements PriceService { | ||
| 163 | priceCalculate.getOrder().setPayPrice(priceCalculate.getOrder().getPayPrice() - diffPayPrice); | 351 | priceCalculate.getOrder().setPayPrice(priceCalculate.getOrder().getPayPrice() - diffPayPrice); |
| 164 | } | 352 | } |
| 165 | 353 | ||
| 166 | - // TODO 芋艿:提前实现 | ||
| 167 | - private Supplier<Double> getMemberDiscountSupplier(Long userId) { | ||
| 168 | - return Suppliers.memoize(() -> { | ||
| 169 | - if (userId == 1) { | ||
| 170 | - return 90d; | ||
| 171 | - } | ||
| 172 | - if (userId == 2) { | ||
| 173 | - return 80d; | 354 | + /** |
| 355 | + * 修改 OrderItem 的 orderPartPrice 价格,同时会修改 Order 的 discountPrice 价格 | ||
| 356 | + * | ||
| 357 | + * 本质:分摊 Order 的 discountPrice 价格,到对应的 OrderItem 的 orderPartPrice 价格中 | ||
| 358 | + * | ||
| 359 | + * @param orderItem 订单商品 SKU | ||
| 360 | + * @param addOrderPartPrice 新增的 | ||
| 361 | + * @param priceCalculate 价格计算结果 | ||
| 362 | + */ | ||
| 363 | + private void modifyOrderItemOrderPartPriceFromDiscountPrice(PriceCalculateRespDTO.OrderItem orderItem, Integer addOrderPartPrice, | ||
| 364 | + PriceCalculateRespDTO priceCalculate) { | ||
| 365 | + // 设置 OrderItem 价格相关字段 | ||
| 366 | + orderItem.setOrderPartPrice(orderItem.getOrderPartPrice() + addOrderPartPrice); | ||
| 367 | + orderItem.setOrderDividePrice(orderItem.getPayPrice() - orderItem.getOrderPartPrice()); | ||
| 368 | + // 设置 Order 相关相关字段 | ||
| 369 | + PriceCalculateRespDTO.Order order = priceCalculate.getOrder(); | ||
| 370 | + order.setDiscountPrice(order.getDiscountPrice() + addOrderPartPrice); | ||
| 371 | + order.setPayPrice(order.getPayPrice() - addOrderPartPrice); | ||
| 372 | + } | ||
| 373 | + | ||
| 374 | + private List<Integer> dividePrice(List<PriceCalculateRespDTO.OrderItem> orderItems, Integer price) { | ||
| 375 | + List<Integer> prices = new ArrayList<>(orderItems.size()); | ||
| 376 | + Integer total = getSumValue(orderItems, PriceCalculateRespDTO.OrderItem::getOrderDividePrice, Integer::sum); | ||
| 377 | + assert total != null; | ||
| 378 | + int remainPrice = price; | ||
| 379 | + // 遍历每一个,进行分摊 | ||
| 380 | + for (int i = 0; i < orderItems.size(); i++) { | ||
| 381 | + PriceCalculateRespDTO.OrderItem orderItem = orderItems.get(i); | ||
| 382 | + int partPrice; | ||
| 383 | + if (i < orderItems.size() - 1) { // 减一的原因,是因为拆分时,如果按照比例,可能会出现.所以最后一个,使用反减 | ||
| 384 | + partPrice = (int) (price * (1.0D * orderItem.getOrderDividePrice() / total)); | ||
| 385 | + remainPrice -= partPrice; | ||
| 386 | + } else { | ||
| 387 | + partPrice = remainPrice; | ||
| 174 | } | 388 | } |
| 175 | - return null; // 无优惠 | ||
| 176 | - }); | 389 | + Assert.isTrue(partPrice > 0, "分摊金额必须大于 0"); |
| 390 | + prices.add(partPrice); | ||
| 391 | + } | ||
| 392 | + return prices; | ||
| 177 | } | 393 | } |
| 178 | 394 | ||
| 179 | private String formatPrice(Integer price) { | 395 | private String formatPrice(Integer price) { |
yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/service/reward/RewardService.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.market.service.reward; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.market.dal.dataobject.reward.RewardActivityDO; | ||
| 4 | + | ||
| 5 | +import java.util.Map; | ||
| 6 | +import java.util.Set; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 满减送 Service 接口 | ||
| 10 | + * | ||
| 11 | + * @author 芋道源码 | ||
| 12 | + */ | ||
| 13 | +public interface RewardService { | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 基于指定的 SPU 编号数组,获得它们匹配的满减送活动 | ||
| 17 | + * | ||
| 18 | + * @param spuIds SPU 编号数组 | ||
| 19 | + * @return 满减送活动,与对应的 SPU 编号的映射。即,value 就是 SPU 编号的集合 | ||
| 20 | + */ | ||
| 21 | + Map<RewardActivityDO, Set<Long>> getMatchRewardActivities(Set<Long> spuIds); | ||
| 22 | + | ||
| 23 | +} |
yudao-module-mall/yudao-module-market-biz/src/main/java/cn/iocoder/yudao/module/market/service/reward/RewardServiceImpl.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.market.service.reward; | ||
| 2 | + | ||
| 3 | +import cn.iocoder.yudao.module.market.dal.dataobject.reward.RewardActivityDO; | ||
| 4 | +import org.springframework.stereotype.Service; | ||
| 5 | +import org.springframework.validation.annotation.Validated; | ||
| 6 | + | ||
| 7 | +import java.util.Collections; | ||
| 8 | +import java.util.Map; | ||
| 9 | +import java.util.Set; | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * 满减送 Service 实现类 | ||
| 13 | + * | ||
| 14 | + * @author 芋道源码 | ||
| 15 | + */ | ||
| 16 | +@Service | ||
| 17 | +@Validated | ||
| 18 | +public class RewardServiceImpl implements RewardService { | ||
| 19 | + | ||
| 20 | + // TODO 芋艿:待实现 | ||
| 21 | + @Override | ||
| 22 | + public Map<RewardActivityDO, Set<Long>> getMatchRewardActivities(Set<Long> spuIds) { | ||
| 23 | + return Collections.emptyMap(); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | +} |
yudao-module-mall/yudao-module-market-biz/src/test/java/cn/iocoder/yudao/module/market/service/price/PriceServiceTest.java
| 1 | package cn.iocoder.yudao.module.market.service.price; | 1 | package cn.iocoder.yudao.module.market.service.price; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.map.MapUtil; | 3 | import cn.hutool.core.map.MapUtil; |
| 4 | -import cn.iocoder.yudao.framework.common.util.collection.SetUtils; | ||
| 5 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; | 4 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 6 | import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateReqDTO; | 5 | import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateReqDTO; |
| 7 | import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateRespDTO; | 6 | import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateRespDTO; |
| 8 | import cn.iocoder.yudao.module.market.dal.dataobject.discount.DiscountProductDO; | 7 | import cn.iocoder.yudao.module.market.dal.dataobject.discount.DiscountProductDO; |
| 8 | +import cn.iocoder.yudao.module.market.dal.dataobject.reward.RewardActivityDO; | ||
| 9 | +import cn.iocoder.yudao.module.market.enums.common.PromotionConditionTypeEnum; | ||
| 9 | import cn.iocoder.yudao.module.market.enums.common.PromotionLevelEnum; | 10 | import cn.iocoder.yudao.module.market.enums.common.PromotionLevelEnum; |
| 10 | import cn.iocoder.yudao.module.market.enums.common.PromotionTypeEnum; | 11 | import cn.iocoder.yudao.module.market.enums.common.PromotionTypeEnum; |
| 11 | import cn.iocoder.yudao.module.market.service.discount.DiscountService; | 12 | import cn.iocoder.yudao.module.market.service.discount.DiscountService; |
| 13 | +import cn.iocoder.yudao.module.market.service.reward.RewardService; | ||
| 12 | import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; | 14 | import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; |
| 13 | import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; | 15 | import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; |
| 14 | import org.junit.jupiter.api.Test; | 16 | import org.junit.jupiter.api.Test; |
| 15 | import org.mockito.InjectMocks; | 17 | import org.mockito.InjectMocks; |
| 16 | import org.mockito.Mock; | 18 | import org.mockito.Mock; |
| 17 | 19 | ||
| 20 | +import java.util.LinkedHashMap; | ||
| 21 | +import java.util.Map; | ||
| 22 | +import java.util.Set; | ||
| 23 | + | ||
| 24 | +import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; | ||
| 18 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; | 25 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| 19 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; | 26 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 20 | import static java.util.Arrays.asList; | 27 | import static java.util.Arrays.asList; |
| @@ -36,6 +43,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest { | @@ -36,6 +43,8 @@ public class PriceServiceTest extends BaseMockitoUnitTest { | ||
| 36 | @Mock | 43 | @Mock |
| 37 | private DiscountService discountService; | 44 | private DiscountService discountService; |
| 38 | @Mock | 45 | @Mock |
| 46 | + private RewardService rewardService; | ||
| 47 | + @Mock | ||
| 39 | private ProductSkuApi productSkuApi; | 48 | private ProductSkuApi productSkuApi; |
| 40 | 49 | ||
| 41 | @Test | 50 | @Test |
| @@ -46,7 +55,7 @@ public class PriceServiceTest extends BaseMockitoUnitTest { | @@ -46,7 +55,7 @@ public class PriceServiceTest extends BaseMockitoUnitTest { | ||
| 46 | .setItems(singletonList(new PriceCalculateReqDTO.Item().setSkuId(10L).setCount(2))); | 55 | .setItems(singletonList(new PriceCalculateReqDTO.Item().setSkuId(10L).setCount(2))); |
| 47 | // mock 方法(商品 SKU 信息) | 56 | // mock 方法(商品 SKU 信息) |
| 48 | ProductSkuRespDTO productSku = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100)); | 57 | ProductSkuRespDTO productSku = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100)); |
| 49 | - when(productSkuApi.getSkuList(eq(SetUtils.asSet(10L)))).thenReturn(singletonList(productSku)); | 58 | + when(productSkuApi.getSkuList(eq(asSet(10L)))).thenReturn(singletonList(productSku)); |
| 50 | 59 | ||
| 51 | // 调用 | 60 | // 调用 |
| 52 | PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO); | 61 | PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO); |
| @@ -96,13 +105,13 @@ public class PriceServiceTest extends BaseMockitoUnitTest { | @@ -96,13 +105,13 @@ public class PriceServiceTest extends BaseMockitoUnitTest { | ||
| 96 | // mock 方法(商品 SKU 信息) | 105 | // mock 方法(商品 SKU 信息) |
| 97 | ProductSkuRespDTO productSku01 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100)); | 106 | ProductSkuRespDTO productSku01 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100)); |
| 98 | ProductSkuRespDTO productSku02 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(20L).setPrice(50)); | 107 | ProductSkuRespDTO productSku02 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(20L).setPrice(50)); |
| 99 | - when(productSkuApi.getSkuList(eq(SetUtils.asSet(10L, 20L)))).thenReturn(asList(productSku01, productSku02)); | 108 | + when(productSkuApi.getSkuList(eq(asSet(10L, 20L)))).thenReturn(asList(productSku01, productSku02)); |
| 100 | // mock 方法(限时折扣 DiscountActivity 信息) | 109 | // mock 方法(限时折扣 DiscountActivity 信息) |
| 101 | DiscountProductDO discountProduct01 = randomPojo(DiscountProductDO.class, o -> o.setActivityId(1000L).setActivityName("活动 1000 号") | 110 | DiscountProductDO discountProduct01 = randomPojo(DiscountProductDO.class, o -> o.setActivityId(1000L).setActivityName("活动 1000 号") |
| 102 | .setSkuId(10L).setPromotionPrice(80)); | 111 | .setSkuId(10L).setPromotionPrice(80)); |
| 103 | DiscountProductDO discountProduct02 = randomPojo(DiscountProductDO.class, o -> o.setActivityId(2000L).setActivityName("活动 2000 号") | 112 | DiscountProductDO discountProduct02 = randomPojo(DiscountProductDO.class, o -> o.setActivityId(2000L).setActivityName("活动 2000 号") |
| 104 | .setSkuId(20L).setPromotionPrice(40)); | 113 | .setSkuId(20L).setPromotionPrice(40)); |
| 105 | - when(discountService.getMatchDiscountProducts(eq(SetUtils.asSet(10L, 20L)))).thenReturn( | 114 | + when(discountService.getMatchDiscountProducts(eq(asSet(10L, 20L)))).thenReturn( |
| 106 | MapUtil.builder(10L, discountProduct01).put(20L, discountProduct02).map()); | 115 | MapUtil.builder(10L, discountProduct01).put(20L, discountProduct02).map()); |
| 107 | 116 | ||
| 108 | // 调用 | 117 | // 调用 |
| @@ -167,4 +176,182 @@ public class PriceServiceTest extends BaseMockitoUnitTest { | @@ -167,4 +176,182 @@ public class PriceServiceTest extends BaseMockitoUnitTest { | ||
| 167 | assertEquals(promotionItem02.getDiscountPrice(), 30); | 176 | assertEquals(promotionItem02.getDiscountPrice(), 30); |
| 168 | } | 177 | } |
| 169 | 178 | ||
| 179 | + /** | ||
| 180 | + * 测试满减送活动,匹配的情况 | ||
| 181 | + */ | ||
| 182 | + @Test | ||
| 183 | + public void testCalculatePrice_rewardActivity() { | ||
| 184 | + // 准备参数 | ||
| 185 | + PriceCalculateReqDTO calculateReqDTO = new PriceCalculateReqDTO().setUserId(randomLongId()) | ||
| 186 | + .setItems(asList(new PriceCalculateReqDTO.Item().setSkuId(10L).setCount(2), | ||
| 187 | + new PriceCalculateReqDTO.Item().setSkuId(20L).setCount(3), | ||
| 188 | + new PriceCalculateReqDTO.Item().setSkuId(30L).setCount(4))); | ||
| 189 | + // mock 方法(商品 SKU 信息) | ||
| 190 | + ProductSkuRespDTO productSku01 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100).setSpuId(1L)); | ||
| 191 | + ProductSkuRespDTO productSku02 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(20L).setPrice(50).setSpuId(2L)); | ||
| 192 | + ProductSkuRespDTO productSku03 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(30L).setPrice(30).setSpuId(3L)); | ||
| 193 | + when(productSkuApi.getSkuList(eq(asSet(10L, 20L, 30L)))).thenReturn(asList(productSku01, productSku02, productSku03)); | ||
| 194 | + // mock 方法(限时折扣 DiscountActivity 信息) | ||
| 195 | + RewardActivityDO rewardActivity01 = randomPojo(RewardActivityDO.class, o -> o.setId(1000L).setName("活动 1000 号") | ||
| 196 | + .setSpuIds(asList(10L, 20L)).setConditionType(PromotionConditionTypeEnum.PRICE.getType()) | ||
| 197 | + .setRules(singletonList(new RewardActivityDO.Rule().setLimit(200).setDiscountPrice(70)))); | ||
| 198 | + RewardActivityDO rewardActivity02 = randomPojo(RewardActivityDO.class, o -> o.setId(2000L).setName("活动 2000 号") | ||
| 199 | + .setSpuIds(singletonList(30L)).setConditionType(PromotionConditionTypeEnum.COUNT.getType()) | ||
| 200 | + .setRules(asList(new RewardActivityDO.Rule().setLimit(1).setDiscountPrice(10), | ||
| 201 | + new RewardActivityDO.Rule().setLimit(2).setDiscountPrice(60), // 最大可满足,因为是 4 个 | ||
| 202 | + new RewardActivityDO.Rule().setLimit(10).setDiscountPrice(100)))); | ||
| 203 | + Map<RewardActivityDO, Set<Long>> matchRewardActivities = new LinkedHashMap<>(); | ||
| 204 | + matchRewardActivities.put(rewardActivity01, asSet(1L, 2L)); | ||
| 205 | + matchRewardActivities.put(rewardActivity02, asSet(3L)); | ||
| 206 | + when(rewardService.getMatchRewardActivities(eq(asSet(1L, 2L, 3L)))).thenReturn(matchRewardActivities); | ||
| 207 | + | ||
| 208 | + // 调用 | ||
| 209 | + PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO); | ||
| 210 | + // 断言 Order 部分 | ||
| 211 | + PriceCalculateRespDTO.Order order = priceCalculate.getOrder(); | ||
| 212 | + assertEquals(order.getOriginalPrice(), 470); | ||
| 213 | + assertEquals(order.getDiscountPrice(), 130); | ||
| 214 | + assertEquals(order.getPointPrice(), 0); | ||
| 215 | + assertEquals(order.getDeliveryPrice(), 0); | ||
| 216 | + assertEquals(order.getPayPrice(), 340); | ||
| 217 | + assertNull(order.getCouponId()); | ||
| 218 | + // 断言 OrderItem 部分 | ||
| 219 | + assertEquals(order.getItems().size(), 3); | ||
| 220 | + PriceCalculateRespDTO.OrderItem orderItem01 = order.getItems().get(0); | ||
| 221 | + assertEquals(orderItem01.getSkuId(), 10L); | ||
| 222 | + assertEquals(orderItem01.getCount(), 2); | ||
| 223 | + assertEquals(orderItem01.getOriginalPrice(), 200); | ||
| 224 | + assertEquals(orderItem01.getOriginalUnitPrice(), 100); | ||
| 225 | + assertEquals(orderItem01.getDiscountPrice(), 0); | ||
| 226 | + assertEquals(orderItem01.getPayPrice(), 200); | ||
| 227 | + assertEquals(orderItem01.getOrderPartPrice(), 40); | ||
| 228 | + assertEquals(orderItem01.getOrderDividePrice(), 160); | ||
| 229 | + PriceCalculateRespDTO.OrderItem orderItem02 = order.getItems().get(1); | ||
| 230 | + assertEquals(orderItem02.getSkuId(), 20L); | ||
| 231 | + assertEquals(orderItem02.getCount(), 3); | ||
| 232 | + assertEquals(orderItem02.getOriginalPrice(), 150); | ||
| 233 | + assertEquals(orderItem02.getOriginalUnitPrice(), 50); | ||
| 234 | + assertEquals(orderItem02.getDiscountPrice(), 0); | ||
| 235 | + assertEquals(orderItem02.getPayPrice(), 150); | ||
| 236 | + assertEquals(orderItem02.getOrderPartPrice(), 30); | ||
| 237 | + assertEquals(orderItem02.getOrderDividePrice(), 120); | ||
| 238 | + PriceCalculateRespDTO.OrderItem orderItem03 = order.getItems().get(2); | ||
| 239 | + assertEquals(orderItem03.getSkuId(), 30L); | ||
| 240 | + assertEquals(orderItem03.getCount(), 4); | ||
| 241 | + assertEquals(orderItem03.getOriginalPrice(), 120); | ||
| 242 | + assertEquals(orderItem03.getOriginalUnitPrice(), 30); | ||
| 243 | + assertEquals(orderItem03.getDiscountPrice(), 0); | ||
| 244 | + assertEquals(orderItem03.getPayPrice(), 120); | ||
| 245 | + assertEquals(orderItem03.getOrderPartPrice(), 60); | ||
| 246 | + assertEquals(orderItem03.getOrderDividePrice(), 60); | ||
| 247 | + // 断言 Promotion 部分(第一个) | ||
| 248 | + assertEquals(priceCalculate.getPromotions().size(), 2); | ||
| 249 | + PriceCalculateRespDTO.Promotion promotion01 = priceCalculate.getPromotions().get(0); | ||
| 250 | + assertEquals(promotion01.getId(), 1000L); | ||
| 251 | + assertEquals(promotion01.getName(), "活动 1000 号"); | ||
| 252 | + assertEquals(promotion01.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType()); | ||
| 253 | + assertEquals(promotion01.getLevel(), PromotionLevelEnum.ORDER.getLevel()); | ||
| 254 | + assertEquals(promotion01.getOriginalPrice(), 350); | ||
| 255 | + assertEquals(promotion01.getDiscountPrice(), 70); | ||
| 256 | + assertTrue(promotion01.getMeet()); | ||
| 257 | + assertEquals(promotion01.getMeetTip(), "满减送:省 0.70 元"); | ||
| 258 | + assertEquals(promotion01.getItems().size(), 2); | ||
| 259 | + PriceCalculateRespDTO.PromotionItem promotionItem011 = promotion01.getItems().get(0); | ||
| 260 | + assertEquals(promotionItem011.getSkuId(), 10L); | ||
| 261 | + assertEquals(promotionItem011.getOriginalPrice(), 200); | ||
| 262 | + assertEquals(promotionItem011.getDiscountPrice(), 40); | ||
| 263 | + PriceCalculateRespDTO.PromotionItem promotionItem012 = promotion01.getItems().get(1); | ||
| 264 | + assertEquals(promotionItem012.getSkuId(), 20L); | ||
| 265 | + assertEquals(promotionItem012.getOriginalPrice(), 150); | ||
| 266 | + assertEquals(promotionItem012.getDiscountPrice(), 30); | ||
| 267 | + // 断言 Promotion 部分(第二个) | ||
| 268 | + PriceCalculateRespDTO.Promotion promotion02 = priceCalculate.getPromotions().get(1); | ||
| 269 | + assertEquals(promotion02.getId(), 2000L); | ||
| 270 | + assertEquals(promotion02.getName(), "活动 2000 号"); | ||
| 271 | + assertEquals(promotion02.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType()); | ||
| 272 | + assertEquals(promotion02.getLevel(), PromotionLevelEnum.ORDER.getLevel()); | ||
| 273 | + assertEquals(promotion02.getOriginalPrice(), 120); | ||
| 274 | + assertEquals(promotion02.getDiscountPrice(), 60); | ||
| 275 | + assertTrue(promotion02.getMeet()); | ||
| 276 | + assertEquals(promotion02.getMeetTip(), "满减送:省 0.60 元"); | ||
| 277 | + PriceCalculateRespDTO.PromotionItem promotionItem02 = promotion02.getItems().get(0); | ||
| 278 | + assertEquals(promotion02.getItems().size(), 1); | ||
| 279 | + assertEquals(promotionItem02.getSkuId(), 30L); | ||
| 280 | + assertEquals(promotionItem02.getOriginalPrice(), 120); | ||
| 281 | + assertEquals(promotionItem02.getDiscountPrice(), 60); | ||
| 282 | + } | ||
| 283 | + | ||
| 284 | + /** | ||
| 285 | + * 测试满减送活动,不匹配的情况 | ||
| 286 | + */ | ||
| 287 | + @Test | ||
| 288 | + public void testCalculatePrice_rewardActivityNotMeet() { | ||
| 289 | + // 准备参数 | ||
| 290 | + PriceCalculateReqDTO calculateReqDTO = new PriceCalculateReqDTO().setUserId(randomLongId()) | ||
| 291 | + .setItems(asList(new PriceCalculateReqDTO.Item().setSkuId(10L).setCount(2), | ||
| 292 | + new PriceCalculateReqDTO.Item().setSkuId(20L).setCount(3))); | ||
| 293 | + // mock 方法(商品 SKU 信息) | ||
| 294 | + ProductSkuRespDTO productSku01 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100).setSpuId(1L)); | ||
| 295 | + ProductSkuRespDTO productSku02 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(20L).setPrice(50).setSpuId(2L)); | ||
| 296 | + when(productSkuApi.getSkuList(eq(asSet(10L, 20L)))).thenReturn(asList(productSku01, productSku02)); | ||
| 297 | + // mock 方法(限时折扣 DiscountActivity 信息) | ||
| 298 | + RewardActivityDO rewardActivity01 = randomPojo(RewardActivityDO.class, o -> o.setId(1000L).setName("活动 1000 号") | ||
| 299 | + .setSpuIds(asList(10L, 20L)).setConditionType(PromotionConditionTypeEnum.PRICE.getType()) | ||
| 300 | + .setRules(singletonList(new RewardActivityDO.Rule().setLimit(351).setDiscountPrice(70)))); | ||
| 301 | + Map<RewardActivityDO, Set<Long>> matchRewardActivities = new LinkedHashMap<>(); | ||
| 302 | + matchRewardActivities.put(rewardActivity01, asSet(1L, 2L)); | ||
| 303 | + when(rewardService.getMatchRewardActivities(eq(asSet(1L, 2L)))).thenReturn(matchRewardActivities); | ||
| 304 | + | ||
| 305 | + // 调用 | ||
| 306 | + PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO); | ||
| 307 | + // 断言 Order 部分 | ||
| 308 | + PriceCalculateRespDTO.Order order = priceCalculate.getOrder(); | ||
| 309 | + assertEquals(order.getOriginalPrice(), 350); | ||
| 310 | + assertEquals(order.getDiscountPrice(), 0); | ||
| 311 | + assertEquals(order.getPointPrice(), 0); | ||
| 312 | + assertEquals(order.getDeliveryPrice(), 0); | ||
| 313 | + assertEquals(order.getPayPrice(), 350); | ||
| 314 | + assertNull(order.getCouponId()); | ||
| 315 | + // 断言 OrderItem 部分 | ||
| 316 | + assertEquals(order.getItems().size(), 2); | ||
| 317 | + PriceCalculateRespDTO.OrderItem orderItem01 = order.getItems().get(0); | ||
| 318 | + assertEquals(orderItem01.getSkuId(), 10L); | ||
| 319 | + assertEquals(orderItem01.getCount(), 2); | ||
| 320 | + assertEquals(orderItem01.getOriginalPrice(), 200); | ||
| 321 | + assertEquals(orderItem01.getOriginalUnitPrice(), 100); | ||
| 322 | + assertEquals(orderItem01.getDiscountPrice(), 0); | ||
| 323 | + assertEquals(orderItem01.getPayPrice(), 200); | ||
| 324 | + assertEquals(orderItem01.getOrderPartPrice(), 0); | ||
| 325 | + assertEquals(orderItem01.getOrderDividePrice(), 200); | ||
| 326 | + PriceCalculateRespDTO.OrderItem orderItem02 = order.getItems().get(1); | ||
| 327 | + assertEquals(orderItem02.getSkuId(), 20L); | ||
| 328 | + assertEquals(orderItem02.getCount(), 3); | ||
| 329 | + assertEquals(orderItem02.getOriginalPrice(), 150); | ||
| 330 | + assertEquals(orderItem02.getOriginalUnitPrice(), 50); | ||
| 331 | + assertEquals(orderItem02.getDiscountPrice(), 0); | ||
| 332 | + assertEquals(orderItem02.getPayPrice(), 150); | ||
| 333 | + assertEquals(orderItem02.getOrderPartPrice(), 0); | ||
| 334 | + assertEquals(orderItem02.getOrderDividePrice(), 150); | ||
| 335 | + // 断言 Promotion 部分 | ||
| 336 | + assertEquals(priceCalculate.getPromotions().size(), 1); | ||
| 337 | + PriceCalculateRespDTO.Promotion promotion01 = priceCalculate.getPromotions().get(0); | ||
| 338 | + assertEquals(promotion01.getId(), 1000L); | ||
| 339 | + assertEquals(promotion01.getName(), "活动 1000 号"); | ||
| 340 | + assertEquals(promotion01.getType(), PromotionTypeEnum.REWARD_ACTIVITY.getType()); | ||
| 341 | + assertEquals(promotion01.getLevel(), PromotionLevelEnum.ORDER.getLevel()); | ||
| 342 | + assertEquals(promotion01.getOriginalPrice(), 350); | ||
| 343 | + assertEquals(promotion01.getDiscountPrice(), 0); | ||
| 344 | + assertFalse(promotion01.getMeet()); | ||
| 345 | + assertEquals(promotion01.getMeetTip(), "TODO"); // TODO 芋艿:后面再想想 | ||
| 346 | + assertEquals(promotion01.getItems().size(), 2); | ||
| 347 | + PriceCalculateRespDTO.PromotionItem promotionItem011 = promotion01.getItems().get(0); | ||
| 348 | + assertEquals(promotionItem011.getSkuId(), 10L); | ||
| 349 | + assertEquals(promotionItem011.getOriginalPrice(), 200); | ||
| 350 | + assertEquals(promotionItem011.getDiscountPrice(), 0); | ||
| 351 | + PriceCalculateRespDTO.PromotionItem promotionItem012 = promotion01.getItems().get(1); | ||
| 352 | + assertEquals(promotionItem012.getSkuId(), 20L); | ||
| 353 | + assertEquals(promotionItem012.getOriginalPrice(), 150); | ||
| 354 | + assertEquals(promotionItem012.getDiscountPrice(), 0); | ||
| 355 | + } | ||
| 356 | + | ||
| 170 | } | 357 | } |