price:完成会员价格的计算~

pull/2/head
YunaiV 2022-10-29 21:46:19 +08:00
parent a88eda340b
commit 084d4baba2
8 changed files with 292 additions and 23 deletions

View File

@ -52,7 +52,7 @@ public class PriceCalculateRespDTO {
*/ */
private Integer originalPrice; private Integer originalPrice;
/** /**
* *
* *
* 200 10 80 * 200 10 80
* *
@ -101,8 +101,12 @@ public class PriceCalculateRespDTO {
* SKU * SKU
*/ */
@Data @Data
public static class OrderItem extends PriceCalculateReqDTO.Item { public static class OrderItem {
/**
* SKU
*/
private Long skuId;
/** /**
* *
*/ */
@ -141,14 +145,18 @@ public class PriceCalculateRespDTO {
/** /**
* *
* {@link Order#discountPrice}{@link Order#couponPrice} * {@link Order#discountPrice}{@link Order#couponPrice}{@link Order#pointPrice}
* *
* taobao order.part_mjz_discount * taobao order.part_mjz_discount
* 使
*/ */
private Integer orderPartPrice; private Integer orderPartPrice;
/** /**
* *
* *
* = {@link #payPrice}
* - {@link #orderPartPrice}
*
* taobao divide_order_fee * taobao divide_order_fee
*/ */
private Integer orderDividePrice; private Integer orderDividePrice;
@ -156,7 +164,7 @@ public class PriceCalculateRespDTO {
} }
/** /**
* *
*/ */
@Data @Data
public static class Promotion { public static class Promotion {
@ -186,11 +194,11 @@ public class PriceCalculateRespDTO {
/** /**
* *
*/ */
private Integer beforePrice; private Integer originalPrice;
/** /**
* *
*/ */
private Integer afterPrice; private Integer discountPrice;
/** /**
* SKU * SKU
*/ */
@ -225,11 +233,11 @@ public class PriceCalculateRespDTO {
/** /**
* *
*/ */
private Integer beforePrice; private Integer originalPrice;
/** /**
* *
*/ */
private Integer afterPrice; private Integer discountPrice;
} }

View File

@ -17,6 +17,8 @@ public enum PromotionTypeEnum implements IntArrayValuable {
DISCOUNT_ACTIVITY(1, "限时折扣"), DISCOUNT_ACTIVITY(1, "限时折扣"),
REWARD_ACTIVITY(2, "满减送"), REWARD_ACTIVITY(2, "满减送"),
MEMBER(3, "会员折扣"),
; ;
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(PromotionTypeEnum::getType).toArray(); public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(PromotionTypeEnum::getType).toArray();

View File

@ -19,20 +19,23 @@ public interface PriceConvert {
default PriceCalculateRespDTO convert(PriceCalculateReqDTO calculateReqDTO, List<ProductSkuRespDTO> skuList) { default PriceCalculateRespDTO convert(PriceCalculateReqDTO calculateReqDTO, List<ProductSkuRespDTO> skuList) {
// 创建 PriceCalculateRespDTO 对象 // 创建 PriceCalculateRespDTO 对象
PriceCalculateRespDTO priceCalculate = new PriceCalculateRespDTO(); PriceCalculateRespDTO priceCalculate = new PriceCalculateRespDTO();
priceCalculate.setOrder(new PriceCalculateRespDTO.Order().setOriginalPrice(0).setActivityPrice(0) // 创建它的 Order 属性
.setDeliveryPrice(0).setPayPrice(0).setItems(new ArrayList<>()) PriceCalculateRespDTO.Order order = new PriceCalculateRespDTO.Order().setOriginalPrice(0).setDiscountPrice(0)
.setCouponId(calculateReqDTO.getCouponId())); .setCouponPrice(0).setPointPrice(0).setDeliveryPrice(0).setPayPrice(0)
priceCalculate.setPromotions(new ArrayList<>()); .setItems(new ArrayList<>()).setCouponId(calculateReqDTO.getCouponId());
priceCalculate.setOrder(order).setPromotions(new ArrayList<>());
// 创建它的 OrderItem 属性 // 创建它的 OrderItem 属性
Map<Long, Integer> skuIdCountMap = CollectionUtils.convertMap(calculateReqDTO.getItems(), Map<Long, Integer> skuIdCountMap = CollectionUtils.convertMap(calculateReqDTO.getItems(),
PriceCalculateReqDTO.Item::getSkuId, PriceCalculateReqDTO.Item::getCount); PriceCalculateReqDTO.Item::getSkuId, PriceCalculateReqDTO.Item::getCount);
skuList.forEach(sku -> { skuList.forEach(sku -> {
Integer count = skuIdCountMap.get(sku.getId()); Integer count = skuIdCountMap.get(sku.getId());
PriceCalculateRespDTO.OrderItem orderItem = new PriceCalculateRespDTO.OrderItem().setCount(count) PriceCalculateRespDTO.OrderItem orderItem = new PriceCalculateRespDTO.OrderItem()
.setOriginalUnitPrice(sku.getPrice()).setOriginalPrice(sku.getPrice() * count).setActivityPrice(0); .setSkuId(sku.getId()).setCount(count).setOriginalUnitPrice(sku.getPrice())
orderItem.setPayPrice(orderItem.getPayPrice()).setPayUnitPrice(orderItem.getOriginalUnitPrice()) .setOriginalPrice(sku.getPrice() * count).setDiscountPrice(0).setOrderPartPrice(0);
.setPayPrice(orderItem.getPayPrice()); orderItem.setPayPrice(orderItem.getOriginalPrice()).setOrderDividePrice(orderItem.getOrderDividePrice());
priceCalculate.getOrder().getItems().add(orderItem); priceCalculate.getOrder().getItems().add(orderItem);
// 补充价格信息到 Order 中
order.setOriginalPrice(order.getOriginalPrice() + orderItem.getOriginalPrice()).setPayPrice(order.getOriginalPrice());
}); });
return priceCalculate; return priceCalculate;
} }

View File

@ -0,0 +1,25 @@
package cn.iocoder.yudao.module.market.service.discount;
import cn.iocoder.yudao.module.market.dal.dataobject.discount.DiscountProductDO;
import java.util.Collection;
import java.util.Map;
/**
* Service
*
* @author
*/
public interface DiscountService {
/**
* SKU
*
*
*
* @param skuIds SKU
* @return
*/
Map<Long, DiscountProductDO> getMatchDiscountProducts(Collection<Long> skuIds);
}

View File

@ -0,0 +1,29 @@
package cn.iocoder.yudao.module.market.service.discount;
import cn.iocoder.yudao.module.market.dal.dataobject.discount.DiscountProductDO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
/**
* Service
*
* @author
*/
@Service
@Validated
public class DiscountServiceImpl implements DiscountService {
// TODO 芋艿:待实现
@Override
public Map<Long, DiscountProductDO> getMatchDiscountProducts(Collection<Long> skuIds) {
Map<Long, DiscountProductDO> products = new HashMap<>();
products.put(1L, new DiscountProductDO().setPromotionPrice(100));
products.put(2L, new DiscountProductDO().setPromotionPrice(50));
return products;
}
}

View File

@ -1,4 +0,0 @@
/**
* TODO
*/
package cn.iocoder.yudao.module.market.service.discount;

View File

@ -1,47 +1,64 @@
package cn.iocoder.yudao.module.market.service.price; package cn.iocoder.yudao.module.market.service.price;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateReqDTO; import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateReqDTO;
import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateRespDTO; import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateRespDTO;
import cn.iocoder.yudao.module.market.convert.price.PriceConvert; import cn.iocoder.yudao.module.market.convert.price.PriceConvert;
import cn.iocoder.yudao.module.market.dal.dataobject.discount.DiscountProductDO;
import cn.iocoder.yudao.module.market.enums.common.PromotionLevelEnum;
import cn.iocoder.yudao.module.market.enums.common.PromotionTypeEnum;
import cn.iocoder.yudao.module.market.service.discount.DiscountService;
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 com.google.common.base.Suppliers;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Supplier;
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.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 java.util.Collections.singletonList;
/** /**
* Service * Service
* *
* min(, ) > >
*
* 1. <a href="https://help.youzan.com/displaylist/detail_4_4-1-60384"></a>
*
* @author * @author
*/ */
@Service @Service
@Validated @Validated
public class PriceServiceImpl implements PriceService { public class PriceServiceImpl implements PriceService {
@Resource
private DiscountService discountService;
@Resource @Resource
private ProductSkuApi productSkuApi; private ProductSkuApi productSkuApi;
@Override @Override
public PriceCalculateRespDTO calculatePrice(PriceCalculateReqDTO calculateReqDTO) { public PriceCalculateRespDTO calculatePrice(PriceCalculateReqDTO calculateReqDTO) {
// 获得商品 SKU 数组 // 获得商品 SKU 数组
List<ProductSkuRespDTO> skuList = checkProductSkus(calculateReqDTO); List<ProductSkuRespDTO> skuList = checkSkus(calculateReqDTO);
// 初始化 PriceCalculateRespDTO 对象 // 初始化 PriceCalculateRespDTO 对象
PriceCalculateRespDTO priceCalculate = PriceConvert.INSTANCE.convert(calculateReqDTO, skuList); PriceCalculateRespDTO priceCalculate = PriceConvert.INSTANCE.convert(calculateReqDTO, skuList);
// 计算【限时折扣】促销 TODO 待实现 // 计算商品级别的价格
calculatePriceForSkuLevel(calculateReqDTO.getUserId(), priceCalculate);
// 计算【满减送】促销 TODO 待实现 // 计算【满减送】促销 TODO 待实现
// 计算【优惠劵】促销 TODO 待实现 // 计算【优惠劵】促销 TODO 待实现
return priceCalculate; return priceCalculate;
} }
private List<ProductSkuRespDTO> checkProductSkus(PriceCalculateReqDTO calculateReqDTO) { private List<ProductSkuRespDTO> checkSkus(PriceCalculateReqDTO calculateReqDTO) {
// 获得商品 SKU 数组 // 获得商品 SKU 数组
Map<Long, Integer> skuIdCountMap = CollectionUtils.convertMap(calculateReqDTO.getItems(), Map<Long, Integer> skuIdCountMap = CollectionUtils.convertMap(calculateReqDTO.getItems(),
PriceCalculateReqDTO.Item::getSkuId, PriceCalculateReqDTO.Item::getCount); PriceCalculateReqDTO.Item::getSkuId, PriceCalculateReqDTO.Item::getCount);
@ -58,4 +75,107 @@ public class PriceServiceImpl implements PriceService {
return skus; return skus;
} }
/**
*
* 1.
* 2.
*
*
*
* @param userId
* @param priceCalculate
*/
private void calculatePriceForSkuLevel(Long userId, PriceCalculateRespDTO priceCalculate) {
// 获取 SKU 级别的所有优惠信息
Supplier<Double> memberDiscountSupplier = getMemberDiscountSupplier(userId);
Map<Long, DiscountProductDO> discountProducts = discountService.getMatchDiscountProducts(
convertSet(priceCalculate.getOrder().getItems(), PriceCalculateRespDTO.OrderItem::getSkuId));
// 处理每个 SKU 的优惠
priceCalculate.getOrder().getItems().forEach(orderItem -> {
// 获取该 SKU 的优惠信息
Double memberDiscount = memberDiscountSupplier.get();
DiscountProductDO discountProduct = discountProducts.get(orderItem.getSkuId());
if (discountProduct != null // 假设优惠价格更贵,则认为没优惠
&& discountProduct.getPromotionPrice() >= orderItem.getOriginalUnitPrice()) {
discountProduct = null;
}
if (memberDiscount == null && discountProduct == null) {
return;
}
// 计算价格,判断选择哪个折扣
Integer memberPrice = memberDiscount != null ? (int) (orderItem.getPayPrice() * memberDiscount / 100) : null;
Integer promotionPrice = discountProduct != null ? discountProduct.getPromotionPrice() * orderItem.getCount() : null;
if (memberPrice == null) {
} else if (promotionPrice == null) {
calculatePriceByMemberDiscount(orderItem, memberDiscount, memberPrice, priceCalculate);
} else if (memberPrice < promotionPrice) {
} else {
calculatePriceByMemberDiscount(orderItem, memberDiscount, memberPrice, priceCalculate);
}
});
}
private void calculatePriceByMemberDiscount(PriceCalculateRespDTO.OrderItem orderItem,
Double memberDiscount, Integer memberPrice,
PriceCalculateRespDTO priceCalculate) {
// 记录优惠明细
addPromotion(priceCalculate, orderItem, null,
PromotionTypeEnum.MEMBER.getName(),
PromotionTypeEnum.MEMBER.getType(), PromotionLevelEnum.SKU.getLevel(), memberPrice,
true, StrUtil.format("会员折扣:省 {} 元", formatPrice(orderItem.getPayPrice() - memberPrice)));
// 修改 SKU 的优惠
modifyOrderItemPayPrice(orderItem, memberPrice, priceCalculate);
}
private void calculatePriceByDiscountActivity(PriceCalculateRespDTO.OrderItem orderItem,
DiscountProductDO discountProduct, Integer promotionPrice,
PriceCalculateRespDTO priceCalculate) {
}
private void addPromotion(PriceCalculateRespDTO priceCalculate, PriceCalculateRespDTO.OrderItem orderItem,
Long id, String name, Integer type, Integer level,
Integer newPayPrice, Boolean meet, String meetTip) {
// 创建营销明细 Item
PriceCalculateRespDTO.PromotionItem promotionItem = new PriceCalculateRespDTO.PromotionItem().setSkuId(orderItem.getSkuId())
.setOriginalPrice(orderItem.getPayPrice()).setDiscountPrice(orderItem.getPayPrice() - newPayPrice);
// 创建营销明细
PriceCalculateRespDTO.Promotion promotion = new PriceCalculateRespDTO.Promotion()
.setId(id).setName(name).setType(type).setLevel(level)
.setOriginalPrice(promotionItem.getOriginalPrice()).setDiscountPrice(promotionItem.getDiscountPrice())
.setItems(singletonList(promotionItem)).setMeet(meet).setMeetTip(meetTip);
priceCalculate.getPromotions().add(promotion);
}
private void modifyOrderItemPayPrice(PriceCalculateRespDTO.OrderItem orderItem, Integer newPayPrice,
PriceCalculateRespDTO priceCalculate) {
int diffPayPrice = orderItem.getPayPrice() - newPayPrice;
// 设置 OrderItem 价格相关字段
orderItem.setDiscountPrice(orderItem.getDiscountPrice() + diffPayPrice);
orderItem.setPayPrice(newPayPrice);
orderItem.setOrderDividePrice(orderItem.getPayPrice() - orderItem.getOrderPartPrice());
// 设置 Order 相关相关字段
priceCalculate.getOrder().setPayPrice(priceCalculate.getOrder().getPayPrice() - diffPayPrice);
}
// TODO 芋艿:提前实现
private Supplier<Double> getMemberDiscountSupplier(Long userId) {
return Suppliers.memoize(() -> {
if (userId == 1) {
return 90d;
}
if (userId == 2) {
return 80d;
}
return null; // 无优惠
});
}
private String formatPrice(Integer price) {
return String.format("%.2f", price / 100d);
}
} }

View File

@ -0,0 +1,86 @@
package cn.iocoder.yudao.module.market.service.price;
import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateReqDTO;
import cn.iocoder.yudao.module.market.api.price.dto.PriceCalculateRespDTO;
import cn.iocoder.yudao.module.market.enums.common.PromotionLevelEnum;
import cn.iocoder.yudao.module.market.enums.common.PromotionTypeEnum;
import cn.iocoder.yudao.module.market.service.discount.DiscountService;
import cn.iocoder.yudao.module.product.api.sku.ProductSkuApi;
import cn.iocoder.yudao.module.product.api.sku.dto.ProductSkuRespDTO;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
/**
* {@link PriceServiceImpl}
*
* @author
*/
public class PriceServiceTest extends BaseMockitoUnitTest {
@InjectMocks
private PriceServiceImpl priceService;
@Mock
private DiscountService discountService;
@Mock
private ProductSkuApi productSkuApi;
@Test
public void testCalculatePrice_memberDiscount() {
// 准备参数
// TODO 芋艿userId = 1实现 9 折;后续改成 mock
PriceCalculateReqDTO calculateReqDTO = new PriceCalculateReqDTO().setUserId(1L)
.setItems(singletonList(new PriceCalculateReqDTO.Item().setSkuId(10L).setCount(2)));
// mock 方法(商品 SKU 信息)
ProductSkuRespDTO productSku = randomPojo(ProductSkuRespDTO.class, o -> o.setId(10L).setPrice(100));
when(productSkuApi.getSkuList(eq(SetUtils.asSet(10L)))).thenReturn(singletonList(productSku));
// 调用
PriceCalculateRespDTO priceCalculate = priceService.calculatePrice(calculateReqDTO);
// 断言 Order 部分
PriceCalculateRespDTO.Order order = priceCalculate.getOrder();
assertEquals(order.getOriginalPrice(), 200);
assertEquals(order.getDiscountPrice(), 0);
assertEquals(order.getPointPrice(), 0);
assertEquals(order.getDeliveryPrice(), 0);
assertEquals(order.getPayPrice(), 180);
assertNull(order.getCouponId());
// 断言 OrderItem 部分
PriceCalculateRespDTO.OrderItem orderItem = order.getItems().get(0);
assertEquals(order.getItems().size(), 1);
assertEquals(orderItem.getSkuId(), 10L);
assertEquals(orderItem.getCount(), 2);
assertEquals(orderItem.getOriginalPrice(), 200);
assertEquals(orderItem.getOriginalUnitPrice(), 100);
assertEquals(orderItem.getDiscountPrice(), 20);
assertEquals(orderItem.getPayPrice(), 180);
assertEquals(orderItem.getOrderPartPrice(), 0);
assertEquals(orderItem.getOrderDividePrice(), 180);
// 断言 Promotion 部分
PriceCalculateRespDTO.Promotion promotion = priceCalculate.getPromotions().get(0);
assertEquals(priceCalculate.getPromotions().size(), 1);
assertNull(promotion.getId());
assertEquals(promotion.getName(), "会员折扣");
assertEquals(promotion.getType(), PromotionTypeEnum.MEMBER.getType());
assertEquals(promotion.getLevel(), PromotionLevelEnum.SKU.getLevel());
assertEquals(promotion.getOriginalPrice(), 200);
assertEquals(promotion.getDiscountPrice(), 20);
assertTrue(promotion.getMeet());
assertEquals(promotion.getMeetTip(), "会员折扣:省 0.20 元");
PriceCalculateRespDTO.PromotionItem promotionItem = promotion.getItems().get(0);
assertEquals(promotion.getItems().size(), 1);
assertEquals(promotionItem.getSkuId(), 10L);
assertEquals(promotionItem.getOriginalPrice(), 200);
assertEquals(promotionItem.getDiscountPrice(), 20);
}
}