diff --git a/core/src/main/java/common/ecommerce/capability/OutboundCapable.java b/core/src/main/java/common/ecommerce/capability/OutboundCapable.java new file mode 100644 index 0000000..d122df5 --- /dev/null +++ b/core/src/main/java/common/ecommerce/capability/OutboundCapable.java @@ -0,0 +1,25 @@ +package common.ecommerce.capability; + +import common.ecommerce.model.fulfillment.OutboundPackage; +import common.ecommerce.model.fulfillment.OutboundResult; +import common.ecommerce.sdk.ChannelContext; + +import java.util.List; + +/** + * 快速出库能力:提交出库任务 + 查询任务结果。 + * + *

出库是异步的:{@link #submit} 只表示任务已受理,真正的成功/失败 + * 要用 {@link #results} 按系统单号回查。

+ * + *

写操作,必须经 {@code IdempotencyGuard} 执行;幂等键按系统单号, + * 与领星「一个订单只能出库一次」的约束一致。

+ */ +public interface OutboundCapable extends PlatformAdapter { + + /** 提交出库任务;返回是否受理。 */ + boolean submit(ChannelContext ctx, List packages); + + /** 按系统单号查询出库结果。 */ + OutboundResult results(ChannelContext ctx, List globalOrderNos); +} diff --git a/core/src/main/java/common/ecommerce/capability/Outbounds.java b/core/src/main/java/common/ecommerce/capability/Outbounds.java new file mode 100644 index 0000000..4267a7c --- /dev/null +++ b/core/src/main/java/common/ecommerce/capability/Outbounds.java @@ -0,0 +1,42 @@ +package common.ecommerce.capability; + +import common.ecommerce.error.ChannelException; +import common.ecommerce.model.Capability; +import common.ecommerce.model.account.ChannelAccount; +import common.ecommerce.model.fulfillment.OutboundPackage; +import common.ecommerce.model.fulfillment.OutboundResult; +import common.ecommerce.sdk.ChannelContext; + +import java.util.List; + +/** + * 门面持有的出库视图。调用前做能力检查,缺失时显式拒绝。 + */ +public final class Outbounds { + + private final PlatformAdapter adapter; + private final ChannelAccount account; + private final ChannelContext ctx; + + public Outbounds(PlatformAdapter adapter, ChannelAccount account, ChannelContext ctx) { + this.adapter = adapter; + this.account = account; + this.ctx = ctx; + } + + public boolean submit(List packages) { + requireOutboundCapable(); + return ((OutboundCapable) adapter).submit(ctx, packages); + } + + public OutboundResult results(List globalOrderNos) { + requireOutboundCapable(); + return ((OutboundCapable) adapter).results(ctx, globalOrderNos); + } + + private void requireOutboundCapable() { + if (!adapter.supports(account, Capability.MARK_SHIPPED) || !(adapter instanceof OutboundCapable)) { + throw new ChannelException.UnsupportedCapabilityException(Capability.MARK_SHIPPED); + } + } +} diff --git a/core/src/main/java/common/ecommerce/model/fulfillment/OutboundPackage.java b/core/src/main/java/common/ecommerce/model/fulfillment/OutboundPackage.java new file mode 100644 index 0000000..90231c8 --- /dev/null +++ b/core/src/main/java/common/ecommerce/model/fulfillment/OutboundPackage.java @@ -0,0 +1,101 @@ +package common.ecommerce.model.fulfillment; + +import java.util.Objects; + +/** + * 一次快速出库请求中的单个包裹。 + * + *

字段以领星「快速出库」接口为准;各平台接入时由各自 Source 翻译。 + * {@code globalOrderNo} 是平台系统单号(领星侧 {@code global_order_no}), + * 也是出库结果的查询键。

+ */ +public final class OutboundPackage { + + private final String globalOrderNo; + private final Long warehouseId; + private final String logisticsTypeId; + private final String waybillNo; + private final String trackingNo; + private final String weightUnit; + private final String realWeight; + private final String sizeUnit; + private final String length; + private final String width; + private final String height; + private final String feeWeight; + private final String logisticsFreight; + private final String logisticsFreightCurrency; + + private OutboundPackage(Builder b) { + this.globalOrderNo = Objects.requireNonNull(b.globalOrderNo, "globalOrderNo"); + this.warehouseId = b.warehouseId; + this.logisticsTypeId = b.logisticsTypeId; + this.waybillNo = b.waybillNo; + this.trackingNo = b.trackingNo; + this.weightUnit = b.weightUnit; + this.realWeight = b.realWeight; + this.sizeUnit = b.sizeUnit; + this.length = b.length; + this.width = b.width; + this.height = b.height; + this.feeWeight = b.feeWeight; + this.logisticsFreight = b.logisticsFreight; + this.logisticsFreightCurrency = b.logisticsFreightCurrency; + } + + public static Builder builder() { + return new Builder(); + } + + public String globalOrderNo() { return globalOrderNo; } + public Long warehouseId() { return warehouseId; } + public String logisticsTypeId() { return logisticsTypeId; } + public String waybillNo() { return waybillNo; } + public String trackingNo() { return trackingNo; } + public String weightUnit() { return weightUnit; } + public String realWeight() { return realWeight; } + public String sizeUnit() { return sizeUnit; } + public String length() { return length; } + public String width() { return width; } + public String height() { return height; } + public String feeWeight() { return feeWeight; } + public String logisticsFreight() { return logisticsFreight; } + public String logisticsFreightCurrency() { return logisticsFreightCurrency; } + + public static final class Builder { + + private String globalOrderNo; + private Long warehouseId; + private String logisticsTypeId; + private String waybillNo; + private String trackingNo; + private String weightUnit; + private String realWeight; + private String sizeUnit; + private String length; + private String width; + private String height; + private String feeWeight; + private String logisticsFreight; + private String logisticsFreightCurrency; + + public Builder globalOrderNo(String v) { this.globalOrderNo = v; return this; } + public Builder warehouseId(Long v) { this.warehouseId = v; return this; } + public Builder logisticsTypeId(String v) { this.logisticsTypeId = v; return this; } + public Builder waybillNo(String v) { this.waybillNo = v; return this; } + public Builder trackingNo(String v) { this.trackingNo = v; return this; } + public Builder weightUnit(String v) { this.weightUnit = v; return this; } + public Builder realWeight(String v) { this.realWeight = v; return this; } + public Builder sizeUnit(String v) { this.sizeUnit = v; return this; } + public Builder length(String v) { this.length = v; return this; } + public Builder width(String v) { this.width = v; return this; } + public Builder height(String v) { this.height = v; return this; } + public Builder feeWeight(String v) { this.feeWeight = v; return this; } + public Builder logisticsFreight(String v) { this.logisticsFreight = v; return this; } + public Builder logisticsFreightCurrency(String v) { this.logisticsFreightCurrency = v; return this; } + + public OutboundPackage build() { + return new OutboundPackage(this); + } + } +} diff --git a/core/src/main/java/common/ecommerce/model/fulfillment/OutboundResult.java b/core/src/main/java/common/ecommerce/model/fulfillment/OutboundResult.java new file mode 100644 index 0000000..44331ed --- /dev/null +++ b/core/src/main/java/common/ecommerce/model/fulfillment/OutboundResult.java @@ -0,0 +1,57 @@ +package common.ecommerce.model.fulfillment; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * 快速出库结果:成功与失败两组,按系统单号对应。 + */ +public final class OutboundResult { + + private final List success; + private final List failure; + + private OutboundResult(List success, List failure) { + this.success = Collections.unmodifiableList(new ArrayList(success)); + this.failure = Collections.unmodifiableList(new ArrayList(failure)); + } + + public static OutboundResult of(List success, List failure) { + return new OutboundResult(success, failure); + } + + public List success() { return success; } + public List failure() { return failure; } + + /** 出库成功的单。 */ + public static final class Success { + + private final String globalOrderNo; + private final String woNumber; + + public Success(String globalOrderNo, String woNumber) { + this.globalOrderNo = globalOrderNo; + this.woNumber = woNumber; + } + + public String globalOrderNo() { return globalOrderNo; } + /** 销售出库单号。 */ + public String woNumber() { return woNumber; } + } + + /** 出库失败的单及原因。 */ + public static final class Failure { + + private final String globalOrderNo; + private final String errorMessage; + + public Failure(String globalOrderNo, String errorMessage) { + this.globalOrderNo = globalOrderNo; + this.errorMessage = errorMessage; + } + + public String globalOrderNo() { return globalOrderNo; } + public String errorMessage() { return errorMessage; } + } +} diff --git a/core/src/main/java/common/ecommerce/sdk/ChannelSdk.java b/core/src/main/java/common/ecommerce/sdk/ChannelSdk.java index 83ace37..3429374 100644 --- a/core/src/main/java/common/ecommerce/sdk/ChannelSdk.java +++ b/core/src/main/java/common/ecommerce/sdk/ChannelSdk.java @@ -9,6 +9,7 @@ import common.ecommerce.capability.CapabilityReport; import common.ecommerce.capability.PlatformAdapter; import common.ecommerce.capability.Products; import common.ecommerce.capability.SalesOrders; +import common.ecommerce.capability.Outbounds; import common.ecommerce.capability.Settlements; import common.ecommerce.idempotency.IdempotencyGuard; import common.ecommerce.model.account.AccountKey; @@ -86,6 +87,12 @@ public final class ChannelSdk implements ProviderResolver { return new Settlements(resolve(key), account, newContext(account, connection)); } + /** 履约 / 出库视图。 */ + public Outbounds outbounds(AccountKey key, ChannelConnection connection) { + ChannelAccount account = requireAccount(key); + return new Outbounds(resolve(key), account, newContext(account, connection)); + } + /** * 聚合器的子店铺列表(如领星下已授权的亚马逊 / 沃尔玛店铺)。 * diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java b/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java index d4587bf..2c7fc1b 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java @@ -88,6 +88,12 @@ public enum LingxingApi { /** 亚马逊结算中心交易明细。POST,offset/length 分页,上限 10000。 */ SETTLEMENT_TRANSACTION_LIST("/bd/sp/api/open/settlement/transaction/detail/list", 10), + /** 快速出库(创建异步出库任务)。POST,qps 10,多平台通用。 */ + FAST_OUTBOUND("/pb/mp/order/v2/fastOutbound", 10), + + /** 快速出库结果查询。POST,qps 10,多平台通用。 */ + FAST_OUTBOUND_RESULT("/pb/mp/order/v2/getFastOutboundResult", 10), + /** 多平台订单列表;当前实现使用的跨平台接口。 */ ORDER_LIST("/pb/mp/order/v2/list", 10); diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapter.java b/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapter.java index fde0a4d..a6cbf91 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapter.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapter.java @@ -1,6 +1,7 @@ package common.ecommerce.lingxing.adapter; import common.ecommerce.capability.AggregatorCapable; +import common.ecommerce.capability.OutboundCapable; import common.ecommerce.capability.ProductCapable; import common.ecommerce.capability.SalesOrderCapable; import common.ecommerce.capability.SettlementCapable; @@ -15,6 +16,8 @@ import common.ecommerce.lingxing.http.LingxingRateLimiter; import common.ecommerce.lingxing.http.LingxingRequestFactory; import common.ecommerce.lingxing.platform.LingxingCall; import common.ecommerce.lingxing.platform.LingxingSubPlatform; +import common.ecommerce.lingxing.platform.LingxingSubPlatformDefaults; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.lingxing.platform.LingxingSubPlatforms; import common.ecommerce.model.Capability; import common.ecommerce.model.ChannelProvider; @@ -23,6 +26,8 @@ import common.ecommerce.model.account.ChannelAccount; import common.ecommerce.model.order.SalesOrder; import common.ecommerce.model.order.SalesOrderQuery; import common.ecommerce.model.order.SalesOrderRef; +import common.ecommerce.model.fulfillment.OutboundPackage; +import common.ecommerce.model.fulfillment.OutboundResult; import common.ecommerce.model.product.ProductListing; import common.ecommerce.model.product.ProductQuery; import common.ecommerce.model.product.ProductRef; @@ -52,19 +57,32 @@ import java.util.Set; * {@link LingxingSubPlatform}。

