You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1062 lines
50 KiB
1062 lines
50 KiB
package common.ecommerce.edayun.adapter; |
|
|
|
import common.ecommerce.edayun.EdayunConstants; |
|
import common.ecommerce.edayun.auth.EdayunTokenClient; |
|
import common.ecommerce.edayun.dto.EdayunAccountFlowDtos; |
|
import common.ecommerce.edayun.dto.EdayunBarcodeDtos; |
|
import common.ecommerce.edayun.dto.EdayunBolDtos; |
|
import common.ecommerce.edayun.dto.EdayunCancelDtos; |
|
import common.ecommerce.edayun.dto.EdayunCancelReturnDtos; |
|
import common.ecommerce.edayun.dto.EdayunCreateOrderDtos; |
|
import common.ecommerce.edayun.dto.EdayunCreateProductDtos; |
|
import common.ecommerce.edayun.dto.EdayunFeeEstimateDtos; |
|
import common.ecommerce.edayun.dto.EdayunGetOrderDtos; |
|
import common.ecommerce.edayun.dto.EdayunGetOrderLabelDtos; |
|
import common.ecommerce.edayun.dto.EdayunInboundDtos; |
|
import common.ecommerce.edayun.dto.EdayunInboundQueryDtos; |
|
import common.ecommerce.edayun.dto.EdayunInventoryDtos; |
|
import common.ecommerce.edayun.dto.EdayunLogisticsProductDtos; |
|
import common.ecommerce.edayun.dto.EdayunOrderCostDtos; |
|
import common.ecommerce.edayun.dto.EdayunRentDetailDtos; |
|
import common.ecommerce.edayun.dto.EdayunReturnDetailDtos; |
|
import common.ecommerce.edayun.dto.EdayunReturnDtos; |
|
import common.ecommerce.edayun.dto.EdayunRmaListDtos; |
|
import common.ecommerce.edayun.dto.EdayunTrackDtos; |
|
import common.ecommerce.edayun.enums.EdayunCancelStatus; |
|
import common.ecommerce.edayun.http.EdayunRequestFactory; |
|
import common.ecommerce.http.HttpRequest; |
|
import common.ecommerce.http.HttpResponse; |
|
import common.ecommerce.http.HttpTransport; |
|
import common.ecommerce.http.JacksonJsonCodec; |
|
import common.ecommerce.http.TokenManager; |
|
import common.ecommerce.shared.spi.AccessToken; |
|
import common.ecommerce.shared.spi.ChannelSecret; |
|
import common.ecommerce.shared.spi.LockProvider; |
|
import common.ecommerce.shared.spi.TokenStore; |
|
import common.ecommerce.shared.support.Page; |
|
import common.ecommerce.warehouse.capability.BarcodePrintCapable; |
|
import common.ecommerce.warehouse.capability.BolCapable; |
|
import common.ecommerce.warehouse.capability.InboundCapable; |
|
import common.ecommerce.warehouse.capability.InventoryCapable; |
|
import common.ecommerce.warehouse.capability.LogisticsProductCapable; |
|
import common.ecommerce.warehouse.capability.OutboundCapable; |
|
import common.ecommerce.warehouse.capability.TrackingCapable; |
|
import common.ecommerce.warehouse.capability.WarehouseFeeCapable; |
|
import common.ecommerce.warehouse.capability.WarehouseFeeEstimateCapable; |
|
import common.ecommerce.warehouse.capability.WarehouseProduct; |
|
import common.ecommerce.warehouse.capability.WarehouseProductCapable; |
|
import common.ecommerce.warehouse.capability.WarehouseReturnCapable; |
|
import common.ecommerce.warehouse.enums.CancelEligibility; |
|
import common.ecommerce.warehouse.enums.InventorySyncStrategy; |
|
import common.ecommerce.warehouse.enums.LabelMode; |
|
import common.ecommerce.warehouse.enums.WarehouseCapability; |
|
import common.ecommerce.warehouse.enums.WarehouseProvider; |
|
import common.ecommerce.warehouse.error.WarehouseException; |
|
import common.ecommerce.warehouse.error.WarehouseUnsupportedCapabilityException; |
|
import common.ecommerce.warehouse.model.BarcodePrintCommand; |
|
import common.ecommerce.warehouse.model.BillingDocument; |
|
import common.ecommerce.warehouse.model.BolDocument; |
|
import common.ecommerce.warehouse.model.CancelCommand; |
|
import common.ecommerce.warehouse.model.CancelResult; |
|
import common.ecommerce.warehouse.model.FeeEstimateQuery; |
|
import common.ecommerce.warehouse.model.FeeEstimateResult; |
|
import common.ecommerce.warehouse.model.FeeQuery; |
|
import common.ecommerce.warehouse.model.InboundCommand; |
|
import common.ecommerce.warehouse.model.InboundOrder; |
|
import common.ecommerce.warehouse.model.InboundReceipt; |
|
import common.ecommerce.warehouse.model.InventoryQuery; |
|
import common.ecommerce.warehouse.model.InventorySnapshot; |
|
import common.ecommerce.warehouse.model.LogisticsProduct; |
|
import common.ecommerce.warehouse.model.OutboundCommand; |
|
import common.ecommerce.warehouse.model.OutboundOrder; |
|
import common.ecommerce.warehouse.model.OutboundQuery; |
|
import common.ecommerce.warehouse.model.OutboundReceipt; |
|
import common.ecommerce.warehouse.model.OutboundRef; |
|
import common.ecommerce.warehouse.model.PackageResult; |
|
import common.ecommerce.warehouse.model.ReturnCommand; |
|
import common.ecommerce.warehouse.model.ReturnProcessCommand; |
|
import common.ecommerce.warehouse.model.ReturnQuery; |
|
import common.ecommerce.warehouse.model.TrackEvent; |
|
import common.ecommerce.warehouse.model.TrackingInfo; |
|
import common.ecommerce.warehouse.model.WarehouseLabel; |
|
import common.ecommerce.warehouse.model.WarehouseReturn; |
|
import common.ecommerce.warehouse.sdk.WarehouseAdapter; |
|
import common.ecommerce.warehouse.sdk.WarehouseContext; |
|
|
|
import java.time.Clock; |
|
import java.util.ArrayList; |
|
import java.util.EnumSet; |
|
import java.util.List; |
|
|
|
/** |
|
* 易达云适配器。 |
|
* |
|
* <p>已实现:商品建档、创建出库单。其余能力<b>不声明</b>,门面调用会显式抛 |
|
* {@code WarehouseUnsupportedCapabilityException},不返回空集合。</p> |
|
* |
|
* <p>适配器只持有协议与基础设施,不持有端点与凭据——两者随每次调用经 |
|
* {@code WarehouseConnection} 传入。</p> |
|
*/ |
|
public final class EdayunAdapter implements WarehouseAdapter, WarehouseProductCapable, |
|
OutboundCapable, WarehouseFeeCapable, WarehouseFeeEstimateCapable, InventoryCapable, |
|
InboundCapable, WarehouseReturnCapable, BolCapable, LogisticsProductCapable, |
|
TrackingCapable, BarcodePrintCapable { |
|
|
|
/** 物流产品查询的适用业务:1 店铺订单(默认口径)。 */ |
|
private static final int APPLICABLE_BUSINESS_STORE_ORDER = 1; |
|
|
|
private final HttpTransport transport; |
|
private final JacksonJsonCodec json; |
|
private final EdayunRequestFactory requestFactory; |
|
private final EdayunTokenClient tokenClient; |
|
private final TokenManager tokenManager; |
|
|
|
public EdayunAdapter(HttpTransport transport, JacksonJsonCodec json) { |
|
this(transport, json, null, null, Clock.systemUTC()); |
|
} |
|
|
|
/** |
|
* @param tokenStore 令牌缓存;为 null 时 {@link #token(WarehouseContext)} 每次都重新登录 |
|
* @param lockProvider 防击穿锁;与 tokenStore 成对提供 |
|
*/ |
|
public EdayunAdapter(HttpTransport transport, JacksonJsonCodec json, |
|
TokenStore tokenStore, LockProvider lockProvider, Clock clock) { |
|
this.transport = transport; |
|
this.json = json; |
|
this.requestFactory = new EdayunRequestFactory(json); |
|
this.tokenClient = new EdayunTokenClient(transport, json, clock); |
|
this.tokenManager = tokenStore == null || lockProvider == null ? null |
|
: new TokenManager(tokenStore, lockProvider, clock, |
|
EdayunConstants.TOKEN_EARLY_REFRESH_SECONDS) { |
|
@Override |
|
protected AccessToken refresh(ChannelSecret secret, AccessToken previous) { |
|
return tokenClient.fetch(secret.endpointBaseUrl(), |
|
secret.clientId(), secret.clientSecret()); |
|
} |
|
}; |
|
} |
|
|
|
@Override |
|
public String adapterId() { |
|
return EdayunConstants.ADAPTER_ID; |
|
} |
|
|
|
@Override |
|
public WarehouseProvider provider() { |
|
return WarehouseProvider.EDAYUN; |
|
} |
|
|
|
/** |
|
* 已实现的能力。 |
|
* |
|
* <p><b>声明了就必须有实现</b>,否则调用方看能力位会以为能用。</p> |
|
*/ |
|
@Override |
|
public EnumSet<WarehouseCapability> capabilities() { |
|
return EnumSet.of(WarehouseCapability.PRODUCT, WarehouseCapability.OUTBOUND, |
|
WarehouseCapability.FEE_SETTLEMENT, WarehouseCapability.FEE_ESTIMATE, |
|
WarehouseCapability.INVENTORY_QUERY, WarehouseCapability.INBOUND, |
|
WarehouseCapability.RETURN, WarehouseCapability.BOL, |
|
WarehouseCapability.LOGISTICS_PRODUCT, WarehouseCapability.TRACKING, |
|
WarehouseCapability.BARCODE_PRINT); |
|
} |
|
|
|
/** |
|
* 取一个可用 token(缓存命中则不登录)。 |
|
* |
|
* <p>token 按 {@code appKey + clientId} 维度缓存——同一 OMS 授权账号下多个仓共用一个。</p> |
|
*/ |
|
public AccessToken token(WarehouseContext ctx) { |
|
ChannelSecret secret = ctx.secret(); |
|
if (tokenManager == null) { |
|
return tokenClient.fetch(ctx.endpointBaseUrl(), secret.clientId(), secret.clientSecret()); |
|
} |
|
return tokenManager.getOrRefresh(tokenKey(secret), secret); |
|
} |
|
|
|
/** 令牌缓存维度。 */ |
|
static String tokenKey(ChannelSecret secret) { |
|
return EdayunConstants.ADAPTER_ID + ":" + secret.appKey() + ":" + secret.clientId(); |
|
} |
|
|
|
// ------------------------------------------------------------------ 商品 |
|
|
|
/** |
|
* 创建产品({@code POST /webApi/skuInfo/v1/createProduct})。 |
|
* |
|
* <p>易达云是「创建」语义,不是「保存」——同 SKU 重复调用大概率报错而不是更新。 |
|
* 结果里带仓侧 {@code skuId},宿主可据此判断是否需要后续更新。</p> |
|
*/ |
|
@Override |
|
public ProductResult createOrUpdate(WarehouseContext ctx, WarehouseProduct product) { |
|
EdayunCreateProductDtos.CreateProductRequest body = |
|
EdayunProductMapper.toCreateRequest(product); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_PRODUCT_CREATE, bodyJson, |
|
"warehouse.product.create"); |
|
EdayunCreateProductDtos.CreateProductResponse parsed = |
|
json.fromJson(resp.body(), EdayunCreateProductDtos.CreateProductResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.product.create"); |
|
|
|
String skuCode = parsed.data != null && parsed.data.skuCode != null |
|
? parsed.data.skuCode : body.skuCode; |
|
return ProductResult.of(skuCode, "CREATED", |
|
parsed.data == null ? null : parsed.data.skuId); |
|
} |
|
|
|
// ------------------------------------------------------------------ 出库 |
|
|
|
/** |
|
* 创建出库单({@code POST /webApi/order/v1/createOrder})。 |
|
* |
|
* <p>仓侧幂等键是 {@code poNumber}(我们映射自 {@code orderRef})——文档要求 |
|
* 「取消重推时平台单号保持不变」。</p> |
|
*/ |
|
@Override |
|
public OutboundReceipt create(WarehouseContext ctx, OutboundCommand cmd) { |
|
requireLabelUrl(cmd); |
|
|
|
EdayunCreateOrderDtos.CreateOrderRequest body = |
|
EdayunOrderMapper.toCreateRequest(cmd, ctx.providerWarehouseId()); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_ORDER_CREATE, bodyJson, |
|
"warehouse.outbound.create"); |
|
EdayunCreateOrderDtos.CreateOrderResponse parsed = |
|
json.fromJson(resp.body(), EdayunCreateOrderDtos.CreateOrderResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.outbound.create"); |
|
|
|
String providerOrderNo = parsed.data == null ? null : parsed.data.orderNo; |
|
if (providerOrderNo == null || providerOrderNo.trim().isEmpty()) { |
|
throw WarehouseException.builder() |
|
.message("edayun createOrder returned no data.orderNo") |
|
.operation("warehouse.outbound.create") |
|
.warehouseCode(ctx.warehouseCode()) |
|
.provider(provider().name()) |
|
.requestId(ctx.requestId()) |
|
.rawResponse(safeRelay(resp.body())) |
|
.retryable(false) |
|
.build(); |
|
} |
|
return OutboundReceipt.of(cmd.outboundRequestNo(), providerOrderNo.trim(), null); |
|
} |
|
|
|
/** |
|
* 平台面单:易达云要的是<b>文件 URL</b>,不是 base64。 |
|
* |
|
* <p>在本地就拒绝 base64,避免发出去被仓方以「面单不可访问」拒绝后难以定位。</p> |
|
*/ |
|
private static void requireLabelUrl(OutboundCommand cmd) { |
|
if (cmd.labelMode() != LabelMode.PLATFORM_LABEL) { |
|
return; |
|
} |
|
String content = cmd.labelContentBase64(); |
|
if (content == null || content.trim().isEmpty()) { |
|
return; |
|
} |
|
if (!content.startsWith("http://") && !content.startsWith("https://")) { |
|
throw new IllegalArgumentException( |
|
"edayun requires a label FILE URL (not base64) for " |
|
+ cmd.outboundRequestNo() |
|
+ "; upload the file and pass its https URL"); |
|
} |
|
} |
|
|
|
/** |
|
* 查询出库单信息({@code POST /webApi/order/v1/getOrderInfo})。 |
|
* |
|
* <p>{@code orderNo} 与 {@code poNumber} 二选一。我们优先用来源单号当 |
|
* {@code poNumber} 查(那是创建时下发的),没有来源单号才用仓侧单号。</p> |
|
*/ |
|
@Override |
|
public OutboundOrder get(WarehouseContext ctx, OutboundRef ref) { |
|
EdayunGetOrderDtos.GetOrderRequest body = new EdayunGetOrderDtos.GetOrderRequest(); |
|
if (hasText(ref.outboundRequestNo())) { |
|
body.poNumber = ref.outboundRequestNo().trim(); |
|
} else if (hasText(ref.providerOrderId())) { |
|
body.orderNo = ref.providerOrderId().trim(); |
|
} else { |
|
throw new IllegalArgumentException( |
|
"edayun needs outboundRequestNo (as poNumber) or providerOrderId (as orderNo)"); |
|
} |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_ORDER_GET, bodyJson, |
|
"warehouse.outbound.get"); |
|
EdayunGetOrderDtos.GetOrderResponse parsed = |
|
json.fromJson(resp.body(), EdayunGetOrderDtos.GetOrderResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.outbound.get"); |
|
if (parsed.data == null) { |
|
return null; |
|
} |
|
return EdayunOrderQueryMapper.toOrder(parsed.data, ctx.warehouseCode(), |
|
ref.outboundRequestNo()); |
|
} |
|
|
|
@Override |
|
public Page<OutboundOrder> query(WarehouseContext ctx, OutboundQuery query) { |
|
throw new WarehouseUnsupportedCapabilityException(WarehouseCapability.OUTBOUND); |
|
} |
|
|
|
/** |
|
* 取消前校验({@code POST /webApi/order/v1/checkCancelStatus})。 |
|
* |
|
* <p>返回能不能不收费取消:{@code Published} 免费 / {@code Shipment} 收费 / |
|
* 其余(含无法识别)按不可取消。</p> |
|
*/ |
|
@Override |
|
public CancelEligibility checkCancel(WarehouseContext ctx, OutboundRef ref) { |
|
EdayunCancelDtos.CancelRequest body = cancelRequestOf( |
|
ref.outboundRequestNo(), ref.providerOrderId()); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_ORDER_CHECK_CANCEL, bodyJson, |
|
"warehouse.outbound.checkCancel"); |
|
EdayunCancelDtos.CheckCancelResponse parsed = |
|
json.fromJson(resp.body(), EdayunCancelDtos.CheckCancelResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.outbound.checkCancel"); |
|
return EdayunCancelStatus.toCore(parsed.data); |
|
} |
|
|
|
/** |
|
* 取消订单({@code POST /webApi/order/v1/cancelOrder})。 |
|
* |
|
* <p>文档说明仓库操作后会收取取消费,可以先用 {@link #checkCancel} 判定。 |
|
* 这里不做「收费必须 acceptCharge」的强制拦截——因为校验与实际取消是两次调用, |
|
* 中间状态可能变化;是否调用由宿主决定。收费与否通过 {@link CancelResult#cancelFee()} |
|
* 之外的 {@code rawStatus} 与后续账单体现。</p> |
|
*/ |
|
@Override |
|
public CancelResult cancel(WarehouseContext ctx, CancelCommand cmd) { |
|
EdayunCancelDtos.CancelRequest body = cancelRequestOf( |
|
cmd.outboundRequestNo(), cmd.providerOrderId()); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_ORDER_CANCEL, bodyJson, |
|
"warehouse.outbound.cancel"); |
|
EdayunCancelDtos.CancelOrderResponse parsed = |
|
json.fromJson(resp.body(), EdayunCancelDtos.CancelOrderResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.outbound.cancel"); |
|
|
|
boolean cancelled = parsed.data != null && parsed.data; |
|
return CancelResult.builder() |
|
.outboundRequestNo(cmd.outboundRequestNo()) |
|
.providerOrderId(cmd.providerOrderId()) |
|
// data=false 表示仓侧拒绝,具体原因在 msg 里 |
|
.cancelled(cancelled) |
|
.eligibility(cancelled ? CancelEligibility.FREE : CancelEligibility.BLOCKED) |
|
.rawStatus(parsed.msg) |
|
.build(); |
|
} |
|
|
|
/** |
|
* 取消类请求体:{@code orderNo}(仓侧)优先级高于 {@code poNumber}(我们的单号)。 |
|
* |
|
* <p>与文档一致:「ORD 单号优先级更高」。</p> |
|
*/ |
|
private static EdayunCancelDtos.CancelRequest cancelRequestOf(String outboundRequestNo, |
|
String providerOrderId) { |
|
EdayunCancelDtos.CancelRequest body = new EdayunCancelDtos.CancelRequest(); |
|
if (hasText(providerOrderId)) { |
|
body.orderNo = providerOrderId.trim(); |
|
} |
|
if (hasText(outboundRequestNo)) { |
|
body.poNumber = outboundRequestNo.trim(); |
|
} |
|
if (body.orderNo == null && body.poNumber == null) { |
|
throw new IllegalArgumentException( |
|
"edayun cancel needs outboundRequestNo (poNumber) or providerOrderId (orderNo)"); |
|
} |
|
return body; |
|
} |
|
|
|
/** |
|
* 查面单({@code POST /webApi/order/v1/getOrderLabel})。 |
|
* |
|
* <p>返回的 {@code labels} 与 {@code trackingNo} 都是逗号拼接、按位置对应; |
|
* 一个包裹(trackingNo)对应一个面单 URL。</p> |
|
*/ |
|
@Override |
|
public List<PackageResult> packages(WarehouseContext ctx, OutboundRef ref) { |
|
EdayunGetOrderLabelDtos.GetLabelRequest body = new EdayunGetOrderLabelDtos.GetLabelRequest(); |
|
if (hasText(ref.outboundRequestNo())) { |
|
body.poNumber = ref.outboundRequestNo().trim(); |
|
} else if (hasText(ref.providerOrderId())) { |
|
body.orderNo = ref.providerOrderId().trim(); |
|
} else { |
|
throw new IllegalArgumentException( |
|
"edayun needs outboundRequestNo (as poNumber) or providerOrderId (as orderNo)"); |
|
} |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_ORDER_LABEL, bodyJson, |
|
"warehouse.outbound.packages"); |
|
EdayunGetOrderLabelDtos.GetLabelResponse parsed = |
|
json.fromJson(resp.body(), EdayunGetOrderLabelDtos.GetLabelResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.outbound.packages"); |
|
return EdayunOrderQueryMapper.toPackages(parsed.data, ref.outboundRequestNo()); |
|
} |
|
|
|
/** 原始响应是否为空串。 */ |
|
private static boolean hasText(String v) { |
|
return v != null && !v.trim().isEmpty(); |
|
} |
|
|
|
// ------------------------------------------------------------------ 入库 |
|
|
|
/** |
|
* 创建自发入库单并发布 |
|
* ({@code POST /webApi/entryInfo/v1/saveEntrySpontaneousOrder},{@code saveType=2})。 |
|
* |
|
* <p>一次调用直接发布,不走「先存草稿再发布」两步——文档里发布是保存的一部分。 |
|
* 快递类(海快 / 空运 / 快递)发布时仓方还要求 {@code shipCompany + waybill}, |
|
* 卡车类要求 {@code estimatedDepartureTime},标准模型没有这几个字段, |
|
* 缺了由仓方报错,不在本地编造。</p> |
|
*/ |
|
@Override |
|
public InboundReceipt create(WarehouseContext ctx, InboundCommand cmd) { |
|
EdayunInboundDtos.SaveEntryRequest body = EdayunInboundMapper.toRequest( |
|
cmd, EdayunInboundMapper.SAVE_PUBLISH, null, ctx.providerWarehouseId()); |
|
return saveEntry(ctx, body, cmd.inboundRequestNo(), "warehouse.inbound.create"); |
|
} |
|
|
|
/** |
|
* 编辑自发入库单({@code saveType=1})。 |
|
* |
|
* @param inboundRef 仓侧入库单号({@code entryCode}),不是我们的来源单号 |
|
*/ |
|
@Override |
|
public void improve(WarehouseContext ctx, String inboundRef, InboundCommand cmd) { |
|
EdayunInboundDtos.SaveEntryRequest body = EdayunInboundMapper.toRequest( |
|
cmd, EdayunInboundMapper.SAVE_UPDATE, inboundRef, ctx.providerWarehouseId()); |
|
saveEntry(ctx, body, cmd.inboundRequestNo(), "warehouse.inbound.improve"); |
|
} |
|
|
|
private InboundReceipt saveEntry(WarehouseContext ctx, EdayunInboundDtos.SaveEntryRequest body, |
|
String inboundRequestNo, String operation) { |
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_INBOUND_SAVE, json.toJson(body), operation); |
|
EdayunInboundDtos.SaveEntryResponse parsed = |
|
json.fromJson(resp.body(), EdayunInboundDtos.SaveEntryResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, operation); |
|
String entryCode = parsed.data == null ? null : parsed.data.entryCode; |
|
return InboundReceipt.of(inboundRequestNo, entryCode); |
|
} |
|
|
|
/** |
|
* 查入库单({@code POST /webApi/entryInfo/v1/selectEntrySkuGoodsDetail})。 |
|
* |
|
* <p>接口按 SKU 逐行返回,这里翻完所有页合并成一张单。 |
|
* <b>只认仓侧 {@code entryCode}(REV 单号)</b>——接口不支持按客户单号查。</p> |
|
* |
|
* <p>接口只回 SKU 级数据:仓库编码取 OMS 配置的仓,单头状态由各 SKU 状态汇总 |
|
* (还有 SKU 没上架,整单就不算完成)。</p> |
|
*/ |
|
@Override |
|
public InboundOrder get(WarehouseContext ctx, String inboundRef) { |
|
List<EdayunInboundQueryDtos.EntrySkuRow> rows = fetchSkuRows(ctx, inboundRef, |
|
"warehouse.inbound.get"); |
|
if (rows.isEmpty()) { |
|
return null; |
|
} |
|
return EdayunInboundQueryMapper.toOrder(inboundRef.trim(), rows, ctx.warehouseCode()); |
|
} |
|
|
|
/** |
|
* 单条查。接口没有列表 / 时间窗,{@code cursor} 按仓侧 {@code entryCode} 解释, |
|
* 返回 0 或 1 条;不传就拒绝,别被当成增量同步入口。 |
|
*/ |
|
@Override |
|
public Page<InboundOrder> query(WarehouseContext ctx, String cursor) { |
|
if (!hasText(cursor)) { |
|
throw WarehouseException.builder() |
|
.message("edayun has no inbound list API; pass entryCode (REV no) as cursor") |
|
.operation("warehouse.inbound.query") |
|
.warehouseCode(ctx.warehouseCode()) |
|
.provider(provider().name()) |
|
.requestId(ctx.requestId()) |
|
.retryable(false) |
|
.build(); |
|
} |
|
InboundOrder order = get(ctx, cursor); |
|
return order == null ? Page.<InboundOrder>empty() |
|
: Page.last(java.util.Collections.singletonList(order)); |
|
} |
|
|
|
/** 翻完所有页,合并 SKU 行。 */ |
|
private List<EdayunInboundQueryDtos.EntrySkuRow> fetchSkuRows( |
|
WarehouseContext ctx, String entryCode, String operation) { |
|
List<EdayunInboundQueryDtos.EntrySkuRow> all = |
|
new ArrayList<EdayunInboundQueryDtos.EntrySkuRow>(); |
|
for (int page = 1; ; page++) { |
|
if (page > MAX_INBOUND_PAGES) { |
|
// 单页 200 条 × 20 页 = 4000 个 SKU,再大多半是单号传错了 |
|
throw WarehouseException.builder() |
|
.message("edayun inbound " + entryCode + " exceeded " + MAX_INBOUND_PAGES |
|
+ " pages of sku detail; check the entryCode") |
|
.operation(operation) |
|
.warehouseCode(ctx.warehouseCode()) |
|
.provider(provider().name()) |
|
.requestId(ctx.requestId()) |
|
.retryable(false) |
|
.build(); |
|
} |
|
EdayunInboundQueryDtos.SelectEntrySkuRequest body = |
|
EdayunInboundQueryMapper.toRequest(entryCode, page); |
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_INBOUND_SKU_DETAIL, |
|
json.toJson(body), operation); |
|
EdayunInboundQueryDtos.SelectEntrySkuResponse parsed = |
|
json.fromJson(resp.body(), EdayunInboundQueryDtos.SelectEntrySkuResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, operation); |
|
|
|
if (parsed.rows == null || parsed.rows.isEmpty()) { |
|
return all; |
|
} |
|
all.addAll(parsed.rows); |
|
if (parsed.rows.size() < body.pageSize) { |
|
return all; |
|
} |
|
} |
|
} |
|
|
|
/** 入库明细翻页上限。 */ |
|
private static final int MAX_INBOUND_PAGES = 20; |
|
|
|
// ------------------------------------------------------------------ 库存 |
|
|
|
/** |
|
* 库存同步策略:接口没有时间维度,只能按页全量拉。 |
|
* |
|
* <p>宿主按本值做全量 diff,不要指望 {@code updatedAfter} 生效。</p> |
|
*/ |
|
@Override |
|
public InventorySyncStrategy syncStrategy() { |
|
return InventorySyncStrategy.FULL_PULL_DIFF; |
|
} |
|
|
|
/** |
|
* 库存查询({@code POST /webApi/userSkuStock/v1/queryInventory})。 |
|
* |
|
* <p>一行 = 一个 SKU 在一个仓的汇总。默认只返回总库存大于 0 的数据, |
|
* 需要零库存时由调用方传 {@code includeZeroStock(true)}。</p> |
|
* |
|
* <p>分页按页码游标:单页最多 500 条,{@code total} 是总行数。</p> |
|
*/ |
|
@Override |
|
public Page<InventorySnapshot> query(WarehouseContext ctx, InventoryQuery query) { |
|
int pageNum = EdayunInventoryMapper.pageNumber(query.cursor()); |
|
EdayunInventoryDtos.QueryInventoryRequest body = |
|
EdayunInventoryMapper.toRequest(query, ctx.providerWarehouseId(), pageNum); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_INVENTORY, bodyJson, |
|
"warehouse.inventory.query"); |
|
EdayunInventoryDtos.QueryInventoryResponse parsed = |
|
json.fromJson(resp.body(), EdayunInventoryDtos.QueryInventoryResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.inventory.query"); |
|
|
|
return EdayunInventoryMapper.toPage(parsed, ctx.warehouseCode(), |
|
java.time.Instant.now(ctx.clock()), pageNum, body.pageSize); |
|
} |
|
|
|
// ------------------------------------------------------------------ 费用 |
|
|
|
/** |
|
* 查询交易明细({@code POST /webApi/accountFlow/list})。 |
|
* |
|
* <p>文档说明「同一个单号的某个计费项有多条数据」,所以<b>不做去重</b>, |
|
* 每条流水都映射成一条 {@link BillingDocument},靠 {@code serialNumber} 区分。</p> |
|
* |
|
* <p>分页:文档要求 {@code pageNum} / {@code pageSize} 必填,当前只取第一页 |
|
* (200 条);超过时截断——<b>待确认是否需要翻页</b>。</p> |
|
*/ |
|
@Override |
|
public List<BillingDocument> settlements(WarehouseContext ctx, FeeQuery query) { |
|
EdayunAccountFlowDtos.ListFlowRequest body = EdayunFeeMapper.toRequest(query); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_ACCOUNT_FLOW, bodyJson, |
|
"warehouse.fee.settlements"); |
|
EdayunAccountFlowDtos.ListFlowResponse parsed = |
|
json.fromJson(resp.body(), EdayunAccountFlowDtos.ListFlowResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.fee.settlements"); |
|
|
|
if (parsed.rows == null || parsed.rows.isEmpty()) { |
|
return java.util.Collections.emptyList(); |
|
} |
|
List<BillingDocument> out = new ArrayList<BillingDocument>(parsed.rows.size()); |
|
for (EdayunAccountFlowDtos.FlowRow row : parsed.rows) { |
|
out.add(EdayunFeeMapper.toDocument(row, ctx.warehouseCode())); |
|
} |
|
return out; |
|
} |
|
|
|
/** |
|
* 查询订单费用明细 |
|
* ({@code POST /webApi/order/cost/v1/selectOrderCostInfoList})。 |
|
* |
|
* <p>与 {@link #settlements}(账户流水)不同:这个按订单拆费用, |
|
* 带应收 / 折扣 / 实付与计费重。落库金额取<b>实付</b>。 |
|
* |
|
* <p>一个订单可能展开成多条 {@link BillingDocument}(逐计费项)。</p> |
|
*/ |
|
@Override |
|
public List<BillingDocument> orderCosts(WarehouseContext ctx, FeeQuery query) { |
|
EdayunOrderCostDtos.SelectOrderCostRequest body = EdayunOrderCostMapper.toRequest(query); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_ORDER_COST, bodyJson, |
|
"warehouse.fee.orderCosts"); |
|
EdayunOrderCostDtos.SelectOrderCostResponse parsed = |
|
json.fromJson(resp.body(), EdayunOrderCostDtos.SelectOrderCostResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.fee.orderCosts"); |
|
|
|
if (parsed.rows == null || parsed.rows.isEmpty()) { |
|
return java.util.Collections.emptyList(); |
|
} |
|
List<BillingDocument> out = new ArrayList<BillingDocument>(); |
|
for (EdayunOrderCostDtos.OrderCostRow row : parsed.rows) { |
|
out.addAll(EdayunOrderCostMapper.toDocuments(row, ctx.warehouseCode())); |
|
} |
|
return out; |
|
} |
|
|
|
/** |
|
* 查询仓租费用明细 |
|
* ({@code POST /webApi/order/cost/v1/selectUserRentDetailList})。 |
|
* |
|
* <p>与 {@link #settlements}(账户流水)、{@link #orderCosts}(按订单拆费用)都不同: |
|
* 这个只出仓储费,按 SKU + 计费日逐条,带租期天数与计费体积。</p> |
|
* |
|
* <p>文档要求计费时间窗必填且不超过 3 个月,所以按<b>时间窗 + 翻页</b>拉: |
|
* 一页 200 条,最多 {@link EdayunRentDetailMapper#MAX_PAGES} 页。仍有剩余时 |
|
* <b>显式失败</b>而不是截断——少算仓租会让对账金额偏小。</p> |
|
*/ |
|
@Override |
|
public List<BillingDocument> storageRents(WarehouseContext ctx, FeeQuery query) { |
|
EdayunRentDetailDtos.SelectRentDetailRequest body = |
|
EdayunRentDetailMapper.toRequest(query, ctx.providerWarehouseId()); |
|
|
|
List<BillingDocument> out = new ArrayList<BillingDocument>(); |
|
long rowsFetched = 0; |
|
for (int page = 1; ; page++) { |
|
body.pageNum = page; |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_RENT_DETAIL, bodyJson, |
|
"warehouse.fee.storageRents"); |
|
EdayunRentDetailDtos.SelectRentDetailResponse parsed = |
|
json.fromJson(resp.body(), EdayunRentDetailDtos.SelectRentDetailResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.fee.storageRents"); |
|
|
|
if (parsed.rows == null || parsed.rows.isEmpty()) { |
|
return out; |
|
} |
|
out.addAll(EdayunRentDetailMapper.toDocuments(parsed.rows, ctx.warehouseCode())); |
|
rowsFetched += parsed.rows.size(); |
|
if (rowsFetched > EdayunRentDetailMapper.MAX_ROWS) { |
|
break; |
|
} |
|
// 不满一页说明到底了(总行数恰好是整页时,下一轮会拿到空页) |
|
if (parsed.rows.size() < EdayunRentDetailMapper.MAX_PAGE_SIZE) { |
|
return out; |
|
} |
|
} |
|
throw WarehouseException.builder() |
|
.message("edayun rent detail exceeded " + EdayunRentDetailMapper.MAX_ROWS |
|
+ " rows (" + EdayunRentDetailMapper.MAX_PAGES + " pages);" |
|
+ " narrow the billing time window and query again") |
|
.operation("warehouse.fee.storageRents") |
|
.warehouseCode(ctx.warehouseCode()) |
|
.provider(provider().name()) |
|
.requestId(ctx.requestId()) |
|
.retryable(false) |
|
.build(); |
|
} |
|
|
|
// ------------------------------------------------------------------ 试算 |
|
|
|
/** |
|
* 费用试算({@code POST /webApi/estimate/v1/trial})。 |
|
* |
|
* <p>只做「按包裹」这一种预估——标准模型只带包裹尺寸重量。 |
|
* 真机实测:按 SKU 预估要求 SKU 在仓侧已建档,虚拟包裹在 PR 环境直接 |
|
* 「内部服务器错误」,两者都不适合作为默认路径。</p> |
|
* |
|
* <p>响应分 {@code successItems} / {@code failItems},只映射前者; |
|
* 全部失败时返回一个 {@code carriers()} 为空的结果(表示该仓无法承运)。 |
|
* 渠道的无报价原因(如「不支持派送」)只在原报文里,不同步到统一模型。</p> |
|
*/ |
|
@Override |
|
public List<FeeEstimateResult> estimate(WarehouseContext ctx, FeeEstimateQuery query) { |
|
EdayunFeeEstimateDtos.TrialRequest body = |
|
EdayunFeeEstimateMapper.toRequest(query, ctx.providerWarehouseId()); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_FEE_ESTIMATE, bodyJson, |
|
"warehouse.fee.estimate"); |
|
EdayunFeeEstimateDtos.TrialResponse parsed = |
|
json.fromJson(resp.body(), EdayunFeeEstimateDtos.TrialResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.fee.estimate"); |
|
|
|
return java.util.Collections.singletonList( |
|
EdayunFeeEstimateMapper.toResult(parsed, ctx.warehouseCode())); |
|
} |
|
|
|
// ------------------------------------------------------------------ 退货 |
|
|
|
/** |
|
* 创建退货单 / 退货预报({@code POST /webApi/rma/info/v1/saveAndPublish})。 |
|
* |
|
* <p>统一模型只对应「自行退回」({@code returnType=2}):它要求退货运单号, |
|
* 正好是买家自己寄回的场景。未妥投 / 返邮 / FBA 退回要退货人地址或亚马逊单号, |
|
* 标准模型没有——见 TODO。</p> |
|
* |
|
* <p>与运去哪不同,<b>响应 {@code data} 就是仓侧退货单号</b>,可直接落 |
|
* {@code providerReturnId};状态不回,只能后续人工或回调补。</p> |
|
*/ |
|
@Override |
|
public WarehouseReturn create(WarehouseContext ctx, ReturnCommand cmd) { |
|
EdayunReturnDtos.SaveReturnRequest body = |
|
EdayunReturnMapper.toRequest(cmd, ctx.providerWarehouseId()); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_RETURN_CREATE, bodyJson, |
|
"warehouse.return.create"); |
|
EdayunReturnDtos.SaveReturnResponse parsed = |
|
json.fromJson(resp.body(), EdayunReturnDtos.SaveReturnResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.return.create"); |
|
|
|
if (parsed.data == null || parsed.data.trim().isEmpty()) { |
|
throw WarehouseException.builder() |
|
.message("edayun return create returned no return order no") |
|
.operation("warehouse.return.create") |
|
.warehouseCode(ctx.warehouseCode()) |
|
.provider(provider().name()) |
|
.requestId(ctx.requestId()) |
|
.rawResponse(safeRelay(resp.body())) |
|
.retryable(false) |
|
.build(); |
|
} |
|
return EdayunReturnMapper.toCreated(cmd, parsed.data.trim(), ctx.warehouseCode()); |
|
} |
|
|
|
/** |
|
* 获取退货单详情({@code POST /webApi/rma/info/v1/getOrderDetail})。 |
|
* |
|
* <p>{@code returnRequestNo} 可以是 OMS 退货单号,也可以是仓方 RMA 号—— |
|
* 仓方只认 {@code rmaCode},所以<b>非 RMA 号要先有创建时拿到的 |
|
* {@code providerReturnId}</b>。这里原样传,查不到由仓方报错。</p> |
|
* |
|
* <p>明细行优先取仓的检验结果({@code checkList}),没有才退回预报 |
|
* ({@code skuList})——对账以实际收到为准。</p> |
|
*/ |
|
@Override |
|
public WarehouseReturn getReturn(WarehouseContext ctx, String returnRequestNo) { |
|
EdayunReturnDetailDtos.GetReturnRequest body = |
|
EdayunReturnDetailMapper.toRequest(returnRequestNo); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_RETURN_DETAIL, bodyJson, |
|
"warehouse.return.get"); |
|
EdayunReturnDetailDtos.GetReturnResponse parsed = |
|
json.fromJson(resp.body(), EdayunReturnDetailDtos.GetReturnResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.return.get"); |
|
|
|
if (parsed.data == null) { |
|
// 单号不存在时仓方可能回 code=200 + data=null,按「查不到」返回 null |
|
return null; |
|
} |
|
return EdayunReturnDetailMapper.toReturn(parsed.data, ctx.warehouseCode(), |
|
body.rmaCode); |
|
} |
|
|
|
/** |
|
* 取消退货单({@code POST /webApi/rma/info/v1/cancel})。 |
|
* |
|
* <p>只认仓方 {@code rmaCode},所以传的必须是创建时拿到的 |
|
* {@code providerReturnId}——用 OMS 单号会被仓方查不到。</p> |
|
* |
|
* <p>响应 {@code data=true} 才算成功。{@code code=200} + {@code data=false} |
|
* 是仓方拒绝(如已收货),这里抛异常而不是静默返回 false—— |
|
* 取消退货是状态变更,调用方必须知道没成功。</p> |
|
*/ |
|
@Override |
|
public boolean cancel(WarehouseContext ctx, String providerReturnNo) { |
|
if (!hasText(providerReturnNo)) { |
|
throw new IllegalArgumentException( |
|
"edayun return cancel needs the provider return no (rmaCode)"); |
|
} |
|
EdayunCancelReturnDtos.CancelReturnRequest body = |
|
new EdayunCancelReturnDtos.CancelReturnRequest(); |
|
body.rmaCode = providerReturnNo.trim(); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_RETURN_CANCEL, bodyJson, |
|
"warehouse.return.cancel"); |
|
EdayunCancelReturnDtos.CancelReturnResponse parsed = |
|
json.fromJson(resp.body(), EdayunCancelReturnDtos.CancelReturnResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.return.cancel"); |
|
|
|
if (parsed.data == null || !parsed.data) { |
|
throw WarehouseException.builder() |
|
.message("edayun return cancel rejected: " + parsed.msg) |
|
.operation("warehouse.return.cancel") |
|
.warehouseCode(ctx.warehouseCode()) |
|
.provider(provider().name()) |
|
.requestId(ctx.requestId()) |
|
.rawResponse(safeRelay(resp.body())) |
|
.retryable(false) |
|
.build(); |
|
} |
|
return true; |
|
} |
|
|
|
/** |
|
* 查询退货单列表({@code POST /webApi/rma/info/v1/selectRmaInfoList})。 |
|
* |
|
* <p>只回单头,没有明细行与退货人地址;要明细请用 {@link #getReturn}。</p> |
|
* |
|
* <p>时间窗不传时仓方默认近一个月——这一点由仓方兜底,本地不补默认值, |
|
* 免得「没给时间窗」和「显式要近一个月」两种情况被混掉。</p> |
|
*/ |
|
@Override |
|
public Page<WarehouseReturn> queryReturns(WarehouseContext ctx, ReturnQuery query) { |
|
int pageNum = EdayunRmaListMapper.pageNumber(query.cursor()); |
|
EdayunRmaListDtos.SelectRmaListRequest body = EdayunRmaListMapper.toRequest( |
|
query, ctx.providerWarehouseId(), ctx.zone(), pageNum); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_RETURN_LIST, bodyJson, |
|
"warehouse.return.query"); |
|
EdayunRmaListDtos.SelectRmaListResponse parsed = |
|
json.fromJson(resp.body(), EdayunRmaListDtos.SelectRmaListResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.return.query"); |
|
|
|
return EdayunRmaListMapper.toPage(parsed, ctx.warehouseCode(), pageNum, body.pageSize); |
|
} |
|
|
|
/** |
|
* 按明细行设置检验后处理方式。 |
|
* |
|
* <p>需要仓侧明细行 id,而查退货单的接口还没有——无法构造,显式拒绝。</p> |
|
*/ |
|
@Override |
|
public void process(WarehouseContext ctx, ReturnProcessCommand cmd) { |
|
throw new WarehouseUnsupportedCapabilityException(WarehouseCapability.RETURN_DISPOSITION); |
|
} |
|
|
|
// ------------------------------------------------------------------ BOL / 物流产品 |
|
|
|
/** |
|
* 查询 BOL(提单 / 提货凭证) |
|
* ({@code POST /webApi/fbaOrder/v1/getOrderBolList})。 |
|
* |
|
* <p>店铺订单与平台仓订单共用,{@code orderNo} 传 EDA 的平台仓订单号 |
|
* (如 {@code FBA473182144})。没有 BOL 时返回空列表,不是错误。</p> |
|
*/ |
|
@Override |
|
public List<BolDocument> orderBols(WarehouseContext ctx, String providerOrderNo) { |
|
EdayunBolDtos.GetBolRequest body = EdayunBolMapper.toRequest(providerOrderNo); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_BOL_LIST, bodyJson, |
|
"warehouse.bol.order"); |
|
EdayunBolDtos.GetBolResponse parsed = |
|
json.fromJson(resp.body(), EdayunBolDtos.GetBolResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.bol.order"); |
|
|
|
return EdayunBolMapper.toDocuments(parsed); |
|
} |
|
|
|
/** |
|
* 查物流产品清单 |
|
* ({@code POST /webApi/order/listByLogisticProductHasPerm})。 |
|
* |
|
* <p>{@code productId = -998}(使用分物流规则)不是真实产品,映射时跳过。</p> |
|
*/ |
|
@Override |
|
public List<LogisticsProduct> products(WarehouseContext ctx, String warehouseCode) { |
|
// 统一模型不区分业务类型,默认查「店铺订单」(1); |
|
// 不传反而返回全账号的产品,量太大 |
|
EdayunLogisticsProductDtos.ListProductRequest body = EdayunLogisticsProductMapper.toRequest( |
|
ctx.providerWarehouseId(), APPLICABLE_BUSINESS_STORE_ORDER); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_LOGISTICS_PRODUCTS, bodyJson, |
|
"warehouse.logistics.products"); |
|
EdayunLogisticsProductDtos.ListProductResponse parsed = |
|
json.fromJson(resp.body(), EdayunLogisticsProductDtos.ListProductResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.logistics.products"); |
|
|
|
return EdayunLogisticsProductMapper.toProducts( |
|
parsed == null ? null : parsed.data, ctx.warehouseCode()); |
|
} |
|
|
|
// ------------------------------------------------------------------ 物流轨迹 |
|
|
|
/** |
|
* 按跟踪号查轨迹节点({@code POST /webApi/track/getTrackInfoList})。 |
|
* |
|
* <p>请求体是<b>纯字符串数组</b>,文档示例的嵌套对象是错的。 |
|
* 仓方无轨迹时返回<b>空列表</b>——真机对查不到的跟踪号回 |
|
* {@code code=500, msg=未匹配到轨迹},那是「没轨迹」不是「调用失败」。</p> |
|
*/ |
|
@Override |
|
public List<TrackEvent> track(WarehouseContext ctx, String trackingNumber) { |
|
EdayunTrackDtos.Track parsed = fetchTrack(ctx, trackingNumber, "warehouse.track"); |
|
return EdayunTrackMapper.toTrackEvents(parsed); |
|
} |
|
|
|
/** |
|
* 按跟踪号查运单级轨迹;除节点外带上网时间与妥投时间。 |
|
* |
|
* <p>文档说明「主要关注上网时间跟妥投时间」,这两个在 |
|
* {@link TrackingInfo#firstOnlineAt()} / {@link TrackingInfo#deliveredAt()}。</p> |
|
*/ |
|
@Override |
|
public TrackingInfo trackShipment(WarehouseContext ctx, String trackingNumber) { |
|
EdayunTrackDtos.Track parsed = fetchTrack(ctx, trackingNumber, "warehouse.track.shipment"); |
|
return EdayunTrackMapper.toTrackingInfo(parsed); |
|
} |
|
|
|
/** |
|
* 查一条轨迹;查不到返回 null。 |
|
* |
|
* <p><b>「未匹配到轨迹」按空结果处理</b>:仓方把这个当业务失败(code=500), |
|
* 但对调用方来说和「运单还没揽收」是同一件事——两者都不该抛异常, |
|
* 否则宿主无法区分「真失败」和「还没轨迹」。</p> |
|
*/ |
|
private EdayunTrackDtos.Track fetchTrack(WarehouseContext ctx, String trackingNumber, |
|
String operation) { |
|
List<String> body = EdayunTrackMapper.toRequest( |
|
java.util.Collections.singletonList(trackingNumber)); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_TRACK, bodyJson, operation); |
|
EdayunTrackDtos.GetTrackResponse parsed = |
|
json.fromJson(resp.body(), EdayunTrackDtos.GetTrackResponse.class); |
|
if (parsed != null && isNoTrackFound(parsed.msg)) { |
|
return null; |
|
} |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, operation); |
|
|
|
return EdayunTrackMapper.find(parsed, trackingNumber); |
|
} |
|
|
|
/** 仓方「没有轨迹」的提示语。 */ |
|
private static boolean isNoTrackFound(String msg) { |
|
return msg != null && (msg.contains("未匹配到轨迹") || msg.contains("跟踪号为空")); |
|
} |
|
|
|
// ------------------------------------------------------------------ 条码打印 |
|
|
|
/** |
|
* 打印 SKU 条码({@code POST /webApi/skuInfo/v2/printBarcode})。 |
|
* |
|
* <p>与入库贴标({@code inbound().printSkuLabel})不同:这个只按 SKU 打, |
|
* 不绑单据,且仓方<b>同步返回 PDF 地址</b>。</p> |
|
* |
|
* <p>真机实测的仓方校验:{@code productInfos} 为空报「产品信息不能为空」, |
|
* 缺 {@code pageSize} 报「纸张尺寸不能为空」,{@code pageSize} 非 0~3 报 |
|
* 「内部服务器错误」,{@code num=0} 同样「内部服务器错误」,超过 50 个 SKU |
|
* 报「最多打印50个SKU」。本地已提前挡住后两者。</p> |
|
*/ |
|
@Override |
|
public WarehouseLabel printBarcodes(WarehouseContext ctx, BarcodePrintCommand cmd) { |
|
EdayunBarcodeDtos.PrintBarcodeRequest body = EdayunBarcodeMapper.toRequest(cmd); |
|
String bodyJson = json.toJson(body); |
|
|
|
HttpResponse resp = send(ctx, EdayunConstants.PATH_BARCODE_PRINT, bodyJson, |
|
"warehouse.barcode.print"); |
|
EdayunBarcodeDtos.PrintBarcodeResponse parsed = |
|
json.fromJson(resp.body(), EdayunBarcodeDtos.PrintBarcodeResponse.class); |
|
requireSuccess(ctx, parsed == null ? null : parsed.code, |
|
parsed == null ? null : parsed.msg, resp, "warehouse.barcode.print"); |
|
|
|
if (parsed.data == null || parsed.data.trim().isEmpty()) { |
|
throw WarehouseException.builder() |
|
.message("edayun printBarcode returned no file url") |
|
.operation("warehouse.barcode.print") |
|
.warehouseCode(ctx.warehouseCode()) |
|
.provider(provider().name()) |
|
.requestId(ctx.requestId()) |
|
.rawResponse(safeRelay(resp.body())) |
|
.retryable(false) |
|
.build(); |
|
} |
|
return EdayunBarcodeMapper.toLabel(parsed.data.trim(), cmd); |
|
} |
|
|
|
// ------------------------------------------------------------------ internals |
|
/** 发业务请求(自动取 token、算签名)。 */ |
|
private HttpResponse send(WarehouseContext ctx, String path, String bodyJson, String operation) { |
|
AccessToken accessToken = token(ctx); |
|
ChannelSecret secret = ctx.secret(); |
|
long timestamp = ctx.clock().millis(); |
|
HttpRequest req = requestFactory.business( |
|
ctx.endpointBaseUrl(), path, "POST", |
|
accessToken.value(), secret.appKey(), secret.appSecret(), |
|
timestamp, bodyJson, operation); |
|
return transport.execute(req); |
|
} |
|
|
|
/** HTTP 非 2xx 与业务码非 200 都要显式失败。 */ |
|
private void requireSuccess(WarehouseContext ctx, Integer code, String msg, |
|
HttpResponse resp, String operation) { |
|
int status = resp.statusCode(); |
|
if (status < 200 || status >= 300) { |
|
throw WarehouseException.builder() |
|
.message("edayun " + operation + " failed: http " + status |
|
+ ", body=" + safeRelay(resp.body())) |
|
.operation(operation) |
|
.warehouseCode(ctx.warehouseCode()) |
|
.provider(provider().name()) |
|
.requestId(ctx.requestId()) |
|
.platformCode(String.valueOf(status)) |
|
.rawResponse(safeRelay(resp.body())) |
|
.retryable(status >= 500) |
|
.build(); |
|
} |
|
if (code == null || code != EdayunConstants.CODE_SUCCESS) { |
|
throw WarehouseException.builder() |
|
.message("edayun " + operation + " business error: code=" + code + ", msg=" + msg) |
|
.operation(operation) |
|
.warehouseCode(ctx.warehouseCode()) |
|
.provider(provider().name()) |
|
.requestId(ctx.requestId()) |
|
.platformCode(code == null ? null : String.valueOf(code)) |
|
.rawResponse(safeRelay(resp.body())) |
|
.retryable(false) |
|
.build(); |
|
} |
|
} |
|
|
|
/** 原始响应可能含敏感信息,异常里只留长度与首段。 */ |
|
private static String safeRelay(String raw) { |
|
if (raw == null) { |
|
return null; |
|
} |
|
return raw.length() <= 300 ? raw : raw.substring(0, 300) + "..."; |
|
} |
|
}
|
|
|