mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2025-01-19 03:30:06 +08:00
【代码优化】商城:价格计算相关逻辑
This commit is contained in:
parent
fee7267799
commit
cb995ba047
@ -100,6 +100,13 @@ public class RewardActivityMatchRespDTO {
|
|||||||
*/
|
*/
|
||||||
private Map<Long, Integer> giveCouponTemplateCounts;
|
private Map<Long, Integer> giveCouponTemplateCounts;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 规则描述
|
||||||
|
*
|
||||||
|
* 通过 {@link #limit}、{@link #discountPrice} 等字段进行拼接
|
||||||
|
*/
|
||||||
|
private String description;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,23 +1,9 @@
|
|||||||
package cn.iocoder.yudao.module.trade.controller.app.order;
|
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.CommonResult;
|
||||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||||
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
|
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.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.*;
|
||||||
import cn.iocoder.yudao.module.trade.controller.app.order.vo.item.AppTradeOrderItemCommentCreateReqVO;
|
import cn.iocoder.yudao.module.trade.controller.app.order.vo.item.AppTradeOrderItemCommentCreateReqVO;
|
||||||
import cn.iocoder.yudao.module.trade.controller.app.order.vo.item.AppTradeOrderItemRespVO;
|
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.delivery.DeliveryExpressService;
|
||||||
import cn.iocoder.yudao.module.trade.service.order.TradeOrderQueryService;
|
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.order.TradeOrderUpdateService;
|
||||||
|
import cn.iocoder.yudao.module.trade.service.price.TradePriceService;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Operation;
|
||||||
import io.swagger.v3.oas.annotations.Parameter;
|
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.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.util.List;
|
||||||
import java.util.*;
|
import java.util.Map;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
||||||
@ -61,24 +48,14 @@ public class AppTradeOrderController {
|
|||||||
private TradeOrderQueryService tradeOrderQueryService;
|
private TradeOrderQueryService tradeOrderQueryService;
|
||||||
@Resource
|
@Resource
|
||||||
private DeliveryExpressService deliveryExpressService;
|
private DeliveryExpressService deliveryExpressService;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private AfterSaleService afterSaleService;
|
private AfterSaleService afterSaleService;
|
||||||
|
@Resource
|
||||||
|
private TradePriceService priceService;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private TradeOrderProperties tradeOrderProperties;
|
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")
|
@GetMapping("/settlement")
|
||||||
@Operation(summary = "获得订单结算信息")
|
@Operation(summary = "获得订单结算信息")
|
||||||
@PreAuthenticated
|
@PreAuthenticated
|
||||||
@ -86,56 +63,11 @@ public class AppTradeOrderController {
|
|||||||
return success(tradeOrderUpdateService.settlementOrder(getLoginUserId(), settlementReqVO));
|
return success(tradeOrderUpdateService.settlementOrder(getLoginUserId(), settlementReqVO));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/settlementProduct")
|
@GetMapping("/settlement-product")
|
||||||
@Operation(summary = "获得商品结算信息")
|
@Operation(summary = "获得商品结算信息", description = "用于商品列表、商品详情,获得参与活动后的价格信息")
|
||||||
public CommonResult<List<AppTradeProductSettlementRespVO>> settlementProduct(@RequestParam("ids") Set<Long> ids) {
|
@Parameter(name = "spuIds", description = "商品 SPU 编号数组")
|
||||||
List<AppTradeProductSettlementRespVO> appTradeProductSettlementRespVOS = new ArrayList<>();
|
public CommonResult<List<AppTradeProductSettlementRespVO>> settlementProduct(@RequestParam("spuIds") List<Long> spuIds) {
|
||||||
MemberLevelRespDTO memberLevel = getMemberLevel();
|
return success(priceService.calculateProductPrice(getLoginUserId(), spuIds));
|
||||||
ids.forEach(spuId -> {
|
|
||||||
List<AppTradeProductSettlementRespVO.Sku> skus = new ArrayList<>();
|
|
||||||
List<ProductSkuRespDTO> 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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/create")
|
@PostMapping("/create")
|
||||||
@ -265,78 +197,4 @@ public class AppTradeOrderController {
|
|||||||
return success(tradeOrderUpdateService.createOrderItemCommentByMember(getLoginUserId(), createReqVO));
|
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<DiscountProductRespDTO> 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<RewardActivityMatchRespDTO> 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package cn.iocoder.yudao.module.trade.controller.app.order.vo;
|
package cn.iocoder.yudao.module.trade.controller.app.order.vo;
|
||||||
|
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -10,54 +9,48 @@ import java.util.List;
|
|||||||
|
|
||||||
@Schema(description = "用户 App - 商品结算信息 Response VO")
|
@Schema(description = "用户 App - 商品结算信息 Response VO")
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
|
||||||
public class AppTradeProductSettlementRespVO {
|
public class AppTradeProductSettlementRespVO {
|
||||||
|
|
||||||
@Schema(description = "spu 商品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
@Schema(description = "SPU 商品编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
private Long id;
|
private Long spuId;
|
||||||
|
|
||||||
@Schema(description = "满减活动对象", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
@Schema(description = "SKU 价格信息数组", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
private Reward reward;
|
|
||||||
|
|
||||||
@Schema(description = "sku 活动信息", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
|
||||||
private List<Sku> skus;
|
private List<Sku> skus;
|
||||||
|
|
||||||
/**
|
@Schema(description = "满减送活动信息", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
* 满减活动
|
private RewardActivity rewardActivity;
|
||||||
*/
|
|
||||||
|
@Schema(description = "满减送活动信息")
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
public static class RewardActivity {
|
||||||
public static class Reward implements Serializable {
|
|
||||||
|
|
||||||
@Schema(description = "满减活动编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
@Schema(description = "满减活动编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Schema(description = "满减活动信息", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
@Schema(description = "优惠规则描述", requiredMode = Schema.RequiredMode.REQUIRED, example = "满 0.5 元减 0.3")
|
||||||
private String rewardActivity;
|
private List<String> ruleDescriptions;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Schema(description = "SKU 价格信息")
|
||||||
* SKU 数组
|
|
||||||
*/
|
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
|
||||||
public static class Sku implements Serializable {
|
public static class Sku implements Serializable {
|
||||||
|
|
||||||
@Schema(description = "商品 SKU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
@Schema(description = "商品 SKU 编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
private Long skuId;
|
private Long id;
|
||||||
|
|
||||||
@Schema(description = "价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
@Schema(description = "支付价格", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
private Integer price;
|
private Integer payPrice; // 优惠后价格
|
||||||
|
|
||||||
@Schema(description = "营销类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
@Schema(description = "营销类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||||
private Integer type; // 对应 PromotionTypeEnum 枚举
|
private Integer promotionType; // 对应 PromotionTypeEnum 枚举
|
||||||
|
|
||||||
@Schema(description = "限时优惠id", requiredMode = Schema.RequiredMode.REQUIRED)
|
@Schema(description = "营销编号", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
private Long discountId;
|
private Long promotionId; // 目前只有限时折扣活动的编号
|
||||||
|
|
||||||
@Schema(description = "活动结束时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
@Schema(description = "活动结束时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
private LocalDateTime endTime;
|
private LocalDateTime promotionEndTime;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -166,7 +166,7 @@ public class TradeOrderUpdateServiceImpl implements TradeOrderUpdateService {
|
|||||||
TradePriceCalculateReqBO calculateReqBO = TradeOrderConvert.INSTANCE.convert(userId, settlementReqVO, cartList);
|
TradePriceCalculateReqBO calculateReqBO = TradeOrderConvert.INSTANCE.convert(userId, settlementReqVO, cartList);
|
||||||
calculateReqBO.getItems().forEach(item -> Assert.isTrue(item.getSelected(), // 防御性编程,保证都是选中的
|
calculateReqBO.getItems().forEach(item -> Assert.isTrue(item.getSelected(), // 防御性编程,保证都是选中的
|
||||||
"商品({}) 未设置为选中", item.getSkuId()));
|
"商品({}) 未设置为选中", item.getSkuId()));
|
||||||
return tradePriceService.calculatePrice(calculateReqBO);
|
return tradePriceService.calculateOrderPrice(calculateReqBO);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package cn.iocoder.yudao.module.trade.service.price;
|
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.TradePriceCalculateReqBO;
|
||||||
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
|
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
|
||||||
|
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 价格计算 Service 接口
|
* 价格计算 Service 接口
|
||||||
*
|
*
|
||||||
@ -13,11 +15,20 @@ import jakarta.validation.Valid;
|
|||||||
public interface TradePriceService {
|
public interface TradePriceService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 价格计算
|
* 【订单】价格计算
|
||||||
*
|
*
|
||||||
* @param calculateReqDTO 计算信息
|
* @param calculateReqDTO 计算信息
|
||||||
* @return 计算结果
|
* @return 计算结果
|
||||||
*/
|
*/
|
||||||
TradePriceCalculateRespBO calculatePrice(@Valid TradePriceCalculateReqBO calculateReqDTO);
|
TradePriceCalculateRespBO calculateOrderPrice(@Valid TradePriceCalculateReqBO calculateReqDTO);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 【商品】价格计算,用于商品列表、商品详情
|
||||||
|
*
|
||||||
|
* @param userId 用户编号,允许为空
|
||||||
|
* @param spuIds 商品 SPU 编号数组
|
||||||
|
* @return 计算结果
|
||||||
|
*/
|
||||||
|
List<AppTradeProductSettlementRespVO> calculateProductPrice(Long userId, List<Long> spuIds);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,33 @@
|
|||||||
package cn.iocoder.yudao.module.trade.service.price;
|
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.ProductSkuApi;
|
||||||
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
|
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.ProductSpuApi;
|
||||||
import cn.iocoder.yudao.module.product.api.spu.dto.ProductSpuRespDTO;
|
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.TradePriceCalculateReqBO;
|
||||||
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateRespBO;
|
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.TradePriceCalculator;
|
||||||
import cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculatorHelper;
|
import cn.iocoder.yudao.module.trade.service.price.calculator.TradePriceCalculatorHelper;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
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.*;
|
||||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
|
||||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_NOT_EXISTS;
|
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.product.enums.ErrorCodeConstants.SKU_STOCK_NOT_ENOUGH;
|
||||||
import static cn.iocoder.yudao.module.trade.enums.ErrorCodeConstants.PRICE_CALCULATE_PAY_PRICE_ILLEGAL;
|
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;
|
private ProductSkuApi productSkuApi;
|
||||||
@Resource
|
@Resource
|
||||||
private ProductSpuApi productSpuApi;
|
private ProductSpuApi productSpuApi;
|
||||||
|
@Resource
|
||||||
|
private DiscountActivityApi discountActivityApi;
|
||||||
|
@Resource
|
||||||
|
private RewardActivityApi rewardActivityApi;
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
private List<TradePriceCalculator> priceCalculators;
|
private List<TradePriceCalculator> priceCalculators;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private TradeDiscountActivityPriceCalculator discountActivityPriceCalculator;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TradePriceCalculateRespBO calculatePrice(TradePriceCalculateReqBO calculateReqBO) {
|
public TradePriceCalculateRespBO calculateOrderPrice(TradePriceCalculateReqBO calculateReqBO) {
|
||||||
// 1.1 获得商品 SKU 数组
|
// 1.1 获得商品 SKU 数组
|
||||||
List<ProductSkuRespDTO> skuList = checkSkuList(calculateReqBO);
|
List<ProductSkuRespDTO> skuList = checkSkuList(calculateReqBO);
|
||||||
// 1.2 获得商品 SPU 数组
|
// 1.2 获得商品 SPU 数组
|
||||||
@ -85,4 +101,60 @@ public class TradePriceServiceImpl implements TradePriceService {
|
|||||||
return productSpuApi.validateSpuList(convertSet(skuList, ProductSkuRespDTO::getSpuId));
|
return productSpuApi.validateSpuList(convertSet(skuList, ProductSkuRespDTO::getSpuId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<AppTradeProductSettlementRespVO> calculateProductPrice(Long userId, List<Long> spuIds) {
|
||||||
|
// 1.1 获得 SPU 与 SKU 的映射
|
||||||
|
List<ProductSkuRespDTO> allSkuList = productSkuApi.getSkuListBySpuId(spuIds);
|
||||||
|
Map<Long, List<ProductSkuRespDTO>> spuIdAndSkuListMap = convertMultiMap(allSkuList, ProductSkuRespDTO::getSpuId);
|
||||||
|
// 1.2 获得会员等级
|
||||||
|
MemberLevelRespDTO level = discountActivityPriceCalculator.getMemberLevel(userId);
|
||||||
|
// 1.3 获得限时折扣活动
|
||||||
|
Map<Long, DiscountProductRespDTO> skuIdAndDiscountMap = convertMap(
|
||||||
|
discountActivityApi.getMatchDiscountProductList(convertSet(allSkuList, ProductSkuRespDTO::getId)),
|
||||||
|
DiscountProductRespDTO::getSkuId);
|
||||||
|
// 1.4 获得满减送活动
|
||||||
|
// TODO 芋艿:这里是有问题的,后面 fix
|
||||||
|
Map<Long, RewardActivityMatchRespDTO> 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<ProductSkuRespDTO> skuList = spuIdAndSkuListMap.get(spuId);
|
||||||
|
List<AppTradeProductSettlementRespVO.Sku> 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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -55,8 +55,7 @@ public class TradeDiscountActivityPriceCalculator implements TradePriceCalculato
|
|||||||
convertSet(result.getItems(), TradePriceCalculateRespBO.OrderItem::getSkuId));
|
convertSet(result.getItems(), TradePriceCalculateRespBO.OrderItem::getSkuId));
|
||||||
Map<Long, DiscountProductRespDTO> discountProductMap = convertMap(discountProducts, DiscountProductRespDTO::getSkuId);
|
Map<Long, DiscountProductRespDTO> discountProductMap = convertMap(discountProducts, DiscountProductRespDTO::getSkuId);
|
||||||
// 1.2 获得会员等级
|
// 1.2 获得会员等级
|
||||||
MemberUserRespDTO user = memberUserApi.getUser(param.getUserId());
|
MemberLevelRespDTO level = getMemberLevel(param.getUserId());
|
||||||
MemberLevelRespDTO level = user != null && user.getLevelId() > 0 ? memberLevelApi.getMemberLevel(user.getLevelId()) : null;
|
|
||||||
|
|
||||||
// 2. 计算每个 SKU 的优惠金额
|
// 2. 计算每个 SKU 的优惠金额
|
||||||
result.getItems().forEach(orderItem -> {
|
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 交易项
|
* @param orderItem 交易项
|
||||||
* @return 优惠价格
|
* @return 优惠价格
|
||||||
*/
|
*/
|
||||||
private Integer calculateActivityPrice(DiscountProductRespDTO discount,
|
public Integer calculateActivityPrice(DiscountProductRespDTO discount,
|
||||||
TradePriceCalculateRespBO.OrderItem orderItem) {
|
TradePriceCalculateRespBO.OrderItem orderItem) {
|
||||||
if (discount == null) {
|
if (discount == null) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -127,7 +140,7 @@ public class TradeDiscountActivityPriceCalculator implements TradePriceCalculato
|
|||||||
* @return 优惠价格
|
* @return 优惠价格
|
||||||
*/
|
*/
|
||||||
public Integer calculateVipPrice(MemberLevelRespDTO level,
|
public Integer calculateVipPrice(MemberLevelRespDTO level,
|
||||||
TradePriceCalculateRespBO.OrderItem orderItem) {
|
TradePriceCalculateRespBO.OrderItem orderItem) {
|
||||||
if (level == null || level.getDiscountPercent() == null) {
|
if (level == null || level.getDiscountPercent() == null) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -54,7 +54,7 @@ public class TradeRewardActivityPriceCalculator implements TradePriceCalculator
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// 处理最新的满减送活动
|
// 处理最新的满减送活动
|
||||||
if(!rewardActivities.isEmpty()){
|
if (!rewardActivities.isEmpty()) {
|
||||||
calculate(param, result, rewardActivities.get(0));
|
calculate(param, result, rewardActivities.get(0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ public class TradePriceServiceImplTest extends BaseMockitoUnitTest {
|
|||||||
.setStatus(ProductSpuStatusEnum.ENABLE.getStatus())));
|
.setStatus(ProductSpuStatusEnum.ENABLE.getStatus())));
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
TradePriceCalculateRespBO calculateRespBO = tradePriceService.calculatePrice(calculateReqBO);
|
TradePriceCalculateRespBO calculateRespBO = tradePriceService.calculateOrderPrice(calculateReqBO);
|
||||||
// 断言
|
// 断言
|
||||||
assertEquals(TradeOrderTypeEnum.NORMAL.getType(), calculateRespBO.getType());
|
assertEquals(TradeOrderTypeEnum.NORMAL.getType(), calculateRespBO.getType());
|
||||||
assertEquals(0, calculateRespBO.getPromotions().size());
|
assertEquals(0, calculateRespBO.getPromotions().size());
|
||||||
|
Loading…
Reference in New Issue
Block a user