From 59d5fef59277919dd5929045f0f7d9d09d12d20c Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 17 Jun 2023 11:35:31 +0800 Subject: [PATCH] =?UTF-8?q?mall=20+=20trade=EF=BC=9A=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=BF=90=E8=B4=B9=E6=A8=A1=E7=89=88=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../trade/enums/ErrorCodeConstants.java | 34 ++++---- .../DeliveryExpressTemplateConvert.java | 27 +++++- .../DeliveryExpressTemplateChargeMapper.java | 5 +- .../DeliveryExpressTemplateFreeMapper.java | 6 +- .../delivery/DeliveryExpressServiceImpl.java | 2 +- .../DeliveryExpressTemplateService.java | 3 +- .../DeliveryExpressTemplateServiceImpl.java | 28 ++----- .../bo/DeliveryExpressTemplateRespBO.java | 9 +- .../service/order/TradeOrderServiceImpl.java | 2 +- .../TradeDeliveryPriceCalculator.java | 15 ++-- .../TradeDeliveryPriceCalculatorTest.java | 83 +++++++++---------- 11 files changed, 112 insertions(+), 102 deletions(-) diff --git a/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java b/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java index 12d921a88..ae7a0a501 100644 --- a/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java +++ b/yudao-module-mall/yudao-module-trade-api/src/main/java/cn/iocoder/yudao/module/trade/enums/ErrorCodeConstants.java @@ -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, "计算快递运费异常,找不到对应的运费模板"); } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/delivery/DeliveryExpressTemplateConvert.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/delivery/DeliveryExpressTemplateConvert.java index bbd57c62e..665dc787d 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/delivery/DeliveryExpressTemplateConvert.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/convert/delivery/DeliveryExpressTemplateConvert.java @@ -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 convertTemplateChargeList(Long templateId, Integer chargeMode, List 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 convertTemplateChargeList(List list); @@ -70,4 +75,22 @@ public interface DeliveryExpressTemplateConvert { return CollectionUtils.convertList(list, vo -> convertTemplateFree(templateId, vo)); } + default Map convertMap(Integer areaId, List templateList, + List chargeList, + List freeList) { + Map> templateIdChargeMap = convertMultiMap(chargeList, + DeliveryExpressTemplateChargeDO::getTemplateId); + Map> templateIdFreeMap = convertMultiMap(freeList, + DeliveryExpressTemplateFreeDO::getTemplateId); + // 组合运费模板配置 RespBO + Map 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; + } } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/delivery/DeliveryExpressTemplateChargeMapper.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/delivery/DeliveryExpressTemplateChargeMapper.java index 985b418ac..a2403f019 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/delivery/DeliveryExpressTemplateChargeMapper.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/delivery/DeliveryExpressTemplateChargeMapper.java @@ -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 selectByTemplateIds(Collection templateIds) { - return selectList(new LambdaQueryWrapperX() - .inIfPresent(DeliveryExpressTemplateChargeDO::getTemplateId, templateIds)); + return selectList(DeliveryExpressTemplateChargeDO::getTemplateId, templateIds); } + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/delivery/DeliveryExpressTemplateFreeMapper.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/delivery/DeliveryExpressTemplateFreeMapper.java index 5cc407354..b64a3c979 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/delivery/DeliveryExpressTemplateFreeMapper.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/delivery/DeliveryExpressTemplateFreeMapper.java @@ -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 selectListByTemplateIds(Collection ids) { - return selectList(new LambdaQueryWrapperX() - .inIfPresent(DeliveryExpressTemplateFreeDO::getTemplateId, ids)); + default List selectListByTemplateIds(Collection templateIds) { + return selectList(DeliveryExpressTemplateFreeDO::getTemplateId, templateIds); } } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressServiceImpl.java index a396a389a..2f08b0b3f 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressServiceImpl.java @@ -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); } } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressTemplateService.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressTemplateService.java index 39edd823e..c455701b2 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressTemplateService.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressTemplateService.java @@ -84,11 +84,12 @@ public interface DeliveryExpressTemplateService { DeliveryExpressTemplateDO validateDeliveryExpressTemplate(Long templateId); /** - * 基于运费模板编号数组和收件人地址区域编号. 获取匹配运费模板 + * 基于运费模板编号数组和收件人地址区域编号,获取匹配运费模板 * * @param ids 编号列表 * @param areaId 区域编号 * @return Map (templateId -> 运费模板设置) */ Map getExpressTemplateMapByIdsAndArea(Collection ids, Integer areaId); + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressTemplateServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressTemplateServiceImpl.java index c4aa22b8f..0cfe5e815 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressTemplateServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/DeliveryExpressTemplateServiceImpl.java @@ -224,36 +224,26 @@ public class DeliveryExpressTemplateServiceImpl implements DeliveryExpressTempla @Override public Map getExpressTemplateMapByIdsAndArea(Collection ids, Integer areaId) { Assert.notNull(areaId, "区域编号 {} 不能为空", areaId); + // 查询 template 数组 if (CollUtil.isEmpty(ids)) { return Collections.emptyMap(); } List templateList = expressTemplateMapper.selectBatchIds(ids); - // 查询 templateCharge - List templeChargeList = expressTemplateChargeMapper.selectByTemplateIds(ids); - Map> templateChargeMap = convertMultiMap(templeChargeList, - DeliveryExpressTemplateChargeDO::getTemplateId); - // 查询 templateFree - List templateFreeList = expressTemplateFreeMapper.selectListByTemplateIds(ids); - Map> templateFreeMap = convertMultiMap(templateFreeList, - DeliveryExpressTemplateFreeDO::getTemplateId); + // 查询 templateCharge 数组 + List chargeList = expressTemplateChargeMapper.selectByTemplateIds(ids); + // 查询 templateFree 数组 + List freeList = expressTemplateFreeMapper.selectListByTemplateIds(ids); + // 组合运费模板配置 RespBO - Map 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 templateChargeList, Integer areaId) { return INSTANCE.convertTemplateCharge(findFirst(templateChargeList, item -> item.getAreaIds().contains(areaId))); } - private DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO findMatchExpressTemplateFree( + private DeliveryExpressTemplateRespBO.Free findMatchExpressTemplateFree( List templateFreeList, Integer areaId) { return INSTANCE.convertTemplateFree(findFirst(templateFreeList, item -> item.getAreaIds().contains(areaId))); } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/bo/DeliveryExpressTemplateRespBO.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/bo/DeliveryExpressTemplateRespBO.java index 6c8764a5c..db0af04eb 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/bo/DeliveryExpressTemplateRespBO.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/delivery/bo/DeliveryExpressTemplateRespBO.java @@ -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; } + } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderServiceImpl.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderServiceImpl.java index 7cd6fe87b..d90be3031 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderServiceImpl.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/order/TradeOrderServiceImpl.java @@ -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 状态为已发货,等待收货 diff --git a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDeliveryPriceCalculator.java b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDeliveryPriceCalculator.java index 1a0598c63..35a3cb92a 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDeliveryPriceCalculator.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDeliveryPriceCalculator.java @@ -77,7 +77,7 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator { List 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 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 orderItems) { + private void calculateExpressFee(double total, DeliveryExpressTemplateRespBO.Charge templateCharge, List 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; } diff --git a/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDeliveryPriceCalculatorTest.java b/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDeliveryPriceCalculatorTest.java index 99fdac64e..1f408cf47 100644 --- a/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDeliveryPriceCalculatorTest.java +++ b/yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDeliveryPriceCalculatorTest.java @@ -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 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 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); } -} \ No newline at end of file + +}