Commit 59d5fef59277919dd5929045f0f7d9d09d12d20c
1 parent
07fe5293
mall + trade:优化运费模版的代码
Showing
11 changed files
with
113 additions
and
103 deletions
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 { |
| 45 | 45 | // ========== Cart 模块 1011002000 ========== |
| 46 | 46 | ErrorCode CARD_ITEM_NOT_FOUND = new ErrorCode(1011002000, "购物车项不存在"); |
| 47 | 47 | |
| 48 | - // ========== 物流配送模块 1011003000 ========== | |
| 49 | - ErrorCode DELIVERY_EXPRESS_NOT_EXISTS = new ErrorCode(1011003000, "快递公司不存在"); | |
| 50 | - // TODO @jason:最好每个模块一段哈。express 一个;exmpresstemplate 一个;pickup 一个 | |
| 51 | - ErrorCode EXPRESS_CODE_DUPLICATE = new ErrorCode(1011003001, "已经存在该编码的快递公司"); | |
| 52 | - ErrorCode EXPRESS_TEMPLATE_NOT_EXISTS = new ErrorCode(1011003002, "运费模板不存在"); | |
| 53 | - ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011003003, "已经存在该运费模板名"); | |
| 54 | - ErrorCode EXPRESS_API_QUERY_ERROR = new ErrorCode(1011003004, "快递查询接口异常"); | |
| 55 | - ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011003005, "快递查询返回失败, 原因:{}"); | |
| 56 | - ErrorCode EXPRESS_CLIENT_NOT_PROVIDE = new ErrorCode(1011003006, "需要接入快递服务商,比如【快递100】"); | |
| 57 | - ErrorCode PICK_UP_STORE_NOT_EXISTS = new ErrorCode(1011003007, "自提门店不存在"); | |
| 58 | - | |
| 59 | - // ========== Price 相关 1011004000 ============ | |
| 60 | - ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1011004000, "支付价格计算异常,原因:价格小于等于 0"); | |
| 61 | - ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_USER_ADDR_IS_EMPTY = new ErrorCode(1011004001, "计算快递运费异常,收件人地址编号为空"); | |
| 62 | - ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_TEMPLATE_NOT_FOUND = new ErrorCode(1011004002, "计算快递运费异常,找不到对应的运费模板"); | |
| 48 | + // ========== Price 相关 1011003000 ============ | |
| 49 | + ErrorCode PRICE_CALCULATE_PAY_PRICE_ILLEGAL = new ErrorCode(1011003000, "支付价格计算异常,原因:价格小于等于 0"); | |
| 50 | + ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_USER_ADDR_IS_EMPTY = new ErrorCode(1011003001, "计算快递运费异常,收件人地址编号为空"); | |
| 51 | + ErrorCode PRICE_CALCULATE_DELIVERY_PRICE_TEMPLATE_NOT_FOUND = new ErrorCode(1011003002, "计算快递运费异常,找不到对应的运费模板"); | |
| 52 | + | |
| 53 | + // ========== 物流 Express 模块 1011004000 ========== | |
| 54 | + ErrorCode EXPRESS_NOT_EXISTS = new ErrorCode(1011004000, "快递公司不存在"); | |
| 55 | + ErrorCode EXPRESS_CODE_DUPLICATE = new ErrorCode(1011004001, "已经存在该编码的快递公司"); | |
| 56 | + ErrorCode EXPRESS_API_QUERY_ERROR = new ErrorCode(1011004002, "快递查询接口异常"); | |
| 57 | + ErrorCode EXPRESS_API_QUERY_FAILED = new ErrorCode(1011004003, "快递查询返回失败,原因:{}"); | |
| 58 | + ErrorCode EXPRESS_CLIENT_NOT_PROVIDE = new ErrorCode(1011004004, "需要接入快递服务商,比如【快递100】"); | |
| 59 | + | |
| 60 | + // ========== 物流 Template 模块 1011005000 ========== | |
| 61 | + ErrorCode EXPRESS_TEMPLATE_NAME_DUPLICATE = new ErrorCode(1011005000, "已经存在该运费模板名"); | |
| 62 | + ErrorCode EXPRESS_TEMPLATE_NOT_EXISTS = new ErrorCode(1011005001, "运费模板不存在"); | |
| 63 | + | |
| 64 | + // ========== 物流 PICK_UP 模块 1011006000 ========== | |
| 65 | + | |
| 66 | + ErrorCode PICK_UP_STORE_NOT_EXISTS = new ErrorCode(1011006000, "自提门店不存在"); | |
| 67 | + | |
| 68 | + | |
| 63 | 69 | } | ... | ... |
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 |
| 7 | 7 | import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateDO; |
| 8 | 8 | import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateFreeDO; |
| 9 | 9 | import cn.iocoder.yudao.module.trade.service.delivery.bo.DeliveryExpressTemplateRespBO; |
| 10 | +import com.google.common.collect.Maps; | |
| 10 | 11 | import org.mapstruct.Mapper; |
| 11 | 12 | import org.mapstruct.factory.Mappers; |
| 12 | 13 | |
| 13 | 14 | import java.util.List; |
| 15 | +import java.util.Map; | |
| 16 | + | |
| 17 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMultiMap; | |
| 18 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.findFirst; | |
| 14 | 19 | |
| 15 | 20 | @Mapper |
| 16 | 21 | public interface DeliveryExpressTemplateConvert { |
| ... | ... | @@ -48,7 +53,7 @@ public interface DeliveryExpressTemplateConvert { |
| 48 | 53 | |
| 49 | 54 | DeliveryExpressTemplateChargeDO convertTemplateCharge(DeliveryExpressTemplateUpdateReqVO.ExpressTemplateChargeUpdateVO vo); |
| 50 | 55 | |
| 51 | - DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO convertTemplateCharge(DeliveryExpressTemplateChargeDO bean); | |
| 56 | + DeliveryExpressTemplateRespBO.Charge convertTemplateCharge(DeliveryExpressTemplateChargeDO bean); | |
| 52 | 57 | |
| 53 | 58 | default List<DeliveryExpressTemplateChargeDO> convertTemplateChargeList(Long templateId, Integer chargeMode, List<ExpressTemplateChargeBaseVO> list) { |
| 54 | 59 | return CollectionUtils.convertList(list, vo -> convertTemplateCharge(templateId, chargeMode, vo)); |
| ... | ... | @@ -60,7 +65,7 @@ public interface DeliveryExpressTemplateConvert { |
| 60 | 65 | |
| 61 | 66 | DeliveryExpressTemplateFreeDO convertTemplateFree(DeliveryExpressTemplateUpdateReqVO.ExpressTemplateFreeUpdateVO vo); |
| 62 | 67 | |
| 63 | - DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO convertTemplateFree(DeliveryExpressTemplateFreeDO bean); | |
| 68 | + DeliveryExpressTemplateRespBO.Free convertTemplateFree(DeliveryExpressTemplateFreeDO bean); | |
| 64 | 69 | |
| 65 | 70 | List<ExpressTemplateChargeBaseVO> convertTemplateChargeList(List<DeliveryExpressTemplateChargeDO> list); |
| 66 | 71 | |
| ... | ... | @@ -70,4 +75,22 @@ public interface DeliveryExpressTemplateConvert { |
| 70 | 75 | return CollectionUtils.convertList(list, vo -> convertTemplateFree(templateId, vo)); |
| 71 | 76 | } |
| 72 | 77 | |
| 78 | + default Map<Long, DeliveryExpressTemplateRespBO> convertMap(Integer areaId, List<DeliveryExpressTemplateDO> templateList, | |
| 79 | + List<DeliveryExpressTemplateChargeDO> chargeList, | |
| 80 | + List<DeliveryExpressTemplateFreeDO> freeList) { | |
| 81 | + Map<Long, List<DeliveryExpressTemplateChargeDO>> templateIdChargeMap = convertMultiMap(chargeList, | |
| 82 | + DeliveryExpressTemplateChargeDO::getTemplateId); | |
| 83 | + Map<Long, List<DeliveryExpressTemplateFreeDO>> templateIdFreeMap = convertMultiMap(freeList, | |
| 84 | + DeliveryExpressTemplateFreeDO::getTemplateId); | |
| 85 | + // 组合运费模板配置 RespBO | |
| 86 | + Map<Long, DeliveryExpressTemplateRespBO> result = Maps.newHashMapWithExpectedSize(templateList.size()); | |
| 87 | + templateList.forEach(template -> { | |
| 88 | + DeliveryExpressTemplateRespBO bo = new DeliveryExpressTemplateRespBO() | |
| 89 | + .setChargeMode(template.getChargeMode()) | |
| 90 | + .setCharge(convertTemplateCharge(findFirst(templateIdChargeMap.get(template.getId()), charge -> charge.getAreaIds().contains(areaId)))) | |
| 91 | + .setFree(convertTemplateFree(findFirst(templateIdFreeMap.get(template.getId()), free -> free.getAreaIds().contains(areaId)))); | |
| 92 | + result.put(template.getId(), bo); | |
| 93 | + }); | |
| 94 | + return result; | |
| 95 | + } | |
| 73 | 96 | } | ... | ... |
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; |
| 2 | 2 | |
| 3 | 3 | |
| 4 | 4 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 5 | -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | |
| 6 | 5 | import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateChargeDO; |
| 7 | 6 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 8 | 7 | import org.apache.ibatis.annotations.Mapper; |
| ... | ... | @@ -24,9 +23,9 @@ public interface DeliveryExpressTemplateChargeMapper extends BaseMapperX<Deliver |
| 24 | 23 | } |
| 25 | 24 | |
| 26 | 25 | default List<DeliveryExpressTemplateChargeDO> selectByTemplateIds(Collection<Long> templateIds) { |
| 27 | - return selectList(new LambdaQueryWrapperX<DeliveryExpressTemplateChargeDO>() | |
| 28 | - .inIfPresent(DeliveryExpressTemplateChargeDO::getTemplateId, templateIds)); | |
| 26 | + return selectList(DeliveryExpressTemplateChargeDO::getTemplateId, templateIds); | |
| 29 | 27 | } |
| 28 | + | |
| 30 | 29 | } |
| 31 | 30 | |
| 32 | 31 | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/main/java/cn/iocoder/yudao/module/trade/dal/mysql/delivery/DeliveryExpressTemplateFreeMapper.java
| 1 | 1 | package cn.iocoder.yudao.module.trade.dal.mysql.delivery; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 4 | -import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; | |
| 5 | 4 | import cn.iocoder.yudao.module.trade.dal.dataobject.delivery.DeliveryExpressTemplateFreeDO; |
| 6 | 5 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 7 | 6 | import org.apache.ibatis.annotations.Mapper; |
| ... | ... | @@ -22,9 +21,8 @@ public interface DeliveryExpressTemplateFreeMapper extends BaseMapperX<DeliveryE |
| 22 | 21 | .eq(DeliveryExpressTemplateFreeDO::getTemplateId, templateId)); |
| 23 | 22 | } |
| 24 | 23 | |
| 25 | - default List<DeliveryExpressTemplateFreeDO> selectListByTemplateIds(Collection<Long> ids) { | |
| 26 | - return selectList(new LambdaQueryWrapperX<DeliveryExpressTemplateFreeDO>() | |
| 27 | - .inIfPresent(DeliveryExpressTemplateFreeDO::getTemplateId, ids)); | |
| 24 | + default List<DeliveryExpressTemplateFreeDO> selectListByTemplateIds(Collection<Long> templateIds) { | |
| 25 | + return selectList(DeliveryExpressTemplateFreeDO::getTemplateId, templateIds); | |
| 28 | 26 | } |
| 29 | 27 | } |
| 30 | 28 | ... | ... |
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 { |
| 75 | 75 | } |
| 76 | 76 | private void validateDeliveryExpressExists(Long id) { |
| 77 | 77 | if (deliveryExpressMapper.selectById(id) == null) { |
| 78 | - throw exception(DELIVERY_EXPRESS_NOT_EXISTS); | |
| 78 | + throw exception(EXPRESS_NOT_EXISTS); | |
| 79 | 79 | } |
| 80 | 80 | } |
| 81 | 81 | ... | ... |
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 { |
| 84 | 84 | DeliveryExpressTemplateDO validateDeliveryExpressTemplate(Long templateId); |
| 85 | 85 | |
| 86 | 86 | /** |
| 87 | - * 基于运费模板编号数组和收件人地址区域编号. 获取匹配运费模板 | |
| 87 | + * 基于运费模板编号数组和收件人地址区域编号,获取匹配运费模板 | |
| 88 | 88 | * |
| 89 | 89 | * @param ids 编号列表 |
| 90 | 90 | * @param areaId 区域编号 |
| 91 | 91 | * @return Map (templateId -> 运费模板设置) |
| 92 | 92 | */ |
| 93 | 93 | Map<Long, DeliveryExpressTemplateRespBO> getExpressTemplateMapByIdsAndArea(Collection<Long> ids, Integer areaId); |
| 94 | + | |
| 94 | 95 | } | ... | ... |
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 |
| 224 | 224 | @Override |
| 225 | 225 | public Map<Long, DeliveryExpressTemplateRespBO> getExpressTemplateMapByIdsAndArea(Collection<Long> ids, Integer areaId) { |
| 226 | 226 | Assert.notNull(areaId, "区域编号 {} 不能为空", areaId); |
| 227 | + // 查询 template 数组 | |
| 227 | 228 | if (CollUtil.isEmpty(ids)) { |
| 228 | 229 | return Collections.emptyMap(); |
| 229 | 230 | } |
| 230 | 231 | List<DeliveryExpressTemplateDO> templateList = expressTemplateMapper.selectBatchIds(ids); |
| 231 | - // 查询 templateCharge | |
| 232 | - List<DeliveryExpressTemplateChargeDO> templeChargeList = expressTemplateChargeMapper.selectByTemplateIds(ids); | |
| 233 | - Map<Long, List<DeliveryExpressTemplateChargeDO>> templateChargeMap = convertMultiMap(templeChargeList, | |
| 234 | - DeliveryExpressTemplateChargeDO::getTemplateId); | |
| 235 | - // 查询 templateFree | |
| 236 | - List<DeliveryExpressTemplateFreeDO> templateFreeList = expressTemplateFreeMapper.selectListByTemplateIds(ids); | |
| 237 | - Map<Long, List<DeliveryExpressTemplateFreeDO>> templateFreeMap = convertMultiMap(templateFreeList, | |
| 238 | - DeliveryExpressTemplateFreeDO::getTemplateId); | |
| 232 | + // 查询 templateCharge 数组 | |
| 233 | + List<DeliveryExpressTemplateChargeDO> chargeList = expressTemplateChargeMapper.selectByTemplateIds(ids); | |
| 234 | + // 查询 templateFree 数组 | |
| 235 | + List<DeliveryExpressTemplateFreeDO> freeList = expressTemplateFreeMapper.selectListByTemplateIds(ids); | |
| 236 | + | |
| 239 | 237 | // 组合运费模板配置 RespBO |
| 240 | - Map<Long, DeliveryExpressTemplateRespBO> result = new HashMap<>(templateList.size()); | |
| 241 | - templateList.forEach(item -> { | |
| 242 | - DeliveryExpressTemplateRespBO bo = new DeliveryExpressTemplateRespBO() | |
| 243 | - .setChargeMode(item.getChargeMode()) | |
| 244 | - .setTemplateCharge(findMatchExpressTemplateCharge(templateChargeMap.get(item.getId()), areaId)) | |
| 245 | - .setTemplateFree(findMatchExpressTemplateFree(templateFreeMap.get(item.getId()), areaId)); | |
| 246 | - result.put(item.getId(), bo); | |
| 247 | - }); | |
| 248 | - return result; | |
| 238 | + return INSTANCE.convertMap(areaId, templateList, chargeList, freeList); | |
| 249 | 239 | } |
| 250 | 240 | |
| 251 | - private DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO findMatchExpressTemplateCharge( | |
| 241 | + private DeliveryExpressTemplateRespBO.Charge findMatchExpressTemplateCharge( | |
| 252 | 242 | List<DeliveryExpressTemplateChargeDO> templateChargeList, Integer areaId) { |
| 253 | 243 | return INSTANCE.convertTemplateCharge(findFirst(templateChargeList, item -> item.getAreaIds().contains(areaId))); |
| 254 | 244 | } |
| 255 | 245 | |
| 256 | - private DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO findMatchExpressTemplateFree( | |
| 246 | + private DeliveryExpressTemplateRespBO.Free findMatchExpressTemplateFree( | |
| 257 | 247 | List<DeliveryExpressTemplateFreeDO> templateFreeList, Integer areaId) { |
| 258 | 248 | return INSTANCE.convertTemplateFree(findFirst(templateFreeList, item -> item.getAreaIds().contains(areaId))); |
| 259 | 249 | } | ... | ... |
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 { |
| 21 | 21 | /** |
| 22 | 22 | * 运费模板快递运费设置 |
| 23 | 23 | */ |
| 24 | - private DeliveryExpressTemplateChargeBO templateCharge; | |
| 24 | + private Charge charge; | |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * 运费模板包邮设置 |
| 28 | 28 | */ |
| 29 | - private DeliveryExpressTemplateFreeBO templateFree; | |
| 29 | + private Free free; | |
| 30 | 30 | |
| 31 | 31 | /** |
| 32 | 32 | * 快递运费模板费用配置 BO |
| ... | ... | @@ -34,7 +34,7 @@ public class DeliveryExpressTemplateRespBO { |
| 34 | 34 | * @author jason |
| 35 | 35 | */ |
| 36 | 36 | @Data |
| 37 | - public static class DeliveryExpressTemplateChargeBO { | |
| 37 | + public static class Charge { | |
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * 首件数量(件数,重量,或体积) |
| ... | ... | @@ -60,7 +60,7 @@ public class DeliveryExpressTemplateRespBO { |
| 60 | 60 | * @author jason |
| 61 | 61 | */ |
| 62 | 62 | @Data |
| 63 | - public static class DeliveryExpressTemplateFreeBO { | |
| 63 | + public static class Free { | |
| 64 | 64 | |
| 65 | 65 | /** |
| 66 | 66 | * 包邮金额,单位:分 |
| ... | ... | @@ -76,4 +76,5 @@ public class DeliveryExpressTemplateRespBO { |
| 76 | 76 | */ |
| 77 | 77 | private Integer freeCount; |
| 78 | 78 | } |
| 79 | + | |
| 79 | 80 | } | ... | ... |
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 { |
| 342 | 342 | // TODO 芋艿:logisticsId 校验存在 发货物流公司 fix |
| 343 | 343 | DeliveryExpressDO deliveryExpress = deliveryExpressService.getDeliveryExpress(deliveryReqVO.getLogisticsId()); |
| 344 | 344 | if (deliveryExpress == null) { |
| 345 | - throw exception(DELIVERY_EXPRESS_NOT_EXISTS); | |
| 345 | + throw exception(EXPRESS_NOT_EXISTS); | |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | // 更新 TradeOrderDO 状态为已发货,等待收货 | ... | ... |
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 { |
| 77 | 77 | List<OrderItem> orderItems = entry.getValue(); |
| 78 | 78 | DeliveryExpressTemplateRespBO templateBO = expressTemplateMap.get(templateId); |
| 79 | 79 | if (templateBO == null) { |
| 80 | - log.error("不能计算快递运费。不能找到 templateId : {}. 对应的运费模板配置 Resp BO", templateId); | |
| 80 | + log.error("[calculateDeliveryPrice][不能计算快递运费,找不到 templateId({}) 对应的运费模板配置]", templateId); | |
| 81 | 81 | continue; |
| 82 | 82 | } |
| 83 | 83 | // 总件数, 总金额, 总重量, 总体积 |
| ... | ... | @@ -93,12 +93,12 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator { |
| 93 | 93 | } |
| 94 | 94 | // 优先判断是否包邮. 如果包邮不计算快递运费 |
| 95 | 95 | if (isExpressFree(templateBO.getChargeMode(), totalCount, totalWeight, |
| 96 | - totalVolume, totalPrice, templateBO.getTemplateFree())) { | |
| 96 | + totalVolume, totalPrice, templateBO.getFree())) { | |
| 97 | 97 | continue; |
| 98 | 98 | } |
| 99 | 99 | // 计算快递运费 |
| 100 | 100 | calculateExpressFeeByChargeMode(totalCount, totalWeight, totalVolume, |
| 101 | - templateBO.getChargeMode(), templateBO.getTemplateCharge(), orderItems); | |
| 101 | + templateBO.getChargeMode(), templateBO.getCharge(), orderItems); | |
| 102 | 102 | |
| 103 | 103 | } |
| 104 | 104 | TradePriceCalculatorHelper.recountAllPrice(result); |
| ... | ... | @@ -115,10 +115,10 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator { |
| 115 | 115 | * @param orderItems SKU 商品项目 |
| 116 | 116 | */ |
| 117 | 117 | private void calculateExpressFeeByChargeMode(double totalCount, double totalWeight, double totalVolume, |
| 118 | - int chargeMode, DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO templateCharge, | |
| 118 | + int chargeMode, DeliveryExpressTemplateRespBO.Charge templateCharge, | |
| 119 | 119 | List<OrderItem> orderItems) { |
| 120 | 120 | if (templateCharge == null) { |
| 121 | - log.error("计算快递运费时,不能找到对应的快递运费模板费用配置。无法计算以下商品 SKU 项目运费: {}", orderItems); | |
| 121 | + log.error("[calculateExpressFeeByChargeMode][计算快递运费时,找不到 SKU({}) 对应的运费模版]", orderItems); | |
| 122 | 122 | return; |
| 123 | 123 | } |
| 124 | 124 | DeliveryExpressChargeModeEnum chargeModeEnum = DeliveryExpressChargeModeEnum.valueOf(chargeMode); |
| ... | ... | @@ -145,7 +145,7 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator { |
| 145 | 145 | * @param templateCharge 快递运费配置 |
| 146 | 146 | * @param orderItems SKU 商品项目 |
| 147 | 147 | */ |
| 148 | - private void calculateExpressFee(double total, DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO templateCharge, List<OrderItem> orderItems) { | |
| 148 | + private void calculateExpressFee(double total, DeliveryExpressTemplateRespBO.Charge templateCharge, List<OrderItem> orderItems) { | |
| 149 | 149 | int deliveryPrice; |
| 150 | 150 | if (total <= templateCharge.getStartCount()) { |
| 151 | 151 | deliveryPrice = templateCharge.getStartPrice(); |
| ... | ... | @@ -174,7 +174,6 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator { |
| 174 | 174 | for (OrderItem item : orderItems) { |
| 175 | 175 | // 更新快递运费 |
| 176 | 176 | item.setDeliveryPrice(dividePrice); |
| 177 | - | |
| 178 | 177 | TradePriceCalculatorHelper.recountPayPrice(item); |
| 179 | 178 | } |
| 180 | 179 | } |
| ... | ... | @@ -190,7 +189,7 @@ public class TradeDeliveryPriceCalculator implements TradePriceCalculator { |
| 190 | 189 | * @param templateFree 包邮配置 |
| 191 | 190 | */ |
| 192 | 191 | private boolean isExpressFree(Integer chargeMode, int totalCount, double totalWeight, |
| 193 | - double totalVolume, int totalPrice, DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO templateFree) { | |
| 192 | + double totalVolume, int totalPrice, DeliveryExpressTemplateRespBO.Free templateFree) { | |
| 194 | 193 | if (templateFree == null) { |
| 195 | 194 | return false; |
| 196 | 195 | } | ... | ... |
yudao-module-mall/yudao-module-trade-biz/src/test/java/cn/iocoder/yudao/module/trade/service/price/calculator/TradeDeliveryPriceCalculatorTest.java
| 1 | 1 | package cn.iocoder.yudao.module.trade.service.price.calculator; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.map.MapUtil; | |
| 3 | 4 | import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; |
| 4 | 5 | import cn.iocoder.yudao.module.member.api.address.AddressApi; |
| 5 | 6 | import cn.iocoder.yudao.module.member.api.address.dto.AddressRespDTO; |
| 7 | +import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryExpressChargeModeEnum; | |
| 8 | +import cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum; | |
| 6 | 9 | import cn.iocoder.yudao.module.trade.service.delivery.DeliveryExpressTemplateService; |
| 7 | 10 | import cn.iocoder.yudao.module.trade.service.delivery.bo.DeliveryExpressTemplateRespBO; |
| 8 | 11 | import cn.iocoder.yudao.module.trade.service.price.bo.TradePriceCalculateReqBO; |
| ... | ... | @@ -14,20 +17,17 @@ import org.mockito.InjectMocks; |
| 14 | 17 | import org.mockito.Mock; |
| 15 | 18 | |
| 16 | 19 | import java.util.ArrayList; |
| 17 | -import java.util.HashMap; | |
| 18 | -import java.util.Map; | |
| 19 | 20 | |
| 20 | 21 | import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; |
| 21 | 22 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; |
| 22 | -import static cn.iocoder.yudao.module.trade.enums.delivery.DeliveryExpressChargeModeEnum.PIECE; | |
| 23 | -import static cn.iocoder.yudao.module.trade.enums.delivery.DeliveryTypeEnum.EXPRESS; | |
| 24 | 23 | import static java.util.Arrays.asList; |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 25 | 25 | import static org.mockito.ArgumentMatchers.eq; |
| 26 | 26 | import static org.mockito.Mockito.when; |
| 27 | 27 | |
| 28 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 29 | - | |
| 30 | 28 | /** |
| 29 | + * {@link TradeDeliveryPriceCalculator} 的单元测试 | |
| 30 | + * | |
| 31 | 31 | * @author jason |
| 32 | 32 | */ |
| 33 | 33 | public class TradeDeliveryPriceCalculatorTest extends BaseMockitoUnitTest { |
| ... | ... | @@ -41,22 +41,22 @@ public class TradeDeliveryPriceCalculatorTest extends BaseMockitoUnitTest { |
| 41 | 41 | |
| 42 | 42 | private TradePriceCalculateReqBO reqBO; |
| 43 | 43 | private TradePriceCalculateRespBO resultBO; |
| 44 | - private AddressRespDTO addressResp; | |
| 45 | - private DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO chargeBO; | |
| 46 | - private DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO freeBO; | |
| 44 | + | |
| 47 | 45 | private DeliveryExpressTemplateRespBO templateRespBO; |
| 46 | + private DeliveryExpressTemplateRespBO.Charge chargeBO; | |
| 47 | + private DeliveryExpressTemplateRespBO.Free freeBO; | |
| 48 | 48 | |
| 49 | 49 | @BeforeEach |
| 50 | 50 | public void init(){ |
| 51 | 51 | // 准备参数 |
| 52 | 52 | reqBO = new TradePriceCalculateReqBO() |
| 53 | - .setDeliveryType(EXPRESS.getMode()) | |
| 53 | + .setDeliveryType(DeliveryTypeEnum.EXPRESS.getMode()) | |
| 54 | 54 | .setAddressId(10L) |
| 55 | 55 | .setUserId(1L) |
| 56 | 56 | .setItems(asList( |
| 57 | 57 | new TradePriceCalculateReqBO.Item().setSkuId(10L).setCount(2).setSelected(true), |
| 58 | 58 | new TradePriceCalculateReqBO.Item().setSkuId(20L).setCount(10).setSelected(true), |
| 59 | - new TradePriceCalculateReqBO.Item().setSkuId(30L).setCount(4).setSelected(false) | |
| 59 | + new TradePriceCalculateReqBO.Item().setSkuId(30L).setCount(4).setSelected(false) // 未选中 | |
| 60 | 60 | )); |
| 61 | 61 | resultBO = new TradePriceCalculateRespBO() |
| 62 | 62 | .setPrice(new TradePriceCalculateRespBO.Price()) |
| ... | ... | @@ -72,18 +72,21 @@ public class TradeDeliveryPriceCalculatorTest extends BaseMockitoUnitTest { |
| 72 | 72 | // 保证价格被初始化上 |
| 73 | 73 | TradePriceCalculatorHelper.recountPayPrice(resultBO.getItems()); |
| 74 | 74 | TradePriceCalculatorHelper.recountAllPrice(resultBO); |
| 75 | + | |
| 75 | 76 | // 准备收件地址数据 |
| 76 | - addressResp = randomPojo(AddressRespDTO.class, item -> item.setAreaId(10)); | |
| 77 | + AddressRespDTO addressResp = randomPojo(AddressRespDTO.class, item -> item.setAreaId(10)); | |
| 78 | + when(addressApi.getAddress(eq(10L), eq(1L))).thenReturn(addressResp); | |
| 79 | + | |
| 77 | 80 | // 准备运费模板费用配置数据 |
| 78 | - chargeBO = randomPojo(DeliveryExpressTemplateRespBO.DeliveryExpressTemplateChargeBO.class, | |
| 81 | + chargeBO = randomPojo(DeliveryExpressTemplateRespBO.Charge.class, | |
| 79 | 82 | item -> item.setStartCount(10D).setStartPrice(1000).setExtraCount(10D).setExtraPrice(2000)); |
| 80 | - // 准备运费模板包邮配置数据 订单总件数 < 包邮件数时 12 < 20 | |
| 81 | - freeBO = randomPojo(DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO.class, | |
| 83 | + // 准备运费模板包邮配置数据:订单总件数 < 包邮件数时 12 < 20 | |
| 84 | + freeBO = randomPojo(DeliveryExpressTemplateRespBO.Free.class, | |
| 82 | 85 | item -> item.setFreeCount(20).setFreePrice(100)); |
| 83 | - // 准备 SP 运费模板 数据 | |
| 86 | + // 准备 SP 运费模板数据 | |
| 84 | 87 | templateRespBO = randomPojo(DeliveryExpressTemplateRespBO.class, |
| 85 | - item -> item.setChargeMode(PIECE.getType()) | |
| 86 | - .setTemplateCharge(chargeBO).setTemplateFree(freeBO)); | |
| 88 | + item -> item.setChargeMode(DeliveryExpressChargeModeEnum.PIECE.getType()) | |
| 89 | + .setCharge(chargeBO).setFree(freeBO)); | |
| 87 | 90 | } |
| 88 | 91 | |
| 89 | 92 | @Test |
| ... | ... | @@ -92,32 +95,27 @@ public class TradeDeliveryPriceCalculatorTest extends BaseMockitoUnitTest { |
| 92 | 95 | // SKU 1 : 100 * 2 = 200 |
| 93 | 96 | // SKU 2 :200 * 10 = 2000 |
| 94 | 97 | // 运费 首件 1000 + 续件 2000 = 3000 |
| 95 | - Map<Long, DeliveryExpressTemplateRespBO> respMap = new HashMap<>(); | |
| 96 | - respMap.put(1L, templateRespBO); | |
| 97 | - | |
| 98 | 98 | // mock 方法 |
| 99 | - when(addressApi.getAddress(eq(10L), eq(1L))).thenReturn(addressResp); | |
| 100 | 99 | when(deliveryExpressTemplateService.getExpressTemplateMapByIdsAndArea(eq(asSet(1L)), eq(10))) |
| 101 | - .thenReturn(respMap); | |
| 100 | + .thenReturn(MapUtil.of(1L, templateRespBO)); | |
| 102 | 101 | |
| 102 | + // 调用 | |
| 103 | 103 | calculator.calculate(reqBO, resultBO); |
| 104 | - | |
| 104 | + // 断言 | |
| 105 | 105 | TradePriceCalculateRespBO.Price price = resultBO.getPrice(); |
| 106 | - | |
| 107 | 106 | assertThat(price) |
| 108 | 107 | .extracting("totalPrice","discountPrice","couponPrice","pointPrice","deliveryPrice","payPrice") |
| 109 | 108 | .containsExactly(2200, 0, 0, 0, 3000, 5200); |
| 110 | - // 断言:SKU | |
| 111 | 109 | assertThat(resultBO.getItems()).hasSize(3); |
| 112 | - // SKU1 | |
| 110 | + // 断言:SKU1 | |
| 113 | 111 | assertThat(resultBO.getItems().get(0)) |
| 114 | 112 | .extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice") |
| 115 | 113 | .containsExactly(100, 2, 0, 0, 0, 1500, 1700); |
| 116 | - // SKU2 | |
| 114 | + // 断言:SKU2 | |
| 117 | 115 | assertThat(resultBO.getItems().get(1)) |
| 118 | 116 | .extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice") |
| 119 | 117 | .containsExactly(200, 10, 0, 0, 0, 1500, 3500); |
| 120 | - // SKU3 未选中 | |
| 118 | + // 断言:SKU3 未选中 | |
| 121 | 119 | assertThat(resultBO.getItems().get(2)) |
| 122 | 120 | .extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice") |
| 123 | 121 | .containsExactly(300, 1, 0, 0, 0, 0, 300); |
| ... | ... | @@ -129,38 +127,33 @@ public class TradeDeliveryPriceCalculatorTest extends BaseMockitoUnitTest { |
| 129 | 127 | // SKU 1 : 100 * 2 = 200 |
| 130 | 128 | // SKU 2 :200 * 10 = 2000 |
| 131 | 129 | // 运费 0 |
| 132 | - Map<Long, DeliveryExpressTemplateRespBO> respMap = new HashMap<>(); | |
| 133 | - respMap.put(1L, templateRespBO); | |
| 134 | - // 准备运费模板包邮配置数据 包邮 订单总件数 > 包邮件数时 12 > 10 | |
| 135 | - freeBO = randomPojo(DeliveryExpressTemplateRespBO.DeliveryExpressTemplateFreeBO.class, | |
| 136 | - item -> item.setFreeCount(10).setFreePrice(1000)); | |
| 137 | - templateRespBO.setTemplateFree(freeBO); | |
| 138 | 130 | // mock 方法 |
| 139 | - when(addressApi.getAddress(eq(10L), eq(1L))).thenReturn(addressResp); | |
| 131 | + // 准备运费模板包邮配置数据 包邮 订单总件数 > 包邮件数时 12 > 10 | |
| 132 | + templateRespBO.setFree(randomPojo(DeliveryExpressTemplateRespBO.Free.class, | |
| 133 | + item -> item.setFreeCount(10).setFreePrice(1000))); | |
| 140 | 134 | when(deliveryExpressTemplateService.getExpressTemplateMapByIdsAndArea(eq(asSet(1L)), eq(10))) |
| 141 | - .thenReturn(respMap); | |
| 135 | + .thenReturn(MapUtil.of(1L, templateRespBO)); | |
| 142 | 136 | |
| 137 | + // 调用 | |
| 143 | 138 | calculator.calculate(reqBO, resultBO); |
| 144 | - | |
| 139 | + // 断言 | |
| 145 | 140 | TradePriceCalculateRespBO.Price price = resultBO.getPrice(); |
| 146 | - | |
| 147 | - // 断言price | |
| 148 | 141 | assertThat(price) |
| 149 | 142 | .extracting("totalPrice","discountPrice","couponPrice","pointPrice","deliveryPrice","payPrice") |
| 150 | 143 | .containsExactly(2200, 0, 0, 0, 0, 2200); |
| 151 | - // 断言:SKU | |
| 152 | 144 | assertThat(resultBO.getItems()).hasSize(3); |
| 153 | - // SKU1 | |
| 145 | + // 断言:SKU1 | |
| 154 | 146 | assertThat(resultBO.getItems().get(0)) |
| 155 | 147 | .extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice") |
| 156 | 148 | .containsExactly(100, 2, 0, 0, 0, 0, 200); |
| 157 | - // SKU2 | |
| 149 | + // 断言:SKU2 | |
| 158 | 150 | assertThat(resultBO.getItems().get(1)) |
| 159 | 151 | .extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice") |
| 160 | 152 | .containsExactly(200, 10, 0, 0, 0, 0, 2000); |
| 161 | - // SKU3 未选中 | |
| 153 | + // 断言:SKU3 未选中 | |
| 162 | 154 | assertThat(resultBO.getItems().get(2)) |
| 163 | 155 | .extracting("price", "count","discountPrice" ,"couponPrice", "pointPrice","deliveryPrice","payPrice") |
| 164 | 156 | .containsExactly(300, 1, 0, 0, 0, 0, 300); |
| 165 | 157 | } |
| 166 | -} | |
| 167 | 158 | \ No newline at end of file |
| 159 | + | |
| 160 | +} | ... | ... |