mall + trade:优化运费模版的代码

This commit is contained in:
YunaiV 2023-06-17 11:35:31 +08:00
parent 07fe52939d
commit 59d5fef592
11 changed files with 112 additions and 102 deletions

View File

@ -45,19 +45,25 @@ public interface ErrorCodeConstants {
// ========== Cart 模块 1011002000 ==========
ErrorCode CARD_ITEM_NOT_FOUND = new ErrorCode(1011002000, "购物车项不存在");
// ========== 物流配送模块 1011003000 ==========
ErrorCode DELIVERY_EXPRESS_NOT_EXISTS = new ErrorCode(1011003000, "快递公司不存在");
// TODO @jason最好每个模块一段哈express 一个exmpresstemplate 一个pickup 一个
ErrorCode EXPRESS_CODE_DUPLICATE = new ErrorCode(1011003001, "已经存在该编码的快递公司");
ErrorCode EXPRESS_TEMPLATE_NOT_EXISTS = new ErrorCode(1011003002, "运费模板不存在");
ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011003003, "已经存在该运费模板名");
ErrorCode EXPRESS_API_QUERY_ERROR = new ErrorCode(1011003004, "快递查询接口异常");
ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011003005, "快递查询返回失败, 原因:{}");
ErrorCode EXPRESS_CLIENT_NOT_PROVIDE = new ErrorCode(1011003006, "需要接入快递服务商,比如【快递100】");
ErrorCode PICK_UP_STORE_NOT_EXISTS = new ErrorCode(1011003007, "自提门店不存在");
// ========== Price 相关 1011003000 ============
ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1011003000, "支付价格计算异常,原因:价格小于等于 0");
ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_USER_ADDR_IS_EMPTY = new ErrorCode(1011003001, "计算快递运费异常,收件人地址编号为空");
ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_TEMPLATE_NOT_FOUND = new ErrorCode(1011003002, "计算快递运费异常,找不到对应的运费模板");
// ========== 物流 Express 模块 1011004000 ==========
ErrorCode EXPRESS_NOT_EXISTS = new ErrorCode(1011004000, "快递公司不存在");
ErrorCode EXPRESS_CODE_DUPLICATE = new ErrorCode(1011004001, "已经存在该编码的快递公司");
ErrorCode EXPRESS_API_QUERY_ERROR = new ErrorCode(1011004002, "快递查询接口异常");
ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011004003, "快递查询返回失败,原因:{}");
ErrorCode EXPRESS_CLIENT_NOT_PROVIDE = new ErrorCode(1011004004, "需要接入快递服务商比如【快递100】");
// ========== 物流 Template 模块 1011005000 ==========
ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011005000, "已经存在该运费模板名");
ErrorCode EXPRESS_TEMPLATE_NOT_EXISTS = new ErrorCode(1011005001, "运费模板不存在");
// ========== 物流 PICK_UP 模块 1011006000 ==========
ErrorCode PICK_UP_STORE_NOT_EXISTS = new ErrorCode(1011006000, "自提门店不存在");
// ========== Price 相关 1011004000 ============
ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1011004000, "支付价格计算异常,原因:价格小于等于 0");
ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_USER_ADDR_IS_EMPTY = new ErrorCode(1011004001, "计算快递运费异常,收件人地址编号为空");
ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_TEMPLATE_NOT_FOUND = new ErrorCode(1011004002, "计算快递运费异常,找不到对应的运费模板");
}

View File

