diff --git a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/reward/dto/RewardActivityMatchRespDTO.java b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/reward/dto/RewardActivityMatchRespDTO.java index 958668461..8a8ad431c 100644 --- a/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/reward/dto/RewardActivityMatchRespDTO.java +++ b/yudao-module-mall/yudao-module-promotion-api/src/main/java/cn/iocoder/yudao/module/promotion/api/reward/dto/RewardActivityMatchRespDTO.java @@ -100,6 +100,13 @@ public class RewardActivityMatchRespDTO { */ private Map giveCouponTemplateCounts; + /** + * 规则描述 + * + * 通过 {@link #limit}、{@link #discountPrice} 等字段进行拼接 + */ + private String description; + } } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java index e17d923ec..4359feb07 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/AppTradeOrderController.java @@ -1,23 +1,9 @@ package cn.iocoder.yudao.module.trade.controller.app.order; -import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated; -import cn.iocoder.yudao.module.member.api.level.MemberLevelApi; -import cn.iocoder.yudao.module.member.api.level.dto.MemberLevelRespDTO; -import cn.iocoder.yudao.module.member.api.user.MemberUserApi; -import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO; import cn.iocoder.yudao.module.pay.api.notify.dto.PayOrderNotifyReqDTO; -import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; -import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; -import cn.iocoder.yudao.module.promotion.api.discount.DiscountActivityApi; -import cn.iocoder.yudao.module.promotion.api.discount.dto.DiscountProductRespDTO; -import cn.iocoder.yudao.module.promotion.api.reward.RewardActivityApi; -import cn.iocoder.yudao.module.promotion.api.reward.dto.RewardActivityMatchRespDTO; -import cn.iocoder.yudao.module.promotion.enums.common.PromotionConditionTypeEnum; -import cn.iocoder.yudao.module.promotion.enums.common.PromotionDiscountTypeEnum; -import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum; import cn.iocoder.yudao.module.trade.controller.app.order.vo.*; import cn.iocoder.yudao.module.trade.controller.app.order.vo.item.AppTradeOrderItemCommentCreateReqVO; import cn.iocoder.yudao.module.trade.controller.app.order.vo.item.AppTradeOrderItemRespVO; @@ -31,6 +17,7 @@ import cn.iocoder.yudao.module.trade.service.aftersale.AfterSaleService; import cn.iocoder.yudao.module.trade.service.delivery.DeliveryExpressService; import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService; import cn.iocoder.yudao.module.trade.service.order.TradeOrderUpdateService; +import cn.iocoder.yudao.module.trade.service.price.TradePriceService; import com.google.common.collect.Maps; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -41,8 +28,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; -import java.time.LocalDateTime; -import java.util.*; +import java.util.List; +import java.util.Map; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; @@ -61,24 +48,14 @@ public class AppTradeOrderController { private TradeOrderQueryService tradeOrderQueryService; @Resource private DeliveryExpressService deliveryExpressService; - @Resource private AfterSaleService afterSaleService; + @Resource + private TradePriceService priceService; @Resource private TradeOrderProperties tradeOrderProperties; - @Resource - private MemberLevelApi memberLevelApi; - @Resource - private MemberUserApi memberUserApi; - @Resource - private DiscountActivityApi discountActivityApi; - @Resource - private RewardActivityApi rewardActivityApi; - @Resource - private ProductSkuApi productKpuApi; - @GetMapping("/settlement") @Operation(summary = "获得订单结算信息") @PreAuthenticated @@ -86,56 +63,11 @@ public class AppTradeOrderController { return success(tradeOrderUpdateService.settlementOrder(getLoginUserId(), settlementReqVO)); } - @GetMapping("/settlementProduct") - @Operation(summary = "获得商品结算信息") - public CommonResult> settlementProduct(@RequestParam("ids") Set ids) { - List appTradeProductSettlementRespVOS = new ArrayList<>(); - MemberLevelRespDTO memberLevel = getMemberLevel(); - ids.forEach(spuId -> { - List skus = new ArrayList<>(); - List skuList = productKpuApi.getSkuListBySpuId(Collections.singletonList(spuId)); - //查询sku的会员和限时优惠 - skuList.forEach(sku -> { - //查询限时优惠价格 - AppTradeProductSettlementRespVO.Sku skuDiscount = calculateDiscountPrice(sku.getId(), sku.getPrice()); - - //查询会员价 - AppTradeProductSettlementRespVO.Sku skuVip = calculateVipPrice(sku.getId(), sku.getPrice(), memberLevel); - - if(skuDiscount != null && skuVip != null){ - if(skuDiscount.getPrice() > skuVip.getPrice()){ - skus.add(skuVip); - }else{ - skus.add(skuDiscount); - } - }else if(skuDiscount != null){ - skus.add(skuDiscount); - }else if(skuVip != null){ - skus.add(skuVip); - } - - }); - AppTradeProductSettlementRespVO.Reward reward = calculateReward(spuId); - AppTradeProductSettlementRespVO respVO = AppTradeProductSettlementRespVO.builder().id(spuId).skus(skus).build(); - if(reward != null){ - //创建满减活动对象 - respVO.setReward(reward); - } - appTradeProductSettlementRespVOS.add(respVO); - }); - return success(appTradeProductSettlementRespVOS); - } - - private MemberLevelRespDTO getMemberLevel() { - Long userId = getLoginUserId(); - if (userId == null) { - return null; - } - MemberUserRespDTO user = memberUserApi.getUser(userId); - if (user.getLevelId() == null || user.getLevelId() <= 0) { - return null; - } - return memberLevelApi.getMemberLevel(user.getLevelId()); + @GetMapping("/settlement-product") + @Operation(summary = "获得商品结算信息", description = "用于商品列表、商品详情,获得参与活动后的价格信息") + @Parameter(name = "spuIds", description = "商品 SPU 编号数组") + public CommonResult> settlementProduct(@RequestParam("spuIds") List spuIds) { + return success(priceService.calculateProductPrice(getLoginUserId(), spuIds)); } @PostMapping("/create") @@ -265,78 +197,4 @@ public class AppTradeOrderController { return success(tradeOrderUpdateService.createOrderItemCommentByMember(getLoginUserId(), createReqVO)); } - /** - * 计算会员 VIP 优惠价格 - * - * @param price 原价 - * @param memberLevel 会员等级 - * @return 优惠价格 - */ - public AppTradeProductSettlementRespVO.Sku calculateVipPrice(Long skuId, Integer price, MemberLevelRespDTO memberLevel) { - if (memberLevel == null || memberLevel.getDiscountPercent() == null) { - return null; - } - Integer newPrice = price * memberLevel.getDiscountPercent() / 100; - return AppTradeProductSettlementRespVO.Sku.builder(). - skuId(skuId). - type(PromotionTypeEnum.MEMBER_LEVEL.getType()). - price(newPrice).build(); - } - - /** - * 计算限时优惠信息 - * - * @param price 原价 - * @param skuId 商品规格id - * @return 优惠价格 - */ - private AppTradeProductSettlementRespVO.Sku calculateDiscountPrice(Long skuId, Integer price) { - if (skuId == null) { - return null; - } - - //根据商品id查询限时优惠 - List matchDiscountProductList = discountActivityApi.getMatchDiscountProductList(Collections.singletonList(skuId)); - if (matchDiscountProductList != null && !matchDiscountProductList.isEmpty()) { - DiscountProductRespDTO discountProductRespDTO = matchDiscountProductList.get(matchDiscountProductList.size() - 1); - AppTradeProductSettlementRespVO.Sku sku = AppTradeProductSettlementRespVO.Sku.builder(). - skuId(skuId). - discountId(discountProductRespDTO.getId()). - type(PromotionTypeEnum.DISCOUNT_ACTIVITY.getType()). - endTime(discountProductRespDTO.getActivityEndTime()). - build(); - Integer discountType = discountProductRespDTO.getDiscountType(); - if(Objects.equals(PromotionDiscountTypeEnum.PRICE.getType(), discountType)){ - sku.setPrice(price - discountProductRespDTO.getDiscountPrice() * 100); - }else if(Objects.equals(PromotionDiscountTypeEnum.PERCENT.getType(), discountType)){ - Integer newPrice = price * discountProductRespDTO.getDiscountPercent() / 100; - sku.setPrice(price - newPrice); - }else{ - throw new IllegalArgumentException("限时折扣活动类型不存在"); - } - return sku; - } - return null; - } - - /** - * 获取第一层满减活动 - * - * @param spuId 商品规格id - * @return 优惠价格 - */ - private AppTradeProductSettlementRespVO.Reward calculateReward(Long spuId) { - List matchRewardActivityList = rewardActivityApi.getRewardActivityBySpuIdsAndStatusAndDateTimeLt(Collections.singletonList(spuId), CommonStatusEnum.ENABLE.getStatus(), LocalDateTime.now()); - if(matchRewardActivityList != null && !matchRewardActivityList.isEmpty()){ - RewardActivityMatchRespDTO rewardActivityMatchRespDTO = matchRewardActivityList.get(matchRewardActivityList.size() - 1); - if(rewardActivityMatchRespDTO != null){ - RewardActivityMatchRespDTO.Rule rule = rewardActivityMatchRespDTO.getRules().get(0); - return AppTradeProductSettlementRespVO.Reward.builder(). - rewardActivity("满" + rule.getLimit() / 100 + (Objects.equals(rewardActivityMatchRespDTO.getConditionType(), PromotionConditionTypeEnum.PRICE.getType())?"元":"件"+"减") +rule.getDiscountPrice() / 100) - .id(rewardActivityMatchRespDTO.getId()).build(); - } - } - return null; - } - } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/vo/AppTradeProductSettlementRespVO.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/vo/AppTradeProductSettlementRespVO.java index 773b49617..3d0ec810c 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/vo/AppTradeProductSettlementRespVO.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/controller/app/order/vo/AppTradeProductSettlementRespVO.java @@ -1,7 +1,6 @@ package cn.iocoder.yudao.module.trade.controller.app.order.vo; import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Builder; import lombok.Data; import java.io.Serializable; @@ -10,54 +9,48 @@ import java.util.List; @Schema(description = "用户 App - 商品结算信息 Response VO") @Data -@Builder public class AppTradeProductSettlementRespVO { - @Schema(description = "spu 商品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") - private Long id; + @Schema(description = "SPU 商品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + private Long spuId; - @Schema(description = "满减活动对象", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") - private Reward reward; - - @Schema(description = "sku 活动信息", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + @Schema(description = "SKU 价格信息数组", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") private List skus; - /** - * 满减活动 - */ + @Schema(description = "满减送活动信息", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + private RewardActivity rewardActivity; + + @Schema(description = "满减送活动信息") @Data - @Builder - public static class Reward implements Serializable { + public static class RewardActivity { @Schema(description = "满减活动编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") private Long id; - @Schema(description = "满减活动信息", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") - private String rewardActivity; + @Schema(description = "优惠规则描述", requiredMode = Schema.RequiredMode.REQUIRED, example = "满 0.5 元减 0.3") + private List ruleDescriptions; } - /** - * SKU 数组 - */ + @Schema(description = "SKU 价格信息") @Data - @Builder public static class Sku implements Serializable { @Schema(description = "商品 SKU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") - private Long skuId; + private Long id; - @Schema(description = "价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") - private Integer price; + @Schema(description = "支付价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") + private Integer payPrice; // 优惠后价格 @Schema(description = "营销类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") - private Integer type; // 对应 PromotionTypeEnum 枚举 + private Integer promotionType; // 对应 PromotionTypeEnum 枚举 - @Schema(description = "限时优惠id", requiredMode = Schema.RequiredMode.REQUIRED) - private Long discountId; + @Schema(description = "营销编号", requiredMode = Schema.RequiredMode.REQUIRED) + private Long promotionId; // 目前只有限时折扣活动的编号 @Schema(description = "活动结束时间", requiredMode = Schema.RequiredMode.REQUIRED) - private LocalDateTime endTime; + private LocalDateTime promotionEndTime; } + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java index 300da3f9f..12cc6000d 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderUpdateServiceImpl.java @@ -166,7 +166,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService { TradePriceCalculateReqBO calculateReqBO = TradeOrderConvert.INSTANCE.convert(userId, settlementReqVO, cartList); calculateReqBO.getItems().forEach(item -> Assert.isTrue(item.getSelected(), // 防御性编程,保证都是选中的 "商品({}) 未设置为选中", item.getSkuId())); - return tradePriceService.calculatePrice(calculateReqBO); + return tradePriceService.calculateOrderPrice(calculateReqBO); } @Override diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/TradePriceService.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/TradePriceService.java index bb1a8bf07..8af150697 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/TradePriceService.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/TradePriceService.java @@ -1,10 +1,12 @@ package cn.iocoder.yudao.module.trade.service.price; +import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeProductSettlementRespVO; import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO; - import jakarta.validation.Valid; +import java.util.List; + /** * 价格计算 Service 接口 * @@ -13,11 +15,20 @@ import jakarta.validation.Valid; public interface TradePriceService { /** - * 价格计算 + * 【订单】价格计算 * * @param calculateReqDTO 计算信息 * @return 计算结果 */ - TradePriceCalculateRespBO calculatePrice(@Valid TradePriceCalculateReqBO calculateReqDTO); + TradePriceCalculateRespBO calculateOrderPrice(@Valid TradePriceCalculateReqBO calculateReqDTO); + + /** + * 【商品】价格计算,用于商品列表、商品详情 + * + * @param userId 用户编号,允许为空 + * @param spuIds 商品 SPU 编号数组 + * @return 计算结果 + */ + List calculateProductPrice(Long userId, List spuIds); } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/TradePriceServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/TradePriceServiceImpl.java index e92d75d62..49fd66b70 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/TradePriceServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/TradePriceServiceImpl.java @@ -1,24 +1,33 @@ package cn.iocoder.yudao.module.trade.service.price; +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; +import cn.iocoder.yudao.module.member.api.level.dto.MemberLevelRespDTO; import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi; import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO; import cn.iocoder.yudao.module.product.api.spu.ProductSpuApi; import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO; +import cn.iocoder.yudao.module.promotion.api.discount.DiscountActivityApi; +import cn.iocoder.yudao.module.promotion.api.discount.dto.DiscountProductRespDTO; +import cn.iocoder.yudao.module.promotion.api.reward.RewardActivityApi; +import cn.iocoder.yudao.module.promotion.api.reward.dto.RewardActivityMatchRespDTO; +import cn.iocoder.yudao.module.promotion.enums.common.PromotionTypeEnum; +import cn.iocoder.yudao.module.trade.controller.app.order.vo.AppTradeProductSettlementRespVO; import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO; +import cn.iocoder.yudao.module.trade.service.price.calculator.TradeDiscountActivityPriceCalculator; import cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculator; import cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculatorHelper; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; -import jakarta.annotation.Resource; +import java.time.LocalDateTime; import java.util.List; import java.util.Map; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet; +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_NOT_EXISTS; import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_STOCK_NOT_ENOUGH; import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.PRICE_CALCULATE_PAY_PRICE_ILLEGAL; @@ -37,12 +46,19 @@ public class TradePriceServiceImpl implements TradePriceService { private ProductSkuApi productSkuApi; @Resource private ProductSpuApi productSpuApi; + @Resource + private DiscountActivityApi discountActivityApi; + @Resource + private RewardActivityApi rewardActivityApi; @Resource private List priceCalculators; + @Resource + private TradeDiscountActivityPriceCalculator discountActivityPriceCalculator; + @Override - public TradePriceCalculateRespBO calculatePrice(TradePriceCalculateReqBO calculateReqBO) { + public TradePriceCalculateRespBO calculateOrderPrice(TradePriceCalculateReqBO calculateReqBO) { // 1.1 获得商品 SKU 数组 List skuList = checkSkuList(calculateReqBO); // 1.2 获得商品 SPU 数组 @@ -85,4 +101,60 @@ public class TradePriceServiceImpl implements TradePriceService { return productSpuApi.validateSpuList(convertSet(skuList, ProductSkuRespDTO::getSpuId)); } + @Override + public List calculateProductPrice(Long userId, List spuIds) { + // 1.1 获得 SPU 与 SKU 的映射 + List allSkuList = productSkuApi.getSkuListBySpuId(spuIds); + Map> spuIdAndSkuListMap = convertMultiMap(allSkuList, ProductSkuRespDTO::getSpuId); + // 1.2 获得会员等级 + MemberLevelRespDTO level = discountActivityPriceCalculator.getMemberLevel(userId); + // 1.3 获得限时折扣活动 + Map skuIdAndDiscountMap = convertMap( + discountActivityApi.getMatchDiscountProductList(convertSet(allSkuList, ProductSkuRespDTO::getId)), + DiscountProductRespDTO::getSkuId); + // 1.4 获得满减送活动 + // TODO 芋艿:这里是有问题的,后面 fix + Map rewardActivityMap = convertMap( + rewardActivityApi.getRewardActivityBySpuIdsAndStatusAndDateTimeLt(spuIds, CommonStatusEnum.ENABLE.getStatus(), LocalDateTime.now()), + RewardActivityMatchRespDTO::getId); + + // 2. 价格计算 + return convertList(spuIds, spuId -> { + AppTradeProductSettlementRespVO spuVO = new AppTradeProductSettlementRespVO().setSpuId(spuId); + // 2.1 优惠价格 + List skuList = spuIdAndSkuListMap.get(spuId); + List skuVOList = convertList(skuList, sku -> { + AppTradeProductSettlementRespVO.Sku skuVO = new AppTradeProductSettlementRespVO.Sku() + .setId(sku.getId()).setPayPrice(sku.getPrice()); + TradePriceCalculateRespBO.OrderItem orderItem = new TradePriceCalculateRespBO.OrderItem() + .setPayPrice(sku.getPrice()).setCount(1); + // 计算限时折扣的优惠价格 + DiscountProductRespDTO discountProduct = skuIdAndDiscountMap.get(orderItem.getSkuId()); + Integer discountPrice = discountActivityPriceCalculator.calculateActivityPrice(discountProduct, orderItem); + // 计算 VIP 优惠金额 + Integer vipPrice = discountActivityPriceCalculator.calculateVipPrice(level, orderItem); + if (discountPrice <= 0 && vipPrice <= 0) { + return skuVO; + } + // 选择一个大的优惠 + if (discountPrice > vipPrice) { + return skuVO.setPayPrice(sku.getPrice() - discountPrice) + .setPromotionType(PromotionTypeEnum.DISCOUNT_ACTIVITY.getType()) + .setPromotionId(discountProduct.getId()).setPromotionEndTime(discountProduct.getActivityEndTime()); + } else { + return skuVO.setPayPrice(sku.getPrice() - vipPrice) + .setPromotionType(PromotionTypeEnum.MEMBER_LEVEL.getType()); + } + }); + spuVO.setSkus(skuVOList); + // 2.2 满减送活动 + RewardActivityMatchRespDTO rewardActivity = rewardActivityMap.get(spuId); + if (rewardActivity != null) { + spuVO.setRewardActivity(new AppTradeProductSettlementRespVO.RewardActivity().setId(rewardActivity.getId()) + .setRuleDescriptions(convertList(rewardActivity.getRules(), RewardActivityMatchRespDTO.Rule::getDescription))); + } + return spuVO; + }); + } + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDiscountActivityPriceCalculator.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDiscountActivityPriceCalculator.java index 6388932b4..79258da84 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDiscountActivityPriceCalculator.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDiscountActivityPriceCalculator.java @@ -55,8 +55,7 @@ public class TradeDiscountActivityPriceCalculator implements TradePriceCalculato convertSet(result.getItems(), TradePriceCalculateRespBO.OrderItem::getSkuId)); Map discountProductMap = convertMap(discountProducts, DiscountProductRespDTO::getSkuId); // 1.2 获得会员等级 - MemberUserRespDTO user = memberUserApi.getUser(param.getUserId()); - MemberLevelRespDTO level = user != null && user.getLevelId() > 0 ? memberLevelApi.getMemberLevel(user.getLevelId()) : null; + MemberLevelRespDTO level = getMemberLevel(param.getUserId()); // 2. 计算每个 SKU 的优惠金额 result.getItems().forEach(orderItem -> { @@ -96,6 +95,20 @@ public class TradeDiscountActivityPriceCalculator implements TradePriceCalculato }); } + /** + * 获得用户的等级 + * + * @param userId 用户编号 + * @return 用户等级 + */ + public MemberLevelRespDTO getMemberLevel(Long userId) { + MemberUserRespDTO user = memberUserApi.getUser(userId); + if (user == null || user.getLevelId() == null || user.getLevelId() <= 0) { + return null; + } + return memberLevelApi.getMemberLevel(user.getLevelId()); + } + /** * 计算优惠活动的价格 * @@ -103,7 +116,7 @@ public class TradeDiscountActivityPriceCalculator implements TradePriceCalculato * @param orderItem 交易项 * @return 优惠价格 */ - private Integer calculateActivityPrice(DiscountProductRespDTO discount, + public Integer calculateActivityPrice(DiscountProductRespDTO discount, TradePriceCalculateRespBO.OrderItem orderItem) { if (discount == null) { return 0; @@ -127,7 +140,7 @@ public class TradeDiscountActivityPriceCalculator implements TradePriceCalculato * @return 优惠价格 */ public Integer calculateVipPrice(MemberLevelRespDTO level, - TradePriceCalculateRespBO.OrderItem orderItem) { + TradePriceCalculateRespBO.OrderItem orderItem) { if (level == null || level.getDiscountPercent() == null) { return 0; } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeRewardActivityPriceCalculator.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeRewardActivityPriceCalculator.java index 73949738c..afbb869dd 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeRewardActivityPriceCalculator.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeRewardActivityPriceCalculator.java @@ -54,7 +54,7 @@ public class TradeRewardActivityPriceCalculator implements TradePriceCalculator return; } // 处理最新的满减送活动 - if(!rewardActivities.isEmpty()){ + if (!rewardActivities.isEmpty()) { calculate(param, result, rewardActivities.get(0)); } } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/TradePriceServiceImplTest.java b/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/TradePriceServiceImplTest.java index b3900e04b..993a43577 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/TradePriceServiceImplTest.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/TradePriceServiceImplTest.java @@ -72,7 +72,7 @@ public class TradePriceServiceImplTest extends BaseMockitoUnitTest { .setStatus(ProductSpuStatusEnum.ENABLE.getStatus()))); // 调用 - TradePriceCalculateRespBO calculateRespBO = tradePriceService.calculatePrice(calculateReqBO); + TradePriceCalculateRespBO calculateRespBO = tradePriceService.calculateOrderPrice(calculateReqBO); // 断言 assertEquals(TradeOrderTypeEnum.NORMAL.getType(), calculateRespBO.getType()); assertEquals(0, calculateRespBO.getPromotions().size());