Commit 89c137a91583a19c78145067b513566548e1156f
1 parent
09433077
fix(mall): 修复独立分销固定佣金为空时报错
独立分销商品的 SKU 固定佣金为空时,预计佣金计算和下单分销记录创建统一按 0 处理,避免最小/最大佣金比较或佣金乘数量时出现空指针异常。 补充分销预计佣金和订单转换单元测试,覆盖固定佣金为空与 SKU 列表为空场景。
Showing
3 changed files
with
42 additions
and
8 deletions
yudao-module-mall/yudao-module-trade/src/main/java/cn/iocoder/yudao/module/trade/convert/order/TradeOrderConvert.java
| 1 | 1 | package cn.iocoder.yudao.module.trade.convert.order; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.util.BooleanUtil; |
| 4 | +import cn.hutool.core.util.ObjectUtil; | |
| 4 | 5 | import cn.hutool.core.util.StrUtil; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; |
| 6 | 7 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| ... | ... | @@ -268,8 +269,8 @@ public interface TradeOrderConvert { |
| 268 | 269 | .setTitle(StrUtil.format("{}成功购买{}", user.getNickname(), item.getSpuName())); |
| 269 | 270 | if (BooleanUtil.isTrue(spu.getSubCommissionType())) { |
| 270 | 271 | // 特殊:单独设置的佣金需要乘以购买数量。关联 https://gitee.com/yudaocode/yudao-mall-uniapp/issues/ICY7SJ |
| 271 | - bo.setFirstFixedPrice(sku.getFirstBrokeragePrice() * item.getCount()) | |
| 272 | - .setSecondFixedPrice(sku.getSecondBrokeragePrice() * item.getCount()); | |
| 272 | + bo.setFirstFixedPrice(ObjectUtil.defaultIfNull(sku.getFirstBrokeragePrice(), 0) * item.getCount()) | |
| 273 | + .setSecondFixedPrice(ObjectUtil.defaultIfNull(sku.getSecondBrokeragePrice(), 0) * item.getCount()); | |
| 273 | 274 | } |
| 274 | 275 | return bo; |
| 275 | 276 | } | ... | ... |
yudao-module-mall/yudao-module-trade/src/main/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageRecordServiceImpl.java
| ... | ... | @@ -342,9 +342,11 @@ public class BrokerageRecordServiceImpl implements BrokerageRecordService { |
| 342 | 342 | List<ProductSkuRespDTO> skuList = productSkuApi.getSkuListBySpuId(ListUtil.of(spuId)); |
| 343 | 343 | if (BooleanUtil.isTrue(spu.getSubCommissionType())) { |
| 344 | 344 | // 3.2.1 商品独立分销模式:直接取 SKU 固定佣金 |
| 345 | - // 注意:固定佣金允许为 0,表示商家主动设为零佣金 | |
| 346 | - Integer fixedMinPrice = getMinValue(skuList, ProductSkuRespDTO::getFirstBrokeragePrice); | |
| 347 | - Integer fixedMaxPrice = getMaxValue(skuList, ProductSkuRespDTO::getFirstBrokeragePrice); | |
| 345 | + // 注意:固定佣金允许为 0,表示商家主动设为零佣金;为空时,也按 0 处理 | |
| 346 | + Integer fixedMinPrice = getMinValue(skuList, | |
| 347 | + sku -> ObjectUtil.defaultIfNull(sku.getFirstBrokeragePrice(), 0)); | |
| 348 | + Integer fixedMaxPrice = getMaxValue(skuList, | |
| 349 | + sku -> ObjectUtil.defaultIfNull(sku.getFirstBrokeragePrice(), 0)); | |
| 348 | 350 | respVO.setBrokerageMinPrice(calculatePrice(null, tradeConfig.getBrokerageFirstPercent(), fixedMinPrice)) |
| 349 | 351 | .setBrokerageMaxPrice(calculatePrice(null, tradeConfig.getBrokerageFirstPercent(), fixedMaxPrice)); |
| 350 | 352 | } else { | ... | ... |
yudao-module-mall/yudao-module-trade/src/test/java/cn/iocoder/yudao/module/trade/service/brokerage/BrokerageRecordServiceImplTest.java
| ... | ... | @@ -202,7 +202,7 @@ public class BrokerageRecordServiceImplTest extends BaseDbUnitTest { |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | @Test |
| 205 | - public void testCalculateProductBrokeragePrice_subCommissionEmptySkuList() { | |
| 205 | + public void testCalculateProductBrokeragePrice_subCommissionNullFixed() { | |
| 206 | 206 | // mock:分销功能已开启,比例 10%,当前用户有分销资格 |
| 207 | 207 | TradeConfigDO tradeConfig = new TradeConfigDO(); |
| 208 | 208 | tradeConfig.setBrokerageEnabled(true); |
| ... | ... | @@ -210,15 +210,46 @@ public class BrokerageRecordServiceImplTest extends BaseDbUnitTest { |
| 210 | 210 | when(tradeConfigService.getTradeConfig()).thenReturn(tradeConfig); |
| 211 | 211 | when(brokerageUserService.getUserBrokerageEnabled(100L)).thenReturn(true); |
| 212 | 212 | |
| 213 | - // mock:商品开启独立分销,但查询不到 SKU 固定佣金 | |
| 213 | + // mock:商品开启独立分销,其中一个 SKU 固定佣金未配置 | |
| 214 | 214 | ProductSpuRespDTO spu = new ProductSpuRespDTO(); |
| 215 | 215 | spu.setSubCommissionType(true); |
| 216 | 216 | when(productSpuApi.getSpu(3L)).thenReturn(spu); |
| 217 | - when(productSkuApi.getSkuListBySpuId(ListUtil.of(3L))).thenReturn(List.of()); | |
| 217 | + | |
| 218 | + ProductSkuRespDTO nullBrokerageSku = new ProductSkuRespDTO(); | |
| 219 | + nullBrokerageSku.setPrice(1000); | |
| 220 | + nullBrokerageSku.setFirstBrokeragePrice(null); | |
| 221 | + ProductSkuRespDTO fixedBrokerageSku = new ProductSkuRespDTO(); | |
| 222 | + fixedBrokerageSku.setPrice(2000); | |
| 223 | + fixedBrokerageSku.setFirstBrokeragePrice(200); | |
| 224 | + when(productSkuApi.getSkuListBySpuId(ListUtil.of(3L))).thenReturn(List.of(nullBrokerageSku, fixedBrokerageSku)); | |
| 218 | 225 | |
| 219 | 226 | // 调用 |
| 220 | 227 | AppBrokerageProductPriceRespVO result = brokerageRecordService.calculateProductBrokeragePrice(100L, 3L); |
| 221 | 228 | |
| 229 | + // 断言:独立分销固定佣金为空时按 0 处理,避免比较最小/最大佣金时报错 | |
| 230 | + assertTrue(result.getEnabled()); | |
| 231 | + assertEquals(0, result.getBrokerageMinPrice()); | |
| 232 | + assertEquals(200, result.getBrokerageMaxPrice()); | |
| 233 | + } | |
| 234 | + | |
| 235 | + @Test | |
| 236 | + public void testCalculateProductBrokeragePrice_subCommissionEmptySkuList() { | |
| 237 | + // mock:分销功能已开启,比例 10%,当前用户有分销资格 | |
| 238 | + TradeConfigDO tradeConfig = new TradeConfigDO(); | |
| 239 | + tradeConfig.setBrokerageEnabled(true); | |
| 240 | + tradeConfig.setBrokerageFirstPercent(10); | |
| 241 | + when(tradeConfigService.getTradeConfig()).thenReturn(tradeConfig); | |
| 242 | + when(brokerageUserService.getUserBrokerageEnabled(100L)).thenReturn(true); | |
| 243 | + | |
| 244 | + // mock:商品开启独立分销,但查询不到 SKU 固定佣金 | |
| 245 | + ProductSpuRespDTO spu = new ProductSpuRespDTO(); | |
| 246 | + spu.setSubCommissionType(true); | |
| 247 | + when(productSpuApi.getSpu(4L)).thenReturn(spu); | |
| 248 | + when(productSkuApi.getSkuListBySpuId(ListUtil.of(4L))).thenReturn(List.of()); | |
| 249 | + | |
| 250 | + // 调用 | |
| 251 | + AppBrokerageProductPriceRespVO result = brokerageRecordService.calculateProductBrokeragePrice(100L, 4L); | |
| 252 | + | |
| 222 | 253 | // 断言:独立分销没有固定佣金时,不回退到全局比例 |
| 223 | 254 | assertTrue(result.getEnabled()); |
| 224 | 255 | assertEquals(0, result.getBrokerageMinPrice()); | ... | ... |