Browse Source
按 SKU + 邮编由仓择优,与既有「运费试算」语义不同,core 新增独立能力: - WarehouseChannelRecommendCapable.recommend(ctx, query) - ChannelRecommendQuery:products[sku,qty] + postcode + signatureRequired + warehouseCode - ChannelRecommendResult:文档两层结构原样保留 optimalPlan(仓 / 渠道 / 总价)+ warehouseQuotes[] (每仓含 inventoryEnough、库存不足原因、各渠道报价与费用明细) - WarehouseCapability 追加 CHANNEL_RECOMMEND(200),既有值未动 - 门面入口 fees().recommend(query),只读、不走幂等 信仓实现: - 请求体 products / postcode / is_sign(是/否),未指定仓时不下发 warehouse_code - isInventoryEnough 兼容 1/0、true/false、是/否,认不出返回 null - optimalPlan 为空是合法结果(该城无可用渠道),返回空而非抛异常; 仅 rst 整体缺失才抛 ValidationException 待核对:文档标注 GET + application/json body,但 OkHttp 不支持 GET 带 body (会直接丢弃),这里按 POST + JSON body 下发,联调时若仓方只认 GET 需换传输实现,改动集中在 XincangConstants.PATH_CHANNEL_RECOMMEND 一处。 Co-Authored-By: Claude Code <noreply@anthropic.com>feature/supplier-lingxing
12 changed files with 1043 additions and 4 deletions
@ -0,0 +1,28 @@
@@ -0,0 +1,28 @@
|
||||
package common.ecommerce.warehouse.capability; |
||||
|
||||
import common.ecommerce.warehouse.model.ChannelRecommendQuery; |
||||
import common.ecommerce.warehouse.model.ChannelRecommendResult; |
||||
import common.ecommerce.warehouse.sdk.WarehouseAdapter; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
|
||||
/** |
||||
* 最优渠道推荐能力:给定 SKU 与目的地邮编,由仓给出推荐仓 + 渠道与全部报价。 |
||||
* |
||||
* <p><b>与 {@link WarehouseFeeEstimateCapable} 不是一回事</b>:那个要求调用方 |
||||
* 指定包裹尺寸重量与物流渠道,问「这么发多少钱」;本能力只给货品与邮编, |
||||
* 由仓回答「发哪儿、走哪条渠道、多少钱」。</p> |
||||
* |
||||
* <p>只读操作,无幂等要求。</p> |
||||
*/ |
||||
public interface WarehouseChannelRecommendCapable extends WarehouseAdapter { |
||||
|
||||
/** |
||||
* 询价并取推荐方案。 |
||||
* |
||||
* @param query SKU 与件数、目的地邮编;必填项在构造期已校验 |
||||
* @return 仓方推荐方案 + 全部仓 × 渠道报价 |
||||
* @throws common.ecommerce.warehouse.error.WarehouseUnsupportedCapabilityException |
||||
* 该仓不声明 {@code CHANNEL_RECOMMEND} |
||||
*/ |
||||
ChannelRecommendResult recommend(WarehouseContext ctx, ChannelRecommendQuery query); |
||||
} |
||||
@ -0,0 +1,129 @@
@@ -0,0 +1,129 @@
|
||||
package common.ecommerce.warehouse.model; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 最优渠道询价条件:按商品 SKU + 目的地邮编,让仓给出推荐渠道与各仓各渠道报价。 |
||||
* |
||||
* <p><b>与 {@link FeeEstimateQuery} 不是一回事</b>:那个要求调用方给包裹尺寸重量与 |
||||
* 指定物流渠道,属于「已知怎么发、问多少钱」;本类只给货品与邮编,由仓决定 |
||||
* 「发哪个仓、走哪个渠道、多少钱」,所以不需要尺寸重量。</p> |
||||
* |
||||
* <p>只读操作,无幂等要求。</p> |
||||
*/ |
||||
public final class ChannelRecommendQuery { |
||||
|
||||
private final List<SkuQuantity> products; |
||||
private final String postalCode; |
||||
/** 是否需要签名服务;null 表示按仓默认。 */ |
||||
private final Boolean signatureRequired; |
||||
/** 指定发货仓;(null/空)表示让仓在所有仓里择优。 */ |
||||
private final String warehouseCode; |
||||
|
||||
private ChannelRecommendQuery(Builder b) { |
||||
if (b.products.isEmpty()) { |
||||
throw new IllegalArgumentException("products must not be empty"); |
||||
} |
||||
this.products = Collections.unmodifiableList(new ArrayList<SkuQuantity>(b.products)); |
||||
this.postalCode = require(b.postalCode, "postalCode"); |
||||
this.signatureRequired = b.signatureRequired; |
||||
this.warehouseCode = b.warehouseCode; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
public List<SkuQuantity> products() { |
||||
return products; |
||||
} |
||||
|
||||
/** 目的地邮编。 */ |
||||
public String postalCode() { |
||||
return postalCode; |
||||
} |
||||
|
||||
/** 是否需要签名服务;null 表示不传。 */ |
||||
public Boolean signatureRequired() { |
||||
return signatureRequired; |
||||
} |
||||
|
||||
/** 指定发货仓;null 表示由仓择优。 */ |
||||
public String warehouseCode() { |
||||
return warehouseCode; |
||||
} |
||||
|
||||
private static String require(String v, String name) { |
||||
if (v == null || v.trim().isEmpty()) { |
||||
throw new IllegalArgumentException(name + " must not be blank"); |
||||
} |
||||
return v.trim(); |
||||
} |
||||
|
||||
/** 一行商品:SKU 与件数。 */ |
||||
public static final class SkuQuantity { |
||||
|
||||
private final String sku; |
||||
private final int quantity; |
||||
|
||||
public SkuQuantity(String sku, int quantity) { |
||||
if (sku == null || sku.trim().isEmpty()) { |
||||
throw new IllegalArgumentException("sku must not be blank"); |
||||
} |
||||
if (quantity <= 0) { |
||||
throw new IllegalArgumentException("quantity must be positive"); |
||||
} |
||||
this.sku = sku.trim(); |
||||
this.quantity = quantity; |
||||
} |
||||
|
||||
public String sku() { |
||||
return sku; |
||||
} |
||||
|
||||
public int quantity() { |
||||
return quantity; |
||||
} |
||||
} |
||||
|
||||
public static final class Builder { |
||||
|
||||
private final List<SkuQuantity> products = new ArrayList<SkuQuantity>(); |
||||
private String postalCode; |
||||
private Boolean signatureRequired; |
||||
private String warehouseCode; |
||||
|
||||
public Builder product(String sku, int quantity) { |
||||
this.products.add(new SkuQuantity(sku, quantity)); |
||||
return this; |
||||
} |
||||
|
||||
public Builder products(List<SkuQuantity> products) { |
||||
if (products != null) { |
||||
this.products.addAll(products); |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
public Builder postalCode(String v) { |
||||
this.postalCode = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder signatureRequired(Boolean v) { |
||||
this.signatureRequired = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder warehouseCode(String v) { |
||||
this.warehouseCode = v; |
||||
return this; |
||||
} |
||||
|
||||
public ChannelRecommendQuery build() { |
||||
return new ChannelRecommendQuery(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,341 @@
@@ -0,0 +1,341 @@
|
||||
package common.ecommerce.warehouse.model; |
||||
|
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 最优渠道询价结果:一个仓方的推荐结论 + 全部仓 × 渠道的报价明细。 |
||||
* |
||||
* <p>文档给两层:一层是「最优方案」(单条),一层是「所有仓库费用列表」。 |
||||
* 两层都保留——只给最优就没法比价,只给明细又得让每个调用方自己算最小值。</p> |
||||
* |
||||
* <p>这是<b>报价</b>,不是账单:实际费用以出库结算为准。</p> |
||||
*/ |
||||
public final class ChannelRecommendResult { |
||||
|
||||
/** 接入通道。 */ |
||||
private final WarehouseProvider sourceProvider; |
||||
/** 最优方案;仓方没给出时为 null。 */ |
||||
private final OptimalPlan optimalPlan; |
||||
/** 所有仓库的报价;可能为空。 */ |
||||
private final List<WarehouseQuote> warehouseQuotes; |
||||
|
||||
private ChannelRecommendResult(Builder b) { |
||||
this.sourceProvider = b.sourceProvider; |
||||
this.optimalPlan = b.optimalPlan; |
||||
this.warehouseQuotes = Collections.unmodifiableList(new ArrayList<WarehouseQuote>(b.warehouseQuotes)); |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
public WarehouseProvider sourceProvider() { |
||||
return sourceProvider; |
||||
} |
||||
|
||||
/** 最优方案;可能为 null。 */ |
||||
public OptimalPlan optimalPlan() { |
||||
return optimalPlan; |
||||
} |
||||
|
||||
/** 所有仓库的报价;可能为空。 */ |
||||
public List<WarehouseQuote> warehouseQuotes() { |
||||
return warehouseQuotes; |
||||
} |
||||
|
||||
/** 仓方的推荐方案。 */ |
||||
public static final class OptimalPlan { |
||||
|
||||
private final String warehouseCode; |
||||
private final String serviceCode; |
||||
private final Money total; |
||||
|
||||
private OptimalPlan(Builder b) { |
||||
this.warehouseCode = require(b.warehouseCode, "warehouseCode"); |
||||
this.serviceCode = require(b.serviceCode, "serviceCode"); |
||||
this.total = b.total; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
/** 推荐发货仓。 */ |
||||
public String warehouseCode() { |
||||
return warehouseCode; |
||||
} |
||||
|
||||
/** 推荐物流方式(仓方渠道码)。 */ |
||||
public String serviceCode() { |
||||
return serviceCode; |
||||
} |
||||
|
||||
/** 总费用;仓方未给时为 null。 */ |
||||
public Money total() { |
||||
return total; |
||||
} |
||||
|
||||
private static String require(String v, String name) { |
||||
if (v == null || v.trim().isEmpty()) { |
||||
throw new IllegalArgumentException(name + " must not be blank"); |
||||
} |
||||
return v.trim(); |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private String warehouseCode; |
||||
private String serviceCode; |
||||
private Money total; |
||||
|
||||
public Builder warehouseCode(String v) { |
||||
this.warehouseCode = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder serviceCode(String v) { |
||||
this.serviceCode = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder total(Money v) { |
||||
this.total = v; |
||||
return this; |
||||
} |
||||
|
||||
public OptimalPlan build() { |
||||
return new OptimalPlan(this); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** 单个仓库的报价:库存是否够 + 各渠道报价。 */ |
||||
public static final class WarehouseQuote { |
||||
|
||||
private final String warehouseCode; |
||||
/** 库存是否足够;null 表示仓方没给。 */ |
||||
private final Boolean inventoryEnough; |
||||
/** 库存不足的原因;正常为空。 */ |
||||
private final String insufficientStockError; |
||||
private final List<ServiceQuote> services; |
||||
|
||||
private WarehouseQuote(Builder b) { |
||||
this.warehouseCode = require(b.warehouseCode, "warehouseCode"); |
||||
this.inventoryEnough = b.inventoryEnough; |
||||
this.insufficientStockError = b.insufficientStockError; |
||||
this.services = Collections.unmodifiableList(new ArrayList<ServiceQuote>(b.services)); |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
public String warehouseCode() { |
||||
return warehouseCode; |
||||
} |
||||
|
||||
/** 库存是否足够;null 表示未知。 */ |
||||
public Boolean inventoryEnough() { |
||||
return inventoryEnough; |
||||
} |
||||
|
||||
/** 库存不足的原因;无则为 null。 */ |
||||
public String insufficientStockError() { |
||||
return insufficientStockError; |
||||
} |
||||
|
||||
/** 该仓下的渠道报价;可能为空。 */ |
||||
public List<ServiceQuote> services() { |
||||
return services; |
||||
} |
||||
|
||||
private static String require(String v, String name) { |
||||
if (v == null || v.trim().isEmpty()) { |
||||
throw new IllegalArgumentException(name + " must not be blank"); |
||||
} |
||||
return v.trim(); |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private String warehouseCode; |
||||
private Boolean inventoryEnough; |
||||
private String insufficientStockError; |
||||
private final List<ServiceQuote> services = new ArrayList<ServiceQuote>(); |
||||
|
||||
public Builder warehouseCode(String v) { |
||||
this.warehouseCode = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder inventoryEnough(Boolean v) { |
||||
this.inventoryEnough = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder insufficientStockError(String v) { |
||||
this.insufficientStockError = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder service(ServiceQuote v) { |
||||
this.services.add(v); |
||||
return this; |
||||
} |
||||
|
||||
public WarehouseQuote build() { |
||||
return new WarehouseQuote(this); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** 单个物流渠道的报价。 */ |
||||
public static final class ServiceQuote { |
||||
|
||||
private final String serviceCode; |
||||
private final Money total; |
||||
private final List<FeeItem> fees; |
||||
|
||||
private ServiceQuote(Builder b) { |
||||
this.serviceCode = require(b.serviceCode, "serviceCode"); |
||||
this.total = b.total; |
||||
this.fees = Collections.unmodifiableList(new ArrayList<FeeItem>(b.fees)); |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
/** 物流方式(仓方渠道码)。 */ |
||||
public String serviceCode() { |
||||
return serviceCode; |
||||
} |
||||
|
||||
/** 总费用;仓方未给时为 null。 */ |
||||
public Money total() { |
||||
return total; |
||||
} |
||||
|
||||
/** 费用明细。 */ |
||||
public List<FeeItem> fees() { |
||||
return fees; |
||||
} |
||||
|
||||
private static String require(String v, String name) { |
||||
if (v == null || v.trim().isEmpty()) { |
||||
throw new IllegalArgumentException(name + " must not be blank"); |
||||
} |
||||
return v.trim(); |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private String serviceCode; |
||||
private Money total; |
||||
private final List<FeeItem> fees = new ArrayList<FeeItem>(); |
||||
|
||||
public Builder serviceCode(String v) { |
||||
this.serviceCode = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder total(Money v) { |
||||
this.total = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder fee(FeeItem v) { |
||||
this.fees.add(v); |
||||
return this; |
||||
} |
||||
|
||||
public ServiceQuote build() { |
||||
return new ServiceQuote(this); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** 一条费用明细。 */ |
||||
public static final class FeeItem { |
||||
|
||||
/** 一级分类,如「快递费」。 */ |
||||
private final String bigFeeType; |
||||
/** 费用项,如「燃油附加费」。 */ |
||||
private final String feeType; |
||||
private final Money fee; |
||||
|
||||
private FeeItem(Builder b) { |
||||
this.bigFeeType = b.bigFeeType; |
||||
this.feeType = b.feeType; |
||||
this.fee = b.fee; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
public String bigFeeType() { |
||||
return bigFeeType; |
||||
} |
||||
|
||||
public String feeType() { |
||||
return feeType; |
||||
} |
||||
|
||||
public Money fee() { |
||||
return fee; |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private String bigFeeType; |
||||
private String feeType; |
||||
private Money fee; |
||||
|
||||
public Builder bigFeeType(String v) { |
||||
this.bigFeeType = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder feeType(String v) { |
||||
this.feeType = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder fee(Money v) { |
||||
this.fee = v; |
||||
return this; |
||||
} |
||||
|
||||
public FeeItem build() { |
||||
return new FeeItem(this); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private WarehouseProvider sourceProvider; |
||||
private OptimalPlan optimalPlan; |
||||
private final List<WarehouseQuote> warehouseQuotes = new ArrayList<WarehouseQuote>(); |
||||
|
||||
public Builder sourceProvider(WarehouseProvider v) { |
||||
this.sourceProvider = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder optimalPlan(OptimalPlan v) { |
||||
this.optimalPlan = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder warehouseQuote(WarehouseQuote v) { |
||||
this.warehouseQuotes.add(v); |
||||
return this; |
||||
} |
||||
|
||||
public ChannelRecommendResult build() { |
||||
return new ChannelRecommendResult(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,155 @@
@@ -0,0 +1,155 @@
|
||||
package common.ecommerce.xincang.adapter; |
||||
|
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.ChannelRecommendQuery; |
||||
import common.ecommerce.warehouse.model.ChannelRecommendResult; |
||||
import common.ecommerce.xincang.dto.XincangDtos; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 最优渠道报文映射({@code api.calculate_order_v3})。 |
||||
* |
||||
* <p>请求体:{@code products[{sku, qty}]} + {@code postcode} + {@code is_sign}, |
||||
* 币种固定 USD(信仓文档金额口径)。</p> |
||||
* |
||||
* <p>响应两层都映射:{@code optimalPlan} 原样给,{@code warehouseFeeList} |
||||
* 逐仓逐渠道展开,费用项保留一级分类与费用名。</p> |
||||
*/ |
||||
public final class XincangChannelRecommendMapper { |
||||
|
||||
private static final String CURRENCY_USD = "USD"; |
||||
/** 信仓只收「是 / 否」。 */ |
||||
private static final String YES = "是"; |
||||
private static final String NO = "否"; |
||||
/** 未指定时按「否」下发(文档必填项)。 */ |
||||
private static final String DEFAULT_SIGN = NO; |
||||
|
||||
private XincangChannelRecommendMapper() { |
||||
} |
||||
|
||||
/** 统一询价条件 → 信仓请求体。 */ |
||||
public static XincangDtos.CalculateV3Request toRequest(ChannelRecommendQuery query) { |
||||
XincangDtos.CalculateV3Request req = new XincangDtos.CalculateV3Request(); |
||||
List<XincangDtos.CalculateV3Request.Product> products = |
||||
new ArrayList<XincangDtos.CalculateV3Request.Product>(query.products().size()); |
||||
for (ChannelRecommendQuery.SkuQuantity item : query.products()) { |
||||
XincangDtos.CalculateV3Request.Product product = new XincangDtos.CalculateV3Request.Product(); |
||||
product.sku = item.sku(); |
||||
product.qty = String.valueOf(item.quantity()); |
||||
products.add(product); |
||||
} |
||||
req.products = products; |
||||
req.postcode = query.postalCode(); |
||||
req.isSign = query.signatureRequired() == null |
||||
? DEFAULT_SIGN |
||||
: (query.signatureRequired() ? YES : NO); |
||||
req.warehouseCode = blankToNull(query.warehouseCode()); |
||||
return req; |
||||
} |
||||
|
||||
/** 信仓响应 → 统一结果。 */ |
||||
public static ChannelRecommendResult toResult(XincangDtos.CalculateV3Result rst) { |
||||
ChannelRecommendResult.Builder builder = ChannelRecommendResult.builder() |
||||
.sourceProvider(WarehouseProvider.XINCANG); |
||||
if (rst.optimalPlan != null) { |
||||
builder.optimalPlan(ChannelRecommendResult.OptimalPlan.builder() |
||||
.warehouseCode(blankToNull(rst.optimalPlan.warehouse) == null |
||||
? "-" : rst.optimalPlan.warehouse.trim()) |
||||
.serviceCode(blankToNull(rst.optimalPlan.serviceCode) == null |
||||
? "-" : rst.optimalPlan.serviceCode.trim()) |
||||
.total(parseMoney(rst.optimalPlan.sum)) |
||||
.build()); |
||||
} |
||||
if (rst.warehouseFeeList != null) { |
||||
for (XincangDtos.CalculateV3Result.WarehouseFee warehouse : rst.warehouseFeeList) { |
||||
if (warehouse == null) { |
||||
continue; |
||||
} |
||||
builder.warehouseQuote(toQuote(warehouse)); |
||||
} |
||||
} |
||||
return builder.build(); |
||||
} |
||||
|
||||
private static ChannelRecommendResult.WarehouseQuote toQuote( |
||||
XincangDtos.CalculateV3Result.WarehouseFee warehouse) { |
||||
ChannelRecommendResult.WarehouseQuote.Builder builder = |
||||
ChannelRecommendResult.WarehouseQuote.builder() |
||||
.warehouseCode(blankToNull(warehouse.warehouse) == null |
||||
? "-" : warehouse.warehouse.trim()) |
||||
.inventoryEnough(parseFlag(warehouse.isInventoryEnough)) |
||||
.insufficientStockError(blankToNull(warehouse.insufficientStockError)); |
||||
if (warehouse.logisticsService != null) { |
||||
for (XincangDtos.CalculateV3Result.LogisticsService service : warehouse.logisticsService) { |
||||
if (service == null || blankToNull(service.serviceCode) == null) { |
||||
continue; |
||||
} |
||||
builder.service(toService(service)); |
||||
} |
||||
} |
||||
return builder.build(); |
||||
} |
||||
|
||||
private static ChannelRecommendResult.ServiceQuote toService( |
||||
XincangDtos.CalculateV3Result.LogisticsService service) { |
||||
ChannelRecommendResult.ServiceQuote.Builder builder = |
||||
ChannelRecommendResult.ServiceQuote.builder() |
||||
.serviceCode(service.serviceCode.trim()) |
||||
.total(parseMoney(service.sum)); |
||||
if (service.fees != null) { |
||||
for (XincangDtos.CalculateV3Result.Fee fee : service.fees) { |
||||
if (fee == null) { |
||||
continue; |
||||
} |
||||
builder.fee(ChannelRecommendResult.FeeItem.builder() |
||||
.bigFeeType(blankToNull(fee.bigFeeType)) |
||||
.feeType(blankToNull(fee.feeType)) |
||||
.fee(parseMoney(fee.customerFee)) |
||||
.build()); |
||||
} |
||||
} |
||||
return builder.build(); |
||||
} |
||||
|
||||
/** |
||||
* 库存是否足够:文档示例给的是 {@code "1"} / {@code "0"}。 |
||||
* |
||||
* <p>历史接口里 {@code isInventoryEnough} 有过 boolean 改成 string 的破坏性变更 |
||||
* (见迁移说明),所以这里同时接受 {@code 1/0} 与 {@code true/false}; |
||||
* 认不出返回 null,不猜。</p> |
||||
*/ |
||||
static Boolean parseFlag(String raw) { |
||||
if (raw == null) { |
||||
return null; |
||||
} |
||||
String s = raw.trim(); |
||||
if ("1".equals(s) || "true".equalsIgnoreCase(s) || "是".equals(s)) { |
||||
return Boolean.TRUE; |
||||
} |
||||
if ("0".equals(s) || "false".equalsIgnoreCase(s) || "否".equals(s)) { |
||||
return Boolean.FALSE; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** 金额字符串 → USD;空值 / 非法返回 null。 */ |
||||
static Money parseMoney(String raw) { |
||||
if (raw == null || raw.trim().isEmpty()) { |
||||
return null; |
||||
} |
||||
try { |
||||
BigDecimal amount = new BigDecimal(raw.trim()); |
||||
return Money.of(amount, CURRENCY_USD); |
||||
} catch (RuntimeException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
private static String blankToNull(String v) { |
||||
return v == null || v.trim().isEmpty() ? null : v.trim(); |
||||
} |
||||
} |
||||
@ -0,0 +1,198 @@
@@ -0,0 +1,198 @@
|
||||
package common.ecommerce.xincang.adapter; |
||||
|
||||
import common.ecommerce.shared.error.ValidationException; |
||||
import common.ecommerce.shared.spi.ChannelSecret; |
||||
import common.ecommerce.shared.testkit.InMemoryLockProvider; |
||||
import common.ecommerce.shared.testkit.InMemoryTokenStore; |
||||
import common.ecommerce.shared.testkit.MutableClock; |
||||
import common.ecommerce.shared.testkit.RecordingTransport; |
||||
import common.ecommerce.shared.testkit.Responses; |
||||
import common.ecommerce.warehouse.enums.WarehouseCapability; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.ChannelRecommendQuery; |
||||
import common.ecommerce.warehouse.model.ChannelRecommendResult; |
||||
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
import common.ecommerce.xincang.XincangConstants; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.Clock; |
||||
import java.time.Duration; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
|
||||
/** |
||||
* 最优渠道({@code api.calculate_order_v3})契约测试。 |
||||
*/ |
||||
class XincangChannelRecommendTest { |
||||
|
||||
private static final String BASE = XincangConstants.TEST_BASE_URL; |
||||
private static final String APP_KEY = "ak-1"; |
||||
|
||||
/** 文档成功示例(截取两层结构)。 */ |
||||
private static final String OK_BODY = "{\"rst\":{" |
||||
+ "\"optimalPlan\":{\"warehouse\":\"CA-3790\",\"service_code\":\"GOFO-G\",\"sum\":\"20.87\"}," |
||||
+ "\"warehouseFeeList\":[" |
||||
+ "{\"warehouse\":\"CA-3790\",\"isInventoryEnough\":\"1\",\"insufficientStockError\":\"\"," |
||||
+ "\"logisticsService\":[" |
||||
+ "{\"service_code\":\"GOFO-G\",\"sum\":\"20.87\",\"fees\":[" |
||||
+ "{\"big_fee_type\":\"快递费\",\"fee_type\":\"基础费\",\"customer_fee\":\"11.87\"}," |
||||
+ "{\"big_fee_type\":\"快递费\",\"fee_type\":\"燃油附加费\",\"customer_fee\":\"5.64\"}]}," |
||||
+ "{\"service_code\":\"USPS-G\",\"sum\":\"22.10\",\"fees\":[]}]}," |
||||
+ "{\"warehouse\":\"NJ-2605\",\"isInventoryEnough\":\"0\"," |
||||
+ "\"insufficientStockError\":\"库存不足\",\"logisticsService\":[]}" |
||||
+ "]}," |
||||
+ "\"errno\":0,\"err\":\"\",\"trace_id\":\"t-v3\"}"; |
||||
|
||||
private RecordingTransport transport; |
||||
private XincangAdapter adapter; |
||||
|
||||
@BeforeEach |
||||
void setUp() { |
||||
transport = RecordingTransport.create(); |
||||
MutableClock clock = MutableClock.atUtc("2026-11-10T00:00:00Z"); |
||||
transport.route(XincangConstants.PATH_LOGIN, req -> Responses.ok( |
||||
"{\"rst\":{\"id\":\"c-1\",\"token\":\"{T-1}\",\"token_expire_time\":\"" |
||||
+ java.time.LocalDateTime.ofInstant(clock.instant().plus(Duration.ofHours(2)), |
||||
XincangConstants.SYSTEM_ZONE) |
||||
.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) |
||||
+ "\"},\"errno\":0,\"err\":\"\",\"trace_id\":\"t-login\"}")); |
||||
adapter = new XincangAdapter(transport, new common.ecommerce.http.JacksonJsonCodec(), |
||||
new InMemoryTokenStore(), new InMemoryLockProvider(), clock); |
||||
} |
||||
|
||||
private static WarehouseContext ctx() { |
||||
return WarehouseContext.of("XC-CA-3790", WarehouseProvider.XINCANG, "CA-3790", |
||||
WarehouseConnection.of(BASE, ChannelSecret.builder() |
||||
.appKey(APP_KEY).appSecret("sk").build()), |
||||
"req-1", Clock.systemUTC()); |
||||
} |
||||
|
||||
private static ChannelRecommendQuery query() { |
||||
return ChannelRecommendQuery.builder() |
||||
.product("NOTEBOOK-MULTI-3", 9) |
||||
.product("BOX-PENS-10", 3) |
||||
.postalCode("937862") |
||||
.signatureRequired(false) |
||||
.build(); |
||||
} |
||||
|
||||
@Test |
||||
void sendsDocumentedRequestShape() { |
||||
transport.route(XincangConstants.PATH_CHANNEL_RECOMMEND, req -> Responses.ok(OK_BODY)); |
||||
|
||||
adapter.recommend(ctx(), query()); |
||||
|
||||
common.ecommerce.http.HttpRequest req = transport.lastDataRequest(); |
||||
assertTrue(req.url().contains("/api.calculate_order_v3?token=%7BT-1%7D"), req.url()); |
||||
assertEquals("application/json", req.contentType()); |
||||
|
||||
String body = req.body(); |
||||
assertTrue(body.contains("\"sku\":\"NOTEBOOK-MULTI-3\""), body); |
||||
assertTrue(body.contains("\"qty\":\"9\""), body); |
||||
assertTrue(body.contains("\"sku\":\"BOX-PENS-10\""), body); |
||||
assertTrue(body.contains("\"qty\":\"3\""), body); |
||||
assertTrue(body.contains("\"postcode\":\"937862\""), body); |
||||
assertTrue(body.contains("\"is_sign\":\"否\""), body); |
||||
// 未指定仓库时不下发 warehouse_code
|
||||
assertFalse(body.contains("warehouse_code"), body); |
||||
} |
||||
|
||||
@Test |
||||
void mapsOptimalPlanAndAllQuotes() { |
||||
transport.route(XincangConstants.PATH_CHANNEL_RECOMMEND, req -> Responses.ok(OK_BODY)); |
||||
|
||||
ChannelRecommendResult result = adapter.recommend(ctx(), query()); |
||||
|
||||
assertEquals(WarehouseProvider.XINCANG, result.sourceProvider()); |
||||
|
||||
// 最优方案
|
||||
assertNotNull(result.optimalPlan()); |
||||
assertEquals("CA-3790", result.optimalPlan().warehouseCode()); |
||||
assertEquals("GOFO-G", result.optimalPlan().serviceCode()); |
||||
assertEquals(0, result.optimalPlan().total().amount().compareTo(new java.math.BigDecimal("20.87"))); |
||||
assertEquals("USD", result.optimalPlan().total().currencyCode()); |
||||
|
||||
// 所有仓库明细
|
||||
assertEquals(2, result.warehouseQuotes().size()); |
||||
ChannelRecommendResult.WarehouseQuote first = result.warehouseQuotes().get(0); |
||||
assertEquals("CA-3790", first.warehouseCode()); |
||||
assertEquals(Boolean.TRUE, first.inventoryEnough()); |
||||
assertNull(first.insufficientStockError(), "空串应转 null"); |
||||
assertEquals(2, first.services().size()); |
||||
|
||||
ChannelRecommendResult.ServiceQuote gogo = first.services().get(0); |
||||
assertEquals("GOFO-G", gogo.serviceCode()); |
||||
assertEquals(2, gogo.fees().size()); |
||||
assertEquals("快递费", gogo.fees().get(0).bigFeeType()); |
||||
assertEquals("燃油附加费", gogo.fees().get(1).feeType()); |
||||
assertEquals(0, gogo.fees().get(1).fee().amount() |
||||
.compareTo(new java.math.BigDecimal("5.64"))); |
||||
|
||||
ChannelRecommendResult.WarehouseQuote second = result.warehouseQuotes().get(1); |
||||
assertEquals(Boolean.FALSE, second.inventoryEnough()); |
||||
assertEquals("库存不足", second.insufficientStockError()); |
||||
assertTrue(second.services().isEmpty()); |
||||
} |
||||
|
||||
@Test |
||||
void signatureIsSentWhenRequested() { |
||||
transport.route(XincangConstants.PATH_CHANNEL_RECOMMEND, req -> Responses.ok(OK_BODY)); |
||||
ChannelRecommendQuery signed = ChannelRecommendQuery.builder() |
||||
.product("SKU-A", 1).postalCode("10001").signatureRequired(true).build(); |
||||
|
||||
adapter.recommend(ctx(), signed); |
||||
|
||||
assertTrue(transport.lastDataRequest().body().contains("\"is_sign\":\"是\"")); |
||||
} |
||||
|
||||
@Test |
||||
void warehouseCodeIsSentWhenSpecified() { |
||||
transport.route(XincangConstants.PATH_CHANNEL_RECOMMEND, req -> Responses.ok(OK_BODY)); |
||||
ChannelRecommendQuery pinned = ChannelRecommendQuery.builder() |
||||
.product("SKU-A", 1).postalCode("10001").warehouseCode("CA-3790").build(); |
||||
|
||||
adapter.recommend(ctx(), pinned); |
||||
|
||||
assertTrue(transport.lastDataRequest().body().contains("\"warehouse_code\":\"CA-3790\"")); |
||||
} |
||||
|
||||
@Test |
||||
void emptyOptimalPlanIsNullNotThrow() { |
||||
transport.route(XincangConstants.PATH_CHANNEL_RECOMMEND, req -> Responses.ok( |
||||
"{\"rst\":{\"optimalPlan\":null,\"warehouseFeeList\":[]}," |
||||
+ "\"errno\":0,\"err\":\"\",\"trace_id\":\"t-empty\"}")); |
||||
|
||||
ChannelRecommendResult result = adapter.recommend(ctx(), query()); |
||||
|
||||
assertNull(result.optimalPlan(), "无可用渠道时为 null"); |
||||
assertTrue(result.warehouseQuotes().isEmpty()); |
||||
} |
||||
|
||||
@Test |
||||
void missingRstIsNotSilentSuccess() { |
||||
transport.route(XincangConstants.PATH_CHANNEL_RECOMMEND, req -> Responses.ok( |
||||
"{\"rst\":null,\"errno\":0,\"err\":\"\"}")); |
||||
|
||||
ValidationException e = assertThrows(ValidationException.class, |
||||
() -> adapter.recommend(ctx(), query())); |
||||
assertTrue(e.getMessage().contains("no rst"), e.getMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void capabilityDeclared() { |
||||
assertTrue(adapter.capabilities().contains(WarehouseCapability.CHANNEL_RECOMMEND)); |
||||
assertTrue(adapter.supports("XC-CA-3790", WarehouseCapability.CHANNEL_RECOMMEND)); |
||||
} |
||||
|
||||
@Test |
||||
void busyFlagAcceptsBothStringForms() { |
||||
assertEquals(Boolean.TRUE, XincangChannelRecommendMapper.parseFlag("1")); |
||||
assertEquals(Boolean.TRUE, XincangChannelRecommendMapper.parseFlag("true")); |
||||
assertEquals(Boolean.FALSE, XincangChannelRecommendMapper.parseFlag("0")); |
||||
assertEquals(Boolean.FALSE, XincangChannelRecommendMapper.parseFlag("false")); |
||||
assertNull(XincangChannelRecommendMapper.parseFlag("")); |
||||
assertNull(XincangChannelRecommendMapper.parseFlag("unknown")); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue