mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-26 01:01:52 +08:00
promotion:完善价格计算,重新接入限时折扣的多种优惠方式
This commit is contained in:
parent
c87bc084e6
commit
636cc794dd
@ -40,4 +40,7 @@ public interface ErrorCodeConstants {
|
||||
ErrorCode REWARD_ACTIVITY_CLOSE_FAIL_STATUS_CLOSED = new ErrorCode(1003006004, "满减送活动已关闭,不能重复关闭");
|
||||
ErrorCode REWARD_ACTIVITY_CLOSE_FAIL_STATUS_END = new ErrorCode(1003006004, "满减送活动已结束,不能关闭");
|
||||
|
||||
// ========== Price 相关 1003007000 ============
|
||||
ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1003007000, "支付价格计算异常,原因:价格小于等于 0");
|
||||
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountAc
|
||||
import cn.iocoder.yudao.module.promotion.controller.admin.discount.vo.DiscountActivityUpdateReqVO;
|
||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountActivityDO;
|
||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProductDO;
|
||||
import cn.iocoder.yudao.module.promotion.service.discount.bo.DiscountProductDetailBO;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Collection;
|
||||
@ -27,7 +28,7 @@ public interface DiscountActivityService {
|
||||
* @param skuIds SKU 编号数组
|
||||
* @return 匹配的限时折扣商品
|
||||
*/
|
||||
Map<Long, DiscountProductDO> getMatchDiscountProducts(Collection<Long> skuIds);
|
||||
Map<Long, DiscountProductDetailBO> getMatchDiscountProducts(Collection<Long> skuIds);
|
||||
|
||||
/**
|
||||
* 创建限时折扣活动
|
||||
|
@ -23,8 +23,7 @@ import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
||||
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.promotion.enums.ErrorCodeConstants.*;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
@ -42,13 +41,10 @@ public class DiscountActivityServiceImpl implements DiscountActivityService {
|
||||
@Resource
|
||||
private DiscountProductMapper discountProductMapper;
|
||||
|
||||
// TODO 芋艿:待实现
|
||||
@Override
|
||||
public Map<Long, DiscountProductDO> getMatchDiscountProducts(Collection<Long> skuIds) {
|
||||
Map<Long, DiscountProductDO> products = new HashMap<>();
|
||||
products.put(1L, new DiscountProductDO().setDiscountPrice(100));
|
||||
products.put(2L, new DiscountProductDO().setDiscountPrice(50));
|
||||
return products;
|
||||
public Map<Long, DiscountProductDetailBO> getMatchDiscountProducts(Collection<Long> skuIds) {
|
||||
List<DiscountProductDetailBO> discountProducts = getRewardProductListBySkuIds(skuIds, singleton(PromotionActivityStatusEnum.RUN.getStatus()));
|
||||
return convertMap(discountProducts, DiscountProductDetailBO::getSkuId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -116,7 +112,7 @@ public class DiscountActivityServiceImpl implements DiscountActivityService {
|
||||
return;
|
||||
}
|
||||
// 查询商品参加的活动
|
||||
List<DiscountProductDetailBO> discountActivityProductList = getRewardActivityListBySkuIds(
|
||||
List<DiscountProductDetailBO> discountActivityProductList = getRewardProductListBySkuIds(
|
||||
convertSet(products, DiscountActivityBaseVO.Product::getSkuId),
|
||||
asList(PromotionActivityStatusEnum.WAIT.getStatus(), PromotionActivityStatusEnum.RUN.getStatus()));
|
||||
if (id != null) { // 排除自己这个活动
|
||||
@ -128,8 +124,8 @@ public class DiscountActivityServiceImpl implements DiscountActivityService {
|
||||
}
|
||||
}
|
||||
|
||||
private List<DiscountProductDetailBO> getRewardActivityListBySkuIds(Collection<Long> skuIds,
|
||||
Collection<Integer> statuses) {
|
||||
private List<DiscountProductDetailBO> getRewardProductListBySkuIds(Collection<Long> skuIds,
|
||||
Collection<Integer> statuses) {
|
||||
// 查询商品
|
||||
List<DiscountProductDO> products = discountProductMapper.selectListBySkuId(skuIds);
|
||||
if (CollUtil.isEmpty(products)) {
|
||||
|
@ -29,7 +29,15 @@ public class DiscountProductDetailBO {
|
||||
*/
|
||||
private Long skuId;
|
||||
/**
|
||||
* 优惠价格,单位:分
|
||||
* 折扣类型
|
||||
*/
|
||||
private Integer discountType;
|
||||
/**
|
||||
* 折扣百分比
|
||||
*/
|
||||
private Integer discountPercent;
|
||||
/**
|
||||
* 优惠金额,单位:分
|
||||
*/
|
||||
private Integer discountPrice;
|
||||
|
||||
|
@ -4,19 +4,20 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
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.price.dto.PriceCalculateReqDTO;
|
||||
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateRespDTO;
|
||||
import cn.iocoder.yudao.module.promotion.convert.price.PriceConvert;
|
||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO;
|
||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProductDO;
|
||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO;
|
||||
import cn.iocoder.yudao.module.promotion.enums.common.*;
|
||||
import cn.iocoder.yudao.module.promotion.service.coupon.CouponService;
|
||||
import cn.iocoder.yudao.module.promotion.service.discount.DiscountActivityService;
|
||||
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.service.discount.bo.DiscountProductDetailBO;
|
||||
import cn.iocoder.yudao.module.promotion.service.reward.RewardActivityService;
|
||||
import com.google.common.base.Suppliers;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
@ -30,9 +31,8 @@ import java.util.function.Supplier;
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getSumValue;
|
||||
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_NO_MATCH_MIN_PRICE;
|
||||
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.COUPON_NO_MATCH_SPU;
|
||||
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.SKU_NOT_EXISTS;
|
||||
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
|
||||
import static java.util.Collections.singletonList;
|
||||
|
||||
/**
|
||||
@ -51,6 +51,7 @@ import static java.util.Collections.singletonList;
|
||||
*/
|
||||
@Service
|
||||
@Validated
|
||||
@Slf4j
|
||||
public class PriceServiceImpl implements PriceService {
|
||||
|
||||
@Resource
|
||||
@ -76,6 +77,13 @@ public class PriceServiceImpl implements PriceService {
|
||||
calculatePriceForOrderLevel(calculateReqDTO.getUserId(), priceCalculate);
|
||||
// 计算优惠劵级别的价格
|
||||
calculatePriceForCouponLevel(calculateReqDTO.getUserId(), calculateReqDTO.getCouponId(), priceCalculate);
|
||||
|
||||
// 如果最终支付金额小于等于 0,则抛出业务异常
|
||||
if (priceCalculate.getOrder().getPayPrice() <= 0) {
|
||||
log.error("[calculatePrice][价格计算不正确,请求 calculateReqDTO({}),结果 priceCalculate({})]",
|
||||
calculateReqDTO, priceCalculate);
|
||||
throw exception(PRICE_CALCULATE_PAY_PRICE_ILLEGAL);
|
||||
}
|
||||
return priceCalculate;
|
||||
}
|
||||
|
||||
@ -111,24 +119,20 @@ public class PriceServiceImpl implements PriceService {
|
||||
private void calculatePriceForSkuLevel(Long userId, PriceCalculateRespDTO priceCalculate) {
|
||||
// 获取 SKU 级别的所有优惠信息
|
||||
Supplier<Double> memberDiscountPercentSupplier = getMemberDiscountPercentSupplier(userId);
|
||||
Map<Long, DiscountProductDO> discountProducts = discountService.getMatchDiscountProducts(
|
||||
Map<Long, DiscountProductDetailBO> discountProducts = discountService.getMatchDiscountProducts(
|
||||
convertSet(priceCalculate.getOrder().getItems(), PriceCalculateRespDTO.OrderItem::getSkuId));
|
||||
|
||||
// 处理每个 SKU 的优惠
|
||||
priceCalculate.getOrder().getItems().forEach(orderItem -> {
|
||||
// 获取该 SKU 的优惠信息
|
||||
Double memberDiscountPercent = memberDiscountPercentSupplier.get();
|
||||
DiscountProductDO discountProduct = discountProducts.get(orderItem.getSkuId());
|
||||
if (discountProduct != null // 假设优惠价格更贵,则认为没优惠
|
||||
&& discountProduct.getDiscountPrice() >= orderItem.getOriginalUnitPrice()) {
|
||||
discountProduct = null;
|
||||
}
|
||||
DiscountProductDetailBO discountProduct = discountProducts.get(orderItem.getSkuId());
|
||||
if (memberDiscountPercent == null && discountProduct == null) {
|
||||
return;
|
||||
}
|
||||
// 计算价格,判断选择哪个折扣
|
||||
Integer memberPrice = memberDiscountPercent != null ? (int) (orderItem.getPayPrice() * memberDiscountPercent / 100) : null;
|
||||
Integer promotionPrice = discountProduct != null ? discountProduct.getDiscountPrice() * orderItem.getCount() : null;
|
||||
Integer promotionPrice = discountProduct != null ? getDiscountProductPrice(discountProduct, orderItem) : null;
|
||||
if (memberPrice == null) {
|
||||
calculatePriceByDiscountActivity(priceCalculate, orderItem, discountProduct, promotionPrice);
|
||||
} else if (promotionPrice == null) {
|
||||
@ -141,6 +145,19 @@ public class PriceServiceImpl implements PriceService {
|
||||
});
|
||||
}
|
||||
|
||||
private Integer getDiscountProductPrice(DiscountProductDetailBO discountProduct,
|
||||
PriceCalculateRespDTO.OrderItem orderItem) {
|
||||
Integer price = orderItem.getPayPrice();
|
||||
if (PromotionDiscountTypeEnum.PRICE.getType().equals(discountProduct.getDiscountType())) { // 减价
|
||||
price -= discountProduct.getDiscountPrice() * orderItem.getCount();
|
||||
} else if (PromotionDiscountTypeEnum.PERCENT.getType().equals(discountProduct.getDiscountType())) { // 打折
|
||||
price = price * discountProduct.getDiscountPercent() / 100;
|
||||
} else {
|
||||
throw new IllegalArgumentException(String.format("优惠活动的商品(%s) 的优惠类型不正确", discountProduct));
|
||||
}
|
||||
return price;
|
||||
}
|
||||
|
||||
private void calculatePriceByMemberDiscount(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem,
|
||||
Integer memberPrice) {
|
||||
// 记录优惠明细
|
||||
@ -152,10 +169,9 @@ public class PriceServiceImpl implements PriceService {
|
||||
}
|
||||
|
||||
private void calculatePriceByDiscountActivity(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem,
|
||||
DiscountProductDO discountProduct, Integer promotionPrice) {
|
||||
DiscountProductDetailBO discountProduct, Integer promotionPrice) {
|
||||
// 记录优惠明细
|
||||
addPromotion(priceCalculate, orderItem, discountProduct.getActivityId(), null
|
||||
/* TODO 芋艿:修复下 discountProduct.getActivityName()*/,
|
||||
addPromotion(priceCalculate, orderItem, discountProduct.getActivityId(), discountProduct.getActivityName(),
|
||||
PromotionTypeEnum.DISCOUNT_ACTIVITY.getType(), PromotionLevelEnum.SKU.getLevel(), promotionPrice,
|
||||
true, StrUtil.format("限时折扣:省 {} 元", formatPrice(orderItem.getPayPrice() - promotionPrice)));
|
||||
// 修改 SKU 的优惠
|
||||
@ -213,7 +229,6 @@ public class PriceServiceImpl implements PriceService {
|
||||
}
|
||||
|
||||
// 分摊金额
|
||||
// TODO 芋艿:limit 不能超过最大价格
|
||||
List<Integer> discountPartPrices = dividePrice(orderItems, rule.getDiscountPrice());
|
||||
// 记录优惠明细
|
||||
addPromotion(priceCalculate, orderItems, rewardActivity.getId(), rewardActivity.getName(),
|
||||
@ -288,7 +303,6 @@ public class PriceServiceImpl implements PriceService {
|
||||
priceCalculate.getOrder().setCouponId(couponId);
|
||||
Integer couponPrice = getCouponPrice(coupon, originPrice);
|
||||
// 分摊金额
|
||||
// TODO 芋艿:limit 不能超过最大价格
|
||||
List<Integer> couponPartPrices = dividePrice(orderItems, couponPrice);
|
||||
// 记录优惠明细
|
||||
addPromotion(priceCalculate, orderItems, coupon.getId(), coupon.getName(),
|
||||
|
@ -7,11 +7,11 @@ import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
|
||||
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateReqDTO;
|
||||
import cn.iocoder.yudao.module.promotion.api.price.dto.PriceCalculateRespDTO;
|
||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.coupon.CouponDO;
|
||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.discount.DiscountProductDO;
|
||||
import cn.iocoder.yudao.module.promotion.dal.dataobject.reward.RewardActivityDO;
|
||||
import cn.iocoder.yudao.module.promotion.enums.common.*;
|
||||
import cn.iocoder.yudao.module.promotion.service.coupon.CouponService;
|
||||
import cn.iocoder.yudao.module.promotion.service.discount.DiscountActivityService;
|
||||
import cn.iocoder.yudao.module.promotion.service.discount.bo.DiscountProductDetailBO;
|
||||
import cn.iocoder.yudao.module.promotion.service.reward.RewardActivityService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
@ -109,13 +109,18 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
|
||||
ProductSkuRespDTO productSku02 = randomPojo(ProductSkuRespDTO.class, o -> o.setId(20L).setPrice(50));
|
||||
when(productSkuApi.getSkuList(eq(asSet(10L, 20L)))).thenReturn(asList(productSku01, productSku02));
|
||||
// mock 方法(限时折扣 DiscountActivity 信息)
|
||||
DiscountProductDO discountProduct01 = randomPojo(DiscountProductDO.class, o -> o.setActivityId(1000L)/*.setActsivityName("活动 1000 号") TODO 芋艿:待完善 */
|
||||
.setSkuId(10L).setDiscountPrice(80));
|
||||
DiscountProductDO discountProduct02 = randomPojo(DiscountProductDO.class, o -> o.setActivityId(2000L)/*.setActivityName("活动 2000 号") TODO 芋艿:待完善 */
|
||||
.setSkuId(20L).setDiscountPrice(80));
|
||||
DiscountProductDetailBO discountProduct01 = randomPojo(DiscountProductDetailBO.class, o -> o.setActivityId(1000L)
|
||||
.setActivityName("活动 1000 号").setSkuId(10L)
|
||||
.setDiscountType(PromotionDiscountTypeEnum.PRICE.getType()).setDiscountPrice(40));
|
||||
DiscountProductDetailBO discountProduct02 = randomPojo(DiscountProductDetailBO.class, o -> o.setActivityId(2000L)
|
||||
.setActivityName("活动 2000 号").setSkuId(20L)
|
||||
.setDiscountType(PromotionDiscountTypeEnum.PERCENT.getType()).setDiscountPercent(60));
|
||||
when(discountService.getMatchDiscountProducts(eq(asSet(10L, 20L)))).thenReturn(
|
||||
MapUtil.builder(10L, discountProduct01).put(20L, discountProduct02).map());
|
||||
|
||||
// 10L: 100 * 2 - 40 * 2 = 120
|
||||
// 20L:50 * 3 - 50 * 3 * 0.4 = 90
|
||||
|
||||
// 调用
|
||||
PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO);
|
||||
// 断言 Order 部分
|
||||
@ -124,7 +129,7 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
|
||||
assertEquals(order.getDiscountPrice(), 0);
|
||||
assertEquals(order.getPointPrice(), 0);
|
||||
assertEquals(order.getDeliveryPrice(), 0);
|
||||
assertEquals(order.getPayPrice(), 280);
|
||||
assertEquals(order.getPayPrice(), 210);
|
||||
assertNull(order.getCouponId());
|
||||
// 断言 OrderItem 部分
|
||||
assertEquals(order.getItems().size(), 2);
|
||||
@ -133,19 +138,19 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
|
||||
assertEquals(orderItem01.getCount(), 2);
|
||||
assertEquals(orderItem01.getOriginalPrice(), 200);
|
||||
assertEquals(orderItem01.getOriginalUnitPrice(), 100);
|
||||
assertEquals(orderItem01.getDiscountPrice(), 40);
|
||||
assertEquals(orderItem01.getPayPrice(), 160);
|
||||
assertEquals(orderItem01.getDiscountPrice(), 80);
|
||||
assertEquals(orderItem01.getPayPrice(), 120);
|
||||
assertEquals(orderItem01.getOrderPartPrice(), 0);
|
||||
assertEquals(orderItem01.getOrderDividePrice(), 160);
|
||||
assertEquals(orderItem01.getOrderDividePrice(), 120);
|
||||
PriceCalculateRespDTO.OrderItem orderItem02 = order.getItems().get(1);
|
||||
assertEquals(orderItem02.getSkuId(), 20L);
|
||||
assertEquals(orderItem02.getCount(), 3);
|
||||
assertEquals(orderItem02.getOriginalPrice(), 150);
|
||||
assertEquals(orderItem02.getOriginalUnitPrice(), 50);
|
||||
assertEquals(orderItem02.getDiscountPrice(), 30);
|
||||
assertEquals(orderItem02.getPayPrice(), 120);
|
||||
assertEquals(orderItem02.getDiscountPrice(), 60);
|
||||
assertEquals(orderItem02.getPayPrice(), 90);
|
||||
assertEquals(orderItem02.getOrderPartPrice(), 0);
|
||||
assertEquals(orderItem02.getOrderDividePrice(), 120);
|
||||
assertEquals(orderItem02.getOrderDividePrice(), 90);
|
||||
// 断言 Promotion 部分
|
||||
assertEquals(priceCalculate.getPromotions().size(), 2);
|
||||
PriceCalculateRespDTO.Promotion promotion01 = priceCalculate.getPromotions().get(0);
|
||||
@ -154,28 +159,28 @@ public class PriceServiceTest extends BaseMockitoUnitTest {
|
||||
assertEquals(promotion01.getType(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType());
|
||||
assertEquals(promotion01.getLevel(), PromotionLevelEnum.SKU.getLevel());
|
||||
assertEquals(promotion01.getOriginalPrice(), 200);
|
||||
assertEquals(promotion01.getDiscountPrice(), 40);
|
||||
assertEquals(promotion01.getDiscountPrice(), 80);
|
||||
assertTrue(promotion01.getMeet());
|
||||
assertEquals(promotion01.getMeetTip(), "限时折扣:省 0.40 元");
|
||||
assertEquals(promotion01.getMeetTip(), "限时折扣:省 0.80 元");
|
||||
PriceCalculateRespDTO.PromotionItem promotionItem01 = promotion01.getItems().get(0);
|
||||
assertEquals(promotion01.getItems().size(), 1);
|
||||
assertEquals(promotionItem01.getSkuId(), 10L);
|
||||
assertEquals(promotionItem01.getOriginalPrice(), 200);
|
||||
assertEquals(promotionItem01.getDiscountPrice(), 40);
|
||||
assertEquals(promotionItem01.getDiscountPrice(), 80);
|
||||
PriceCalculateRespDTO.Promotion promotion02 = priceCalculate.getPromotions().get(1);
|
||||
assertEquals(promotion02.getId(), 2000L);
|
||||
assertEquals(promotion02.getName(), "活动 2000 号");
|
||||
assertEquals(promotion02.getType(), PromotionTypeEnum.DISCOUNT_ACTIVITY.getType());
|
||||
assertEquals(promotion02.getLevel(), PromotionLevelEnum.SKU.getLevel());
|
||||
assertEquals(promotion02.getOriginalPrice(), 150);
|
||||
assertEquals(promotion02.getDiscountPrice(), 30);
|
||||
assertEquals(promotion02.getDiscountPrice(), 60);
|
||||
assertTrue(promotion02.getMeet());
|
||||
assertEquals(promotion02.getMeetTip(), "限时折扣:省 0.30 元");
|
||||
assertEquals(promotion02.getMeetTip(), "限时折扣:省 0.60 元");
|
||||
PriceCalculateRespDTO.PromotionItem promotionItem02 = promotion02.getItems().get(0);
|
||||
assertEquals(promotion02.getItems().size(), 1);
|
||||
assertEquals(promotionItem02.getSkuId(), 20L);
|
||||
assertEquals(promotionItem02.getOriginalPrice(), 150);
|
||||
assertEquals(promotionItem02.getDiscountPrice(), 30);
|
||||
assertEquals(promotionItem02.getDiscountPrice(), 60);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user