|
|
|
|
package common.ecommerce.edayun.adapter;
|
|
|
|
|
|
|
|
|
|
import common.ecommerce.edayun.EdayunConstants;
|
|
|
|
|
import common.ecommerce.edayun.auth.EdayunTokenClient;
|
|
|
|
|
import common.ecommerce.edayun.dto.EdayunCreateOrderDtos;
|
|
|
|
|
import common.ecommerce.edayun.dto.EdayunAccountFlowDtos;
|
|
|
|
|
import common.ecommerce.edayun.dto.EdayunCancelDtos;
|
|
|
|
|
import common.ecommerce.edayun.dto.EdayunCreateProductDtos;
|
|
|
|
|
import common.ecommerce.edayun.dto.EdayunGetOrderDtos;
|
|
|
|
|
import common.ecommerce.edayun.dto.EdayunGetOrderLabelDtos;
|
|
|
|
|
import common.ecommerce.edayun.dto.EdayunOrderCostDtos;
|
|
|
|
|
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.OutboundCapable;
|
|
|
|
|
import common.ecommerce.warehouse.capability.WarehouseFeeCapable;
|
|
|
|
|
import common.ecommerce.warehouse.capability.WarehouseProduct;
|
|
|
|
|
import common.ecommerce.warehouse.capability.WarehouseProductCapable;
|
|
|
|
|
import common.ecommerce.warehouse.enums.CancelEligibility;
|
|
|
|
|
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.CancelCommand;
|
|
|
|
|
import common.ecommerce.warehouse.model.BillingDocument;
|
|
|
|
|
import common.ecommerce.warehouse.model.CancelResult;
|
|
|
|
|
import common.ecommerce.warehouse.model.FeeQuery;
|
|
|
|
|
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.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 {
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 取一个可用 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/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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------ 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) + "...";
|
|
|
|
|
}
|
|
|
|
|
}
|