Browse Source
结构(避免单个平台类随能力增长膨胀): - 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 <noreply@anthropic.com>feature/supplier-lingxing
16 changed files with 651 additions and 126 deletions
@ -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 列表。 |
||||||
|
* |
||||||
|
* <p>接口 {@code /erp/sc/data/mws/listing}:POST,{@code offset}/{@code length} 分页, |
||||||
|
* {@code length} 上限 1000;令牌桶容量 1,且按「账户 + path」共享,必须串行调用。</p> |
||||||
|
*/ |
||||||
|
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<ProductListing> 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<String, Object> body = new LinkedHashMap<String, Object>(); |
||||||
|
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<AmazonListingDto> dtos = call.json().listAt(envelope.body(), "data", AmazonListingDto.class); |
||||||
|
List<ProductListing> out = new ArrayList<ProductListing>(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<String> images = dto.getSmallImageUrl() == null |
||||||
|
? Collections.<String>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; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -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; |
||||||
|
} |
||||||
|
} |
||||||
@ -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 店铺列表。 |
||||||
|
* |
||||||
|
* <p>接口 {@code /erp/sc/data/seller/lists} 是 GET、非分页,一次返回企业下全部已授权 SC 店铺。 |
||||||
|
* 令牌桶容量 1,按「账户 + path」限流。</p> |
||||||
|
*/ |
||||||
|
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<SubChannel> 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<AmazonShopDto> shops = call.json().listAt(envelope.body(), "data", AmazonShopDto.class); |
||||||
|
List<SubChannel> out = new ArrayList<SubChannel>(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); |
||||||
|
} |
||||||
|
} |
||||||
@ -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[]})。 |
||||||
|
* |
||||||
|
* <p>唯一键 {@code sid + seller_sku}。这里只保留映射到统一模型所需的字段, |
||||||
|
* 其余字段(费用、排名、销量、尺寸等)暂不解析。</p> |
||||||
|
* |
||||||
|
* <p>注意 {@code listing_update_date} 是<b>零时区时间</b>,而 {@code pair_update_time} |
||||||
|
* 是北京时间——两者不能混用。</p> |
||||||
|
*/ |
||||||
|
@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; |
||||||
|
} |
||||||
@ -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[]})。 |
||||||
|
* |
||||||
|
* <p>唯一键 {@code sid},后续调用亚马逊相关接口时用它指定店铺。</p> |
||||||
|
*/ |
||||||
|
@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; |
||||||
|
} |
||||||
@ -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; |
||||||
|
|
||||||
|
/** |
||||||
|
* 子平台的订单能力。 |
||||||
|
* |
||||||
|
* <p>只负责「拼请求体 + 解析响应」;限流、取 token、签名、业务码校验由 |
||||||
|
* {@link LingxingCall} 提供。</p> |
||||||
|
*/ |
||||||
|
public interface LingxingOrderSource { |
||||||
|
|
||||||
|
/** 分页拉取销售订单。 */ |
||||||
|
Page<SalesOrder> 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); |
||||||
|
} |
||||||
|
} |
||||||
@ -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<ProductListing> products(LingxingCall call, ProductQuery query); |
||||||
|
} |
||||||
@ -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<SubChannel> shops(LingxingCall call); |
||||||
|
} |
||||||
@ -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; |
||||||
|
|
||||||
|
/** |
||||||
|
* 领星 · 沃尔玛的订单实现。 |
||||||
|
* |
||||||
|
* <p>当前复用领星跨平台的订单列表接口 {@link LingxingApi#ORDER_LIST}; |
||||||
|
* 沃尔玛若确认为独立订单接口,只需把 {@link #orderApi()} 换成对应枚举值。</p> |
||||||
|
*/ |
||||||
|
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<SalesOrder> 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<String, Object> 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<LingxingOrderDto> dtos = mapper.parseList(envelope.body()); |
||||||
|
List<SalesOrder> orders = new ArrayList<SalesOrder>(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<String, Object> orderBody(ChannelAccount account, Instant from, Instant to, |
||||||
|
int offset, int length) { |
||||||
|
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||||
|
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; |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue