From c1b283fbebd95b2cdca373331b6fbbba596f28bc Mon Sep 17 00:00:00 2001 From: chenchuchuan <13554600537@163.com> Date: Mon, 2 Nov 2026 17:42:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(lingxing):=20=E6=8B=86=E5=88=86=E8=83=BD?= =?UTF-8?q?=E5=8A=9B=20Source=EF=BC=8C=E6=8E=A5=E5=85=A5=E4=BA=9A=E9=A9=AC?= =?UTF-8?q?=E9=80=8A=20SC=20=E5=BA=97=E9=93=BA=E4=B8=8E=20Listing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 结构(避免单个平台类随能力增长膨胀): - LingxingSubPlatform 瘦成能力组合入口:orderSource / productSource / shopSource - 新增 platform/order|product|shop 三个能力接口 - 沃尔玛订单迁到 walmart/order/WalmartOrderSource - AmazonScSubPlatform 组合 AmazonShopSource + AmazonProductSource - LingxingAdapter 实现 ProductCapable,按子平台分派 接口(官方文档核对): - LingxingApi 新增 SHOP_LIST_AMAZON_SC (/erp/sc/data/seller/lists, GET, qps 1) - LingxingApi 新增 LISTING_AMAZON_SC (/erp/sc/data/mws/listing, POST, qps 1) - LingxingRequestFactory 增加 get(...):GET 无业务参数时 query 只带 app_key/timestamp/sign - LingxingCall 增加 get(...) 与 execute(...) 映射要点: - 店铺:sid 作为 externalStoreId,platform_code 10001 - Listing:seller_sku 作为 platformSku(参与 canonicalKey) - listing_update_date 是零时区时间,按 UTC 解析 订单接口未核对,亚马逊暂不声明订单能力。 Co-Authored-By: Claude --- .../common/ecommerce/lingxing/LingxingApi.java | 7 +- .../lingxing/adapter/LingxingAdapter.java | 62 +++++++++++- .../lingxing/http/LingxingRequestFactory.java | 45 +++++++++ .../ecommerce/lingxing/platform/LingxingCall.java | 17 +++- .../lingxing/platform/LingxingSubPlatform.java | 50 ++++------ .../lingxing/platform/LingxingSubPlatforms.java | 4 +- .../platform/amazon/AmazonProductSource.java | 109 +++++++++++++++++++++ .../platform/amazon/AmazonScSubPlatform.java | 44 +++++++++ .../lingxing/platform/amazon/AmazonShopSource.java | 52 ++++++++++ .../platform/amazon/dto/AmazonListingDto.java | 73 ++++++++++++++ .../platform/amazon/dto/AmazonShopDto.java | 61 ++++++++++++ .../platform/order/LingxingOrderSource.java | 24 +++++ .../platform/product/LingxingProductSource.java | 15 +++ .../lingxing/platform/shop/LingxingShopSource.java | 15 +++ .../platform/walmart/WalmartSubPlatform.java | 92 ++--------------- .../platform/walmart/order/WalmartOrderSource.java | 107 ++++++++++++++++++++ 16 files changed, 651 insertions(+), 126 deletions(-) create mode 100644 lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonProductSource.java create mode 100644 lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonScSubPlatform.java create mode 100644 lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonShopSource.java create mode 100644 lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/dto/AmazonListingDto.java create mode 100644 lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/dto/AmazonShopDto.java create mode 100644 lingxing/src/main/java/common/ecommerce/lingxing/platform/order/LingxingOrderSource.java create mode 100644 lingxing/src/main/java/common/ecommerce/lingxing/platform/product/LingxingProductSource.java create mode 100644 lingxing/src/main/java/common/ecommerce/lingxing/platform/shop/LingxingShopSource.java create mode 100644 lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/order/WalmartOrderSource.java diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java b/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java index abd5d9b..fe70aca 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java @@ -20,8 +20,11 @@ public enum LingxingApi { /** 续约令牌:用 refresh_token 延长有效期,每个 refresh_token 只能用一次。 */ TOKEN_REFRESH("/api/auth-server/oauth/refresh", 100), - // 店铺列表、商品列表的路径尚未核对,不预置占位值; - // 拿到官方文档后在此追加,一条路径只写一次。 + /** 亚马逊 SC 店铺列表(企业已授权的全部 SC 店铺)。GET,非分页。 */ + SHOP_LIST_AMAZON_SC("/erp/sc/data/seller/lists", 1), + + /** 亚马逊 SC Listing 列表。POST,offset/length 分页,上限 1000。 */ + LISTING_AMAZON_SC("/erp/sc/data/mws/listing", 1), /** 多平台订单列表;当前实现使用的跨平台接口。 */ 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 1b4faa3..8e18099 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapter.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapter.java @@ -5,9 +5,11 @@ import common.ecommerce.lingxing.http.LingxingRateLimiter; import common.ecommerce.lingxing.http.LingxingRequestFactory; import common.ecommerce.lingxing.auth.LingxingTokenClient; import common.ecommerce.lingxing.platform.LingxingCall; +import common.ecommerce.lingxing.platform.LingxingSubPlatform; import common.ecommerce.lingxing.platform.LingxingSubPlatforms; import common.ecommerce.capability.AggregatorCapable; +import common.ecommerce.capability.ProductCapable; import common.ecommerce.capability.SalesOrderCapable; import common.ecommerce.error.AuthException; import common.ecommerce.error.ChannelException; @@ -22,6 +24,10 @@ import common.ecommerce.model.Platform; import common.ecommerce.model.order.SalesOrder; import common.ecommerce.model.order.SalesOrderQuery; import common.ecommerce.model.order.SalesOrderRef; +import common.ecommerce.model.product.ProductListing; +import common.ecommerce.model.product.ProductQuery; +import common.ecommerce.model.product.ProductRef; +import common.ecommerce.model.product.ProductSyncStrategy; import common.ecommerce.sdk.ChannelContext; import common.ecommerce.spi.AccessToken; import common.ecommerce.spi.ChannelSecret; @@ -41,7 +47,7 @@ import java.util.Set; *

一个适配器服务多个子平台,子平台由 {@code ChannelAccount.platformCode} 决定, * 能力按子平台细化({@link #supports})。首期只实现订单列表拉取。

*/ -public final class LingxingAdapter implements SalesOrderCapable, AggregatorCapable { +public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, AggregatorCapable { private final HttpTransport transport; private final JacksonJsonCodec json; @@ -103,7 +109,7 @@ public final class LingxingAdapter implements SalesOrderCapable, AggregatorCapab @Override public EnumSet capabilities() { - return EnumSet.of(Capability.SALES_ORDER); + return EnumSet.of(Capability.SALES_ORDER, Capability.PRODUCT); } /** @@ -118,7 +124,17 @@ public final class LingxingAdapter implements SalesOrderCapable, AggregatorCapab return false; } Platform p = LingxingConstants.toPlatform(account.platformCode()); - return LingxingSubPlatforms.isRegistered(p) && LingxingSubPlatforms.of(p).supports(capability); + if (!LingxingSubPlatforms.isRegistered(p)) { + return false; + } + LingxingSubPlatform impl = LingxingSubPlatforms.of(p); + if (capability == Capability.SALES_ORDER) { + return impl.orderSource() != null; + } + if (capability == Capability.PRODUCT) { + return impl.productSource() != null; + } + return false; } /** @@ -142,8 +158,44 @@ public final class LingxingAdapter implements SalesOrderCapable, AggregatorCapab requireCapability(); ChannelSecret secret = ctx.secret(); AccessToken token = tokenManager.getOrRefresh(tokenKey(secret), secret); - LingxingCall call = new LingxingCall(ctx, token, requestFactory, rateLimiter, json); - return LingxingSubPlatforms.of(ctx.platform()).orders(call, query); + LingxingCall call = new LingxingCall(ctx, token, requestFactory, rateLimiter, json, transport); + LingxingSubPlatform impl = LingxingSubPlatforms.of(ctx.platform()); + if (impl.orderSource() == null) { + throw new ChannelException.UnsupportedCapabilityException(Capability.SALES_ORDER); + } + return impl.orderSource().orders(call, query); + } + + @Override + public ProductSyncStrategy syncStrategy() { + // 亚马逊 Listing 支持按「All Listing 报表更新时间」增量;其它子平台接入时再各自声明 + return ProductSyncStrategy.INCREMENTAL_TIME_WINDOW; + } + + @Override + public Page pull(ChannelContext ctx, ProductQuery query) { + ChannelSecret secret = ctx.secret(); + AccessToken token = tokenManager.getOrRefresh(tokenKey(secret), secret); + LingxingCall call = new LingxingCall(ctx, token, requestFactory, rateLimiter, json, transport); + LingxingSubPlatform impl = LingxingSubPlatforms.of(ctx.platform()); + if (impl.productSource() == null) { + throw new ChannelException.UnsupportedCapabilityException(Capability.PRODUCT); + } + return impl.productSource().products(call, query); + } + + /** 商品详情接口未接入,显式拒绝。 */ + @Override + public ProductListing get(ChannelContext ctx, ProductRef ref) { + throw ChannelException.builder() + .message("lingxing product detail is not implemented yet") + .operation("products.get") + .provider(provider().name()) + .omsStoreId(ctx.omsStoreId()) + .requestId(ctx.requestId()) + .platformCode(ref.platformSku()) + .retryable(false) + .build(); } /** 单订单详情接口待核对(领星以列表接口为主),先显式拒绝而不是返回 null。 */ diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/http/LingxingRequestFactory.java b/lingxing/src/main/java/common/ecommerce/lingxing/http/LingxingRequestFactory.java index d18622e..7a66e00 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/http/LingxingRequestFactory.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/http/LingxingRequestFactory.java @@ -64,6 +64,51 @@ public final class LingxingRequestFactory { .build(); } + /** + * GET 请求:参数放在 query 并参与签名。 + * + *

领星部分接口(如亚马逊店铺列表)是 GET 且无业务参数, + * 此时 query 里只有 {@code app_key} / {@code timestamp} / {@code sign}。

+ */ + public HttpRequest get(String baseUrl, String path, Map params, ChannelSecret secret, + AccessToken token, ChannelContext ctx, String operation) { + Map signParams = new LinkedHashMap(); + if (params != null) { + for (Map.Entry e : params.entrySet()) { + if (e.getValue() != null) { + signParams.put(e.getKey(), stringify(e.getValue())); + } + } + } + signParams.put("access_token", token.value()); + signParams.put("app_key", secret.appKey()); + String timestamp = String.valueOf(Instant.now(clock).getEpochSecond()); + signParams.put("timestamp", timestamp); + + String sign = LingxingSigner.sign(signParams, secret); + + StringBuilder url = new StringBuilder(baseUrl).append(path).append('?'); + if (params != null) { + for (Map.Entry e : params.entrySet()) { + if (e.getValue() != null) { + url.append(urlEncode(e.getKey())).append('=') + .append(urlEncode(stringify(e.getValue()))).append('&'); + } + } + } + url.append("app_key=").append(urlEncode(secret.appKey())) + .append("×tamp=").append(timestamp) + .append("&sign=").append(urlEncode(sign)); + + return HttpRequest.builder() + .method("GET") + .url(url.toString()) + .header("Authorization", token.tokenType() + " " + token.value()) + .header("X-Request-Id", ctx.requestId()) + .operation(operation) + .build(); + } + /** 集合按 JSON 参与签名;其余按字符串。 */ private String stringify(Object value) { if (value instanceof Collection || value instanceof Map) { diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingCall.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingCall.java index 2d5b4d8..ab2489d 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingCall.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingCall.java @@ -26,14 +26,17 @@ public final class LingxingCall { private final LingxingRequestFactory requestFactory; private final LingxingRateLimiter rateLimiter; private final JacksonJsonCodec json; + private final common.ecommerce.http.HttpTransport transport; public LingxingCall(ChannelContext ctx, AccessToken token, LingxingRequestFactory requestFactory, - LingxingRateLimiter rateLimiter, JacksonJsonCodec json) { + LingxingRateLimiter rateLimiter, JacksonJsonCodec json, + common.ecommerce.http.HttpTransport transport) { this.ctx = ctx; this.token = token; this.requestFactory = requestFactory; this.rateLimiter = rateLimiter; this.json = json; + this.transport = transport; } public ChannelContext ctx() { @@ -58,6 +61,18 @@ public final class LingxingCall { return requestFactory.post(ctx.endpointBaseUrl(), api.path(), body, ctx.secret(), token, ctx, operation); } + /** 构造一个已签名的 GET 请求(无业务参数)。 */ + public HttpRequest get(LingxingApi api, String operation) { + rateLimiter.acquire(api.path(), api.permitsPerSecond()); + return requestFactory.get(ctx.endpointBaseUrl(), api.path(), java.util.Collections.emptyMap(), + ctx.secret(), token, ctx, operation); + } + + /** 发请求。传输层由子平台持有,这里只做一次转发,避免每个子平台重复写。 */ + public HttpResponse execute(HttpRequest request) { + return transport.execute(request); + } + /** 校验 HTTP 状态与业务码,返回原始 body 供子平台解析。 */ public String body(HttpResponse response, String operation) { return LingxingEnvelope.of(response, json, operation, ctx.omsStoreId(), ctx.requestId()).body(); 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 b7c3621..e6fb1dc 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatform.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatform.java @@ -1,8 +1,6 @@ package common.ecommerce.lingxing.platform; -import common.ecommerce.capability.AggregatorCapable; -import common.ecommerce.model.Capability; -import common.ecommerce.model.Platform; +import common.ecommerce.capability.AggregatorCapable.SubChannel; import common.ecommerce.model.order.SalesOrder; import common.ecommerce.model.order.SalesOrderQuery; import common.ecommerce.model.product.ProductListing; @@ -12,45 +10,33 @@ import common.ecommerce.model.support.Page; import java.util.List; /** - * 领星侧的一个子平台(亚马逊、沃尔玛、eBay……)。 + * 领星子平台的能力分派入口:只做组合,不写业务逻辑。 * - *

同一个聚合器下各子平台的接口路径、请求体、响应字段都不同,差异全部收敛到实现里; - * {@code LingxingAdapter} 只做分派。

+ *

各能力由独立的 Source 实现({@link common.ecommerce.lingxing.platform.order.LingxingOrderSource}、 + * {@link common.ecommerce.lingxing.platform.product.LingxingProductSource}、 + * {@link common.ecommerce.lingxing.platform.shop.LingxingShopSource})提供, + * 子平台不支持的 Source 传 {@code null} 即可,调用时统一抛 + * {@code UnsupportedCapabilityException}。

* - *

不支持的能力不覆写即可,默认实现直接抛 - * {@code ChannelException.UnsupportedCapabilityException}——不静默返回空。

+ *

这样单个平台类不会随能力增长而膨胀,能力之间也互不干扰。

*/ public interface LingxingSubPlatform { /** 对应的业务平台,用于注册表与日志。 */ - Platform platform(); + common.ecommerce.model.Platform platform(); - /** 这个子平台实际支持哪些能力;{@code LingxingAdapter.supports} 据此细化。 */ - boolean supports(Capability capability); - - /** 销售订单分页拉取。 */ - default Page orders(LingxingCall call, SalesOrderQuery query) { - throw unsupported(Capability.SALES_ORDER); - } - - /** 单订单详情。 */ - default SalesOrder order(LingxingCall call, String channelOrderId) { - throw unsupported(Capability.SALES_ORDER); - } - - /** 商品 / Listing 分页拉取。 */ - default Page products(LingxingCall call, ProductQuery query) { - throw unsupported(Capability.PRODUCT); + /** 订单能力;不支持时为 null。 */ + default common.ecommerce.lingxing.platform.order.LingxingOrderSource orderSource() { + return null; } - /** 店铺(子渠道)列表。 */ - default List shops(LingxingCall call) { - throw unsupported(Capability.SALES_ORDER); + /** 商品能力;不支持时为 null。 */ + default common.ecommerce.lingxing.platform.product.LingxingProductSource productSource() { + return null; } - /** 缺失能力的统一异常,避免各实现各写一遍。 */ - static common.ecommerce.error.ChannelException.UnsupportedCapabilityException unsupported( - Capability capability) { - return new common.ecommerce.error.ChannelException.UnsupportedCapabilityException(capability); + /** 店铺能力;不支持时为 null。 */ + default common.ecommerce.lingxing.platform.shop.LingxingShopSource shopSource() { + return null; } } 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 58ecdb3..b31e11f 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java @@ -30,7 +30,9 @@ public final class LingxingSubPlatforms { static { Map map = new EnumMap(Platform.class); register(map, new WalmartSubPlatform(SHARED_TRANSPORT, SHARED_JSON)); - // 亚马逊、eBay、SHEIN… 落地后继续 register(...) + register(map, new common.ecommerce.lingxing.platform.amazon.AmazonScSubPlatform( + SHARED_TRANSPORT, SHARED_JSON)); + // eBay、SHEIN… 落地后继续 register(...) BY_PLATFORM = Collections.unmodifiableMap(map); } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonProductSource.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonProductSource.java new file mode 100644 index 0000000..075620a --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonProductSource.java @@ -0,0 +1,109 @@ +package common.ecommerce.lingxing.platform.amazon; + +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.amazon.dto.AmazonListingDto; +import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.model.ChannelProvider; +import common.ecommerce.model.product.ProductListing; +import common.ecommerce.model.product.ProductQuery; +import common.ecommerce.model.support.Page; +import common.ecommerce.model.support.PlatformTime; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * 领星 · 亚马逊 SC 商品 / Listing 列表。 + * + *

接口 {@code /erp/sc/data/mws/listing}:POST,{@code offset}/{@code length} 分页, + * {@code length} 上限 1000;令牌桶容量 1,且按「账户 + path」共享,必须串行调用。

+ */ +public final class AmazonProductSource implements LingxingProductSource { + + private static final int MAX_PAGE_SIZE = 1000; + + /** listing_update_date 是零时区时间,格式 Y-m-d H:i:s。 */ + private static final DateTimeFormatter LISTING_TIME = + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + @Override + public Page products(LingxingCall call, ProductQuery query) { + int length = Math.min(query.pageSize() <= 0 ? 100 : query.pageSize(), MAX_PAGE_SIZE); + int offset = query.cursor() == null ? 0 : Integer.parseInt(query.cursor()); + + Map body = new LinkedHashMap(); + body.put("sid", call.ctx().account().externalStoreId()); + body.put("offset", offset); + body.put("length", length); + Instant from = query.updatedAfter(); + if (from != null) { + // 网关要求零时区时间字符串 + body.put("listing_update_start_time", LISTING_TIME.format( + LocalDateTime.ofInstant(from, ZoneOffset.UTC))); + } + Instant to = query.updatedBefore(); + if (to != null) { + body.put("listing_update_end_time", LISTING_TIME.format( + LocalDateTime.ofInstant(to, ZoneOffset.UTC))); + } + + HttpRequest req = call.signed(LingxingApi.LISTING_AMAZON_SC, body, "products.pull"); + HttpResponse resp = call.execute(req); + LingxingEnvelope envelope = call.envelope(resp, "products.pull"); + + List dtos = call.json().listAt(envelope.body(), "data", AmazonListingDto.class); + List out = new ArrayList(dtos.size()); + for (AmazonListingDto dto : dtos) { + out.add(toProductListing(dto, call)); + } + + int nextOffset = offset + dtos.size(); + boolean hasMore = dtos.size() >= length + && (envelope.total() < 0 || nextOffset < envelope.total()); + return Page.of(out, hasMore ? String.valueOf(nextOffset) : null); + } + + private ProductListing toProductListing(AmazonListingDto dto, LingxingCall call) { + List images = dto.getSmallImageUrl() == null + ? Collections.emptyList() + : Collections.singletonList(dto.getSmallImageUrl()); + + return ProductListing.builder() + .omsStoreId(call.ctx().omsStoreId()) + // seller_sku 是平台侧 SKU,参与 canonicalKey + .platformSku(dto.getSellerSku()) + .title(dto.getItemName()) + .imageUrls(images) + // 0 停售 / 1 在售;保留原值,由宿主解释 + .status(dto.getStatus() == null ? null : String.valueOf(dto.getStatus())) + .updatedAt(listingTime(dto.getListingUpdateDate(), call)) + .sourceProvider(ChannelProvider.LINGXING) + .build(); + } + + /** 报表更新时间是零时区时间,按 UTC 解析;缺失时返回 null。 */ + private static PlatformTime listingTime(String raw, LingxingCall call) { + if (raw == null || raw.trim().isEmpty()) { + return null; + } + try { + Instant utc = LocalDateTime.parse(raw.trim(), LISTING_TIME).toInstant(ZoneOffset.UTC); + return PlatformTime.of(utc, ZoneId.of(call.ctx().account().marketplaceZone())); + } catch (DateTimeParseException e) { + return null; + } + } +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonScSubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonScSubPlatform.java new file mode 100644 index 0000000..b0ac6ca --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonScSubPlatform.java @@ -0,0 +1,44 @@ +package common.ecommerce.lingxing.platform.amazon; + +import common.ecommerce.http.HttpTransport; +import common.ecommerce.http.JacksonJsonCodec; +import common.ecommerce.lingxing.platform.LingxingSubPlatform; +import common.ecommerce.lingxing.platform.order.LingxingOrderSource; +import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.shop.LingxingShopSource; +import common.ecommerce.model.Platform; + +/** + * 领星 · 亚马逊 SC 子平台:只做能力组合,不写业务逻辑。 + */ +public final class AmazonScSubPlatform implements LingxingSubPlatform { + + private final LingxingProductSource products; + private final LingxingShopSource shops; + + public AmazonScSubPlatform(HttpTransport transport, JacksonJsonCodec json) { + this.products = new AmazonProductSource(); + this.shops = new AmazonShopSource(); + } + + @Override + public Platform platform() { + return Platform.AMAZON_SC; + } + + @Override + public LingxingProductSource productSource() { + return products; + } + + @Override + public LingxingShopSource shopSource() { + return shops; + } + + /** 亚马逊 SC 的订单接口暂未核对,接入前不声明订单能力。 */ + @Override + public LingxingOrderSource orderSource() { + return null; + } +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonShopSource.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonShopSource.java new file mode 100644 index 0000000..e919a26 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonShopSource.java @@ -0,0 +1,52 @@ +package common.ecommerce.lingxing.platform.amazon; + +import common.ecommerce.capability.AggregatorCapable.SubChannel; +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.amazon.dto.AmazonShopDto; +import common.ecommerce.lingxing.platform.shop.LingxingShopSource; +import common.ecommerce.model.Platform; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * 领星 · 亚马逊 SC 店铺列表。 + * + *

接口 {@code /erp/sc/data/seller/lists} 是 GET、非分页,一次返回企业下全部已授权 SC 店铺。 + * 令牌桶容量 1,按「账户 + path」限流。

+ */ +public final class AmazonShopSource implements LingxingShopSource { + + /** 领星侧 Amazon SC 的 platform_code。 */ + private static final int AMAZON_SC_PLATFORM_CODE = 10001; + + /** 店铺状态:1 = 正常同步。 */ + private static final int STATUS_ACTIVE = 1; + + @Override + public List shops(LingxingCall call) { + HttpRequest req = call.get(LingxingApi.SHOP_LIST_AMAZON_SC, "shops.list"); + HttpResponse resp = call.execute(req); + LingxingEnvelope envelope = call.envelope(resp, "shops.list"); + + List shops = call.json().listAt(envelope.body(), "data", AmazonShopDto.class); + List out = new ArrayList(shops.size()); + for (AmazonShopDto shop : shops) { + if (shop.getSid() == null || shop.getStatus() == null || shop.getStatus() != STATUS_ACTIVE) { + continue; + } + // platformCode 取领星侧 Amazon SC 的平台码 10001;externalStoreId 用领星店铺 id(sid) + out.add(new SubChannel( + AMAZON_SC_PLATFORM_CODE, + String.valueOf(shop.getSid()), + Platform.AMAZON_SC, + shop.getName() == null ? shop.getSellerId() : shop.getName())); + } + return Collections.unmodifiableList(out); + } +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/dto/AmazonListingDto.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/dto/AmazonListingDto.java new file mode 100644 index 0000000..47ec7e6 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/dto/AmazonListingDto.java @@ -0,0 +1,73 @@ +package common.ecommerce.lingxing.platform.amazon.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +/** + * 领星 · 亚马逊 SC Listing({@code /erp/sc/data/mws/listing} 的 {@code data[]})。 + * + *

唯一键 {@code sid + seller_sku}。这里只保留映射到统一模型所需的字段, + * 其余字段(费用、排名、销量、尺寸等)暂不解析。

+ * + *

注意 {@code listing_update_date} 是零时区时间,而 {@code pair_update_time} + * 是北京时间——两者不能混用。

+ */ +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class AmazonListingDto { + + /** 店铺 id。 */ + @JsonProperty("sid") + private Long sid; + + /** 平台 SKU(MSKU),参与 canonicalKey。 */ + @JsonProperty("seller_sku") + private String sellerSku; + + /** ASIN。 */ + @JsonProperty("asin") + private String asin; + + /** 父 ASIN。 */ + @JsonProperty("parent_asin") + private String parentAsin; + + /** 标题。 */ + @JsonProperty("item_name") + private String itemName; + + /** 商品缩略图地址(单张)。 */ + @JsonProperty("small_image_url") + private String smallImageUrl; + + /** 状态:0 停售,1 在售。 */ + @JsonProperty("status") + private Integer status; + + /** 是否删除:0 否,1 是。 */ + @JsonProperty("is_delete") + private Integer isDelete; + + /** 币种。 */ + @JsonProperty("currency_code") + private String currencyCode; + + /** 售价(不含促销、运费、积分)。 */ + @JsonProperty("price") + private String price; + + /** All Listing 报表更新时间,零时区,格式 {@code Y-m-d H:i:s}。 */ + @JsonProperty("listing_update_date") + private String listingUpdateDate; + + /** 配送方式,如 FBM / FBA。 */ + @JsonProperty("fulfillment_channel_type") + private String fulfillmentChannelType; + + /** 商城所在国家。 */ + @JsonProperty("marketplace") + private String marketplace; +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/dto/AmazonShopDto.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/dto/AmazonShopDto.java new file mode 100644 index 0000000..2858f02 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/dto/AmazonShopDto.java @@ -0,0 +1,61 @@ +package common.ecommerce.lingxing.platform.amazon.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +/** + * 领星 · 亚马逊 SC 店铺({@code /erp/sc/data/seller/lists} 的 {@code data[]})。 + * + *

唯一键 {@code sid},后续调用亚马逊相关接口时用它指定店铺。

+ */ +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class AmazonShopDto { + + /** 店铺 id,领星 ERP 对企业已授权店铺的唯一标识。 */ + @JsonProperty("sid") + private Long sid; + + /** 站点 id。 */ + @JsonProperty("mid") + private Integer mid; + + /** 店铺名。 */ + @JsonProperty("name") + private String name; + + /** 亚马逊店铺 id(sellerId)。 */ + @JsonProperty("seller_id") + private String sellerId; + + /** 店铺账户名称。 */ + @JsonProperty("account_name") + private String accountName; + + /** 店铺账号 id。 */ + @JsonProperty("seller_account_id") + private Long sellerAccountId; + + /** 站点简称,如 NA / EU。 */ + @JsonProperty("region") + private String region; + + /** 商城所在国家名称。 */ + @JsonProperty("country") + private String country; + + /** 是否授权广告:0 否,1 是。 */ + @JsonProperty("has_ads_setting") + private Integer hasAdsSetting; + + /** 市场 id。 */ + @JsonProperty("marketplace_id") + private String marketplaceId; + + /** 店铺状态:0 停止同步,1 正常,2 授权异常,3 欠费停服。 */ + @JsonProperty("status") + private Integer status; +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/order/LingxingOrderSource.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/order/LingxingOrderSource.java new file mode 100644 index 0000000..73e2543 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/order/LingxingOrderSource.java @@ -0,0 +1,24 @@ +package common.ecommerce.lingxing.platform.order; + +import common.ecommerce.model.order.SalesOrder; +import common.ecommerce.model.order.SalesOrderQuery; +import common.ecommerce.model.support.Page; +import common.ecommerce.lingxing.platform.LingxingCall; + +/** + * 子平台的订单能力。 + * + *

只负责「拼请求体 + 解析响应」;限流、取 token、签名、业务码校验由 + * {@link LingxingCall} 提供。

+ */ +public interface LingxingOrderSource { + + /** 分页拉取销售订单。 */ + Page orders(LingxingCall call, SalesOrderQuery query); + + /** 单订单详情;没有详情接口的实现直接抛出即可。 */ + default SalesOrder order(LingxingCall call, String channelOrderId) { + throw new common.ecommerce.error.ChannelException.UnsupportedCapabilityException( + common.ecommerce.model.Capability.SALES_ORDER); + } +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/product/LingxingProductSource.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/product/LingxingProductSource.java new file mode 100644 index 0000000..edc59dd --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/product/LingxingProductSource.java @@ -0,0 +1,15 @@ +package common.ecommerce.lingxing.platform.product; + +import common.ecommerce.lingxing.platform.LingxingCall; +import common.ecommerce.model.product.ProductListing; +import common.ecommerce.model.product.ProductQuery; +import common.ecommerce.model.support.Page; + +/** + * 子平台的商品 / Listing 能力。 + */ +public interface LingxingProductSource { + + /** 分页拉取商品。 */ + Page products(LingxingCall call, ProductQuery query); +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/shop/LingxingShopSource.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shop/LingxingShopSource.java new file mode 100644 index 0000000..80e33ec --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shop/LingxingShopSource.java @@ -0,0 +1,15 @@ +package common.ecommerce.lingxing.platform.shop; + +import common.ecommerce.capability.AggregatorCapable.SubChannel; +import common.ecommerce.lingxing.platform.LingxingCall; + +import java.util.List; + +/** + * 子平台的店铺(子渠道)能力。 + */ +public interface LingxingShopSource { + + /** 拉取企业已授权到领星的全部店铺。 */ + List shops(LingxingCall call); +} 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 c441415..1ae0d52 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 @@ -1,51 +1,21 @@ package common.ecommerce.lingxing.platform.walmart; -import common.ecommerce.http.HttpRequest; -import common.ecommerce.http.HttpResponse; import common.ecommerce.http.HttpTransport; import common.ecommerce.http.JacksonJsonCodec; -import common.ecommerce.lingxing.LingxingApi; -import common.ecommerce.lingxing.LingxingConstants; -import common.ecommerce.lingxing.dto.LingxingOrderDto; -import common.ecommerce.lingxing.http.LingxingEnvelope; -import common.ecommerce.lingxing.mapper.LingxingOrderMapper; -import common.ecommerce.lingxing.platform.LingxingCall; import common.ecommerce.lingxing.platform.LingxingSubPlatform; -import common.ecommerce.model.Capability; +import common.ecommerce.lingxing.platform.order.LingxingOrderSource; +import common.ecommerce.lingxing.platform.walmart.order.WalmartOrderSource; import common.ecommerce.model.Platform; -import common.ecommerce.model.account.ChannelAccount; -import common.ecommerce.model.order.SalesOrder; -import common.ecommerce.model.order.SalesOrderQuery; -import common.ecommerce.model.support.Page; - -import java.time.Duration; -import java.time.Instant; -import java.time.ZoneId; -import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; /** - * 领星 · 沃尔玛子平台。 - * - *

当前复用领星跨平台的订单列表接口 {@link LingxingApi#ORDER_LIST}; - * 沃尔玛若确认为独立订单接口,只需把 {@link #orderApi()} 换成对应枚举值。

+ * 领星 · 沃尔玛子平台:只做能力组合,不写业务逻辑。 */ public final class WalmartSubPlatform implements LingxingSubPlatform { - private final HttpTransport transport; - private final LingxingOrderMapper mapper; + private final LingxingOrderSource orders; public WalmartSubPlatform(HttpTransport transport, JacksonJsonCodec json) { - this.transport = transport; - this.mapper = new LingxingOrderMapper(json); - } - - /** 该子平台使用的订单接口;当前为领星统一接口。 */ - protected LingxingApi orderApi() { - return LingxingApi.ORDER_LIST; + this.orders = new WalmartOrderSource(transport, json); } @Override @@ -54,55 +24,7 @@ public final class WalmartSubPlatform implements LingxingSubPlatform { } @Override - public boolean supports(Capability capability) { - return capability == Capability.SALES_ORDER; - } - - @Override - public Page orders(LingxingCall call, SalesOrderQuery query) { - ChannelAccount account = call.ctx().account(); - ZoneId zone = ZoneId.of(account.marketplaceZone()); - - Instant from = query.updatedAfter(); - Instant to = query.updatedBefore() != null ? query.updatedBefore() : Instant.now(call.ctx().clock()); - if (Duration.between(from, to).toDays() > LingxingConstants.MAX_QUERY_DAYS) { - from = to.minus(Duration.ofDays(LingxingConstants.MAX_QUERY_DAYS)); - } - - int length = Math.min(query.pageSize() <= 0 ? 100 : query.pageSize(), LingxingConstants.MAX_PAGE_SIZE); - int offset = query.cursor() == null ? 0 : Integer.parseInt(query.cursor()); - Map body = orderBody(account, from, to, offset, length); - - HttpRequest req = call.signed(orderApi(), body, "salesOrders.pull"); - HttpResponse resp = transport.execute(req); - LingxingEnvelope envelope = call.envelope(resp, "salesOrders.pull"); - - List dtos = mapper.parseList(envelope.body()); - List orders = new ArrayList(dtos.size()); - for (LingxingOrderDto dto : dtos) { - orders.add(mapper.toSalesOrder(dto, account.omsStoreId(), zone)); - } - - int nextOffset = offset + dtos.size(); - boolean hasMore = dtos.size() >= length - && (envelope.total() < 0 || nextOffset < envelope.total()); - return Page.of(orders, hasMore ? String.valueOf(nextOffset) : null); - } - - private Map orderBody(ChannelAccount account, Instant from, Instant to, - int offset, int length) { - Map body = new LinkedHashMap(); - body.put("offset", offset); - body.put("length", length); - body.put("date_type", "update_time"); - body.put("start_time", from.getEpochSecond()); - body.put("end_time", to.getEpochSecond()); - if (account.platformCode() != null) { - body.put("platform_code", Collections.singletonList(account.platformCode())); - } - if (account.externalStoreId() != null) { - body.put("store_id", Collections.singletonList(account.externalStoreId())); - } - return body; + public LingxingOrderSource orderSource() { + return orders; } } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/order/WalmartOrderSource.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/order/WalmartOrderSource.java new file mode 100644 index 0000000..6f181e2 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/order/WalmartOrderSource.java @@ -0,0 +1,107 @@ +package common.ecommerce.lingxing.platform.walmart.order; + +import common.ecommerce.http.HttpRequest; +import common.ecommerce.http.HttpResponse; +import common.ecommerce.http.HttpTransport; +import common.ecommerce.http.JacksonJsonCodec; +import common.ecommerce.lingxing.LingxingApi; +import common.ecommerce.lingxing.LingxingConstants; +import common.ecommerce.lingxing.dto.LingxingOrderDto; +import common.ecommerce.lingxing.http.LingxingEnvelope; +import common.ecommerce.lingxing.mapper.LingxingOrderMapper; +import common.ecommerce.lingxing.platform.LingxingCall; +import common.ecommerce.lingxing.platform.order.LingxingOrderSource; +import common.ecommerce.model.account.ChannelAccount; +import common.ecommerce.model.order.SalesOrder; +import common.ecommerce.model.order.SalesOrderQuery; +import common.ecommerce.model.support.Page; + +import java.time.Duration; +import java.time.Instant; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * 领星 · 沃尔玛的订单实现。 + * + *

当前复用领星跨平台的订单列表接口 {@link LingxingApi#ORDER_LIST}; + * 沃尔玛若确认为独立订单接口,只需把 {@link #orderApi()} 换成对应枚举值。

+ */ +public final class WalmartOrderSource implements LingxingOrderSource { + + private final HttpTransport transport; + private final LingxingOrderMapper mapper; + + public WalmartOrderSource(HttpTransport transport, JacksonJsonCodec json) { + this.transport = transport; + this.mapper = new LingxingOrderMapper(json); + } + + /** 该子平台使用的订单接口;当前为领星统一接口。 */ + private LingxingApi orderApi() { + return LingxingApi.ORDER_LIST; + } + + @Override + public Page orders(LingxingCall call, SalesOrderQuery query) { + ChannelAccount account = call.ctx().account(); + ZoneId zone = ZoneId.of(account.marketplaceZone()); + + Instant from = query.updatedAfter(); + Instant to = query.updatedBefore() != null ? query.updatedBefore() : Instant.now(call.ctx().clock()); + if (Duration.between(from, to).toDays() > LingxingConstants.MAX_QUERY_DAYS) { + from = to.minus(Duration.ofDays(LingxingConstants.MAX_QUERY_DAYS)); + } + + int length = Math.min(query.pageSize() <= 0 ? 100 : query.pageSize(), LingxingConstants.MAX_PAGE_SIZE); + int offset = query.cursor() == null ? 0 : Integer.parseInt(query.cursor()); + Map body = orderBody(account, from, to, offset, length); + + HttpRequest req = call.signed(orderApi(), body, "salesOrders.pull"); + HttpResponse resp = transport.execute(req); + LingxingEnvelope envelope = call.envelope(resp, "salesOrders.pull"); + + List dtos = mapper.parseList(envelope.body()); + List orders = new ArrayList(dtos.size()); + for (LingxingOrderDto dto : dtos) { + try { + orders.add(mapper.toSalesOrder(dto, account.omsStoreId(), zone)); + } catch (RuntimeException e) { + throw common.ecommerce.error.ValidationException.class.cast( + new common.ecommerce.error.ValidationException( + common.ecommerce.error.ChannelException.builder() + .message("lingxing order mapping failed: " + e.getMessage()) + .operation("salesOrders.pull") + .omsStoreId(call.ctx().omsStoreId()) + .requestId(call.ctx().requestId()) + .rawResponse(envelope.body()), e)); + } + } + + int nextOffset = offset + dtos.size(); + boolean hasMore = dtos.size() >= length + && (envelope.total() < 0 || nextOffset < envelope.total()); + return Page.of(orders, hasMore ? String.valueOf(nextOffset) : null); + } + + private Map orderBody(ChannelAccount account, Instant from, Instant to, + int offset, int length) { + Map body = new LinkedHashMap(); + body.put("offset", offset); + body.put("length", length); + body.put("date_type", "update_time"); + body.put("start_time", from.getEpochSecond()); + body.put("end_time", to.getEpochSecond()); + if (account.platformCode() != null) { + body.put("platform_code", Collections.singletonList(account.platformCode())); + } + if (account.externalStoreId() != null) { + body.put("store_id", Collections.singletonList(account.externalStoreId())); + } + return body; + } +}