Browse Source
# Conflicts: # lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/AmazonShopSource.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/AmazonScSubPlatform.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/dto/AmazonOrderDetailDto.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/dto/AmazonOrderDetailItemDto.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/dto/AmazonOrderDto.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/dto/AmazonOrderItemDto.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/dto/AmazonSettlementTransactionDto.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/dto/FbmOrderDetailDto.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/dto/FbmOrderDto.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/amazon/sc/dto/FbmOrderItemDto.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/WalmartSubPlatform.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/dto/WalmartListingDto.java # lingxing/src/main/java/common/ecommerce/lingxing/platform/walmart/product/WalmartProductSource.javafeature/supplier-lingxing
41 changed files with 3394 additions and 99 deletions
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
package common.ecommerce.capability; |
||||
|
||||
import common.ecommerce.model.settlement.SettlementQuery; |
||||
import common.ecommerce.model.settlement.SettlementTransaction; |
||||
import common.ecommerce.model.support.Page; |
||||
import common.ecommerce.sdk.ChannelContext; |
||||
|
||||
/** |
||||
* 结算 / 账单能力:拉取平台结算交易明细。 |
||||
* |
||||
* <p>一行代表一笔费用(佣金、配送费、退款、广告费等),平台可能按 |
||||
* 结算周期或修改时间分页给出。重复同步按 {@code transactionKey} upsert。</p> |
||||
*/ |
||||
public interface SettlementCapable extends PlatformAdapter { |
||||
|
||||
Page<SettlementTransaction> pull(ChannelContext ctx, SettlementQuery query); |
||||
} |
||||
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
package common.ecommerce.capability; |
||||
|
||||
import common.ecommerce.error.ChannelException; |
||||
import common.ecommerce.model.Capability; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.settlement.SettlementQuery; |
||||
import common.ecommerce.model.settlement.SettlementTransaction; |
||||
import common.ecommerce.model.support.Page; |
||||
import common.ecommerce.sdk.ChannelContext; |
||||
|
||||
/** |
||||
* 门面持有的结算视图。调用前做能力检查,缺失时显式拒绝。 |
||||
*/ |
||||
public final class Settlements { |
||||
|
||||
private final PlatformAdapter adapter; |
||||
private final ChannelAccount account; |
||||
private final ChannelContext ctx; |
||||
|
||||
public Settlements(PlatformAdapter adapter, ChannelAccount account, ChannelContext ctx) { |
||||
this.adapter = adapter; |
||||
this.account = account; |
||||
this.ctx = ctx; |
||||
} |
||||
|
||||
public Page<SettlementTransaction> pull(SettlementQuery query) { |
||||
requireSettlementCapable(); |
||||
return ((SettlementCapable) adapter).pull(ctx, query); |
||||
} |
||||
|
||||
private void requireSettlementCapable() { |
||||
if (!adapter.supports(account, Capability.BILLING) || !(adapter instanceof SettlementCapable)) { |
||||
throw new ChannelException.UnsupportedCapabilityException(Capability.BILLING); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,183 @@
@@ -0,0 +1,183 @@
|
||||
package common.ecommerce.model.settlement; |
||||
|
||||
import java.time.Instant; |
||||
import java.time.LocalDate; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 结算交易明细查询条件(单次查询,不推进水位线)。 |
||||
* |
||||
* <p>平台通常要求「结算时间」与「修改时间」二选一,或给出精确搜索值; |
||||
* 具体校验在适配器里做,core 只承载条件。</p> |
||||
*/ |
||||
public final class SettlementQuery { |
||||
|
||||
private final LocalDate settledAfter; |
||||
private final LocalDate settledBefore; |
||||
private final Instant modifiedAfter; |
||||
private final Instant modifiedBefore; |
||||
private final String eventType; |
||||
private final String transactionType; |
||||
private final String searchField; |
||||
private final List<String> searchValues; |
||||
private final int pageSize; |
||||
private final String cursor; |
||||
|
||||
private SettlementQuery(Builder b) { |
||||
this.settledAfter = b.settledAfter; |
||||
this.settledBefore = b.settledBefore; |
||||
this.modifiedAfter = b.modifiedAfter; |
||||
this.modifiedBefore = b.modifiedBefore; |
||||
this.eventType = b.eventType; |
||||
this.transactionType = b.transactionType; |
||||
this.searchField = b.searchField; |
||||
this.searchValues = Collections.unmodifiableList(new ArrayList<String>(b.searchValues)); |
||||
this.pageSize = b.pageSize; |
||||
this.cursor = b.cursor; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
/** 结算时间起(含),按平台站点日期。 */ |
||||
public LocalDate settledAfter() { |
||||
return settledAfter; |
||||
} |
||||
|
||||
/** 结算时间止(含)。 */ |
||||
public LocalDate settledBefore() { |
||||
return settledBefore; |
||||
} |
||||
|
||||
/** 修改时间起(含)。时区语义由适配器解释。 */ |
||||
public Instant modifiedAfter() { |
||||
return modifiedAfter; |
||||
} |
||||
|
||||
/** 修改时间止(含)。 */ |
||||
public Instant modifiedBefore() { |
||||
return modifiedBefore; |
||||
} |
||||
|
||||
/** 来源事件,多个用逗号分隔,如 {@code ShipmentEventList,RefundEventList}。 */ |
||||
public String eventType() { |
||||
return eventType; |
||||
} |
||||
|
||||
/** 交易类型,如 Commission / Tax。 */ |
||||
public String transactionType() { |
||||
return transactionType; |
||||
} |
||||
|
||||
/** 精确搜索字段,如 {@code id} / {@code amazon_order_id} / {@code settlement_id}。 */ |
||||
public String searchField() { |
||||
return searchField; |
||||
} |
||||
|
||||
public List<String> searchValues() { |
||||
return searchValues; |
||||
} |
||||
|
||||
public int pageSize() { |
||||
return pageSize; |
||||
} |
||||
|
||||
public String cursor() { |
||||
return cursor; |
||||
} |
||||
|
||||
public Builder toBuilder() { |
||||
return builder() |
||||
.settledAfter(settledAfter) |
||||
.settledBefore(settledBefore) |
||||
.modifiedAfter(modifiedAfter) |
||||
.modifiedBefore(modifiedBefore) |
||||
.eventType(eventType) |
||||
.transactionType(transactionType) |
||||
.searchField(searchField) |
||||
.searchValues(searchValues) |
||||
.pageSize(pageSize) |
||||
.cursor(cursor); |
||||
} |
||||
|
||||
public static final class Builder { |
||||
|
||||
private LocalDate settledAfter; |
||||
private LocalDate settledBefore; |
||||
private Instant modifiedAfter; |
||||
private Instant modifiedBefore; |
||||
private String eventType; |
||||
private String transactionType; |
||||
private String searchField; |
||||
private List<String> searchValues = Collections.emptyList(); |
||||
private int pageSize = 100; |
||||
private String cursor; |
||||
|
||||
public Builder settledAfter(LocalDate settledAfter) { |
||||
this.settledAfter = settledAfter; |
||||
return this; |
||||
} |
||||
|
||||
public Builder settledBefore(LocalDate settledBefore) { |
||||
this.settledBefore = settledBefore; |
||||
return this; |
||||
} |
||||
|
||||
public Builder modifiedAfter(Instant modifiedAfter) { |
||||
this.modifiedAfter = modifiedAfter; |
||||
return this; |
||||
} |
||||
|
||||
public Builder modifiedBefore(Instant modifiedBefore) { |
||||
this.modifiedBefore = modifiedBefore; |
||||
return this; |
||||
} |
||||
|
||||
public Builder eventType(String eventType) { |
||||
this.eventType = eventType; |
||||
return this; |
||||
} |
||||
|
||||
public Builder transactionType(String transactionType) { |
||||
this.transactionType = transactionType; |
||||
return this; |
||||
} |
||||
|
||||
public Builder searchField(String searchField) { |
||||
this.searchField = searchField; |
||||
return this; |
||||
} |
||||
|
||||
public Builder searchValues(List<String> searchValues) { |
||||
this.searchValues = searchValues == null |
||||
? Collections.<String>emptyList() : searchValues; |
||||
return this; |
||||
} |
||||
|
||||
public Builder searchValues(String... searchValues) { |
||||
List<String> values = new ArrayList<String>(); |
||||
if (searchValues != null) { |
||||
Collections.addAll(values, searchValues); |
||||
} |
||||
this.searchValues = values; |
||||
return this; |
||||
} |
||||
|
||||
public Builder pageSize(int pageSize) { |
||||
this.pageSize = pageSize; |
||||
return this; |
||||
} |
||||
|
||||
public Builder cursor(String cursor) { |
||||
this.cursor = cursor; |
||||
return this; |
||||
} |
||||
|
||||
public SettlementQuery build() { |
||||
return new SettlementQuery(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,281 @@
@@ -0,0 +1,281 @@
|
||||
package common.ecommerce.model.settlement; |
||||
|
||||
import common.ecommerce.model.ChannelProvider; |
||||
import common.ecommerce.model.support.Money; |
||||
import common.ecommerce.model.support.PlatformTime; |
||||
|
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* 平台结算交易明细(一行费用)。 |
||||
* |
||||
* <p>用于对账:一笔订单可能拆成佣金、配送费、退款、广告费等多行, |
||||
* 每行由 {@code transactionKey} 唯一标识。{@code transactionKey} 必须稳定, |
||||
* 否则重复同步会产生重复账单;平台在未结算阶段可能变更 key 的, |
||||
* 由适配器文档单独说明。</p> |
||||
*/ |
||||
public final class SettlementTransaction { |
||||
|
||||
private final String omsStoreId; |
||||
private final String transactionKey; |
||||
private final ChannelProvider sourceProvider; |
||||
private final String sourceAccountId; |
||||
private final String channelOrderId; |
||||
private final String storeName; |
||||
private final String countryCode; |
||||
private final String fulfillment; |
||||
private final String eventType; |
||||
private final String transactionType; |
||||
private final String sellerSku; |
||||
private final String localSku; |
||||
private final String asin; |
||||
private final Money amount; |
||||
private final int quantity; |
||||
private final String processingStatus; |
||||
private final String fundTransferStatus; |
||||
private final String settlementId; |
||||
private final PlatformTime postedAt; |
||||
private final String marketplaceName; |
||||
|
||||
private SettlementTransaction(Builder b) { |
||||
this.omsStoreId = Objects.requireNonNull(b.omsStoreId, "omsStoreId"); |
||||
this.transactionKey = Objects.requireNonNull(b.transactionKey, "transactionKey"); |
||||
this.sourceProvider = Objects.requireNonNull(b.sourceProvider, "sourceProvider"); |
||||
this.sourceAccountId = b.sourceAccountId; |
||||
this.channelOrderId = b.channelOrderId; |
||||
this.storeName = b.storeName; |
||||
this.countryCode = b.countryCode; |
||||
this.fulfillment = b.fulfillment; |
||||
this.eventType = b.eventType; |
||||
this.transactionType = b.transactionType; |
||||
this.sellerSku = b.sellerSku; |
||||
this.localSku = b.localSku; |
||||
this.asin = b.asin; |
||||
this.amount = b.amount; |
||||
this.quantity = b.quantity; |
||||
this.processingStatus = b.processingStatus; |
||||
this.fundTransferStatus = b.fundTransferStatus; |
||||
this.settlementId = b.settlementId; |
||||
this.postedAt = b.postedAt; |
||||
this.marketplaceName = b.marketplaceName; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
public String omsStoreId() { |
||||
return omsStoreId; |
||||
} |
||||
|
||||
/** 交易明细唯一标识,重复同步按它 upsert。 */ |
||||
public String transactionKey() { |
||||
return transactionKey; |
||||
} |
||||
|
||||
public ChannelProvider sourceProvider() { |
||||
return sourceProvider; |
||||
} |
||||
|
||||
public String sourceAccountId() { |
||||
return sourceAccountId; |
||||
} |
||||
|
||||
public String channelOrderId() { |
||||
return channelOrderId; |
||||
} |
||||
|
||||
public String storeName() { |
||||
return storeName; |
||||
} |
||||
|
||||
public String countryCode() { |
||||
return countryCode; |
||||
} |
||||
|
||||
public String fulfillment() { |
||||
return fulfillment; |
||||
} |
||||
|
||||
/** 来源事件,如 Shipment / Refund / ServiceFee。 */ |
||||
public String eventType() { |
||||
return eventType; |
||||
} |
||||
|
||||
/** 交易类型,如 Commission / Tax / Refund。 */ |
||||
public String transactionType() { |
||||
return transactionType; |
||||
} |
||||
|
||||
public String sellerSku() { |
||||
return sellerSku; |
||||
} |
||||
|
||||
public String localSku() { |
||||
return localSku; |
||||
} |
||||
|
||||
public String asin() { |
||||
return asin; |
||||
} |
||||
|
||||
/** 金额;平台未给币种时为 null。 */ |
||||
public Money amount() { |
||||
return amount; |
||||
} |
||||
|
||||
public int quantity() { |
||||
return quantity; |
||||
} |
||||
|
||||
/** 结算状态:Open / Closed / Reconciled。 */ |
||||
public String processingStatus() { |
||||
return processingStatus; |
||||
} |
||||
|
||||
/** 转账状态:Succeeded / Processing / Failed / Unknown。 */ |
||||
public String fundTransferStatus() { |
||||
return fundTransferStatus; |
||||
} |
||||
|
||||
public String settlementId() { |
||||
return settlementId; |
||||
} |
||||
|
||||
/** 结算时间;平台未给可解析时间时为 null。 */ |
||||
public PlatformTime postedAt() { |
||||
return postedAt; |
||||
} |
||||
|
||||
public String marketplaceName() { |
||||
return marketplaceName; |
||||
} |
||||
|
||||
public static final class Builder { |
||||
|
||||
private String omsStoreId; |
||||
private String transactionKey; |
||||
private ChannelProvider sourceProvider; |
||||
private String sourceAccountId; |
||||
private String channelOrderId; |
||||
private String storeName; |
||||
private String countryCode; |
||||
private String fulfillment; |
||||
private String eventType; |
||||
private String transactionType; |
||||
private String sellerSku; |
||||
private String localSku; |
||||
private String asin; |
||||
private Money amount; |
||||
private int quantity; |
||||
private String processingStatus; |
||||
private String fundTransferStatus; |
||||
private String settlementId; |
||||
private PlatformTime postedAt; |
||||
private String marketplaceName; |
||||
|
||||
public Builder omsStoreId(String omsStoreId) { |
||||
this.omsStoreId = omsStoreId; |
||||
return this; |
||||
} |
||||
|
||||
public Builder transactionKey(String transactionKey) { |
||||
this.transactionKey = transactionKey; |
||||
return this; |
||||
} |
||||
|
||||
public Builder sourceProvider(ChannelProvider sourceProvider) { |
||||
this.sourceProvider = sourceProvider; |
||||
return this; |
||||
} |
||||
|
||||
public Builder sourceAccountId(String sourceAccountId) { |
||||
this.sourceAccountId = sourceAccountId; |
||||
return this; |
||||
} |
||||
|
||||
public Builder channelOrderId(String channelOrderId) { |
||||
this.channelOrderId = channelOrderId; |
||||
return this; |
||||
} |
||||
|
||||
public Builder storeName(String storeName) { |
||||
this.storeName = storeName; |
||||
return this; |
||||
} |
||||
|
||||
public Builder countryCode(String countryCode) { |
||||
this.countryCode = countryCode; |
||||
return this; |
||||
} |
||||
|
||||
public Builder fulfillment(String fulfillment) { |
||||
this.fulfillment = fulfillment; |
||||
return this; |
||||
} |
||||
|
||||
public Builder eventType(String eventType) { |
||||
this.eventType = eventType; |
||||
return this; |
||||
} |
||||
|
||||
public Builder transactionType(String transactionType) { |
||||
this.transactionType = transactionType; |
||||
return this; |
||||
} |
||||
|
||||
public Builder sellerSku(String sellerSku) { |
||||
this.sellerSku = sellerSku; |
||||
return this; |
||||
} |
||||
|
||||
public Builder localSku(String localSku) { |
||||
this.localSku = localSku; |
||||
return this; |
||||
} |
||||
|
||||
public Builder asin(String asin) { |
||||
this.asin = asin; |
||||
return this; |
||||
} |
||||
|
||||
public Builder amount(Money amount) { |
||||
this.amount = amount; |
||||
return this; |
||||
} |
||||
|
||||
public Builder quantity(int quantity) { |
||||
this.quantity = quantity; |
||||
return this; |
||||
} |
||||
|
||||
public Builder processingStatus(String processingStatus) { |
||||
this.processingStatus = processingStatus; |
||||
return this; |
||||
} |
||||
|
||||
public Builder fundTransferStatus(String fundTransferStatus) { |
||||
this.fundTransferStatus = fundTransferStatus; |
||||
return this; |
||||
} |
||||
|
||||
public Builder settlementId(String settlementId) { |
||||
this.settlementId = settlementId; |
||||
return this; |
||||
} |
||||
|
||||
public Builder postedAt(PlatformTime postedAt) { |
||||
this.postedAt = postedAt; |
||||
return this; |
||||
} |
||||
|
||||
public Builder marketplaceName(String marketplaceName) { |
||||
this.marketplaceName = marketplaceName; |
||||
return this; |
||||
} |
||||
|
||||
public SettlementTransaction build() { |
||||
return new SettlementTransaction(this); |
||||
} |
||||
} |
||||
} |
||||
@ -1,42 +1,50 @@
@@ -1,42 +1,50 @@
|
||||
package common.ecommerce.lingxing.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; |
||||
import common.ecommerce.model.product.ProductQuery; |
||||
import common.ecommerce.model.support.Page; |
||||
|
||||
import java.util.List; |
||||
import common.ecommerce.lingxing.platform.order.LingxingOrderSource; |
||||
import common.ecommerce.lingxing.platform.product.LingxingProductSource; |
||||
import common.ecommerce.lingxing.platform.settlement.LingxingSettlementSource; |
||||
import common.ecommerce.lingxing.platform.shop.LingxingShopSource; |
||||
|
||||
/** |
||||
* 领星子平台的<b>能力分派入口</b>:只做组合,不写业务逻辑。 |
||||
* |
||||
* <p>各能力由独立的 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}。</p> |
||||
* |
||||
* <p>这样单个平台类不会随能力增长而膨胀,能力之间也互不干扰。</p> |
||||
* <p>各能力由独立的 Source 实现({@link LingxingOrderSource}、 |
||||
* {@link LingxingProductSource}、{@link LingxingShopSource}、 |
||||
* {@link LingxingSettlementSource})提供,子平台不支持的 Source 传 {@code null} 即可, |
||||
* 调用时统一抛 {@code UnsupportedCapabilityException}。</p> |
||||
*/ |
||||
public interface LingxingSubPlatform { |
||||
|
||||
/** 对应的业务平台,用于注册表与日志。 */ |
||||
common.ecommerce.model.Platform platform(); |
||||
|
||||
/** 订单能力;不支持时为 null。 */ |
||||
default common.ecommerce.lingxing.platform.order.LingxingOrderSource orderSource() { |
||||
/** 订单能力(领星标准订单接口);不支持时为 null。 */ |
||||
default LingxingOrderSource orderSource() { |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 亚马逊 FBM(自发货)路由订单能力;不支持时为 null。 |
||||
* |
||||
* <p>这套接口的主键是领星系统单号 {@code order_number},与 |
||||
* {@link #orderSource()} 的平台订单号不同,因此单独成一路,不混用。</p> |
||||
*/ |
||||
default LingxingOrderSource fbmOrderSource() { |
||||
return null; |
||||
} |
||||
|
||||
/** 商品能力;不支持时为 null。 */ |
||||
default common.ecommerce.lingxing.platform.product.LingxingProductSource productSource() { |
||||
default LingxingProductSource productSource() { |
||||
return null; |
||||
} |
||||
|
||||
/** 店铺能力;不支持时为 null。 */ |
||||
default common.ecommerce.lingxing.platform.shop.LingxingShopSource shopSource() { |
||||
default LingxingShopSource shopSource() { |
||||
return null; |
||||
} |
||||
|
||||
/** 结算 / 账单能力;不支持时为 null。 */ |
||||
default LingxingSettlementSource settlementSource() { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
@ -0,0 +1,42 @@
@@ -0,0 +1,42 @@
|
||||
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.amazon.vc.order.VcOrderSource; |
||||
import common.ecommerce.lingxing.platform.amazon.vc.shop.VcShopSource; |
||||
import common.ecommerce.lingxing.platform.order.LingxingOrderSource; |
||||
import common.ecommerce.lingxing.platform.shop.LingxingShopSource; |
||||
import common.ecommerce.model.Platform; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 VC 子平台:店铺与订单,只做能力组合。 |
||||
* |
||||
* <p>VC 目前没有商品与结算接口;能力由 Source 是否装配决定, |
||||
* 未装配的能力不会被声明。</p> |
||||
*/ |
||||
public final class AmazonVcSubPlatform implements LingxingSubPlatform { |
||||
|
||||
private final LingxingOrderSource orders; |
||||
private final LingxingShopSource shops; |
||||
|
||||
public AmazonVcSubPlatform(HttpTransport transport, JacksonJsonCodec json) { |
||||
this.orders = new VcOrderSource(); |
||||
this.shops = new VcShopSource(); |
||||
} |
||||
|
||||
@Override |
||||
public Platform platform() { |
||||
return Platform.AMAZON_VC; |
||||
} |
||||
|
||||
@Override |
||||
public LingxingOrderSource orderSource() { |
||||
return orders; |
||||
} |
||||
|
||||
@Override |
||||
public LingxingShopSource shopSource() { |
||||
return shops; |
||||
} |
||||
} |
||||
@ -0,0 +1,298 @@
@@ -0,0 +1,298 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.order; |
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
||||
import common.ecommerce.error.ChannelException; |
||||
import common.ecommerce.error.ValidationException; |
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.LingxingConstants; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.mapper.LingxingOrderMapper; |
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.lingxing.platform.amazon.dto.FbmOrderDetailDto; |
||||
import common.ecommerce.lingxing.platform.amazon.dto.FbmOrderDto; |
||||
import common.ecommerce.lingxing.platform.amazon.dto.FbmOrderItemDto; |
||||
import common.ecommerce.lingxing.platform.order.LingxingOrderSource; |
||||
import common.ecommerce.model.ChannelProvider; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.order.OrderLine; |
||||
import common.ecommerce.model.order.SalesOrder; |
||||
import common.ecommerce.model.order.SalesOrderQuery; |
||||
import common.ecommerce.model.support.Money; |
||||
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.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; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 FBM(自发货)路由订单。 |
||||
* |
||||
* <p>对应领星 ERP「销售 > 订单管理」,与 {@link AmazonOrderSource} |
||||
* ({@code /erp/sc/data/mws/orders})是两个不同视角:</p> |
||||
* <ul> |
||||
* <li>本类的主键是<b>领星系统单号</b> {@code order_number},详情接口也按它查; |
||||
* 平台订单号在 {@code platform_list} 里</li> |
||||
* <li>{@link AmazonOrderSource} 的主键是亚马逊订单号,字段更全(币种、金额、UTC 时间)</li> |
||||
* </ul> |
||||
* |
||||
* <p>两者不要混用同一个 canonicalKey 存储:需要跨渠道去重时用 |
||||
* {@link AmazonOrderSource},需要按领星系统单号查 FBM 作业数据时用本类。</p> |
||||
* |
||||
* <p><b>时区待核对</b>:文档没有标注 {@code purchase_time} 的时区, |
||||
* 这里按领星 ERP 系统时间(北京时间)解析。首次真实联调时应打印报文确认。</p> |
||||
*/ |
||||
public final class AmazonFbmOrderSource implements LingxingOrderSource { |
||||
|
||||
/** 接口时间格式。 */ |
||||
private static final DateTimeFormatter DATE_TIME = |
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
||||
|
||||
/** 文档未注明时区,按 ERP 系统时间(北京时间)解释,待真实报文核对。 */ |
||||
private static final ZoneId SYSTEM_ZONE = PlatformTime.BEIJING_ZONE; |
||||
|
||||
private static final String OP_PULL = "salesOrders.pull.fbm"; |
||||
private static final String OP_GET = "salesOrders.get.fbm"; |
||||
|
||||
@Override |
||||
public Page<SalesOrder> orders(LingxingCall call, SalesOrderQuery query) { |
||||
ChannelAccount account = call.ctx().account(); |
||||
ZoneId zone = ZoneId.of(account.marketplaceZone()); |
||||
String sid = requireSid(account, call); |
||||
|
||||
int length = Math.min(query.pageSize() <= 0 ? 100 : query.pageSize(), |
||||
LingxingConstants.FBM_MAX_PAGE_SIZE); |
||||
int page = query.cursor() == null ? 1 : Integer.parseInt(query.cursor()); |
||||
|
||||
Instant from = query.updatedAfter(); |
||||
Instant to = query.updatedBefore() != null ? query.updatedBefore() : Instant.now(call.ctx().clock()); |
||||
|
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("sid", sid); |
||||
body.put("page", page); |
||||
body.put("length", length); |
||||
body.put("start_time", systemTime(from)); |
||||
body.put("end_time", systemTime(to)); |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.ORDER_LIST_FBM_AMAZON_SC, body, OP_PULL); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP_PULL); |
||||
|
||||
List<FbmOrderDto> dtos = call.json().listAt(envelope.body(), "data", FbmOrderDto.class); |
||||
List<SalesOrder> orders = new ArrayList<SalesOrder>(dtos.size()); |
||||
for (FbmOrderDto dto : dtos) { |
||||
try { |
||||
orders.add(toSalesOrder(dto, account, zone)); |
||||
} catch (RuntimeException e) { |
||||
throw mappingFailure(e, envelope, call, OP_PULL); |
||||
} |
||||
} |
||||
|
||||
// page 从 1 开始;满页才可能有下一页,total 存在时再用它兜底
|
||||
int nextPage = page + 1; |
||||
boolean hasMore = dtos.size() >= length |
||||
&& (envelope.total() < 0 || (long) page * length < envelope.total()); |
||||
return Page.of(orders, hasMore ? String.valueOf(nextPage) : null); |
||||
} |
||||
|
||||
@Override |
||||
public SalesOrder order(LingxingCall call, String channelOrderId) { |
||||
ChannelAccount account = call.ctx().account(); |
||||
ZoneId zone = ZoneId.of(account.marketplaceZone()); |
||||
|
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
// 详情接口按领星系统单号查,不是平台订单号
|
||||
body.put("order_number", channelOrderId); |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.ORDER_DETAIL_FBM_AMAZON_SC, body, OP_GET); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP_GET); |
||||
|
||||
// data 是对象,不是数组
|
||||
JsonNode data = call.json().nodeAt(envelope.body(), "data"); |
||||
if (data == null || data.isNull()) { |
||||
throw ChannelException.builder() |
||||
.message("lingxing fbm order not found: " + channelOrderId) |
||||
.operation(OP_GET) |
||||
.provider(ChannelProvider.LINGXING.name()) |
||||
.omsStoreId(call.ctx().omsStoreId()) |
||||
.requestId(call.ctx().requestId()) |
||||
.platformCode(channelOrderId) |
||||
.retryable(false) |
||||
.rawResponse(envelope.body()) |
||||
.build(); |
||||
} |
||||
|
||||
FbmOrderDetailDto dto = call.json().mapper().convertValue(data, FbmOrderDetailDto.class); |
||||
try { |
||||
return toSalesOrder(dto, account, zone); |
||||
} catch (RuntimeException e) { |
||||
throw mappingFailure(e, envelope, call, OP_GET); |
||||
} |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- mapping
|
||||
|
||||
/** 列表条目:没有商品行与金额,商品明细要再调详情。 */ |
||||
private SalesOrder toSalesOrder(FbmOrderDto dto, ChannelAccount account, ZoneId zone) { |
||||
String orderNumber = requireOrderNumber(dto.getOrderNumber()); |
||||
Instant occur = systemTime(dto.getPurchaseTime()); |
||||
|
||||
return SalesOrder.builder() |
||||
.omsStoreId(account.omsStoreId()) |
||||
.channelOrderId(orderNumber) |
||||
.status(dto.getStatus()) |
||||
// FBM 列表不返回买家姓名;国家有
|
||||
.buyerName(null) |
||||
.shipToCountryCode(trimToNull(dto.getCountryCode())) |
||||
.lines(Collections.<OrderLine>emptyList()) |
||||
// 列表接口不返回订单金额
|
||||
.totalAmount(null) |
||||
.occurredAt(requireOccurredAt(occur, orderNumber, zone)) |
||||
.sourceProvider(ChannelProvider.LINGXING) |
||||
.sourceAccountId(null) |
||||
.build(); |
||||
} |
||||
|
||||
/** 详情条目:带商品行与金额。 */ |
||||
private SalesOrder toSalesOrder(FbmOrderDetailDto dto, ChannelAccount account, ZoneId zone) { |
||||
String orderNumber = requireOrderNumber(dto.getOrderNumber()); |
||||
Instant occur = systemTime(dto.getPurchaseTime()); |
||||
|
||||
List<FbmOrderItemDto> items = safe(dto.getOrderItem()); |
||||
List<OrderLine> lines = new ArrayList<OrderLine>(items.size()); |
||||
String firstCurrency = null; |
||||
for (int i = 0; i < items.size(); i++) { |
||||
FbmOrderItemDto item = items.get(i); |
||||
String currency = trimToNull(item.getCurrencyCode()); |
||||
if (firstCurrency == null) { |
||||
firstCurrency = currency; |
||||
} |
||||
String sku = firstNonBlank(item.getMsku(), item.getSku(), item.getPlatformOrderId()); |
||||
lines.add(OrderLine.builder() |
||||
.lineId(firstNonBlank(item.getOrderItemNo(), item.getPlatformOrderId(), |
||||
item.getSku(), "line-" + i)) |
||||
.platformSku(sku == null ? "" : sku) |
||||
.quantity(item.getQuality() == null ? 0 : item.getQuality()) |
||||
.unitPrice(moneyOrNull(item.getItemUnitPrice(), currency)) |
||||
.build()); |
||||
} |
||||
|
||||
// 订单总金额没有独立币种,按商品行币种解释;没有商品行时用账户币种
|
||||
String totalCurrency = firstNonBlank(firstCurrency, account.currency()); |
||||
|
||||
return SalesOrder.builder() |
||||
.omsStoreId(account.omsStoreId()) |
||||
.channelOrderId(orderNumber) |
||||
.status(dto.getOrderStatus()) |
||||
.buyerName(null) |
||||
.shipToCountryCode(null) |
||||
.lines(lines) |
||||
.totalAmount(moneyOrNull(dto.getOrderPriceAmount(), totalCurrency)) |
||||
.occurredAt(requireOccurredAt(occur, orderNumber, zone)) |
||||
.sourceProvider(ChannelProvider.LINGXING) |
||||
.sourceAccountId(null) |
||||
.build(); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- internals
|
||||
|
||||
/** 文档没写时区,按 {@link #SYSTEM_ZONE} 解释;缺失或格式不对返回 null。 */ |
||||
private static Instant systemTime(String value) { |
||||
String text = trimToNull(value); |
||||
if (text == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return LocalDateTime.parse(text, DATE_TIME).atZone(SYSTEM_ZONE).toInstant(); |
||||
} catch (DateTimeParseException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
private static String systemTime(Instant instant) { |
||||
return DATE_TIME.format(LocalDateTime.ofInstant(instant, SYSTEM_ZONE)); |
||||
} |
||||
|
||||
private static PlatformTime requireOccurredAt(Instant occur, String orderNumber, ZoneId zone) { |
||||
if (occur == null) { |
||||
throw new IllegalArgumentException( |
||||
"lingxing fbm order has no parseable purchase_time: " + orderNumber); |
||||
} |
||||
return PlatformTime.of(occur, zone); |
||||
} |
||||
|
||||
private static String requireOrderNumber(String value) { |
||||
String orderNumber = trimToNull(value); |
||||
if (orderNumber == null) { |
||||
throw new IllegalArgumentException("lingxing fbm order has no order_number"); |
||||
} |
||||
return orderNumber; |
||||
} |
||||
|
||||
/** FBM 列表必填 sid,且是领星店铺 id;缺失时直接拒绝,不发注定失败的请求。 */ |
||||
private static String requireSid(ChannelAccount account, LingxingCall call) { |
||||
String sid = trimToNull(account.externalStoreId()); |
||||
if (sid != null) { |
||||
return sid; |
||||
} |
||||
throw new ValidationException(ChannelException.builder() |
||||
.message("lingxing fbm order query requires externalStoreId (sid)") |
||||
.operation(OP_PULL) |
||||
.provider(ChannelProvider.LINGXING.name()) |
||||
.omsStoreId(call.ctx().omsStoreId()) |
||||
.requestId(call.ctx().requestId()) |
||||
.retryable(false)); |
||||
} |
||||
|
||||
private static ValidationException mappingFailure(RuntimeException e, LingxingEnvelope envelope, |
||||
LingxingCall call, String operation) { |
||||
return new ValidationException(ChannelException.builder() |
||||
.message("lingxing fbm order mapping failed: " + e.getMessage()) |
||||
.operation(operation) |
||||
.provider(ChannelProvider.LINGXING.name()) |
||||
.omsStoreId(call.ctx().omsStoreId()) |
||||
.requestId(call.ctx().requestId()) |
||||
.rawResponse(envelope.body()), e); |
||||
} |
||||
|
||||
private static Money moneyOrNull(String value, String currency) { |
||||
String cleaned = LingxingOrderMapper.cleanAmount(value); |
||||
if (cleaned == null || trimToNull(currency) == null) { |
||||
return null; |
||||
} |
||||
return Money.of(cleaned, currency.trim().toUpperCase()); |
||||
} |
||||
|
||||
private static <T> List<T> safe(List<T> in) { |
||||
return in == null ? Collections.<T>emptyList() : in; |
||||
} |
||||
|
||||
private static String firstNonBlank(String... values) { |
||||
for (String value : values) { |
||||
String trimmed = trimToNull(value); |
||||
if (trimmed != null) { |
||||
return trimmed; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static String trimToNull(String value) { |
||||
if (value == null) { |
||||
return null; |
||||
} |
||||
String trimmed = value.trim(); |
||||
return trimmed.isEmpty() ? null : trimmed; |
||||
} |
||||
} |
||||
@ -0,0 +1,370 @@
@@ -0,0 +1,370 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.order; |
||||
|
||||
import common.ecommerce.error.ChannelException; |
||||
import common.ecommerce.error.ValidationException; |
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.LingxingConstants; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.mapper.LingxingOrderMapper; |
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.lingxing.platform.amazon.dto.AmazonOrderDetailDto; |
||||
import common.ecommerce.lingxing.platform.amazon.dto.AmazonOrderDetailItemDto; |
||||
import common.ecommerce.lingxing.platform.amazon.dto.AmazonOrderDto; |
||||
import common.ecommerce.lingxing.platform.amazon.dto.AmazonOrderItemDto; |
||||
import common.ecommerce.lingxing.platform.order.LingxingOrderSource; |
||||
import common.ecommerce.model.ChannelProvider; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.order.OrderLine; |
||||
import common.ecommerce.model.order.SalesOrder; |
||||
import common.ecommerce.model.order.SalesOrderQuery; |
||||
import common.ecommerce.model.support.Money; |
||||
import common.ecommerce.model.support.Page; |
||||
import common.ecommerce.model.support.PlatformTime; |
||||
|
||||
import java.time.Duration; |
||||
import java.time.Instant; |
||||
import java.time.LocalDateTime; |
||||
import java.time.OffsetDateTime; |
||||
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 订单。 |
||||
* |
||||
* <p>列表 {@link LingxingApi#ORDER_LIST_AMAZON_SC}:POST,{@code offset}/{@code length} |
||||
* 分页(上限 5000),查询窗口不超过 1 年,{@code total} 在<b>顶层</b>。</p> |
||||
* |
||||
* <p>详情 {@link LingxingApi#ORDER_DETAIL_AMAZON_SC}:只有详情返回单价/税额/折扣, |
||||
* 列表接口没有这些字段。</p> |
||||
* |
||||
* <p>与多平台订单的区别:状态是字符串枚举、时间是字符串、{@code data} 直接是数组。 |
||||
* 因此不复用 {@link LingxingOrderMapper},只复用它的金额清洗规则。</p> |
||||
*/ |
||||
public final class AmazonOrderSource implements LingxingOrderSource { |
||||
|
||||
/** 亚马逊接口时间参数格式:{@code yyyy-MM-dd HH:mm:ss}(UTC)。 */ |
||||
private static final DateTimeFormatter DATE_TIME = |
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
||||
|
||||
/** {@code date_type=3}:平台更新时间(UTC),与 updatedAfter/updatedBefore 语义一致。 */ |
||||
private static final int DATE_TYPE_PLATFORM_UPDATE_UTC = 3; |
||||
|
||||
private static final String OP_PULL = "salesOrders.pull.amazon"; |
||||
private static final String OP_GET = "salesOrders.get.amazon"; |
||||
|
||||
@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()); |
||||
// 亚马逊查询窗口 ≤1 年;超出时截断,由 SyncEngine 分段推进
|
||||
if (Duration.between(from, to).toDays() > LingxingConstants.AMAZON_MAX_QUERY_DAYS) { |
||||
from = to.minus(Duration.ofDays(LingxingConstants.AMAZON_MAX_QUERY_DAYS)); |
||||
} |
||||
|
||||
int length = Math.min(query.pageSize() <= 0 ? 100 : query.pageSize(), |
||||
LingxingConstants.AMAZON_MAX_PAGE_SIZE); |
||||
int offset = query.cursor() == null ? 0 : Integer.parseInt(query.cursor()); |
||||
|
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("start_date", utc(from)); |
||||
body.put("end_date", utc(to)); |
||||
body.put("date_type", DATE_TYPE_PLATFORM_UPDATE_UTC); |
||||
body.put("offset", offset); |
||||
body.put("length", length); |
||||
Long sid = parseSid(account.externalStoreId()); |
||||
if (sid != null) { |
||||
body.put("sid", sid); |
||||
} |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.ORDER_LIST_AMAZON_SC, body, OP_PULL); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP_PULL); |
||||
|
||||
List<AmazonOrderDto> dtos = call.json().listAt(envelope.body(), "data", AmazonOrderDto.class); |
||||
List<SalesOrder> orders = new ArrayList<SalesOrder>(dtos.size()); |
||||
for (AmazonOrderDto dto : dtos) { |
||||
try { |
||||
orders.add(toSalesOrder(dto, account, zone)); |
||||
} catch (RuntimeException e) { |
||||
throw mappingFailure(e, envelope, call, OP_PULL); |
||||
} |
||||
} |
||||
|
||||
// 满页才可能有下一页;total 在顶层,存在时再用它兜底
|
||||
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); |
||||
} |
||||
|
||||
@Override |
||||
public SalesOrder order(LingxingCall call, String channelOrderId) { |
||||
ChannelAccount account = call.ctx().account(); |
||||
ZoneId zone = ZoneId.of(account.marketplaceZone()); |
||||
|
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("order_id", channelOrderId); |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.ORDER_DETAIL_AMAZON_SC, body, OP_GET); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP_GET); |
||||
|
||||
List<AmazonOrderDetailDto> dtos = |
||||
call.json().listAt(envelope.body(), "data", AmazonOrderDetailDto.class); |
||||
if (dtos.isEmpty()) { |
||||
throw ChannelException.builder() |
||||
.message("lingxing amazon order not found: " + channelOrderId) |
||||
.operation(OP_GET) |
||||
.provider(ChannelProvider.LINGXING.name()) |
||||
.omsStoreId(call.ctx().omsStoreId()) |
||||
.requestId(call.ctx().requestId()) |
||||
.platformCode(channelOrderId) |
||||
.retryable(false) |
||||
.rawResponse(envelope.body()) |
||||
.build(); |
||||
} |
||||
|
||||
try { |
||||
return toSalesOrder(dtos.get(0), account, zone); |
||||
} catch (RuntimeException e) { |
||||
throw mappingFailure(e, envelope, call, OP_GET); |
||||
} |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- mapping
|
||||
|
||||
/** 列表条目:只有 SKU / ASIN / 数量,金额与税额需要再调详情。 */ |
||||
private SalesOrder toSalesOrder(AmazonOrderDto dto, ChannelAccount account, ZoneId zone) { |
||||
String channelOrderId = requireOrderId(dto.getAmazonOrderId()); |
||||
String currency = firstNonBlank(dto.getOrderTotalCurrencyCode(), account.currency()); |
||||
|
||||
List<OrderLine> lines = new ArrayList<OrderLine>(); |
||||
List<AmazonOrderItemDto> items = safe(dto.getItemList()); |
||||
for (int i = 0; i < items.size(); i++) { |
||||
AmazonOrderItemDto item = items.get(i); |
||||
String sku = firstNonBlank(item.getSellerSku(), item.getLocalSku(), item.getAsin()); |
||||
lines.add(OrderLine.builder() |
||||
.lineId(firstNonBlank(item.getSellerSku(), item.getAsin(), |
||||
item.getLocalSku(), "line-" + i)) |
||||
.platformSku(sku == null ? "" : sku) |
||||
.quantity(parseInt(item.getQuantityOrdered())) |
||||
.build()); |
||||
} |
||||
|
||||
Instant occur = parsePurchase(dto.getPurchaseDate(), dto.getPurchaseDateUtc(), |
||||
dto.getPurchaseDateLocal(), zone); |
||||
|
||||
return SalesOrder.builder() |
||||
.omsStoreId(account.omsStoreId()) |
||||
.channelOrderId(channelOrderId) |
||||
.status(dto.getOrderStatus()) |
||||
// 亚马逊订单列表不返回买家姓名与收货国家
|
||||
.buyerName(null) |
||||
.shipToCountryCode(null) |
||||
.lines(lines) |
||||
.totalAmount(moneyOrNull(dto.getOrderTotalAmount(), currency)) |
||||
.occurredAt(requireOccurredAt(occur, channelOrderId, zone)) |
||||
.sourceProvider(ChannelProvider.LINGXING) |
||||
.sourceAccountId(dto.getSid() == null ? null : String.valueOf(dto.getSid())) |
||||
.build(); |
||||
} |
||||
|
||||
/** 详情条目:带完整金额。 */ |
||||
private SalesOrder toSalesOrder(AmazonOrderDetailDto dto, ChannelAccount account, ZoneId zone) { |
||||
String channelOrderId = requireOrderId(dto.getAmazonOrderId()); |
||||
String currency = firstNonBlank(dto.getCurrency(), account.currency()); |
||||
|
||||
List<OrderLine> lines = new ArrayList<OrderLine>(); |
||||
List<AmazonOrderDetailItemDto> items = safe(dto.getItemList()); |
||||
for (int i = 0; i < items.size(); i++) { |
||||
AmazonOrderDetailItemDto item = items.get(i); |
||||
String sku = firstNonBlank(item.getSellerSku(), item.getSku(), item.getAsin()); |
||||
lines.add(OrderLine.builder() |
||||
// order_item_id 亚马逊可能变更,不能当稳定主键;仅作展示用
|
||||
.lineId(firstNonBlank(item.getOrderItemId(), item.getAsin(), |
||||
item.getSellerSku(), "line-" + i)) |
||||
.platformSku(sku == null ? "" : sku) |
||||
.quantity(item.getQuantityOrdered() == null ? 0 : item.getQuantityOrdered()) |
||||
.unitPrice(moneyOrNull(item.getUnitPriceAmount(), currency)) |
||||
.tax(moneyOrNull(item.getItemTaxAmount(), currency)) |
||||
.discount(moneyOrNull(item.getPromotionDiscountAmount(), currency)) |
||||
.build()); |
||||
} |
||||
|
||||
// 详情没有原始 ISO 的 purchase_date,优先用 UTC 的 local_utc 字段
|
||||
Instant occur = parsePurchase(null, dto.getPurchaseDateLocalUtc(), |
||||
dto.getPurchaseDateLocal(), zone); |
||||
|
||||
return SalesOrder.builder() |
||||
.omsStoreId(account.omsStoreId()) |
||||
.channelOrderId(channelOrderId) |
||||
.status(dto.getOrderStatus()) |
||||
.buyerName(null) |
||||
.shipToCountryCode(null) |
||||
.lines(lines) |
||||
.totalAmount(moneyOrNull(dto.getOrderTotalAmount(), currency)) |
||||
.occurredAt(requireOccurredAt(occur, channelOrderId, zone)) |
||||
.sourceProvider(ChannelProvider.LINGXING) |
||||
.sourceAccountId(dto.getSid() == null ? null : String.valueOf(dto.getSid())) |
||||
.build(); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- internals
|
||||
|
||||
/** |
||||
* 订购时间按精度回落:亚马逊原始 ISO(带 Z)→ UTC 字符串 → 站点时间字符串。 |
||||
*/ |
||||
static Instant parsePurchase(String iso, String utc, String local, ZoneId marketplaceZone) { |
||||
Instant instant = parseIso(iso); |
||||
if (instant != null) { |
||||
return instant; |
||||
} |
||||
instant = parseUtc(utc); |
||||
if (instant != null) { |
||||
return instant; |
||||
} |
||||
return parseLocal(local, marketplaceZone); |
||||
} |
||||
|
||||
static Instant parseIso(String value) { |
||||
String text = trimToNull(value); |
||||
if (text == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return Instant.parse(text); |
||||
} catch (DateTimeParseException ignored) { |
||||
// 回落 OffsetDateTime,兼容 +00:00 形式
|
||||
} |
||||
try { |
||||
return OffsetDateTime.parse(text).toInstant(); |
||||
} catch (DateTimeParseException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
static Instant parseUtc(String value) { |
||||
String text = trimToNull(value); |
||||
if (text == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return LocalDateTime.parse(text, DATE_TIME).toInstant(ZoneOffset.UTC); |
||||
} catch (DateTimeParseException ignored) { |
||||
// 兼容 ISO 形式
|
||||
return parseIso(text); |
||||
} |
||||
} |
||||
|
||||
static Instant parseLocal(String value, ZoneId marketplaceZone) { |
||||
String text = trimToNull(value); |
||||
if (text == null || marketplaceZone == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return LocalDateTime.parse(text, DATE_TIME).atZone(marketplaceZone).toInstant(); |
||||
} catch (DateTimeParseException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** {@code occurredAt} 是 SalesOrder 的必填项,缺失时带单号报错,不伪造 1970。 */ |
||||
private static PlatformTime requireOccurredAt(Instant occur, String channelOrderId, ZoneId zone) { |
||||
if (occur == null) { |
||||
throw new IllegalArgumentException( |
||||
"lingxing amazon order has no purchase time: " + channelOrderId); |
||||
} |
||||
return PlatformTime.of(occur, zone); |
||||
} |
||||
|
||||
private static String requireOrderId(String value) { |
||||
String id = trimToNull(value); |
||||
if (id == null) { |
||||
throw new IllegalArgumentException("lingxing amazon order has no amazon_order_id"); |
||||
} |
||||
return id; |
||||
} |
||||
|
||||
private static ValidationException mappingFailure(RuntimeException e, LingxingEnvelope envelope, |
||||
LingxingCall call, String operation) { |
||||
return new ValidationException(ChannelException.builder() |
||||
.message("lingxing amazon order mapping failed: " + e.getMessage()) |
||||
.operation(operation) |
||||
.provider(ChannelProvider.LINGXING.name()) |
||||
.omsStoreId(call.ctx().omsStoreId()) |
||||
.requestId(call.ctx().requestId()) |
||||
.rawResponse(envelope.body()), e); |
||||
} |
||||
|
||||
/** 金额为 0 时领星可能不返回币种,此时无法构造 Money,保留 null 而不是造零。 */ |
||||
private static Money moneyOrNull(String value, String currency) { |
||||
String cleaned = LingxingOrderMapper.cleanAmount(value); |
||||
if (cleaned == null || currency == null) { |
||||
return null; |
||||
} |
||||
return Money.of(cleaned, currency.toUpperCase()); |
||||
} |
||||
|
||||
private static int parseInt(String value) { |
||||
String text = trimToNull(value); |
||||
if (text == null) { |
||||
return 0; |
||||
} |
||||
try { |
||||
return Integer.parseInt(text); |
||||
} catch (NumberFormatException e) { |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
/** 亚马逊接口的 {@code sid} 是数字;配置里填了非数字时退化为不按店铺过滤。 */ |
||||
private static Long parseSid(String externalStoreId) { |
||||
String value = trimToNull(externalStoreId); |
||||
if (value == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return Long.valueOf(value); |
||||
} catch (NumberFormatException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
private static String utc(Instant instant) { |
||||
return DATE_TIME.format(LocalDateTime.ofInstant(instant, ZoneOffset.UTC)); |
||||
} |
||||
|
||||
private static <T> List<T> safe(List<T> in) { |
||||
return in == null ? Collections.<T>emptyList() : in; |
||||
} |
||||
|
||||
private static String firstNonBlank(String... values) { |
||||
for (String value : values) { |
||||
String trimmed = trimToNull(value); |
||||
if (trimmed != null) { |
||||
return trimmed; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static String trimToNull(String value) { |
||||
if (value == null) { |
||||
return null; |
||||
} |
||||
String trimmed = value.trim(); |
||||
return trimmed.isEmpty() ? null : trimmed; |
||||
} |
||||
} |
||||
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
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; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 FBM 订单列表条目 |
||||
* ({@code /erp/sc/routing/order/Order/getOrderList} 的 {@code data[]})。 |
||||
* |
||||
* <p>这是领星 ERP「销售 > 订单管理」的自发货订单视图,主键是系统单号 |
||||
* {@code order_number},不是亚马逊订单号;平台单号在 {@code platform_list} 里, |
||||
* 当前不解析(避免数组元素类型不一致导致整页解析失败)。</p> |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class FbmOrderDto { |
||||
|
||||
/** 领星系统单号,详情接口的入参。 */ |
||||
@JsonProperty("order_number") |
||||
private String orderNumber; |
||||
|
||||
/** 订单状态(中文原值,如 待发货 / 已发货 / 已取消)。 */ |
||||
@JsonProperty("status") |
||||
private String status; |
||||
|
||||
/** 订单类型,如 线上订单 / 手工订单。 */ |
||||
@JsonProperty("order_from") |
||||
private String orderFrom; |
||||
|
||||
/** 目的国代码。 */ |
||||
@JsonProperty("country_code") |
||||
private String countryCode; |
||||
|
||||
/** 订购时间。 */ |
||||
@JsonProperty("purchase_time") |
||||
private String purchaseTime; |
||||
|
||||
/** 付款时间。 */ |
||||
@JsonProperty("payment_time") |
||||
private String paymentTime; |
||||
|
||||
@JsonProperty("logistics_type_id") |
||||
private String logisticsTypeId; |
||||
|
||||
@JsonProperty("logistics_provider_id") |
||||
private String logisticsProviderId; |
||||
|
||||
@JsonProperty("logistics_type_name") |
||||
private String logisticsTypeName; |
||||
|
||||
@JsonProperty("logistics_provider_name") |
||||
private String logisticsProviderName; |
||||
|
||||
/** 发货仓库 id。 */ |
||||
@JsonProperty("wid") |
||||
private Long wid; |
||||
|
||||
@JsonProperty("warehouse_name") |
||||
private String warehouseName; |
||||
|
||||
@JsonProperty("customer_comment") |
||||
private String customerComment; |
||||
} |
||||
@ -0,0 +1,55 @@
@@ -0,0 +1,55 @@
|
||||
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; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 FBM 订单详情的商品行({@code data.order_item[]})。 |
||||
* |
||||
* <p>字段名 {@code MSKU} 是全大写,Jackson 按注解原样匹配;数量字段文档写作 |
||||
* {@code quality}(不是 quantity),照文档保留。</p> |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class FbmOrderItemDto { |
||||
|
||||
/** 平台订单号。 */ |
||||
@JsonProperty("platform_order_id") |
||||
private String platformOrderId; |
||||
|
||||
/** 平台 MSKU。 */ |
||||
@JsonProperty("MSKU") |
||||
private String msku; |
||||
|
||||
/** 订单明细单号。 */ |
||||
@JsonProperty("order_item_no") |
||||
private String orderItemNo; |
||||
|
||||
@JsonProperty("pic_url") |
||||
private String picUrl; |
||||
|
||||
/** 领星本地 SKU。 */ |
||||
@JsonProperty("sku") |
||||
private String sku; |
||||
|
||||
@JsonProperty("product_name") |
||||
private String productName; |
||||
|
||||
/** 数量;文档字段名是 {@code quality}。 */ |
||||
@JsonProperty("quality") |
||||
private Integer quality; |
||||
|
||||
/** 单价。 */ |
||||
@JsonProperty("item_unit_price") |
||||
private String itemUnitPrice; |
||||
|
||||
/** 单价币种。 */ |
||||
@JsonProperty("currency_code") |
||||
private String currencyCode; |
||||
|
||||
@JsonProperty("customization") |
||||
private String customization; |
||||
} |
||||
@ -0,0 +1,259 @@
@@ -0,0 +1,259 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.settlement; |
||||
|
||||
import common.ecommerce.error.ChannelException; |
||||
import common.ecommerce.error.ValidationException; |
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.LingxingConstants; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.mapper.LingxingOrderMapper; |
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.lingxing.platform.amazon.dto.AmazonSettlementTransactionDto; |
||||
import common.ecommerce.lingxing.platform.settlement.LingxingSettlementSource; |
||||
import common.ecommerce.model.ChannelProvider; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.settlement.SettlementQuery; |
||||
import common.ecommerce.model.settlement.SettlementTransaction; |
||||
import common.ecommerce.model.support.Money; |
||||
import common.ecommerce.model.support.Page; |
||||
import common.ecommerce.model.support.PlatformTime; |
||||
|
||||
import java.time.Instant; |
||||
import java.time.LocalDate; |
||||
import java.time.LocalDateTime; |
||||
import java.time.OffsetDateTime; |
||||
import java.time.ZoneId; |
||||
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; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊结算中心交易明细。 |
||||
* |
||||
* <p>接口 {@link LingxingApi#SETTLEMENT_TRANSACTION_LIST}:POST,{@code offset}/{@code length} |
||||
* 分页(上限 10000),令牌桶 10 req/s。</p> |
||||
* |
||||
* <p>查询窗口规则(官方文档):结算时间 {@code startDate/endDate} 双闭区间、 |
||||
* 间隔不得超过 7 天;没有搜索值时,结算时间与修改时间二选一必填。 |
||||
* 修改时间按<b>北京时间</b>传。</p> |
||||
*/ |
||||
public final class AmazonSettlementSource implements LingxingSettlementSource { |
||||
|
||||
/** 结算日期格式。 */ |
||||
private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
||||
/** 修改时间格式(北京时间)。 */ |
||||
private static final DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
||||
|
||||
private static final String OP = "settlements.pull.amazon"; |
||||
|
||||
@Override |
||||
public Page<SettlementTransaction> transactions(LingxingCall call, SettlementQuery query) { |
||||
ChannelAccount account = call.ctx().account(); |
||||
ZoneId zone = ZoneId.of(account.marketplaceZone()); |
||||
|
||||
requireQueryWindow(query, call); |
||||
|
||||
int length = Math.min(query.pageSize() <= 0 ? 100 : query.pageSize(), |
||||
LingxingConstants.SETTLEMENT_MAX_PAGE_SIZE); |
||||
int offset = query.cursor() == null ? 0 : Integer.parseInt(query.cursor()); |
||||
|
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("offset", offset); |
||||
body.put("length", length); |
||||
|
||||
Long sid = parseSid(account.externalStoreId()); |
||||
if (sid != null) { |
||||
body.put("sids", Collections.singletonList(sid)); |
||||
} |
||||
|
||||
LocalDate settledStart = query.settledAfter(); |
||||
LocalDate settledEnd = query.settledBefore(); |
||||
if (settledStart != null && settledEnd != null) { |
||||
// 双闭区间,最长 7 天:起点早于 end-6 时截断
|
||||
LocalDate earliest = settledEnd.minusDays(LingxingConstants.SETTLEMENT_MAX_QUERY_DAYS - 1L); |
||||
if (settledStart.isBefore(earliest)) { |
||||
settledStart = earliest; |
||||
} |
||||
} |
||||
if (settledStart != null) { |
||||
body.put("startDate", DATE.format(settledStart)); |
||||
} |
||||
if (settledEnd != null) { |
||||
body.put("endDate", DATE.format(settledEnd)); |
||||
} |
||||
|
||||
// 请求字段明确是北京时间,不能用 UTC 或站点时区
|
||||
if (query.modifiedAfter() != null) { |
||||
body.put("gmtModifiedStart", DATE_TIME.format( |
||||
LocalDateTime.ofInstant(query.modifiedAfter(), PlatformTime.BEIJING_ZONE))); |
||||
} |
||||
if (query.modifiedBefore() != null) { |
||||
body.put("gmtModifiedEnd", DATE_TIME.format( |
||||
LocalDateTime.ofInstant(query.modifiedBefore(), PlatformTime.BEIJING_ZONE))); |
||||
} |
||||
|
||||
if (query.eventType() != null) { |
||||
body.put("eventType", query.eventType()); |
||||
} |
||||
if (query.transactionType() != null) { |
||||
body.put("type", query.transactionType()); |
||||
} |
||||
if (query.searchField() != null) { |
||||
body.put("searchField", query.searchField()); |
||||
} |
||||
if (!query.searchValues().isEmpty()) { |
||||
body.put("searchValue", query.searchValues()); |
||||
} |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.SETTLEMENT_TRANSACTION_LIST, body, OP); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP); |
||||
|
||||
List<AmazonSettlementTransactionDto> records = |
||||
call.json().listAt(envelope.body(), "data.records", AmazonSettlementTransactionDto.class); |
||||
List<SettlementTransaction> out = new ArrayList<SettlementTransaction>(records.size()); |
||||
for (AmazonSettlementTransactionDto dto : records) { |
||||
try { |
||||
out.add(toTransaction(dto, account, zone)); |
||||
} catch (RuntimeException e) { |
||||
throw new ValidationException(ChannelException.builder() |
||||
.message("lingxing settlement mapping failed: " + e.getMessage()) |
||||
.operation(OP) |
||||
.provider(ChannelProvider.LINGXING.name()) |
||||
.omsStoreId(call.ctx().omsStoreId()) |
||||
.requestId(call.ctx().requestId()) |
||||
.rawResponse(envelope.body()), e); |
||||
} |
||||
} |
||||
|
||||
int nextOffset = offset + records.size(); |
||||
boolean hasMore = records.size() >= length |
||||
&& (envelope.total() < 0 || nextOffset < envelope.total()); |
||||
return Page.of(out, hasMore ? String.valueOf(nextOffset) : null); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- mapping
|
||||
|
||||
private SettlementTransaction toTransaction(AmazonSettlementTransactionDto dto, |
||||
ChannelAccount account, ZoneId zone) { |
||||
String key = firstNonBlank(dto.getUniqueKey(), dto.getMd5(), dto.getId()); |
||||
if (key == null) { |
||||
throw new IllegalArgumentException( |
||||
"settlement record has neither uniqueKey, md5 nor id: " + dto.getAmazonOrderId()); |
||||
} |
||||
|
||||
return SettlementTransaction.builder() |
||||
.omsStoreId(account.omsStoreId()) |
||||
.transactionKey(key) |
||||
.sourceProvider(ChannelProvider.LINGXING) |
||||
.sourceAccountId(dto.getSid() == null ? null : String.valueOf(dto.getSid())) |
||||
.channelOrderId(trimToNull(dto.getAmazonOrderId())) |
||||
.storeName(trimToNull(dto.getStoreName())) |
||||
.countryCode(trimToNull(dto.getCountryCode())) |
||||
.fulfillment(trimToNull(dto.getFulfillment())) |
||||
.eventType(trimToNull(dto.getEventType())) |
||||
.transactionType(trimToNull(dto.getType())) |
||||
.sellerSku(trimToNull(dto.getSellerSku())) |
||||
.localSku(trimToNull(dto.getLocalSku())) |
||||
.asin(trimToNull(dto.getAsin())) |
||||
.amount(moneyOrNull(dto.getCurrencyAmount(), dto.getCurrencyCode())) |
||||
.quantity(dto.getQuantity() == null ? 0 : dto.getQuantity()) |
||||
.processingStatus(trimToNull(dto.getProcessingStatus())) |
||||
.fundTransferStatus(trimToNull(dto.getFundTransferStatus())) |
||||
.settlementId(firstNonBlank(dto.getSettlementId(), dto.getFid())) |
||||
.postedAt(postedAt(dto.getPostedDate(), dto.getPostedDateLocale(), zone)) |
||||
.marketplaceName(trimToNull(dto.getMarketplaceName())) |
||||
.build(); |
||||
} |
||||
|
||||
/** 结算时间优先用 0 时区的 postedDate,缺失时回落站点时间 postedDateLocale。 */ |
||||
static PlatformTime postedAt(String utc, String locale, ZoneId zone) { |
||||
Instant instant = parseInstant(utc); |
||||
if (instant == null) { |
||||
instant = parseInstant(locale); |
||||
} |
||||
return instant == null ? null : PlatformTime.of(instant, zone); |
||||
} |
||||
|
||||
private static Instant parseInstant(String value) { |
||||
String text = trimToNull(value); |
||||
if (text == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
// postedDate: 2024-06-06T08:29:00Z
|
||||
return Instant.parse(text); |
||||
} catch (DateTimeParseException ignored) { |
||||
// postedDateLocale: 2022-09-10T23:55:12+01:00
|
||||
} |
||||
try { |
||||
return OffsetDateTime.parse(text).toInstant(); |
||||
} catch (DateTimeParseException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
private static Money moneyOrNull(String amount, String currency) { |
||||
String cleaned = LingxingOrderMapper.cleanAmount(amount); |
||||
if (cleaned == null || trimToNull(currency) == null) { |
||||
return null; |
||||
} |
||||
return Money.of(cleaned, currency.trim().toUpperCase()); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- internals
|
||||
|
||||
/** |
||||
* 文档要求:没有搜索值时,「结算时间」与「修改时间」二选一必填。 |
||||
* 在发请求前显式校验,避免拿一个必然失败的请求去烧配额。 |
||||
*/ |
||||
private static void requireQueryWindow(SettlementQuery query, LingxingCall call) { |
||||
boolean hasSettled = query.settledAfter() != null || query.settledBefore() != null; |
||||
boolean hasModified = query.modifiedAfter() != null || query.modifiedBefore() != null; |
||||
if (hasSettled || hasModified || !query.searchValues().isEmpty()) { |
||||
return; |
||||
} |
||||
throw new ValidationException(ChannelException.builder() |
||||
.message("lingxing settlement query requires settled window, modified window or searchValues") |
||||
.operation(OP) |
||||
.provider(ChannelProvider.LINGXING.name()) |
||||
.omsStoreId(call.ctx().omsStoreId()) |
||||
.requestId(call.ctx().requestId()) |
||||
.retryable(false)); |
||||
} |
||||
|
||||
private static Long parseSid(String externalStoreId) { |
||||
String value = trimToNull(externalStoreId); |
||||
if (value == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return Long.valueOf(value); |
||||
} catch (NumberFormatException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
private static String firstNonBlank(String... values) { |
||||
for (String value : values) { |
||||
String trimmed = trimToNull(value); |
||||
if (trimmed != null) { |
||||
return trimmed; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static String trimToNull(String value) { |
||||
if (value == null) { |
||||
return null; |
||||
} |
||||
String trimmed = value.trim(); |
||||
return trimmed.isEmpty() ? null : trimmed; |
||||
} |
||||
} |
||||
@ -0,0 +1,102 @@
@@ -0,0 +1,102 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.vc.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 VC 订单详情的商品行(DF / PO 共用)。 |
||||
* |
||||
* <p>两套详情接口的字段高度重合,只有 {@code waiting_shipped_amount} 与 |
||||
* {@code to_ship_amount} 的区别,用同一个 DTO 接收,未知字段忽略。</p> |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class VcOrderDetailItemDto { |
||||
|
||||
@JsonProperty("sequence_number") |
||||
private String sequenceNumber; |
||||
|
||||
@JsonProperty("asin") |
||||
private String asin; |
||||
|
||||
@JsonProperty("asin_url") |
||||
private String asinUrl; |
||||
|
||||
/** 平台 MSKU。 */ |
||||
@JsonProperty("msku") |
||||
private String msku; |
||||
|
||||
@JsonProperty("item_name") |
||||
private String itemName; |
||||
|
||||
@JsonProperty("large_main_image_url") |
||||
private String largeMainImageUrl; |
||||
|
||||
@JsonProperty("medium_main_image_url") |
||||
private String mediumMainImageUrl; |
||||
|
||||
@JsonProperty("small_main_image_url") |
||||
private String smallMainImageUrl; |
||||
|
||||
@JsonProperty("pic_url") |
||||
private String picUrl; |
||||
|
||||
@JsonProperty("local_sku") |
||||
private String localSku; |
||||
|
||||
@JsonProperty("local_name") |
||||
private String localName; |
||||
|
||||
/** 采购量;文档类型是 string。 */ |
||||
@JsonProperty("purchase_amount") |
||||
private String purchaseAmount; |
||||
|
||||
@JsonProperty("shipped_amount") |
||||
private String shippedAmount; |
||||
|
||||
/** DF 的待发货量。 */ |
||||
@JsonProperty("waiting_shipped_amount") |
||||
private String waitingShippedAmount; |
||||
|
||||
/** PO 的待发货量。 */ |
||||
@JsonProperty("to_ship_amount") |
||||
private String toShipAmount; |
||||
|
||||
@JsonProperty("available_amount") |
||||
private Integer availableAmount; |
||||
|
||||
/** 单价。 */ |
||||
@JsonProperty("unit_price") |
||||
private String unitPrice; |
||||
|
||||
/** 成本价。 */ |
||||
@JsonProperty("net_price") |
||||
private String netPrice; |
||||
|
||||
@JsonProperty("net_price_currency_code") |
||||
private String netPriceCurrencyCode; |
||||
|
||||
@JsonProperty("tax_amount") |
||||
private String taxAmount; |
||||
|
||||
@JsonProperty("tax_amount_currency_code") |
||||
private String taxAmountCurrencyCode; |
||||
|
||||
@JsonProperty("tax_rate") |
||||
private String taxRate; |
||||
|
||||
@JsonProperty("tax_rate_percent") |
||||
private String taxRatePercent; |
||||
|
||||
@JsonProperty("deal_total_price") |
||||
private String dealTotalPrice; |
||||
|
||||
@JsonProperty("deal_unit_price") |
||||
private String dealUnitPrice; |
||||
|
||||
@JsonProperty("is_back_order_allowed") |
||||
private String isBackOrderAllowed; |
||||
} |
||||
@ -0,0 +1,102 @@
@@ -0,0 +1,102 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.vc.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 VC 订单详情【DF】 |
||||
* ({@code /basicOpen/platformOrder/vcOrderDf/detail} 的 {@code data})。 |
||||
* |
||||
* <p>{@code data} 是对象,不是数组。</p> |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class VcOrderDfDetailDto { |
||||
|
||||
@JsonProperty("vc_store_id") |
||||
private String vcStoreId; |
||||
|
||||
@JsonProperty("seller_name") |
||||
private String sellerName; |
||||
|
||||
@JsonProperty("local_po_number") |
||||
private String localPoNumber; |
||||
|
||||
@JsonProperty("purchase_order_number") |
||||
private String purchaseOrderNumber; |
||||
|
||||
/** 下单时间。 */ |
||||
@JsonProperty("purchase_order_date") |
||||
private String purchaseOrderDate; |
||||
|
||||
/** New / SHIPPED / ACCEPTED / CANCELED。 */ |
||||
@JsonProperty("purchase_order_state") |
||||
private String purchaseOrderState; |
||||
|
||||
/** 0 DF、1 PO。 */ |
||||
@JsonProperty("purchase_order_type") |
||||
private String purchaseOrderType; |
||||
|
||||
@JsonProperty("bill_to_party_id") |
||||
private String billToPartyId; |
||||
|
||||
@JsonProperty("ship_from_party_id") |
||||
private String shipFromPartyId; |
||||
|
||||
@JsonProperty("related_warehouse_id") |
||||
private String relatedWarehouseId; |
||||
|
||||
@JsonProperty("related_warehouse_name") |
||||
private String relatedWarehouseName; |
||||
|
||||
@JsonProperty("ship_method") |
||||
private String shipMethod; |
||||
|
||||
/** 要求发货时间。 */ |
||||
@JsonProperty("ship_window_time") |
||||
private String shipWindowTime; |
||||
|
||||
/** 承诺送达时间。 */ |
||||
@JsonProperty("promised_delivery_date") |
||||
private String promisedDeliveryDate; |
||||
|
||||
@JsonProperty("is_pslip_required") |
||||
private String isPslipRequired; |
||||
|
||||
@JsonProperty("is_gift") |
||||
private String isGift; |
||||
|
||||
@JsonProperty("is_scheduled_delivery_shipment") |
||||
private String isScheduledDeliveryShipment; |
||||
|
||||
@JsonProperty("is_priority_shipment") |
||||
private String isPriorityShipment; |
||||
|
||||
@JsonProperty("message_to_customer") |
||||
private String messageToCustomer; |
||||
|
||||
@JsonProperty("total_price") |
||||
private String totalPrice; |
||||
|
||||
@JsonProperty("currency_code") |
||||
private String currencyCode; |
||||
|
||||
@JsonProperty("currency_icon") |
||||
private String currencyIcon; |
||||
|
||||
/** 货物数量;文档类型是 string。 */ |
||||
@JsonProperty("item_amount") |
||||
private String itemAmount; |
||||
|
||||
@JsonProperty("remark") |
||||
private String remark; |
||||
|
||||
@JsonProperty("items") |
||||
private List<VcOrderDetailItemDto> items = new ArrayList<VcOrderDetailItemDto>(); |
||||
} |
||||
@ -0,0 +1,120 @@
@@ -0,0 +1,120 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.vc.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 VC 订单列表条目 |
||||
* ({@code /basicOpen/platformOrder/vcOrder/pageList} 的 {@code data[]})。 |
||||
* |
||||
* <p>列表已经带商品行与订单总额,可以直接映射成统一订单;详情接口补充 |
||||
* 发货窗口、仓库、收件方等信息。</p> |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class VcOrderDto { |
||||
|
||||
@JsonProperty("gmt_create") |
||||
private String gmtCreate; |
||||
|
||||
@JsonProperty("gmt_modified") |
||||
private String gmtModified; |
||||
|
||||
/** 领星订单 id。 */ |
||||
@JsonProperty("id") |
||||
private String id; |
||||
|
||||
/** 订单编号,平台单号,参与 canonicalKey。 */ |
||||
@JsonProperty("purchase_order_number") |
||||
private String purchaseOrderNumber; |
||||
|
||||
/** 客户订单号(DF 类型)。 */ |
||||
@JsonProperty("customer_order_number") |
||||
private String customerOrderNumber; |
||||
|
||||
/** VC 店铺 id。 */ |
||||
@JsonProperty("vc_store_id") |
||||
private String vcStoreId; |
||||
|
||||
@JsonProperty("seller_name") |
||||
private String sellerName; |
||||
|
||||
/** 订单类型:0 DF、1 PO、2 DI。 */ |
||||
@JsonProperty("purchase_order_type") |
||||
private Integer purchaseOrderType; |
||||
|
||||
/** DF:New / SHIPPED / ACCEPTED / CANCELLED;PO:Acknowledged / Closed。 */ |
||||
@JsonProperty("purchase_order_state") |
||||
private String purchaseOrderState; |
||||
|
||||
/** 订单流转状态:0 待处理、1 待发货、2 已完成、3 已取消。 */ |
||||
@JsonProperty("purchase_order_process_state") |
||||
private Integer purchaseOrderProcessState; |
||||
|
||||
/** 订单下单时间。 */ |
||||
@JsonProperty("purchase_order_date") |
||||
private String purchaseOrderDate; |
||||
|
||||
@JsonProperty("ack_status") |
||||
private Integer ackStatus; |
||||
|
||||
@JsonProperty("ack_status_desc") |
||||
private String ackStatusDesc; |
||||
|
||||
@JsonProperty("ack_update_time") |
||||
private String ackUpdateTime; |
||||
|
||||
/** DF 要求发货时间。 */ |
||||
@JsonProperty("ship_window_time") |
||||
private String shipWindowTime; |
||||
|
||||
/** PO 要求起始发货时间。 */ |
||||
@JsonProperty("ship_window_start") |
||||
private String shipWindowStart; |
||||
|
||||
/** PO 要求截止发货时间;文档字段名是 ship_windows_end。 */ |
||||
@JsonProperty("ship_windows_end") |
||||
private String shipWindowsEnd; |
||||
|
||||
@JsonProperty("total_price") |
||||
private String totalPrice; |
||||
|
||||
@JsonProperty("currency_code") |
||||
private String currencyCode; |
||||
|
||||
@JsonProperty("currency_icon") |
||||
private String currencyIcon; |
||||
|
||||
/** 货物总数量。 */ |
||||
@JsonProperty("item_amount") |
||||
private Integer itemAmount; |
||||
|
||||
/** 本地 PO 号,PO 详情接口的入参。 */ |
||||
@JsonProperty("local_po_number") |
||||
private String localPoNumber; |
||||
|
||||
@JsonProperty("ship_to_party_id") |
||||
private String shipToPartyId; |
||||
|
||||
@JsonProperty("remark") |
||||
private String remark; |
||||
|
||||
@JsonProperty("shipment_confirm_status") |
||||
private Integer shipmentConfirmStatus; |
||||
|
||||
@JsonProperty("shipment_label_status") |
||||
private Integer shipmentLabelStatus; |
||||
|
||||
@JsonProperty("print_num") |
||||
private Integer printNum; |
||||
|
||||
/** 商品行。 */ |
||||
@JsonProperty("purchase_order_sku_list") |
||||
private List<VcOrderItemDto> purchaseOrderSkuList = new ArrayList<VcOrderItemDto>(); |
||||
} |
||||
@ -0,0 +1,111 @@
@@ -0,0 +1,111 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.vc.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 VC 订单列表的商品行 |
||||
* ({@code data[].purchase_order_sku_list[]})。 |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class VcOrderItemDto { |
||||
|
||||
@JsonProperty("id") |
||||
private String id; |
||||
|
||||
@JsonProperty("vc_store_id") |
||||
private String vcStoreId; |
||||
|
||||
@JsonProperty("asin") |
||||
private String asin; |
||||
|
||||
@JsonProperty("parent_asin") |
||||
private String parentAsin; |
||||
|
||||
@JsonProperty("item_name") |
||||
private String itemName; |
||||
|
||||
/** 采购量。 */ |
||||
@JsonProperty("purchase_amount") |
||||
private Integer purchaseAmount; |
||||
|
||||
@JsonProperty("sequence_number") |
||||
private String sequenceNumber; |
||||
|
||||
/** 平台商品编码(Vendor SKU)。 */ |
||||
@JsonProperty("vendor_product_id") |
||||
private String vendorProductId; |
||||
|
||||
@JsonProperty("local_po_number") |
||||
private String localPoNumber; |
||||
|
||||
@JsonProperty("purchase_order_number") |
||||
private String purchaseOrderNumber; |
||||
|
||||
/** 单价。 */ |
||||
@JsonProperty("unit_price") |
||||
private String unitPrice; |
||||
|
||||
/** 成本价。 */ |
||||
@JsonProperty("net_price") |
||||
private String netPrice; |
||||
|
||||
@JsonProperty("net_price_currency_code") |
||||
private String netPriceCurrencyCode; |
||||
|
||||
/** 税额。 */ |
||||
@JsonProperty("tax_amount") |
||||
private String taxAmount; |
||||
|
||||
@JsonProperty("tax_amount_currency_code") |
||||
private String taxAmountCurrencyCode; |
||||
|
||||
@JsonProperty("tax_rate") |
||||
private String taxRate; |
||||
|
||||
@JsonProperty("tax_rate_percent") |
||||
private String taxRatePercent; |
||||
|
||||
@JsonProperty("deal_total_price") |
||||
private String dealTotalPrice; |
||||
|
||||
@JsonProperty("deal_unit_price") |
||||
private String dealUnitPrice; |
||||
|
||||
@JsonProperty("shipped_amount") |
||||
private Integer shippedAmount; |
||||
|
||||
@JsonProperty("to_ship_amount") |
||||
private Integer toShipAmount; |
||||
|
||||
@JsonProperty("local_name") |
||||
private String localName; |
||||
|
||||
@JsonProperty("local_sku") |
||||
private String localSku; |
||||
|
||||
@JsonProperty("product_id") |
||||
private String productId; |
||||
|
||||
@JsonProperty("available_amount") |
||||
private Integer availableAmount; |
||||
|
||||
@JsonProperty("asin_url") |
||||
private String asinUrl; |
||||
|
||||
@JsonProperty("pic_url") |
||||
private String picUrl; |
||||
|
||||
@JsonProperty("accepted_quantity") |
||||
private Integer acceptedQuantity; |
||||
|
||||
@JsonProperty("rejected_quantity") |
||||
private Integer rejectedQuantity; |
||||
|
||||
@JsonProperty("received_quantity") |
||||
private Integer receivedQuantity; |
||||
} |
||||
@ -0,0 +1,92 @@
@@ -0,0 +1,92 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.vc.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 VC 订单详情【PO】 |
||||
* ({@code /basicOpen/platformOrder/vcOrderPo/detail} 的 {@code data})。 |
||||
* |
||||
* <p>PO 详情按 {@code local_po_number} 查询,不是按订单编号。</p> |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class VcOrderPoDetailDto { |
||||
|
||||
@JsonProperty("vc_store_id") |
||||
private String vcStoreId; |
||||
|
||||
@JsonProperty("seller_name") |
||||
private String sellerName; |
||||
|
||||
@JsonProperty("purchase_order_number") |
||||
private String purchaseOrderNumber; |
||||
|
||||
/** 本地 PO 号,PO 详情接口的入参。 */ |
||||
@JsonProperty("local_po_number") |
||||
private String localPoNumber; |
||||
|
||||
@JsonProperty("purchase_order_date") |
||||
private String purchaseOrderDate; |
||||
|
||||
/** Acknowledged / Closed。 */ |
||||
@JsonProperty("purchase_order_state") |
||||
private String purchaseOrderState; |
||||
|
||||
/** 0 待处理、1 确认中、2 确认成功、3 确认失败。 */ |
||||
@JsonProperty("purchase_order_process_state") |
||||
private String purchaseOrderProcessState; |
||||
|
||||
@JsonProperty("payment_method") |
||||
private String paymentMethod; |
||||
|
||||
/** 0 DF、1 PO。 */ |
||||
@JsonProperty("purchase_order_type") |
||||
private String purchaseOrderType; |
||||
|
||||
@JsonProperty("remark") |
||||
private String remark; |
||||
|
||||
@JsonProperty("related_warehouse_id") |
||||
private String relatedWarehouseId; |
||||
|
||||
@JsonProperty("related_warehouse_name") |
||||
private String relatedWarehouseName; |
||||
|
||||
@JsonProperty("ship_to_party_id") |
||||
private String shipToPartyId; |
||||
|
||||
@JsonProperty("total_price") |
||||
private String totalPrice; |
||||
|
||||
@JsonProperty("currency_code") |
||||
private String currencyCode; |
||||
|
||||
@JsonProperty("currency_icon") |
||||
private String currencyIcon; |
||||
|
||||
/** 货物数量;文档类型是 string。 */ |
||||
@JsonProperty("item_amount") |
||||
private String itemAmount; |
||||
|
||||
@JsonProperty("ship_window_start") |
||||
private String shipWindowStart; |
||||
|
||||
@JsonProperty("ship_window_end") |
||||
private String shipWindowEnd; |
||||
|
||||
@JsonProperty("delivery_window_start") |
||||
private String deliveryWindowStart; |
||||
|
||||
@JsonProperty("delivery_window_end") |
||||
private String deliveryWindowEnd; |
||||
|
||||
@JsonProperty("items") |
||||
private List<VcOrderDetailItemDto> items = new ArrayList<VcOrderDetailItemDto>(); |
||||
} |
||||
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.vc.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 VC 店铺({@code /basicOpen/platformAuth/vcSeller/pageList} 的 {@code data[]})。 |
||||
* |
||||
* <p>唯一键是 {@code vc_store_id},VC 订单接口与详情接口都用它指定店铺。</p> |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class VcShopDto { |
||||
|
||||
/** 账号 id。 */ |
||||
@JsonProperty("account_id") |
||||
private Long accountId; |
||||
|
||||
/** SELLER_ID。 */ |
||||
@JsonProperty("seller_id") |
||||
private String sellerId; |
||||
|
||||
/** 账号名称。 */ |
||||
@JsonProperty("account_name") |
||||
private String accountName; |
||||
|
||||
/** 站点简称,如 na。 */ |
||||
@JsonProperty("region") |
||||
private String region; |
||||
|
||||
/** 站点名称,如 北美洲。 */ |
||||
@JsonProperty("region_name") |
||||
private String regionName; |
||||
|
||||
/** VC 店铺 id,唯一键。 */ |
||||
@JsonProperty("vc_store_id") |
||||
private String vcStoreId; |
||||
|
||||
/** 店铺名称。 */ |
||||
@JsonProperty("name") |
||||
private String name; |
||||
|
||||
/** |
||||
* 店铺授权服务状态: |
||||
* -1 删除、0 暂停同步、1 正常同步、2 授权异常。 |
||||
*/ |
||||
@JsonProperty("status") |
||||
private Integer status; |
||||
|
||||
/** 站点 id。 */ |
||||
@JsonProperty("mid") |
||||
private Integer mid; |
||||
} |
||||
@ -0,0 +1,411 @@
@@ -0,0 +1,411 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.vc.order; |
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
||||
import common.ecommerce.error.ChannelException; |
||||
import common.ecommerce.error.ValidationException; |
||||
import common.ecommerce.http.HttpRequest; |
||||
import common.ecommerce.http.HttpResponse; |
||||
import common.ecommerce.lingxing.LingxingApi; |
||||
import common.ecommerce.lingxing.LingxingConstants; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.mapper.LingxingOrderMapper; |
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.lingxing.platform.amazon.vc.dto.VcOrderDetailItemDto; |
||||
import common.ecommerce.lingxing.platform.amazon.vc.dto.VcOrderDfDetailDto; |
||||
import common.ecommerce.lingxing.platform.amazon.vc.dto.VcOrderDto; |
||||
import common.ecommerce.lingxing.platform.amazon.vc.dto.VcOrderItemDto; |
||||
import common.ecommerce.lingxing.platform.amazon.vc.dto.VcOrderPoDetailDto; |
||||
import common.ecommerce.lingxing.platform.order.LingxingOrderSource; |
||||
import common.ecommerce.model.ChannelProvider; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
import common.ecommerce.model.order.OrderLine; |
||||
import common.ecommerce.model.order.SalesOrder; |
||||
import common.ecommerce.model.order.SalesOrderQuery; |
||||
import common.ecommerce.model.support.Money; |
||||
import common.ecommerce.model.support.Page; |
||||
import common.ecommerce.model.support.PlatformTime; |
||||
|
||||
import java.time.Instant; |
||||
import java.time.LocalDate; |
||||
import java.time.LocalDateTime; |
||||
import java.time.ZoneId; |
||||
import java.time.format.DateTimeFormatter; |
||||
import java.time.format.DateTimeParseException; |
||||
import java.time.temporal.ChronoUnit; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 VC 订单。 |
||||
* |
||||
* <p>列表 {@link LingxingApi#ORDER_LIST_AMAZON_VC} 同时覆盖 DF / PO / DI, |
||||
* 并且已经返回商品行与订单总额。详情分两个接口:</p> |
||||
* <ul> |
||||
* <li>{@link LingxingApi#ORDER_DETAIL_DF_AMAZON_VC}:入参 {@code vc_store_id} + {@code purchase_order_number}</li> |
||||
* <li>{@link LingxingApi#ORDER_DETAIL_PO_AMAZON_VC}:入参 {@code local_po_number}</li> |
||||
* </ul> |
||||
* |
||||
* <p>{@link #order} 先用列表的 {@code search_field=purchase_order_number} 查出订单类型与 |
||||
* {@code local_po_number},再按类型走对应详情;DI 没有详情接口,直接用列表数据。</p> |
||||
* |
||||
* <p><b>时区待核对</b>:文档没有标注 {@code purchase_order_date} 的时区, |
||||
* 查询日期与时间解析都按领星系统时间(北京时间)处理。</p> |
||||
*/ |
||||
public final class VcOrderSource implements LingxingOrderSource { |
||||
|
||||
private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
||||
private static final DateTimeFormatter DATE_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
||||
|
||||
/** 文档未注明时区,按 ERP 系统时间(北京时间)解释,待真实报文核对。 */ |
||||
private static final ZoneId SYSTEM_ZONE = PlatformTime.BEIJING_ZONE; |
||||
|
||||
private static final int TYPE_DF = 0; |
||||
private static final int TYPE_PO = 1; |
||||
private static final int TYPE_DI = 2; |
||||
|
||||
/** 查询时间类型:3 = 订单更新时间,与 updatedAfter/updatedBefore 对齐。 */ |
||||
private static final int SEARCH_FIELD_TIME_UPDATE = 3; |
||||
|
||||
private static final String OP_PULL = "salesOrders.pull.vc"; |
||||
private static final String OP_GET = "salesOrders.get.vc"; |
||||
|
||||
@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()); |
||||
|
||||
LocalDate endDate = LocalDateTime.ofInstant(to, SYSTEM_ZONE).toLocalDate(); |
||||
// 文档:start_date 是开区间(按日期理解),多取一天避免漏掉起始日
|
||||
LocalDate startDate = LocalDateTime.ofInstant(from, SYSTEM_ZONE).toLocalDate().minusDays(1); |
||||
// 文档:查询区间最长 90 天
|
||||
if (ChronoUnit.DAYS.between(startDate, endDate) > LingxingConstants.VC_MAX_QUERY_DAYS) { |
||||
startDate = endDate.minusDays(LingxingConstants.VC_MAX_QUERY_DAYS); |
||||
} |
||||
|
||||
int length = Math.min(query.pageSize() <= 0 ? 20 : query.pageSize(), |
||||
LingxingConstants.VC_MAX_PAGE_SIZE); |
||||
int offset = query.cursor() == null ? 0 : Integer.parseInt(query.cursor()); |
||||
|
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("purchase_order_type", Arrays.asList(TYPE_DF, TYPE_PO, TYPE_DI)); |
||||
body.put("offset", offset); |
||||
body.put("length", length); |
||||
body.put("search_field_time", SEARCH_FIELD_TIME_UPDATE); |
||||
body.put("start_date", DATE.format(startDate)); |
||||
body.put("end_date", DATE.format(endDate)); |
||||
if (trimToNull(account.externalStoreId()) != null) { |
||||
body.put("vc_store_ids", Collections.singletonList(account.externalStoreId().trim())); |
||||
} |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.ORDER_LIST_AMAZON_VC, body, OP_PULL); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP_PULL); |
||||
|
||||
List<VcOrderDto> dtos = call.json().listAt(envelope.body(), "data", VcOrderDto.class); |
||||
List<SalesOrder> orders = new ArrayList<SalesOrder>(dtos.size()); |
||||
for (VcOrderDto dto : dtos) { |
||||
try { |
||||
orders.add(toSalesOrder(dto, account, zone)); |
||||
} catch (RuntimeException e) { |
||||
throw mappingFailure(e, envelope, call, OP_PULL); |
||||
} |
||||
} |
||||
|
||||
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); |
||||
} |
||||
|
||||
@Override |
||||
public SalesOrder order(LingxingCall call, String channelOrderId) { |
||||
ChannelAccount account = call.ctx().account(); |
||||
ZoneId zone = ZoneId.of(account.marketplaceZone()); |
||||
|
||||
VcOrderDto summary = findOrder(call, channelOrderId); |
||||
if (summary == null) { |
||||
throw ChannelException.builder() |
||||
.message("lingxing vc order not found: " + channelOrderId) |
||||
.operation(OP_GET) |
||||
.provider(ChannelProvider.LINGXING.name()) |
||||
.omsStoreId(call.ctx().omsStoreId()) |
||||
.requestId(call.ctx().requestId()) |
||||
.platformCode(channelOrderId) |
||||
.retryable(false) |
||||
.build(); |
||||
} |
||||
|
||||
int type = summary.getPurchaseOrderType() == null ? -1 : summary.getPurchaseOrderType(); |
||||
if (type == TYPE_DF) { |
||||
VcOrderDfDetailDto detail = dfDetail(call, summary); |
||||
if (detail != null) { |
||||
return toSalesOrder(detail, account, zone); |
||||
} |
||||
} else if (type == TYPE_PO) { |
||||
String localPoNumber = trimToNull(summary.getLocalPoNumber()); |
||||
if (localPoNumber != null) { |
||||
VcOrderPoDetailDto detail = poDetail(call, localPoNumber); |
||||
if (detail != null) { |
||||
return toSalesOrder(detail, account, zone); |
||||
} |
||||
} |
||||
} |
||||
|
||||
// DI 没有详情接口;详情返回空时也退回列表数据(列表已带完整商品行)
|
||||
return toSalesOrder(summary, account, zone); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- requests
|
||||
|
||||
/** 用订单编号精确搜索,拿到订单类型与 local_po_number。 */ |
||||
private VcOrderDto findOrder(LingxingCall call, String channelOrderId) { |
||||
ChannelAccount account = call.ctx().account(); |
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("purchase_order_type", Arrays.asList(TYPE_DF, TYPE_PO, TYPE_DI)); |
||||
body.put("offset", 0); |
||||
body.put("length", 20); |
||||
body.put("search_field", "purchase_order_number"); |
||||
body.put("search_value", Collections.singletonList(channelOrderId)); |
||||
if (trimToNull(account.externalStoreId()) != null) { |
||||
body.put("vc_store_ids", Collections.singletonList(account.externalStoreId().trim())); |
||||
} |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.ORDER_LIST_AMAZON_VC, body, OP_GET); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP_GET); |
||||
|
||||
List<VcOrderDto> dtos = call.json().listAt(envelope.body(), "data", VcOrderDto.class); |
||||
return dtos.isEmpty() ? null : dtos.get(0); |
||||
} |
||||
|
||||
private VcOrderDfDetailDto dfDetail(LingxingCall call, VcOrderDto summary) { |
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("vc_store_id", firstNonBlank(summary.getVcStoreId(), |
||||
call.ctx().account().externalStoreId())); |
||||
body.put("purchase_order_number", summary.getPurchaseOrderNumber()); |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.ORDER_DETAIL_DF_AMAZON_VC, body, OP_GET); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP_GET); |
||||
|
||||
JsonNode data = call.json().nodeAt(envelope.body(), "data"); |
||||
if (data == null || data.isNull()) { |
||||
return null; |
||||
} |
||||
return call.json().mapper().convertValue(data, VcOrderDfDetailDto.class); |
||||
} |
||||
|
||||
private VcOrderPoDetailDto poDetail(LingxingCall call, String localPoNumber) { |
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("local_po_number", localPoNumber); |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.ORDER_DETAIL_PO_AMAZON_VC, body, OP_GET); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP_GET); |
||||
|
||||
JsonNode data = call.json().nodeAt(envelope.body(), "data"); |
||||
if (data == null || data.isNull()) { |
||||
return null; |
||||
} |
||||
return call.json().mapper().convertValue(data, VcOrderPoDetailDto.class); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- mapping
|
||||
|
||||
private SalesOrder toSalesOrder(VcOrderDto dto, ChannelAccount account, ZoneId zone) { |
||||
String orderId = firstNonBlank(dto.getPurchaseOrderNumber(), |
||||
dto.getCustomerOrderNumber(), dto.getId()); |
||||
if (orderId == null) { |
||||
throw new IllegalArgumentException("lingxing vc order has no purchase_order_number"); |
||||
} |
||||
String currency = trimToNull(dto.getCurrencyCode()); |
||||
|
||||
List<OrderLine> lines = new ArrayList<OrderLine>(); |
||||
List<VcOrderItemDto> items = safe(dto.getPurchaseOrderSkuList()); |
||||
for (int i = 0; i < items.size(); i++) { |
||||
VcOrderItemDto item = items.get(i); |
||||
String lineCurrency = firstNonBlank(item.getNetPriceCurrencyCode(), |
||||
item.getTaxAmountCurrencyCode(), currency); |
||||
String sku = firstNonBlank(item.getVendorProductId(), item.getLocalSku(), item.getAsin()); |
||||
lines.add(OrderLine.builder() |
||||
.lineId(firstNonBlank(item.getId(), item.getSequenceNumber(), |
||||
item.getVendorProductId(), "line-" + i)) |
||||
.platformSku(sku == null ? "" : sku) |
||||
.quantity(item.getPurchaseAmount() == null ? 0 : item.getPurchaseAmount()) |
||||
.unitPrice(moneyOrNull(item.getUnitPrice(), lineCurrency)) |
||||
.tax(moneyOrNull(item.getTaxAmount(), |
||||
firstNonBlank(item.getTaxAmountCurrencyCode(), currency))) |
||||
.build()); |
||||
} |
||||
|
||||
Instant occur = parseSystemTime(firstNonBlank(dto.getPurchaseOrderDate(), dto.getGmtCreate())); |
||||
|
||||
return SalesOrder.builder() |
||||
.omsStoreId(account.omsStoreId()) |
||||
.channelOrderId(orderId) |
||||
.status(dto.getPurchaseOrderState()) |
||||
.buyerName(null) |
||||
.shipToCountryCode(null) |
||||
.lines(lines) |
||||
.totalAmount(moneyOrNull(dto.getTotalPrice(), currency)) |
||||
.occurredAt(requireOccurredAt(occur, orderId, zone)) |
||||
.sourceProvider(ChannelProvider.LINGXING) |
||||
.sourceAccountId(trimToNull(dto.getVcStoreId())) |
||||
.build(); |
||||
} |
||||
|
||||
private SalesOrder toSalesOrder(VcOrderDfDetailDto dto, ChannelAccount account, ZoneId zone) { |
||||
String orderId = requireOrderId(dto.getPurchaseOrderNumber()); |
||||
String currency = trimToNull(dto.getCurrencyCode()); |
||||
|
||||
List<OrderLine> lines = new ArrayList<OrderLine>(); |
||||
List<VcOrderDetailItemDto> items = safe(dto.getItems()); |
||||
for (int i = 0; i < items.size(); i++) { |
||||
lines.add(toLine(items.get(i), currency, i)); |
||||
} |
||||
|
||||
return buildOrder(account, zone, orderId, dto.getPurchaseOrderState(), |
||||
dto.getVcStoreId(), dto.getTotalPrice(), currency, lines, |
||||
dto.getPurchaseOrderDate()); |
||||
} |
||||
|
||||
private SalesOrder toSalesOrder(VcOrderPoDetailDto dto, ChannelAccount account, ZoneId zone) { |
||||
String orderId = requireOrderId(dto.getPurchaseOrderNumber()); |
||||
String currency = trimToNull(dto.getCurrencyCode()); |
||||
|
||||
List<OrderLine> lines = new ArrayList<OrderLine>(); |
||||
List<VcOrderDetailItemDto> items = safe(dto.getItems()); |
||||
for (int i = 0; i < items.size(); i++) { |
||||
lines.add(toLine(items.get(i), currency, i)); |
||||
} |
||||
|
||||
return buildOrder(account, zone, orderId, dto.getPurchaseOrderState(), |
||||
dto.getVcStoreId(), dto.getTotalPrice(), currency, lines, |
||||
dto.getPurchaseOrderDate()); |
||||
} |
||||
|
||||
private static OrderLine toLine(VcOrderDetailItemDto item, String headerCurrency, int index) { |
||||
String lineCurrency = firstNonBlank(item.getNetPriceCurrencyCode(), |
||||
item.getTaxAmountCurrencyCode(), headerCurrency); |
||||
String sku = firstNonBlank(item.getMsku(), item.getLocalSku(), item.getAsin()); |
||||
return OrderLine.builder() |
||||
.lineId(firstNonBlank(item.getSequenceNumber(), item.getAsin(), |
||||
item.getMsku(), "line-" + index)) |
||||
.platformSku(sku == null ? "" : sku) |
||||
.quantity(parseInt(item.getPurchaseAmount())) |
||||
.unitPrice(moneyOrNull(item.getUnitPrice(), lineCurrency)) |
||||
.tax(moneyOrNull(item.getTaxAmount(), |
||||
firstNonBlank(item.getTaxAmountCurrencyCode(), headerCurrency))) |
||||
.build(); |
||||
} |
||||
|
||||
private static SalesOrder buildOrder(ChannelAccount account, ZoneId zone, String orderId, |
||||
String status, String vcStoreId, String totalPrice, |
||||
String currency, List<OrderLine> lines, String orderDate) { |
||||
Instant occur = parseSystemTime(orderDate); |
||||
return SalesOrder.builder() |
||||
.omsStoreId(account.omsStoreId()) |
||||
.channelOrderId(orderId) |
||||
.status(status) |
||||
.buyerName(null) |
||||
.shipToCountryCode(null) |
||||
.lines(lines) |
||||
.totalAmount(moneyOrNull(totalPrice, currency)) |
||||
.occurredAt(requireOccurredAt(occur, orderId, zone)) |
||||
.sourceProvider(ChannelProvider.LINGXING) |
||||
.sourceAccountId(trimToNull(vcStoreId)) |
||||
.build(); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- internals
|
||||
|
||||
private static String requireOrderId(String value) { |
||||
String orderId = trimToNull(value); |
||||
if (orderId == null) { |
||||
throw new IllegalArgumentException("lingxing vc order has no purchase_order_number"); |
||||
} |
||||
return orderId; |
||||
} |
||||
|
||||
/** 文档没写时区,按 {@link #SYSTEM_ZONE} 解释;缺失或格式不对返回 null。 */ |
||||
private static Instant parseSystemTime(String value) { |
||||
String text = trimToNull(value); |
||||
if (text == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return LocalDateTime.parse(text, DATE_TIME).atZone(SYSTEM_ZONE).toInstant(); |
||||
} catch (DateTimeParseException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
private static PlatformTime requireOccurredAt(Instant occur, String orderId, ZoneId zone) { |
||||
if (occur == null) { |
||||
throw new IllegalArgumentException( |
||||
"lingxing vc order has no parseable purchase_order_date: " + orderId); |
||||
} |
||||
return PlatformTime.of(occur, zone); |
||||
} |
||||
|
||||
private static ValidationException mappingFailure(RuntimeException e, LingxingEnvelope envelope, |
||||
LingxingCall call, String operation) { |
||||
return new ValidationException(ChannelException.builder() |
||||
.message("lingxing vc order mapping failed: " + e.getMessage()) |
||||
.operation(operation) |
||||
.provider(ChannelProvider.LINGXING.name()) |
||||
.omsStoreId(call.ctx().omsStoreId()) |
||||
.requestId(call.ctx().requestId()) |
||||
.rawResponse(envelope.body()), e); |
||||
} |
||||
|
||||
private static Money moneyOrNull(String value, String currency) { |
||||
String cleaned = LingxingOrderMapper.cleanAmount(value); |
||||
if (cleaned == null || trimToNull(currency) == null) { |
||||
return null; |
||||
} |
||||
return Money.of(cleaned, currency.trim().toUpperCase()); |
||||
} |
||||
|
||||
private static int parseInt(String value) { |
||||
String text = trimToNull(value); |
||||
if (text == null) { |
||||
return 0; |
||||
} |
||||
try { |
||||
return Integer.parseInt(text); |
||||
} catch (NumberFormatException e) { |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
private static <T> List<T> safe(List<T> in) { |
||||
return in == null ? Collections.<T>emptyList() : in; |
||||
} |
||||
|
||||
private static String firstNonBlank(String... values) { |
||||
for (String value : values) { |
||||
String trimmed = trimToNull(value); |
||||
if (trimmed != null) { |
||||
return trimmed; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static String trimToNull(String value) { |
||||
if (value == null) { |
||||
return null; |
||||
} |
||||
String trimmed = value.trim(); |
||||
return trimmed.isEmpty() ? null : trimmed; |
||||
} |
||||
} |
||||
@ -0,0 +1,74 @@
@@ -0,0 +1,74 @@
|
||||
package common.ecommerce.lingxing.platform.amazon.vc.shop; |
||||
|
||||
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.LingxingConstants; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.lingxing.platform.amazon.vc.dto.VcShopDto; |
||||
import common.ecommerce.lingxing.platform.shop.LingxingShopSource; |
||||
import common.ecommerce.model.Platform; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 领星 · 亚马逊 VC 店铺列表。 |
||||
* |
||||
* <p>接口 {@code /basicOpen/platformAuth/vcSeller/pageList}:POST,{@code offset}/{@code length} |
||||
* 分页,length 默认 20、上限 200,令牌桶 1 req/s。</p> |
||||
* |
||||
* <p>{@link LingxingShopSource#shops(LingxingCall)} 要返回全量店铺,所以这里内部翻页, |
||||
* 直到返回不足一页或达到 {@code total}。</p> |
||||
*/ |
||||
public final class VcShopSource implements LingxingShopSource { |
||||
|
||||
/** 店铺授权服务状态:1 = 正常同步。 */ |
||||
private static final int STATUS_ACTIVE = 1; |
||||
|
||||
private static final String OP = "shops.list.vc"; |
||||
|
||||
@Override |
||||
public List<SubChannel> shops(LingxingCall call) { |
||||
int length = LingxingConstants.VC_MAX_PAGE_SIZE; |
||||
int offset = 0; |
||||
List<SubChannel> out = new ArrayList<SubChannel>(); |
||||
|
||||
while (true) { |
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("offset", offset); |
||||
body.put("length", length); |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.SHOP_LIST_AMAZON_VC, body, OP); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP); |
||||
|
||||
List<VcShopDto> shops = call.json().listAt(envelope.body(), "data", VcShopDto.class); |
||||
for (VcShopDto shop : shops) { |
||||
if (shop.getVcStoreId() == null |
||||
|| shop.getStatus() == null || shop.getStatus() != STATUS_ACTIVE) { |
||||
continue; |
||||
} |
||||
out.add(new SubChannel( |
||||
LingxingConstants.PLATFORM_CODE_AMAZON_VC, |
||||
shop.getVcStoreId(), |
||||
Platform.AMAZON_VC, |
||||
shop.getName() == null ? shop.getSellerId() : shop.getName())); |
||||
} |
||||
|
||||
offset += shops.size(); |
||||
boolean exhausted = shops.size() < length |
||||
|| (envelope.total() >= 0 && offset >= envelope.total()); |
||||
if (exhausted) { |
||||
break; |
||||
} |
||||
} |
||||
|
||||
return Collections.unmodifiableList(out); |
||||
} |
||||
} |
||||
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
package common.ecommerce.lingxing.platform.multiplatform.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
||||
import com.fasterxml.jackson.annotation.JsonProperty; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
|
||||
/** |
||||
* 领星 · 多平台店铺({@code /pb/mp/shop/v2/getSellerList} 的 {@code data.list[]})。 |
||||
* |
||||
* <p>{@code store_id} 是多平台店铺唯一键;{@code sid} 只有亚马逊店铺才有意义。</p> |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
@JsonIgnoreProperties(ignoreUnknown = true) |
||||
public class MultiPlatformShopDto { |
||||
|
||||
/** 店铺 ID,多平台唯一键。 */ |
||||
@JsonProperty("store_id") |
||||
private String storeId; |
||||
|
||||
/** 亚马逊店铺 sid,非亚马逊平台为空。 */ |
||||
@JsonProperty("sid") |
||||
private String sid; |
||||
|
||||
/** 店铺名称。 */ |
||||
@JsonProperty("store_name") |
||||
private String storeName; |
||||
|
||||
/** 平台 code;响应里可能是字符串,用 Integer 接收。 */ |
||||
@JsonProperty("platform_code") |
||||
private Integer platformCode; |
||||
|
||||
/** 平台名称,如 Walmart。 */ |
||||
@JsonProperty("platform_name") |
||||
private String platformName; |
||||
|
||||
/** 店铺币种。 */ |
||||
@JsonProperty("currency") |
||||
private String currency; |
||||
|
||||
/** 店铺同步状态:1 启用、0 停用。 */ |
||||
@JsonProperty("is_sync") |
||||
private Integer isSync; |
||||
|
||||
/** 店铺授权状态:1 正常授权、0 授权失败。 */ |
||||
@JsonProperty("status") |
||||
private Integer status; |
||||
} |
||||
@ -0,0 +1,83 @@
@@ -0,0 +1,83 @@
|
||||
package common.ecommerce.lingxing.platform.multiplatform.shop; |
||||
|
||||
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.LingxingConstants; |
||||
import common.ecommerce.lingxing.http.LingxingEnvelope; |
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.lingxing.platform.multiplatform.dto.MultiPlatformShopDto; |
||||
import common.ecommerce.lingxing.platform.shop.LingxingShopSource; |
||||
import common.ecommerce.model.Platform; |
||||
import common.ecommerce.model.account.ChannelAccount; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* 领星 · 多平台店铺列表。 |
||||
* |
||||
* <p>接口 {@code /pb/mp/shop/v2/getSellerList}:POST,{@code offset}/{@code length} |
||||
* 分页(上限 200),令牌桶 10 req/s。</p> |
||||
* |
||||
* <p>只返回当前账户 {@code platformCode} 对应的店铺,并过滤为「启用 + 正常授权」。 |
||||
* 响应结构是 {@code data.list[]} + {@code data.total}。</p> |
||||
*/ |
||||
public final class MultiPlatformShopSource implements LingxingShopSource { |
||||
|
||||
private static final int SYNC_ENABLED = 1; |
||||
private static final int STATUS_AUTHORIZED = 1; |
||||
|
||||
private static final String OP = "shops.list.multi"; |
||||
|
||||
@Override |
||||
public List<SubChannel> shops(LingxingCall call) { |
||||
ChannelAccount account = call.ctx().account(); |
||||
int length = LingxingConstants.MULTI_PLATFORM_MAX_PAGE_SIZE; |
||||
int offset = 0; |
||||
List<SubChannel> out = new ArrayList<SubChannel>(); |
||||
|
||||
while (true) { |
||||
Map<String, Object> body = new LinkedHashMap<String, Object>(); |
||||
body.put("offset", offset); |
||||
body.put("length", length); |
||||
body.put("is_sync", SYNC_ENABLED); |
||||
body.put("status", STATUS_AUTHORIZED); |
||||
if (account.platformCode() != null) { |
||||
body.put("platform_code", Collections.singletonList(account.platformCode())); |
||||
} |
||||
|
||||
HttpRequest req = call.signed(LingxingApi.SHOP_LIST_MULTI_PLATFORM, body, OP); |
||||
HttpResponse resp = call.execute(req); |
||||
LingxingEnvelope envelope = call.envelope(resp, OP); |
||||
|
||||
List<MultiPlatformShopDto> shops = |
||||
call.json().listAt(envelope.body(), "data.list", MultiPlatformShopDto.class); |
||||
for (MultiPlatformShopDto shop : shops) { |
||||
if (shop.getStoreId() == null || shop.getStoreId().trim().isEmpty()) { |
||||
continue; |
||||
} |
||||
Integer code = shop.getPlatformCode(); |
||||
Platform platform = LingxingConstants.toPlatform(code); |
||||
out.add(new SubChannel( |
||||
code == null ? 0 : code, |
||||
shop.getStoreId(), |
||||
platform == null ? Platform.CUSTOM : platform, |
||||
shop.getStoreName())); |
||||
} |
||||
|
||||
offset += shops.size(); |
||||
boolean exhausted = shops.size() < length |
||||
|| (envelope.total() >= 0 && offset >= envelope.total()); |
||||
if (exhausted) { |
||||
break; |
||||
} |
||||
} |
||||
|
||||
return Collections.unmodifiableList(out); |
||||
} |
||||
} |
||||
@ -0,0 +1,18 @@
@@ -0,0 +1,18 @@
|
||||
package common.ecommerce.lingxing.platform.settlement; |
||||
|
||||
import common.ecommerce.lingxing.platform.LingxingCall; |
||||
import common.ecommerce.model.settlement.SettlementQuery; |
||||
import common.ecommerce.model.settlement.SettlementTransaction; |
||||
import common.ecommerce.model.support.Page; |
||||
|
||||
/** |
||||
* 子平台的结算 / 账单能力。 |
||||
* |
||||
* <p>只负责「拼请求体 + 解析响应」;限流、取 token、签名、业务码校验由 |
||||
* {@link LingxingCall} 提供。</p> |
||||
*/ |
||||
public interface LingxingSettlementSource { |
||||
|
||||
/** 分页拉取结算交易明细。 */ |
||||
Page<SettlementTransaction> transactions(LingxingCall call, SettlementQuery query); |
||||
} |
||||
Loading…
Reference in new issue