@ -7,10 +7,15 @@ import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemp
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateFreeDO;
import cn.iocoder.yudao.module.trade.service.delivery.bo.DeliveryExpressTemplateRespBO;
import com.google.common.collect.Maps;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMultiMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.findFirst;
@Mapper
public interface DeliveryExpressTemplateConvert {
@ -48,7 +53,7 @@ public interface DeliveryExpressTemplateConvert {
DeliveryExpressTemplateChargeDO convertTemplateCharge(DeliveryExpressTemplateUpdateReqVO.ExpressTemplateChargeUpdateVO vo);
DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO convertTemplateCharge(DeliveryExpressTemplateChargeDO bean);
DeliveryExpressTemplateRespBO.Charge convertTemplateCharge(DeliveryExpressTemplateChargeDO bean);
default List<DeliveryExpressTemplateChargeDO> convertTemplateChargeList(Long templateId, Integer chargeMode, List<ExpressTemplateChargeBaseVO> list) {
return CollectionUtils.convertList(list, vo -> convertTemplateCharge(templateId, chargeMode, vo));
@ -60,7 +65,7 @@ public interface DeliveryExpressTemplateConvert {
DeliveryExpressTemplateFreeDO convertTemplateFree(DeliveryExpressTemplateUpdateReqVO.ExpressTemplateFreeUpdateVO vo);
DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO convertTemplateFree(DeliveryExpressTemplateFreeDO bean);
DeliveryExpressTemplateRespBO.Free convertTemplateFree(DeliveryExpressTemplateFreeDO bean);
List<ExpressTemplateChargeBaseVO> convertTemplateChargeList(List<DeliveryExpressTemplateChargeDO> list);
@ -70,4 +75,22 @@ public interface DeliveryExpressTemplateConvert {
return CollectionUtils.convertList(list, vo -> convertTemplateFree(templateId, vo));
}
default Map<Long, DeliveryExpressTemplateRespBO> convertMap(Integer areaId, List<DeliveryExpressTemplateDO> templateList,
List<DeliveryExpressTemplateChargeDO> chargeList,
List<DeliveryExpressTemplateFreeDO> freeList) {
Map<Long, List<DeliveryExpressTemplateChargeDO>> templateIdChargeMap = convertMultiMap(chargeList,
DeliveryExpressTemplateChargeDO::getTemplateId);
Map<Long, List<DeliveryExpressTemplateFreeDO>> templateIdFreeMap = convertMultiMap(freeList,
DeliveryExpressTemplateFreeDO::getTemplateId);
// 组合运费模板配置 RespBO
Map<Long, DeliveryExpressTemplateRespBO> result = Maps.newHashMapWithExpectedSize(templateList.size());
templateList.forEach(template -> {
DeliveryExpressTemplateRespBO bo = new DeliveryExpressTemplateRespBO()
.setChargeMode(template.getChargeMode())
.setCharge(convertTemplateCharge(findFirst(templateIdChargeMap.get(template.getId()), charge -> charge.getAreaIds().contains(areaId))))
.setFree(convertTemplateFree(findFirst(templateIdFreeMap.get(template.getId()), free -> free.getAreaIds().contains(areaId))));
result.put(template.getId(), bo);
});
return result;
}
}

View File

@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.trade.dal.mysql.delivery;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateChargeDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
@ -24,9 +23,9 @@ public interface DeliveryExpressTemplateChargeMapper extends BaseMapperX<Deliver
}
default List<DeliveryExpressTemplateChargeDO> selectByTemplateIds(Collection<Long> templateIds) {
return selectList(new LambdaQueryWrapperX<DeliveryExpressTemplateChargeDO>()
.inIfPresent(DeliveryExpressTemplateChargeDO::getTemplateId, templateIds));
return selectList(DeliveryExpressTemplateChargeDO::getTemplateId, templateIds);
}
}

View File

@ -1,7 +1,6 @@
package cn.iocoder.yudao.module.trade.dal.mysql.delivery;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateFreeDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.ibatis.annotations.Mapper;
@ -22,9 +21,8 @@ public interface DeliveryExpressTemplateFreeMapper extends BaseMapperX<DeliveryE
.eq(DeliveryExpressTemplateFreeDO::getTemplateId, templateId));
}
default List<DeliveryExpressTemplateFreeDO> selectListByTemplateIds(Collection<Long> ids) {
return selectList(new LambdaQueryWrapperX<DeliveryExpressTemplateFreeDO>()
.inIfPresent(DeliveryExpressTemplateFreeDO::getTemplateId, ids));
default List<DeliveryExpressTemplateFreeDO> selectListByTemplateIds(Collection<Long> templateIds) {
return selectList(DeliveryExpressTemplateFreeDO::getTemplateId, templateIds);
}
}

View File

@ -75,7 +75,7 @@ public class DeliveryExpressServiceImpl implements DeliveryExpressService {
}
private void validateDeliveryExpressExists(Long id) {
if (deliveryExpressMapper.selectById(id) == null) {
throw exception(DELIVERY_EXPRESS_NOT_EXISTS);
throw exception(EXPRESS_NOT_EXISTS);
}
}

View File

@ -84,11 +84,12 @@ public interface DeliveryExpressTemplateService {
DeliveryExpressTemplateDO validateDeliveryExpressTemplate(Long templateId);
/**
* 基于运费模板编号数组和收件人地址区域编号. 获取匹配运费模板
* 基于运费模板编号数组和收件人地址区域编号获取匹配运费模板
*
* @param ids 编号列表
* @param areaId 区域编号
* @return Map (templateId -> 运费模板设置)
*/
Map<Long, DeliveryExpressTemplateRespBO> getExpressTemplateMapByIdsAndArea(Collection<Long> ids, Integer areaId);
}

View File

@ -224,36 +224,26 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla
@Override
public Map<Long, DeliveryExpressTemplateRespBO> getExpressTemplateMapByIdsAndArea(Collection<Long> ids, Integer areaId) {
Assert.notNull(areaId, "区域编号 {} 不能为空", areaId);
// 查询 template 数组
if (CollUtil.isEmpty(ids)) {
return Collections.emptyMap();
}
List<DeliveryExpressTemplateDO> templateList = expressTemplateMapper.selectBatchIds(ids);
// 查询 templateCharge
List<DeliveryExpressTemplateChargeDO> templeChargeList = expressTemplateChargeMapper.selectByTemplateIds(ids);
Map<Long, List<DeliveryExpressTemplateChargeDO>> templateChargeMap = convertMultiMap(templeChargeList,
DeliveryExpressTemplateChargeDO::getTemplateId);
// 查询 templateFree
List<DeliveryExpressTemplateFreeDO> templateFreeList = expressTemplateFreeMapper.selectListByTemplateIds(ids);
Map<Long, List<DeliveryExpressTemplateFreeDO>> templateFreeMap = convertMultiMap(templateFreeList,
DeliveryExpressTemplateFreeDO::getTemplateId);
// 查询 templateCharge 数组
List<DeliveryExpressTemplateChargeDO> chargeList = expressTemplateChargeMapper.selectByTemplateIds(ids);
// 查询 templateFree 数组
List<DeliveryExpressTemplateFreeDO> freeList = expressTemplateFreeMapper.selectListByTemplateIds(ids);
// 组合运费模板配置 RespBO
Map<Long, DeliveryExpressTemplateRespBO> result = new HashMap<>(templateList.size());
templateList.forEach(item -> {
DeliveryExpressTemplateRespBO bo = new DeliveryExpressTemplateRespBO()
.setChargeMode(item.getChargeMode())
.setTemplateCharge(findMatchExpressTemplateCharge(templateChargeMap.get(item.getId()), areaId))
.setTemplateFree(findMatchExpressTemplateFree(templateFreeMap.get(item.getId()), areaId));
result.put(item.getId(), bo);
});
return result;
return INSTANCE.convertMap(areaId, templateList, chargeList, freeList);
}
private DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO findMatchExpressTemplateCharge(
private DeliveryExpressTemplateRespBO.Charge findMatchExpressTemplateCharge(
List<DeliveryExpressTemplateChargeDO> templateChargeList, Integer areaId) {
return INSTANCE.convertTemplateCharge(findFirst(templateChargeList, item -> item.getAreaIds().contains(areaId)));
}
private DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO findMatchExpressTemplateFree(
private DeliveryExpressTemplateRespBO.Free findMatchExpressTemplateFree(
List<DeliveryExpressTemplateFreeDO> templateFreeList, Integer areaId) {
return INSTANCE.convertTemplateFree(findFirst(templateFreeList, item -> item.getAreaIds().contains(areaId)));
}

View File

@ -21,12 +21,12 @@ public class DeliveryExpressTemplateRespBO {
/**
* 运费模板快递运费设置
*/
private DeliveryExpressTemplateChargeBO templateCharge;
private Charge charge;
/**
* 运费模板包邮设置
*/
private DeliveryExpressTemplateFreeBO templateFree;
private Free free;
/**
* 快递运费模板费用配置 BO
@ -34,7 +34,7 @@ public class DeliveryExpressTemplateRespBO {
* @author jason
*/
@Data
public static class DeliveryExpressTemplateChargeBO {
public static class Charge {
/**
* 首件数量(件数,重量或体积)
@ -60,7 +60,7 @@ public class DeliveryExpressTemplateRespBO {
* @author jason
*/
@Data
public static class DeliveryExpressTemplateFreeBO {
public static class Free {
/**
* 包邮金额单位
@ -76,4 +76,5 @@ public class DeliveryExpressTemplateRespBO {
*/
private Integer freeCount;
}
}

View File

@ -342,7 +342,7 @@ public class TradeOrderServiceImpl implements TradeOrderService {
// TODO 芋艿logisticsId 校验存在 发货物流公司 fix
DeliveryExpressDO deliveryExpress = deliveryExpressService.getDeliveryExpress(deliveryReqVO.getLogisticsId());
if (deliveryExpress == null) {
throw exception(DELIVERY_EXPRESS_NOT_EXISTS);
throw exception(EXPRESS_NOT_EXISTS);
}
// 更新 TradeOrderDO 状态为已发货等待收货

View File

@ -77,7 +77,7 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator {
List<OrderItem> orderItems = entry.getValue();
DeliveryExpressTemplateRespBO templateBO = expressTemplateMap.get(templateId);
if (templateBO == null) {
log.error("不能计算快递运费。不能找到 templateId : {}. 对应的运费模板配置 Resp BO", templateId);
log.error("[calculateDeliveryPrice][不能计算快递运费,找不到 templateId({}) 对应的运费模板配置]", templateId);
continue;
}
// 总件数, 总金额, 总重量 总体积
@ -93,12 +93,12 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator {
}
// 优先判断是否包邮. 如果包邮不计算快递运费
if (isExpressFree(templateBO.getChargeMode(), totalCount, totalWeight,
totalVolume, totalPrice, templateBO.getTemplateFree())) {
totalVolume, totalPrice, templateBO.getFree())) {
continue;
}
// 计算快递运费
calculateExpressFeeByChargeMode(totalCount, totalWeight, totalVolume,
templateBO.getChargeMode(), templateBO.getTemplateCharge(), orderItems);
templateBO.getChargeMode(), templateBO.getCharge(), orderItems);
}
TradePriceCalculatorHelper.recountAllPrice(result);
@ -115,10 +115,10 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator {
* @param orderItems SKU 商品项目
*/
private void calculateExpressFeeByChargeMode(double totalCount, double totalWeight, double totalVolume,
int chargeMode, DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO templateCharge,
int chargeMode, DeliveryExpressTemplateRespBO.Charge templateCharge,
List<OrderItem> orderItems) {
if (templateCharge == null) {
log.error("计算快递运费时,不能找到对应的快递运费模板费用配置。无法计算以下商品 SKU 项目运费: {}", orderItems);
log.error("[calculateExpressFeeByChargeMode][计算快递运费时,找不到 SKU({}) 对应的运费模版]", orderItems);
return;
}
DeliveryExpressChargeModeEnum chargeModeEnum = DeliveryExpressChargeModeEnum.valueOf(chargeMode);
@ -145,7 +145,7 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator {
* @param templateCharge 快递运费配置
* @param orderItems SKU 商品项目
*/
private void calculateExpressFee(double total, DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO templateCharge, List<OrderItem> orderItems) {
private void calculateExpressFee(double total, DeliveryExpressTemplateRespBO.Charge templateCharge, List<OrderItem> orderItems) {
int deliveryPrice;
if (total <= templateCharge.getStartCount()) {
deliveryPrice = templateCharge.getStartPrice();
@ -174,7 +174,6 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator {
for (OrderItem item : orderItems) {
// 更新快递运费
item.setDeliveryPrice(dividePrice);
TradePriceCalculatorHelper.recountPayPrice(item);
}
}
@ -190,7 +189,7 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator {
* @param templateFree 包邮配置
*/
private boolean isExpressFree(Integer chargeMode, int totalCount, double totalWeight,
double totalVolume, int totalPrice, DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO templateFree) {
double totalVolume, int totalPrice, DeliveryExpressTemplateRespBO.Free templateFree) {
if (templateFree == null) {
return false;
}

View File

@ -1,8 +1,11 @@
package cn.iocoder.yudao.module.trade.service.price.calculator;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
import cn.iocoder.yudao.module.member.api.address.AddressApi;
import cn.iocoder.yudao.module.member.api.address.dto.AddressRespDTO;
import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryExpressChargeModeEnum;
import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum;
import cn.iocoder.yudao.module.trade.service.delivery.DeliveryExpressTemplateService;
import cn.iocoder.yudao.module.trade.service.delivery.bo.DeliveryExpressTemplateRespBO;
import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO;
@ -14,20 +17,17 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo;
import static cn.iocoder.yudao.module.trade.enums.delivery.DeliveryExpressChargeModeEnum.PIECE;
import static cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum.EXPRESS;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.assertj.core.api.Assertions.assertThat;
/**
* {@link TradeDeliveryPriceCalculator} 的单元测试
*
* @author jason
*/
public class TradeDeliveryPriceCalculatorTest extends BaseMockitoUnitTest {
@ -41,22 +41,22 @@ public class TradeDeliveryPriceCalculatorTest extends BaseMockitoUnitTest {
private TradePriceCalculateReqBO reqBO;
private TradePriceCalculateRespBO resultBO;
private AddressRespDTO addressResp;
private DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO chargeBO;
private DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO freeBO;
private DeliveryExpressTemplateRespBO templateRespBO;
private DeliveryExpressTemplateRespBO.Charge chargeBO;
private DeliveryExpressTemplateRespBO.Free freeBO;
@BeforeEach
public void init(){
// 准备参数
reqBO = new TradePriceCalculateReqBO()
.setDeliveryType(EXPRESS.getMode())
.setDeliveryType(DeliveryTypeEnum.EXPRESS.getMode())
.setAddressId(10L)
.setUserId(1L)
.setItems(asList(
new TradePriceCalculateReqBO.Item().setSkuId(10L).setCount(2).setSelected(true),
new TradePriceCalculateReqBO.Item().setSkuId(20L).setCount(10).setSelected(true),
new TradePriceCalculateReqBO.Item().setSkuId(30L).setCount(4).setSelected(false)
new TradePriceCalculateReqBO.Item().setSkuId(30L).setCount(4).setSelected(false) // 未选中
));
resultBO = new TradePriceCalculateRespBO()
.setPrice(new TradePriceCalculateRespBO.Price())
@ -72,18 +72,21 @@ public class TradeDeliveryPriceCalculatorTest extends BaseMockitoUnitTest {
// 保证价格被初始化上
TradePriceCalculatorHelper.recountPayPrice(resultBO.getItems());
TradePriceCalculatorHelper.recountAllPrice(resultBO);
// 准备收件地址数据
addressResp = randomPojo(AddressRespDTO.class, item -> item.setAreaId(10));
AddressRespDTO addressResp = randomPojo(AddressRespDTO.class, item -> item.setAreaId(10));
when(addressApi.getAddress(eq(10L), eq(1L))).thenReturn(addressResp);
// 准备运费模板费用配置数据
chargeBO = randomPojo(DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO.class,
chargeBO = randomPojo(DeliveryExpressTemplateRespBO.Charge.class,
item -> item.setStartCount(10D).setStartPrice(1000).setExtraCount(10D).setExtraPrice(2000));
// 准备运费模板包邮配置数据 订单总件数 < 包邮件数时 12 < 20
freeBO = randomPojo(DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO.class,
// 准备运费模板包邮配置数据订单总件数 < 包邮件数时 12 < 20
freeBO = randomPojo(DeliveryExpressTemplateRespBO.Free.class,
item -> item.setFreeCount(20).setFreePrice(100));
// 准备 SP 运费模板 数据
// 准备 SP 运费模板数据
templateRespBO = randomPojo(DeliveryExpressTemplateRespBO.class,
item -> item.setChargeMode(PIECE.getType())
.setTemplateCharge(chargeBO).setTemplateFree(freeBO));
item -> item.setChargeMode(DeliveryExpressChargeModeEnum.PIECE.getType())
.setCharge(chargeBO).setFree(freeBO));
}
@Test
@ -92,32 +95,27 @@ public class TradeDeliveryPriceCalculatorTest extends BaseMockitoUnitTest {
// SKU 1 : 100 * 2 = 200
// SKU 2 200 * 10 = 2000
// 运费 首件 1000 + 续件 2000 = 3000
Map<Long, DeliveryExpressTemplateRespBO> respMap = new HashMap<>();
respMap.put(1L, templateRespBO);
// mock 方法
when(addressApi.getAddress(eq(10L), eq(1L))).thenReturn(addressResp);
when(deliveryExpressTemplateService.getExpressTemplateMapByIdsAndArea(eq(asSet(1L)), eq(10)))
.thenReturn(respMap);
.thenReturn(MapUtil.of(1L, templateRespBO));
// 调用
calculator.calculate(reqBO, resultBO);
// 断言
TradePriceCalculateRespBO.Price price = resultBO.getPrice();
assertThat(price)
.extracting("totalPrice","discountPrice","couponPrice","pointPrice","deliveryPrice","payPrice")
.containsExactly(2200, 0, 0, 0, 3000, 5200);
// 断言SKU
assertThat(resultBO.getItems()).hasSize(3);
// SKU1
// 断言SKU1
assertThat(resultBO.getItems().get(0))
.extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice")
.containsExactly(100, 2, 0, 0, 0, 1500, 1700);
// SKU2
// 断言SKU2
assertThat(resultBO.getItems().get(1))
.extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice")
.containsExactly(200, 10, 0, 0, 0, 1500, 3500);
// SKU3 未选中
// 断言SKU3 未选中
assertThat(resultBO.getItems().get(2))
.extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice")
.containsExactly(300, 1, 0, 0, 0, 0, 300);
@ -129,38 +127,33 @@ public class TradeDeliveryPriceCalculatorTest extends BaseMockitoUnitTest {
// SKU 1 : 100 * 2 = 200
// SKU 2 200 * 10 = 2000
// 运费 0
Map<Long, DeliveryExpressTemplateRespBO> respMap = new HashMap<>();
respMap.put(1L, templateRespBO);
// 准备运费模板包邮配置数据 包邮 订单总件数 > 包邮件数时 12 > 10
freeBO = randomPojo(DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO.class,
item -> item.setFreeCount(10).setFreePrice(1000));
templateRespBO.setTemplateFree(freeBO);
// mock 方法
when(addressApi.getAddress(eq(10L), eq(1L))).thenReturn(addressResp);
// 准备运费模板包邮配置数据 包邮 订单总件数 > 包邮件数时 12 > 10
templateRespBO.setFree(randomPojo(DeliveryExpressTemplateRespBO.Free.class,
item -> item.setFreeCount(10).setFreePrice(1000)));
when(deliveryExpressTemplateService.getExpressTemplateMapByIdsAndArea(eq(asSet(1L)), eq(10)))
.thenReturn(respMap);
.thenReturn(MapUtil.of(1L, templateRespBO));
// 调用
calculator.calculate(reqBO, resultBO);
// 断言
TradePriceCalculateRespBO.Price price = resultBO.getPrice();
// 断言price
assertThat(price)
.extracting("totalPrice","discountPrice","couponPrice","pointPrice","deliveryPrice","payPrice")
.containsExactly(2200, 0, 0, 0, 0, 2200);
// 断言SKU
assertThat(resultBO.getItems()).hasSize(3);
// SKU1
// 断言SKU1
assertThat(resultBO.getItems().get(0))
.extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice")
.containsExactly(100, 2, 0, 0, 0, 0, 200);
// SKU2
// 断言SKU2
assertThat(resultBO.getItems().get(1))
.extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice")
.containsExactly(200, 10, 0, 0, 0, 0, 2000);
// SKU3 未选中
// 断言SKU3 未选中
assertThat(resultBO.getItems().get(2))
.extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice")
.containsExactly(300, 1, 0, 0, 0, 0, 300);
}
}
}