*/ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, SettlementCapable, - AggregatorCapable { + OutboundCapable, AggregatorCapable { private final HttpTransport transport; private final JacksonJsonCodec json; private final TokenManager tokenManager; private final LingxingRateLimiter rateLimiter; private final LingxingRequestFactory requestFactory; + private final LingxingSubPlatforms subPlatforms; /** * 适配器只持有协议与基础设施,不持有端点与凭据——两者随每次调用传入。 + * + *

子平台装配默认用 {@code LingxingSubPlatformDefaults.all(...)}; + * 需要替换或只装一部分时用另一个构造器。

*/ public LingxingAdapter(HttpTransport transport, JacksonJsonCodec json, TokenStore tokenStore, LockProvider lockProvider, Clock clock) { + this(transport, json, tokenStore, lockProvider, clock, null); + } + + /** + * @param subPlatforms 子平台注册表;传 {@code null} 表示装配全部已实现子平台 + */ + public LingxingAdapter(HttpTransport transport, JacksonJsonCodec json, + TokenStore tokenStore, LockProvider lockProvider, Clock clock, + LingxingSubPlatforms subPlatforms) { this.transport = transport; this.json = json; final LingxingTokenClient tokenClient = new LingxingTokenClient(transport, json, clock); @@ -90,6 +108,8 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, }; this.rateLimiter = new LingxingRateLimiter(); this.requestFactory = new LingxingRequestFactory(json, clock); + this.subPlatforms = subPlatforms != null ? subPlatforms + : LingxingSubPlatformDefaults.all(transport, json); } @Override @@ -112,7 +132,7 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, @Override public EnumSet capabilities() { - return EnumSet.of(Capability.SALES_ORDER, Capability.PRODUCT, Capability.BILLING); + return EnumSet.of(Capability.SALES_ORDER, Capability.PRODUCT, Capability.BILLING, Capability.MARK_SHIPPED); } /** @@ -127,10 +147,10 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, return false; } Platform p = LingxingConstants.toPlatform(account.platformCode()); - if (p == null || !LingxingSubPlatforms.isRegistered(p)) { + if (p == null || !subPlatforms.isRegistered(p)) { return false; } - LingxingSubPlatform impl = LingxingSubPlatforms.of(p); + LingxingSubPlatform impl = subPlatforms.of(p); if (capability == Capability.SALES_ORDER) { return impl.orderSource() != null; } @@ -140,6 +160,9 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, if (capability == Capability.BILLING) { return impl.settlementSource() != null; } + if (capability == Capability.MARK_SHIPPED) { + return impl.outboundSource() != null; + } return false; } @@ -148,7 +171,7 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, */ @Override public List listSubChannels(ChannelContext ctx) { - LingxingSubPlatform impl = LingxingSubPlatforms.of(ctx.platform()); + LingxingSubPlatform impl = subPlatforms.of(ctx.platform()); if (impl.shopSource() == null) { throw ChannelException.builder() .message("lingxing shop list is not supported for " + ctx.platform()) @@ -165,7 +188,7 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, @Override public Page pull(ChannelContext ctx, SalesOrderQuery query) { requireCapability(); - LingxingSubPlatform impl = LingxingSubPlatforms.of(ctx.platform()); + LingxingSubPlatform impl = subPlatforms.of(ctx.platform()); if (impl.orderSource() == null) { throw new ChannelException.UnsupportedCapabilityException(Capability.SALES_ORDER); } @@ -174,7 +197,7 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, @Override public SalesOrder get(ChannelContext ctx, SalesOrderRef ref) { - LingxingSubPlatform impl = LingxingSubPlatforms.of(ctx.platform()); + LingxingSubPlatform impl = subPlatforms.of(ctx.platform()); if (impl.orderSource() == null) { throw new ChannelException.UnsupportedCapabilityException(Capability.SALES_ORDER); } @@ -189,7 +212,7 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, * 跨渠道同步仍用标准订单路由。

*/ public Page pullFbmOrders(ChannelContext ctx, SalesOrderQuery query) { - LingxingSubPlatform impl = LingxingSubPlatforms.of(ctx.platform()); + LingxingSubPlatform impl = subPlatforms.of(ctx.platform()); if (impl.fbmOrderSource() == null) { throw new ChannelException.UnsupportedCapabilityException(Capability.SALES_ORDER); } @@ -202,7 +225,7 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, * @param orderNumber 领星系统单号,不是平台订单号 */ public SalesOrder getFbmOrder(ChannelContext ctx, String orderNumber) { - LingxingSubPlatform impl = LingxingSubPlatforms.of(ctx.platform()); + LingxingSubPlatform impl = subPlatforms.of(ctx.platform()); if (impl.fbmOrderSource() == null) { throw new ChannelException.UnsupportedCapabilityException(Capability.SALES_ORDER); } @@ -211,7 +234,7 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, @Override public Page pull(ChannelContext ctx, SettlementQuery query) { - LingxingSubPlatform impl = LingxingSubPlatforms.of(ctx.platform()); + LingxingSubPlatform impl = subPlatforms.of(ctx.platform()); if (impl.settlementSource() == null) { throw new ChannelException.UnsupportedCapabilityException(Capability.BILLING); } @@ -219,6 +242,24 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, } @Override + public boolean submit(ChannelContext ctx, List packages) { + return outboundSource(ctx).submit(call(ctx), packages); + } + + @Override + public OutboundResult results(ChannelContext ctx, List globalOrderNos) { + return outboundSource(ctx).results(call(ctx), globalOrderNos); + } + + private LingxingOutboundSource outboundSource(ChannelContext ctx) { + LingxingOutboundSource source = subPlatforms.of(ctx.platform()).outboundSource(); + if (source == null) { + throw new ChannelException.UnsupportedCapabilityException(Capability.MARK_SHIPPED); + } + return source; + } + + @Override public ProductSyncStrategy syncStrategy() { // 亚马逊 Listing 支持按「All Listing 报表更新时间」增量;其它子平台接入时再各自声明 return ProductSyncStrategy.INCREMENTAL_TIME_WINDOW; @@ -226,7 +267,7 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, @Override public Page pull(ChannelContext ctx, ProductQuery query) { - LingxingSubPlatform impl = LingxingSubPlatforms.of(ctx.platform()); + LingxingSubPlatform impl = subPlatforms.of(ctx.platform()); if (impl.productSource() == null) { throw new ChannelException.UnsupportedCapabilityException(Capability.PRODUCT); } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatform.java index 43035a2..8929945 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatform.java @@ -1,6 +1,7 @@ package common.ecommerce.lingxing.platform; import common.ecommerce.lingxing.platform.order.LingxingOrderSource; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.lingxing.platform.product.LingxingProductSource; import common.ecommerce.lingxing.platform.settlement.LingxingSettlementSource; import common.ecommerce.lingxing.platform.shop.LingxingShopSource; @@ -47,4 +48,9 @@ public interface LingxingSubPlatform { default LingxingSettlementSource settlementSource() { return null; } + + /** 快速出库能力;不支持时为 null。多平台通用接口。 */ + default LingxingOutboundSource outboundSource() { + return null; + } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatformDefaults.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatformDefaults.java new file mode 100644 index 0000000..d6b8c64 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatformDefaults.java @@ -0,0 +1,40 @@ +package common.ecommerce.lingxing.platform; + +import common.ecommerce.http.JacksonJsonCodec; +import common.ecommerce.http.HttpTransport; +import common.ecommerce.lingxing.platform.aliexpress.AliexpressSubPlatform; +import common.ecommerce.lingxing.platform.amazon.sc.AmazonScSubPlatform; +import common.ecommerce.lingxing.platform.amazon.vc.AmazonVcSubPlatform; +import common.ecommerce.lingxing.platform.ebay.EbaySubPlatform; +import common.ecommerce.lingxing.platform.shein.SheinSubPlatform; +import common.ecommerce.lingxing.platform.shopify.ShopifySubPlatform; +import common.ecommerce.lingxing.platform.temu.TemuSubPlatform; +import common.ecommerce.lingxing.platform.tiktok.TiktokSubPlatform; +import common.ecommerce.lingxing.platform.walmart.WalmartSubPlatform; + +/** + * 领星子平台的默认装配:把当前已实现的子平台全部注册进来。 + * + *

要把某个子平台换掉或只装一部分(测试、灰度),直接用 + * {@link LingxingSubPlatforms#builder()} 自行组装,不要改这里。

+ */ +public final class LingxingSubPlatformDefaults { + + private LingxingSubPlatformDefaults() { + } + + /** 用给定的传输与编解码装配全部已实现子平台。 */ + public static LingxingSubPlatforms all(HttpTransport transport, JacksonJsonCodec json) { + return LingxingSubPlatforms.builder() + .register(new AmazonScSubPlatform(transport, json)) + .register(new AmazonVcSubPlatform(transport, json)) + .register(new WalmartSubPlatform(transport, json)) + .register(new TemuSubPlatform(transport, json)) + .register(new SheinSubPlatform(transport, json)) + .register(new ShopifySubPlatform(transport, json)) + .register(new EbaySubPlatform(transport, json)) + .register(new AliexpressSubPlatform(transport, json)) + .register(new TiktokSubPlatform(transport, json)) + .build(); + } +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java index 059cbd6..370b782 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java @@ -1,71 +1,40 @@ package common.ecommerce.lingxing.platform; import common.ecommerce.error.ChannelException; -import common.ecommerce.http.JacksonJsonCodec; -import common.ecommerce.http.OkHttpTransport; -import common.ecommerce.lingxing.platform.amazon.sc.AmazonScSubPlatform; -import common.ecommerce.lingxing.platform.amazon.vc.AmazonVcSubPlatform; import common.ecommerce.model.Platform; -import common.ecommerce.lingxing.platform.walmart.WalmartSubPlatform; import java.util.Collections; import java.util.EnumMap; import java.util.Map; +import java.util.Objects; import java.util.Set; /** * 子平台注册表:业务平台 → 领星侧实现。 * - *

新增一个子平台 = 写一个 {@link LingxingSubPlatform} 实现 + 在这里登记一行。 - * 未登记的平台在调用时显式失败,不静默返回空。

+ *

注册表按适配器实例组装(见 {@link #builder()}),不是全局静态: + * 多个 SDK 实例互不干扰,测试可以只装配部分子平台。 + * 路由的第一层仍由调用方传入的 {@code AccountKey} 决定(provider + platform), + * 这里只回答「该 platform 用哪个实现」。

+ * + *

未登记的平台在调用时显式失败,不静默返回空;同一平台重复注册在 + * {@link Builder#build()} 时直接抛异常,避免静默覆盖。

*/ public final class LingxingSubPlatforms { - private static final Map BY_PLATFORM; - - /** - * 子平台实现共享同一套传输与编解码;端点与凭据不在这里,随每次调用传入。 - */ - private static final OkHttpTransport SHARED_TRANSPORT = new OkHttpTransport("common-ecommerce/1.0.0 (lingxing)"); - private static final JacksonJsonCodec SHARED_JSON = new JacksonJsonCodec(); - - static { - Map map = new EnumMap(Platform.class); - register(map, new WalmartSubPlatform(SHARED_TRANSPORT, SHARED_JSON)); - register(map, new AmazonScSubPlatform(SHARED_TRANSPORT, SHARED_JSON)); - register(map, new AmazonVcSubPlatform(SHARED_TRANSPORT, SHARED_JSON)); - register(map, new common.ecommerce.lingxing.platform.amazon.sc.AmazonScSubPlatform( - SHARED_TRANSPORT, SHARED_JSON)); - register(map, new common.ecommerce.lingxing.platform.amazon.vc.AmazonVcSubPlatform( - SHARED_TRANSPORT, SHARED_JSON)); - register(map, new common.ecommerce.lingxing.platform.temu.TemuSubPlatform( - SHARED_TRANSPORT, SHARED_JSON)); - register(map, new common.ecommerce.lingxing.platform.shopify.ShopifySubPlatform( - SHARED_TRANSPORT, SHARED_JSON)); - register(map, new common.ecommerce.lingxing.platform.ebay.EbaySubPlatform( - SHARED_TRANSPORT, SHARED_JSON)); - register(map, new common.ecommerce.lingxing.platform.aliexpress.AliexpressSubPlatform( - SHARED_TRANSPORT, SHARED_JSON)); - register(map, new common.ecommerce.lingxing.platform.shein.SheinSubPlatform( - SHARED_TRANSPORT, SHARED_JSON)); - register(map, new common.ecommerce.lingxing.platform.tiktok.TiktokSubPlatform( - SHARED_TRANSPORT, SHARED_JSON)); - // 其它平台落地后继续 register(...) - BY_PLATFORM = Collections.unmodifiableMap(map); - } + private final Map byPlatform; - private static void register(Map map, LingxingSubPlatform impl) { - map.put(impl.platform(), impl); + private LingxingSubPlatforms(Map byPlatform) { + this.byPlatform = Collections.unmodifiableMap(byPlatform); } - private LingxingSubPlatforms() { + public static Builder builder() { + return new Builder(); } - /** - * 按业务平台取实现;未登记时抛带平台信息的异常,便于定位是哪个子平台还没接。 - */ - public static LingxingSubPlatform of(Platform platform) { - LingxingSubPlatform impl = BY_PLATFORM.get(platform); + /** 按业务平台取实现;未登记时抛带平台信息的异常。 */ + public LingxingSubPlatform of(Platform platform) { + LingxingSubPlatform impl = byPlatform.get(platform); if (impl == null) { throw ChannelException.builder() .message("lingxing sub platform not implemented: " + platform) @@ -78,12 +47,42 @@ public final class LingxingSubPlatforms { return impl; } - public static boolean isRegistered(Platform platform) { - return BY_PLATFORM.containsKey(platform); + public boolean isRegistered(Platform platform) { + return byPlatform.containsKey(platform); } /** 已登记的平台,便于能力报告与日志。 */ - public static Set registered() { - return BY_PLATFORM.keySet(); + public Set registered() { + return byPlatform.keySet(); + } + + public static final class Builder { + + private final Map map = + new EnumMap(Platform.class); + + public Builder register(LingxingSubPlatform impl) { + Objects.requireNonNull(impl, "subPlatform"); + Platform platform = Objects.requireNonNull(impl.platform(), "platform"); + LingxingSubPlatform previous = map.put(platform, impl); + if (previous != null && previous != impl) { + throw new IllegalStateException("duplicate lingxing sub platform for " + platform + + ": " + previous.getClass().getName() + " / " + impl.getClass().getName()); + } + return this; + } + + public Builder registerAll(Iterable impls) { + if (impls != null) { + for (LingxingSubPlatform impl : impls) { + register(impl); + } + } + return this; + } + + public LingxingSubPlatforms build() { + return new LingxingSubPlatforms(map); + } } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/aliexpress/AliexpressSubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/aliexpress/AliexpressSubPlatform.java index 8cba475..75def5c 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/aliexpress/AliexpressSubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/aliexpress/AliexpressSubPlatform.java @@ -5,6 +5,8 @@ import common.ecommerce.http.JacksonJsonCodec; import common.ecommerce.lingxing.platform.LingxingSubPlatform; import common.ecommerce.lingxing.platform.aliexpress.product.AliexpressProductSource; import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.multiplatform.outbound.MultiPlatformOutboundSource; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.model.Platform; /** @@ -12,6 +14,9 @@ import common.ecommerce.model.Platform; */ public final class AliexpressSubPlatform implements LingxingSubPlatform { + /** 快速出库是多平台通用接口,各子平台共用。 */ + private static final MultiPlatformOutboundSource OUTBOUND = new MultiPlatformOutboundSource(); + private final LingxingProductSource products; public AliexpressSubPlatform(HttpTransport transport, JacksonJsonCodec json) { @@ -27,4 +32,9 @@ public final class AliexpressSubPlatform implements LingxingSubPlatform { public LingxingProductSource productSource() { return products; } + + @Override + public LingxingOutboundSource outboundSource() { + return OUTBOUND; + } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/AmazonScSubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/AmazonScSubPlatform.java index 36aed4f..308d4ed 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/AmazonScSubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/AmazonScSubPlatform.java @@ -8,6 +8,8 @@ import common.ecommerce.lingxing.platform.amazon.order.AmazonOrderSource; import common.ecommerce.lingxing.platform.amazon.settlement.AmazonSettlementSource; import common.ecommerce.lingxing.platform.order.LingxingOrderSource; import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.multiplatform.outbound.MultiPlatformOutboundSource; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.lingxing.platform.settlement.LingxingSettlementSource; import common.ecommerce.lingxing.platform.shop.LingxingShopSource; import common.ecommerce.lingxing.platform.amazon.sc.product.AmazonScProductSource; @@ -19,6 +21,9 @@ import common.ecommerce.model.Platform; */ public final class AmazonScSubPlatform implements LingxingSubPlatform { + /** 快速出库是多平台通用接口,各子平台共用。 */ + private static final MultiPlatformOutboundSource OUTBOUND = new MultiPlatformOutboundSource(); + private final LingxingOrderSource orders; private final LingxingOrderSource fbmOrders; private final LingxingProductSource products; @@ -62,4 +67,9 @@ public final class AmazonScSubPlatform implements LingxingSubPlatform { public LingxingSettlementSource settlementSource() { return settlements; } + + @Override + public LingxingOutboundSource outboundSource() { + return OUTBOUND; + } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/vc/AmazonVcSubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/vc/AmazonVcSubPlatform.java index 43af041..dc09643 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/vc/AmazonVcSubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/vc/AmazonVcSubPlatform.java @@ -6,6 +6,8 @@ import common.ecommerce.lingxing.platform.LingxingSubPlatform; import common.ecommerce.lingxing.platform.amazon.vc.order.VcOrderSource; import common.ecommerce.lingxing.platform.order.LingxingOrderSource; import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.multiplatform.outbound.MultiPlatformOutboundSource; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.lingxing.platform.shop.LingxingShopSource; import common.ecommerce.lingxing.platform.amazon.vc.product.AmazonVcProductSource; import common.ecommerce.lingxing.platform.amazon.vc.shop.VcShopSource; @@ -16,6 +18,9 @@ import common.ecommerce.model.Platform; */ public final class AmazonVcSubPlatform implements LingxingSubPlatform { + /** 快速出库是多平台通用接口,各子平台共用。 */ + private static final MultiPlatformOutboundSource OUTBOUND = new MultiPlatformOutboundSource(); + private final LingxingOrderSource orders; private final LingxingProductSource products; private final LingxingShopSource shops; @@ -45,4 +50,9 @@ public final class AmazonVcSubPlatform implements LingxingSubPlatform { public LingxingShopSource shopSource() { return shops; } + + @Override + public LingxingOutboundSource outboundSource() { + return OUTBOUND; + } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/ebay/EbaySubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/ebay/EbaySubPlatform.java index 9a46c47..d7a2826 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/ebay/EbaySubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/ebay/EbaySubPlatform.java @@ -5,6 +5,8 @@ import common.ecommerce.http.JacksonJsonCodec; import common.ecommerce.lingxing.platform.LingxingSubPlatform; import common.ecommerce.lingxing.platform.ebay.product.EbayProductSource; import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.multiplatform.outbound.MultiPlatformOutboundSource; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.model.Platform; /** @@ -12,6 +14,9 @@ import common.ecommerce.model.Platform; */ public final class EbaySubPlatform implements LingxingSubPlatform { + /** 快速出库是多平台通用接口,各子平台共用。 */ + private static final MultiPlatformOutboundSource OUTBOUND = new MultiPlatformOutboundSource(); + private final LingxingProductSource products; public EbaySubPlatform(HttpTransport transport, JacksonJsonCodec json) { @@ -27,4 +32,9 @@ public final class EbaySubPlatform implements LingxingSubPlatform { public LingxingProductSource productSource() { return products; } + + @Override + public LingxingOutboundSource outboundSource() { + return OUTBOUND; + } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/multiplatform/outbound/MultiPlatformOutboundSource.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/multiplatform/outbound/MultiPlatformOutboundSource.java new file mode 100644 index 0000000..21f40b4 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/multiplatform/outbound/MultiPlatformOutboundSource.java @@ -0,0 +1,112 @@ +package common.ecommerce.lingxing.platform.multiplatform.outbound; + +import common.ecommerce.http.HttpRequest; +import common.ecommerce.http.HttpResponse; +import common.ecommerce.lingxing.LingxingApi; +import common.ecommerce.lingxing.http.LingxingEnvelope; +import common.ecommerce.lingxing.platform.LingxingCall; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; +import common.ecommerce.model.fulfillment.OutboundPackage; +import common.ecommerce.model.fulfillment.OutboundResult; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * 领星 · 快速出库(多平台通用)。 + * + *

两个接口都不带平台维度,靠领星系统单号 {@code global_order_no} 定位订单, + * 因此所有子平台共用同一实现。

+ * + *
    + *
  • {@code POST /pb/mp/order/v2/fastOutbound}:创建异步出库任务,返回 {@code data: true}
  • + *
  • {@code POST /pb/mp/order/v2/getFastOutboundResult}:按单号数组查结果, + * 返回 {@code data.success[]} 与 {@code data.failure[]}
  • + *
+ * + *

两个接口的令牌桶都是 10 req/s。

+ */ +public final class MultiPlatformOutboundSource implements LingxingOutboundSource { + + /** 单次最多 1000 单(两个接口一致)。 */ + private static final int MAX_BATCH = 1000; + + private static final String OP_SUBMIT = "outbound.submit"; + private static final String OP_RESULTS = "outbound.results"; + + @Override + public boolean submit(LingxingCall call, List packages) { + if (packages == null || packages.isEmpty()) { + throw new IllegalArgumentException("packages must not be empty"); + } + if (packages.size() > MAX_BATCH) { + throw new IllegalArgumentException("at most " + MAX_BATCH + " packages per call, got " + packages.size()); + } + + List> body = new ArrayList>(packages.size()); + for (OutboundPackage p : packages) { + body.add(toBody(p)); + } + Map payload = new LinkedHashMap(); + payload.put("package", body); + + HttpRequest req = call.signed(LingxingApi.FAST_OUTBOUND, payload, OP_SUBMIT); + HttpResponse resp = call.execute(req); + LingxingEnvelope envelope = call.envelope(resp, OP_SUBMIT); + // 该接口 data 是 boolean,true 表示任务已受理(不代表出库成功) + return "true".equalsIgnoreCase(call.json().stringAt(envelope.body(), "data")); + } + + @Override + public OutboundResult results(LingxingCall call, List globalOrderNos) { + if (globalOrderNos == null || globalOrderNos.isEmpty()) { + throw new IllegalArgumentException("globalOrderNos must not be empty"); + } + if (globalOrderNos.size() > MAX_BATCH) { + throw new IllegalArgumentException("at most " + MAX_BATCH + " orders per call, got " + globalOrderNos.size()); + } + + Map payload = new LinkedHashMap(); + payload.put("global_order_no", new ArrayList(globalOrderNos)); + + HttpRequest req = call.signed(LingxingApi.FAST_OUTBOUND_RESULT, payload, OP_RESULTS); + HttpResponse resp = call.execute(req); + LingxingEnvelope envelope = call.envelope(resp, OP_RESULTS); + + List ok = call.json().listAt(envelope.body(), "data.success", OutboundOkDto.class); + List fail = call.json().listAt(envelope.body(), "data.failure", OutboundFailDto.class); + + List success = new ArrayList(ok.size()); + for (OutboundOkDto dto : ok) { + success.add(new OutboundResult.Success(dto.getGlobalOrderNo(), dto.getWoNumber())); + } + List failure = new ArrayList(fail.size()); + for (OutboundFailDto dto : fail) { + failure.add(new OutboundResult.Failure(dto.getGlobalOrderNo(), dto.getErrorMessage())); + } + return OutboundResult.of(success, failure); + } + + /** 统一模型 → 领星出库请求体;值为 null 的字段不发送。 */ + private static Map toBody(OutboundPackage p) { + Map item = new LinkedHashMap(); + item.put("global_order_no", p.globalOrderNo()); + item.put("wid", p.warehouseId()); + item.put("logistics_type_id", p.logisticsTypeId()); + item.put("waybill_no", p.waybillNo()); + item.put("tracking_no", p.trackingNo()); + item.put("weight_unit", p.weightUnit()); + item.put("real_weight", p.realWeight()); + item.put("size_unit", p.sizeUnit()); + item.put("length", p.length()); + item.put("width", p.width()); + item.put("height", p.height()); + item.put("fee_weight", p.feeWeight()); + item.put("logistics_freight", p.logisticsFreight()); + item.put("logistics_freight_currency_code", p.logisticsFreightCurrency()); + item.values().removeAll(java.util.Collections.singleton(null)); + return item; + } +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/multiplatform/outbound/OutboundFailDto.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/multiplatform/outbound/OutboundFailDto.java new file mode 100644 index 0000000..3ecfbb4 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/multiplatform/outbound/OutboundFailDto.java @@ -0,0 +1,21 @@ +package common.ecommerce.lingxing.platform.multiplatform.outbound; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +/** 快速出库结果 · 失败项({@code data.failure[]})。 */ +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class OutboundFailDto { + + /** 系统单号。 */ + @JsonProperty("global_order_no") + private String globalOrderNo; + + /** 失败原因,如「正在处理」「库存不足」「订单没提交快速出库」。 */ + @JsonProperty("error_message") + private String errorMessage; +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/multiplatform/outbound/OutboundOkDto.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/multiplatform/outbound/OutboundOkDto.java new file mode 100644 index 0000000..03d8233 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/multiplatform/outbound/OutboundOkDto.java @@ -0,0 +1,21 @@ +package common.ecommerce.lingxing.platform.multiplatform.outbound; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +/** 快速出库结果 · 成功项({@code data.success[]})。 */ +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class OutboundOkDto { + + /** 系统单号。 */ + @JsonProperty("global_order_no") + private String globalOrderNo; + + /** 销售出库单号。 */ + @JsonProperty("wo_number") + private String woNumber; +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/outbound/LingxingOutboundSource.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/outbound/LingxingOutboundSource.java new file mode 100644 index 0000000..1effacc --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/outbound/LingxingOutboundSource.java @@ -0,0 +1,21 @@ +package common.ecommerce.lingxing.platform.outbound; + +import common.ecommerce.lingxing.platform.LingxingCall; +import common.ecommerce.model.fulfillment.OutboundPackage; +import common.ecommerce.model.fulfillment.OutboundResult; + +import java.util.List; + +/** + * 子平台的出库能力。 + * + *

出库是异步的:{@code submit} 只表示任务受理,结果用 {@code results} 回查。

+ */ +public interface LingxingOutboundSource { + + /** 提交出库任务;返回是否受理。 */ + boolean submit(LingxingCall call, List packages); + + /** 按系统单号查询出库结果。 */ + OutboundResult results(LingxingCall call, List globalOrderNos); +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/shein/SheinSubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shein/SheinSubPlatform.java index 82856d0..2b15742 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/shein/SheinSubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shein/SheinSubPlatform.java @@ -4,6 +4,8 @@ import common.ecommerce.http.HttpTransport; import common.ecommerce.http.JacksonJsonCodec; import common.ecommerce.lingxing.platform.LingxingSubPlatform; import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.multiplatform.outbound.MultiPlatformOutboundSource; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.lingxing.platform.shein.product.SheinProductSource; import common.ecommerce.model.Platform; @@ -14,6 +16,9 @@ import common.ecommerce.model.Platform; */ public final class SheinSubPlatform implements LingxingSubPlatform { + /** 快速出库是多平台通用接口,各子平台共用。 */ + private static final MultiPlatformOutboundSource OUTBOUND = new MultiPlatformOutboundSource(); + private final LingxingProductSource products; public SheinSubPlatform(HttpTransport transport, JacksonJsonCodec json) { @@ -29,4 +34,9 @@ public final class SheinSubPlatform implements LingxingSubPlatform { public LingxingProductSource productSource() { return products; } + + @Override + public LingxingOutboundSource outboundSource() { + return OUTBOUND; + } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/ShopifySubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/ShopifySubPlatform.java index 1da60df..83bc296 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/ShopifySubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/ShopifySubPlatform.java @@ -4,6 +4,8 @@ import common.ecommerce.http.HttpTransport; import common.ecommerce.http.JacksonJsonCodec; import common.ecommerce.lingxing.platform.LingxingSubPlatform; import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.multiplatform.outbound.MultiPlatformOutboundSource; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.lingxing.platform.shopify.product.ShopifyProductSource; import common.ecommerce.model.Platform; @@ -12,6 +14,9 @@ import common.ecommerce.model.Platform; */ public final class ShopifySubPlatform implements LingxingSubPlatform { + /** 快速出库是多平台通用接口,各子平台共用。 */ + private static final MultiPlatformOutboundSource OUTBOUND = new MultiPlatformOutboundSource(); + private final LingxingProductSource products; public ShopifySubPlatform(HttpTransport transport, JacksonJsonCodec json) { @@ -27,4 +32,9 @@ public final class ShopifySubPlatform implements LingxingSubPlatform { public LingxingProductSource productSource() { return products; } + + @Override + public LingxingOutboundSource outboundSource() { + return OUTBOUND; + } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/TemuSubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/TemuSubPlatform.java index 32de4fe..751dbf5 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/TemuSubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/TemuSubPlatform.java @@ -4,6 +4,8 @@ import common.ecommerce.http.HttpTransport; import common.ecommerce.http.JacksonJsonCodec; import common.ecommerce.lingxing.platform.LingxingSubPlatform; import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.multiplatform.outbound.MultiPlatformOutboundSource; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.lingxing.platform.temu.product.TemuProductSource; import common.ecommerce.model.Platform; @@ -14,6 +16,9 @@ import common.ecommerce.model.Platform; */ public final class TemuSubPlatform implements LingxingSubPlatform { + /** 快速出库是多平台通用接口,各子平台共用。 */ + private static final MultiPlatformOutboundSource OUTBOUND = new MultiPlatformOutboundSource(); + private final LingxingProductSource products; public TemuSubPlatform(HttpTransport transport, JacksonJsonCodec json) { @@ -29,4 +34,9 @@ public final class TemuSubPlatform implements LingxingSubPlatform { public LingxingProductSource productSource() { return products; } + + @Override + public LingxingOutboundSource outboundSource() { + return OUTBOUND; + } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/tiktok/TiktokSubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/tiktok/TiktokSubPlatform.java index dd5d769..c9737f6 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/tiktok/TiktokSubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/tiktok/TiktokSubPlatform.java @@ -4,6 +4,8 @@ import common.ecommerce.http.HttpTransport; import common.ecommerce.http.JacksonJsonCodec; import common.ecommerce.lingxing.platform.LingxingSubPlatform; import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.multiplatform.outbound.MultiPlatformOutboundSource; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.lingxing.platform.tiktok.product.TiktokProductSource; import common.ecommerce.model.Platform; @@ -12,6 +14,9 @@ import common.ecommerce.model.Platform; */ public final class TiktokSubPlatform implements LingxingSubPlatform { + /** 快速出库是多平台通用接口,各子平台共用。 */ + private static final MultiPlatformOutboundSource OUTBOUND = new MultiPlatformOutboundSource(); + private final LingxingProductSource products; public TiktokSubPlatform(HttpTransport transport, JacksonJsonCodec json) { @@ -27,4 +32,9 @@ public final class TiktokSubPlatform implements LingxingSubPlatform { public LingxingProductSource productSource() { return products; } + + @Override + public LingxingOutboundSource outboundSource() { + return OUTBOUND; + } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/WalmartSubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/WalmartSubPlatform.java index 6564aea..baa7c78 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/WalmartSubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/WalmartSubPlatform.java @@ -6,7 +6,8 @@ import common.ecommerce.lingxing.platform.LingxingSubPlatform; import common.ecommerce.lingxing.platform.multiplatform.shop.MultiPlatformShopSource; import common.ecommerce.lingxing.platform.order.LingxingOrderSource; import common.ecommerce.lingxing.platform.product.LingxingProductSource; -import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.multiplatform.outbound.MultiPlatformOutboundSource; +import common.ecommerce.lingxing.platform.outbound.LingxingOutboundSource; import common.ecommerce.lingxing.platform.shop.LingxingShopSource; import common.ecommerce.lingxing.platform.walmart.order.WalmartOrderSource; import common.ecommerce.lingxing.platform.walmart.product.WalmartProductSource; @@ -20,6 +21,9 @@ import common.ecommerce.model.Platform; */ public final class WalmartSubPlatform implements LingxingSubPlatform { + /** 快速出库是多平台通用接口,各子平台共用。 */ + private static final MultiPlatformOutboundSource OUTBOUND = new MultiPlatformOutboundSource(); + private final LingxingOrderSource orders; private final LingxingProductSource products; private final LingxingShopSource shops; @@ -49,4 +53,9 @@ public final class WalmartSubPlatform implements LingxingSubPlatform { public LingxingShopSource shopSource() { return shops; } + + @Override + public LingxingOutboundSource outboundSource() { + return OUTBOUND; + } } diff --git a/lingxing/src/test/java/common/ecommerce/lingxing/platform/amazon/sc/AmazonScProductLiveTest.java b/lingxing/src/test/java/common/ecommerce/lingxing/platform/amazon/sc/AmazonScProductLiveTest.java new file mode 100644 index 0000000..2e5d9df --- /dev/null +++ b/lingxing/src/test/java/common/ecommerce/lingxing/platform/amazon/sc/AmazonScProductLiveTest.java @@ -0,0 +1,229 @@ +package common.ecommerce.lingxing.platform.amazon.sc; + +import common.ecommerce.capability.AggregatorCapable.SubChannel; +import common.ecommerce.http.HttpRequest; +import common.ecommerce.http.HttpResponse; +import common.ecommerce.http.HttpTransport; +import common.ecommerce.http.JacksonJsonCodec; +import common.ecommerce.http.OkHttpTransport; +import common.ecommerce.lingxing.LingxingConstants; +import common.ecommerce.lingxing.adapter.LingxingAdapter; +import common.ecommerce.model.ChannelProvider; +import common.ecommerce.model.Platform; +import common.ecommerce.model.account.AccountKey; +import common.ecommerce.model.account.ChannelAccount; +import common.ecommerce.model.product.ProductListing; +import common.ecommerce.model.product.ProductQuery; +import common.ecommerce.model.support.Page; +import common.ecommerce.sdk.ChannelConnection; +import common.ecommerce.sdk.ChannelContext; +import common.ecommerce.spi.ChannelSecret; +import common.ecommerce.testkit.InMemoryLockProvider; +import common.ecommerce.testkit.InMemoryTokenStore; +import org.junit.jupiter.api.Test; + +import java.time.Clock; +import java.time.Duration; +import java.time.Instant; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * 领星 · 亚马逊 SC 真实环境测试:先查店铺列表拿 sid,再用 sid 拉 Listing。 + * + *

跑之前填 APP_KEY / APP_SECRET 即可;SID 留空时会自动从店铺列表取第一个。

+ *
+ * mvn -pl lingxing -am test -Dtest=AmazonScProductLiveTest
+ * 
+ * + *

调用前确认出口 IP 已加入领星白名单。本类只用于联调,不要把真实凭证提交到仓库。

+ * + *

两个接口的令牌桶容量都是 1,且按「账户 + path」共享,必须串行调用。

+ */ +class AmazonScProductLiveTest { + + // ==================== 填写这里 ==================== + /** 领星 ERP 开放接口的 AppID。 */ + private static final String APP_KEY = "ak_MWoHJz2tcIwKF"; + /** 领星 ERP 开放接口的 AppSecret。 */ + private static final String APP_SECRET = "YOUkd5efWJSK71jLvDyz+g=="; + /** + * 亚马逊店铺的领星 sid。 + * 留空则自动调用店铺列表接口,取第一个正常状态的店铺。 + */ + private static final String SID = "13839"; + // ================================================ + + private static final String BASE_URL = LingxingConstants.PRODUCTION_BASE_URL; + + private final JacksonJsonCodec json = new JacksonJsonCodec(); + private final LoggingTransport transport = + new LoggingTransport(new OkHttpTransport("common-ecommerce-smoke")); + + /** + * 店铺列表:一次性返回企业已授权到领星的全部亚马逊 SC 店铺。 + * + *

接口 {@code GET /erp/sc/data/seller/lists},非分页。打印每个店铺的 sid 与名称, + * 同时打印请求与响应原始报文(见 LoggingTransport),便于核对字段名。

+ */ + @Test + void listsAmazonShops() { + requireConfigured(); + + List channels = adapter().listSubChannels(shopListContext()); + System.out.println("[shops] subChannels size=" + channels.size()); + for (SubChannel c : channels) { + System.out.println(" platformCode=" + c.platformCode() + + " sid=" + c.externalStoreId() + + " platform=" + c.platform() + + " name=" + c.name()); + } + assertNotNull(channels); + } + + /** 拿第一个正常店铺的 sid,调 Listing 接口。 */ + @Test + void pullsOnePageOfListings() { + requireConfigured(); + + String sid = resolveSid(); + System.out.println("[listings] 使用 sid=" + sid); + + ChannelContext ctx = ChannelContext.of(amazonAccount(sid), connection(), + "smoke-" + System.currentTimeMillis(), Clock.systemUTC()); + + Instant to = Instant.now(); + Instant from = to.minus(Duration.ofDays(7)); + + Page page = adapter().pull(ctx, ProductQuery.builder() + .updatedAfter(from) + .updatedBefore(to) + .pageSize(20) + .build()); + + System.out.println("[listings] window=" + from + " .. " + to + + " items=" + page.size() + " nextCursor=" + page.nextCursor()); + for (ProductListing p : page.items()) { + System.out.println(" sku=" + p.platformSku() + + " title=" + p.title() + + " status=" + p.status() + + " images=" + p.imageUrls().size() + + " updatedAt=" + (p.updatedAt() == null ? null : p.updatedAt().utc())); + } + assertFalse(page.size() > 20, "pageSize 未被平台遵守"); + } + + /** 不带时间窗,确认默认分页与解析也能走通。 */ + @Test + void pullsFirstPageWithoutTimeWindow() { + requireConfigured(); + + String sid = resolveSid(); + ChannelContext ctx = ChannelContext.of(amazonAccount(sid), connection(), + "smoke-" + System.currentTimeMillis(), Clock.systemUTC()); + + Page page = adapter().pull(ctx, ProductQuery.builder().pageSize(10).build()); + + System.out.println("[listings/no-window] sid=" + sid + " items=" + page.size() + + " nextCursor=" + page.nextCursor()); + for (ProductListing p : page.items()) { + System.out.println(" sku=" + p.platformSku() + " status=" + p.status()); + } + // 店铺可能没有商品,空列表也算通过 + assertNotNull(page); + } + + // ---------------------------------------------------------------- helpers + + private static void requireConfigured() { + org.junit.jupiter.api.Assumptions.assumeTrue( + !APP_KEY.startsWith("在这里填") && !APP_SECRET.startsWith("在这里填"), + "请先填写 AmazonScProductLiveTest 顶部的 APP_KEY / APP_SECRET"); + } + + /** 优先用常量里的 SID;留空则从店铺列表取第一个。 */ + private String resolveSid() { + if (SID != null && !SID.trim().isEmpty() && !SID.startsWith("在这里填")) { + return SID.trim(); + } + List channels = adapter().listSubChannels(shopListContext()); + if (channels.isEmpty()) { + throw new AssertionError("店铺列表为空:该企业没有已授权且状态正常的亚马逊 SC 店铺"); + } + return channels.get(0).externalStoreId(); + } + + private LingxingAdapter adapter() { + return new LingxingAdapter(transport, json, + new InMemoryTokenStore(), new InMemoryLockProvider(), Clock.systemUTC()); + } + + private ChannelConnection connection() { + return ChannelConnection.of(BASE_URL, ChannelSecret.builder() + .appKey(APP_KEY).appSecret(APP_SECRET).build()); + } + + /** 店铺列表所用的上下文:店铺维度此时还不存在,用占位 omsStoreId。 */ + private ChannelContext shopListContext() { + return ChannelContext.of(placeholderAccount(), connection(), + "smoke-shops-" + System.currentTimeMillis(), Clock.systemUTC()); + } + + private ChannelAccount placeholderAccount() { + return ChannelAccount.builder() + .key(AccountKey.of(ChannelProvider.LINGXING, Platform.AMAZON_SC, "oms-amz-discovery")) + .externalStoreId("discovery") + .platformCode(10001) + .credentialRef("smoke") + .marketplaceZone("America/Los_Angeles") + .currency("USD") + .build(); + } + + private ChannelAccount amazonAccount(String sid) { + return ChannelAccount.builder() + .key(AccountKey.of(ChannelProvider.LINGXING, Platform.AMAZON_SC, "oms-amz-smoke")) + .externalStoreId(sid) + .platformCode(10001) + .credentialRef("smoke") + .marketplaceZone("America/Los_Angeles") + .currency("USD") + .build(); + } + + /** 打印真实请求与响应,签名/字段名不对时靠这个定位。Authorization 只打指纹。 */ + static final class LoggingTransport implements HttpTransport { + + private static final int MAX_BODY = 2000; + + private final HttpTransport delegate; + + LoggingTransport(HttpTransport delegate) { + this.delegate = delegate; + } + + @Override + public HttpResponse execute(HttpRequest request) { + System.out.println("--> " + request.method() + " " + request.url()); + System.out.println(" headers " + mask(request.headers().toString())); + System.out.println(" body " + truncate(request.body())); + HttpResponse response = delegate.execute(request); + System.out.println("<-- " + response.statusCode() + " (" + response.latencyMillis() + " ms) " + + truncate(response.body())); + return response; + } + + private static String mask(String headers) { + return headers.replaceAll("(?i)(bearer\\s+)[^,\\]]+", "$1***"); + } + + private static String truncate(String body) { + if (body == null) { + return "null"; + } + return body.length() <= MAX_BODY ? body : body.substring(0, MAX_BODY) + "...(" + body.length() + " chars)"; + } + } +}