diff --git a/firstleg/src/main/java/com/baiyue/firstleg/repository/FbaPricingRepository.java b/firstleg/src/main/java/com/baiyue/firstleg/repository/FbaPricingRepository.java index dbeba115d..26c6ffcda 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/repository/FbaPricingRepository.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/repository/FbaPricingRepository.java @@ -169,7 +169,7 @@ public class FbaPricingRepository { public List matchRules(InboundPricingEntity inbound, LocalDateTime at) { return rules.selectList(new LambdaQueryWrapper() .eq(CustomerChargeRuleEntity::getTenantId, inbound.getTenantId()) - .eq(CustomerChargeRuleEntity::getCustomerId, inbound.getCustomerId()) + .eq(CustomerChargeRuleEntity::getCustomerId, inbound.getTenantId()) .eq(CustomerChargeRuleEntity::getBusinessType, inbound.getBusinessType()) .eq(CustomerChargeRuleEntity::getTaxMode, inbound.getTaxMode()) .eq(CustomerChargeRuleEntity::getStatus, "ACTIVE") diff --git a/firstleg/src/main/java/com/baiyue/firstleg/service/FbaPricingService.java b/firstleg/src/main/java/com/baiyue/firstleg/service/FbaPricingService.java index 7be0be609..757d74779 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/service/FbaPricingService.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/service/FbaPricingService.java @@ -348,9 +348,6 @@ public class FbaPricingService { if (!"FBA".equals(inbound.getDestinationType()) && !"OVERSEAS".equals(inbound.getDestinationType())) { throw conflict("当前目的地类型不支持自动计费"); } - if (inbound.getCustomerId() == null || inbound.getCustomerId() <= 0) { - throw conflict("入库单缺少订单客户归属,不能生成对客费用"); - } if (inbound.getWarehouseId() == null || !StringUtils.hasText(inbound.getChannelCode())) { throw conflict("入库单缺少起运仓或渠道匹配维度"); } @@ -428,7 +425,7 @@ public class FbaPricingService { fee.setChargeRuleId(null); fee.setChargeRuleVersion(null); fee.setCarrierRateId(rate.getId()); fee.setCarrierRateVersion(rate.getRateVersion()); fee.setPriceBookVersionId(estimate.priceBookVersionId()); - fee.setRuleCode(null); fee.setCustomerId(inbound.getCustomerId()); + fee.setRuleCode(null); fee.setCustomerId(tenantId); fee.setCustomerName(inbound.getCustomerName()); fee.setBusinessType(inbound.getBusinessType()); fee.setTaxMode(inbound.getTaxMode()); fee.setPricingTaxMode(inbound.getPricingTaxMode()); fee.setRoundingScale(estimate.roundingScale()); fee.setRoundingMode(estimate.roundingMode()); diff --git a/firstleg/src/main/java/com/baiyue/firstleg/service/InboundFeeDetailService.java b/firstleg/src/main/java/com/baiyue/firstleg/service/InboundFeeDetailService.java index fefa9f9d9..b35a25a7a 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/service/InboundFeeDetailService.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/service/InboundFeeDetailService.java @@ -100,7 +100,7 @@ public class InboundFeeDetailService { detail.setInboundCode(inbound.getInboundCode()); detail.setDirection("RECEIVABLE"); detail.setBusinessDate(now.toLocalDate()); - detail.setCustomerId(inbound.getCustomerId()); + detail.setCustomerId(tenantId); detail.setCustomerName(inbound.getCustomerName()); detail.setInvoiceTitle(inbound.getCompanyName()); detail.setFeeCategorySnapshot(feeItem.getCategory()); @@ -186,7 +186,7 @@ public class InboundFeeDetailService { detail.setSourceType("FBA_PRICING"); detail.setSourceFeeId(fee.getId()); detail.setSourceFeeVersion(fee.getFeeVersion()); detail.setSourceFeeNo(fee.getFeeNo()); detail.setInboundCode(inbound.getInboundCode()); detail.setDirection("RECEIVABLE"); - detail.setBusinessDate(now.toLocalDate()); detail.setCustomerId(inbound.getCustomerId()); + detail.setBusinessDate(now.toLocalDate()); detail.setCustomerId(tenantId); detail.setCustomerName(inbound.getCustomerName()); detail.setInvoiceTitle(inbound.getCompanyName()); detail.setFeeCategorySnapshot(feeItem.getCategory()); detail.setTradeScopeSnapshot(feeItem.getTradeScope()); detail.setFeeTypeNameSnapshot(feeItem.getFeeTypeName()); detail.setChannelCode(inbound.getChannelCode()); diff --git a/firstleg/src/main/java/com/baiyue/firstleg/service/MeasurementBatchService.java b/firstleg/src/main/java/com/baiyue/firstleg/service/MeasurementBatchService.java index 8839170c7..a2720ac58 100644 --- a/firstleg/src/main/java/com/baiyue/firstleg/service/MeasurementBatchService.java +++ b/firstleg/src/main/java/com/baiyue/firstleg/service/MeasurementBatchService.java @@ -60,7 +60,7 @@ public class MeasurementBatchService { } validateSource(command); List calculatedItems = calculateItems( - command, dimensionWeighting.load(command.tenantId()), inbound.getCustomerId()); + command, dimensionWeighting.load(command.tenantId())); InboundMeasurementEntity measurement; if (command.measurementId() == null) { @@ -140,8 +140,7 @@ public class MeasurementBatchService { } private List calculateItems( - MeasurementBatchCommand command, CustomerDimensionWeightingService.RuleSet dimensionRules, - Long customerId) { + MeasurementBatchCommand command, CustomerDimensionWeightingService.RuleSet dimensionRules) { Set itemNumbers = new HashSet<>(); return command.items().stream().map(source -> { if (!itemNumbers.add(source.itemNo())) { @@ -163,8 +162,8 @@ public class MeasurementBatchService { baseLength, baseWidth, baseHeight); String requestedMode = normalizeMode(source.coefficientMode()); CustomerChargeRuleEntity coefficientRule = "MULTIPLY".equals(requestedMode) - ? requireCoefficientRule(command.tenantId(), customerId, source.chargeRuleId()) - : requestedMode == null ? findDefaultCoefficientRule(command.tenantId(), customerId) : null; + ? requireCoefficientRule(command.tenantId(), source.chargeRuleId()) + : requestedMode == null ? findDefaultCoefficientRule(command.tenantId()) : null; String coefficientMode = coefficientRule != null ? "MULTIPLY" : requestedMode; boolean useDimensionRule = "ADD".equals(coefficientMode); BigDecimal addition = weighted.edgeAdditionCm() == null @@ -257,15 +256,13 @@ public class MeasurementBatchService { return normalized; } - private CustomerChargeRuleEntity findDefaultCoefficientRule(long tenantId, Long customerId) { - if (customerId == null || customerId <= 0) return null; - return pricingRepository.findDefaultRule(tenantId, customerId); + private CustomerChargeRuleEntity findDefaultCoefficientRule(long tenantId) { + return pricingRepository.findDefaultRule(tenantId, tenantId); } - private CustomerChargeRuleEntity requireCoefficientRule(long tenantId, Long customerId, Long ruleId) { - Long effectiveCustomerId = tenantId; + private CustomerChargeRuleEntity requireCoefficientRule(long tenantId, Long ruleId) { CustomerChargeRuleEntity rule = ruleId == null - ? pricingRepository.findDefaultRule(tenantId, effectiveCustomerId) + ? pricingRepository.findDefaultRule(tenantId, tenantId) : pricingRepository.findRule(tenantId, ruleId); if (rule == null || !"ACTIVE".equals(rule.getStatus()) || !java.util.Objects.equals(rule.getTenantId(), tenantId)) { diff --git a/firstleg/src/test/java/com/baiyue/firstleg/service/FbaPricingServiceTest.java b/firstleg/src/test/java/com/baiyue/firstleg/service/FbaPricingServiceTest.java index 992f7262e..afc86c84c 100644 --- a/firstleg/src/test/java/com/baiyue/firstleg/service/FbaPricingServiceTest.java +++ b/firstleg/src/test/java/com/baiyue/firstleg/service/FbaPricingServiceTest.java @@ -98,7 +98,7 @@ class FbaPricingServiceTest { assertThat(fee.getValue().getPricingTaxMode()).isEqualTo("TAX_INCLUDED"); assertThat(fee.getValue().getChargeRuleId()).isNull(); assertThat(fee.getValue().getCarrierId()).isNull(); - assertThat(fee.getValue().getCustomerId()).isEqualTo(11L); + assertThat(fee.getValue().getCustomerId()).isEqualTo(TENANT); assertThat(fee.getValue().getConversionFactor()).isEqualByComparingTo("167"); assertThat(fee.getValue().getRoundingScale()).isEqualTo(6); assertThat(fee.getValue().getRoundingMode()).isEqualTo("HALF_UP"); @@ -151,19 +151,20 @@ class FbaPricingServiceTest { } @Test - @DisplayName("缺少订单客户归属时拒绝生成对客费用") - void rejectsMissingOrderCustomerOwnership() { + @DisplayName("订单客户字段为空时使用租户归属生成对客费用") + void usesTenantOwnershipWhenOrderCustomerIsMissing() { readyRepository(); InboundPricingEntity inbound = inbound("200.000", "0.2000"); inbound.setCustomerId(null); when(repository.findInbound(TENANT, INBOUND)).thenReturn(inbound); + when(priceBookEstimator.estimate(any())).thenReturn( + estimate("KG", "200.000", "2.000000", "400.00", "0.00")); - assertThatThrownBy(() -> service.calculate( - TENANT, OPERATOR, INBOUND, NOW, "calc-no-customer")) - .isInstanceOf(BizException.class) - .hasMessageContaining("缺少订单客户归属"); - verify(priceBookEstimator, never()).estimate(any()); - verify(repository, never()).insertFee(any()); + assertThat(service.calculate(TENANT, OPERATOR, INBOUND, NOW, "calc-no-customer")) + .isNotNull(); + ArgumentCaptor fee = ArgumentCaptor.forClass(InboundFeeEntity.class); + verify(repository).insertFee(fee.capture()); + assertThat(fee.getValue().getCustomerId()).isEqualTo(TENANT); } @Test diff --git a/firstleg/src/test/java/com/baiyue/firstleg/service/MeasurementBatchServiceTest.java b/firstleg/src/test/java/com/baiyue/firstleg/service/MeasurementBatchServiceTest.java index 14ad0a2bf..e0691314a 100644 --- a/firstleg/src/test/java/com/baiyue/firstleg/service/MeasurementBatchServiceTest.java +++ b/firstleg/src/test/java/com/baiyue/firstleg/service/MeasurementBatchServiceTest.java @@ -427,7 +427,7 @@ class MeasurementBatchServiceTest { InboundOrderEntity inbound = new InboundOrderEntity(); inbound.setId(10L); inbound.setTenantId(1L); - inbound.setCustomerId(1L); + inbound.setCustomerId(null); inbound.setProcessStatus("WAIT_MEASURE"); inbound.setVersion(0); return inbound;