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.
216 lines
9.2 KiB
216 lines
9.2 KiB
|
1 week ago
|
package common.ecommerce.hengjian.adapter;
|
||
|
|
|
||
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
||
|
|
import common.ecommerce.hengjian.HengjianConstants;
|
||
|
|
import common.ecommerce.hengjian.dto.HengjianCarrierGroupDtos;
|
||
|
|
import common.ecommerce.hengjian.dto.HengjianWarehouseDtos;
|
||
|
|
import common.ecommerce.hengjian.http.HengjianEnvelope;
|
||
|
|
import common.ecommerce.hengjian.http.HengjianRateLimiter;
|
||
|
|
import common.ecommerce.hengjian.http.HengjianRequestFactory;
|
||
|
|
import common.ecommerce.http.HttpRequest;
|
||
|
|
import common.ecommerce.http.HttpResponse;
|
||
|
|
import common.ecommerce.http.HttpTransport;
|
||
|
|
import common.ecommerce.http.JacksonJsonCodec;
|
||
|
|
import common.ecommerce.warehouse.capability.LogisticsProductCapable;
|
||
|
|
import common.ecommerce.warehouse.capability.WarehouseDirectoryCapable;
|
||
|
|
import common.ecommerce.warehouse.enums.WarehouseCapability;
|
||
|
|
import common.ecommerce.warehouse.enums.WarehouseProvider;
|
||
|
|
import common.ecommerce.warehouse.error.WarehouseException;
|
||
|
|
import common.ecommerce.warehouse.model.LogisticsProduct;
|
||
|
|
import common.ecommerce.warehouse.model.WarehouseFacility;
|
||
|
|
import common.ecommerce.warehouse.sdk.WarehouseContext;
|
||
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.EnumSet;
|
||
|
|
import java.util.Iterator;
|
||
|
|
import java.util.LinkedHashMap;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 恒健适配器。
|
||
|
|
*
|
||
|
|
* <p><b>本期只接只读基础资料</b>:仓库目录、承运商 / 物流产品、按仓的物流渠道。
|
||
|
|
* 这几个接口不依赖任何单号、也不产生副作用,是验证鉴权与信封最快的一步;
|
||
|
|
* 因此只声明 {@code DIRECTORY} 与 {@code LOGISTICS_PRODUCT} 两个能力位。</p>
|
||
|
|
*
|
||
|
|
* <p>出库 / 库存 / 入库 / 退货等接口<b>尚未接入</b>,能力位不声明:
|
||
|
|
* 门面调用会显式抛 {@code WarehouseUnsupportedCapabilityException},不返回空集合。</p>
|
||
|
|
*
|
||
|
|
* <p>适配器只持有协议与基础设施,不持有端点与凭据——两者随每次调用经
|
||
|
|
* {@code WarehouseConnection} 传入。</p>
|
||
|
|
*/
|
||
|
|
public final class HengjianAdapter implements WarehouseDirectoryCapable, LogisticsProductCapable {
|
||
|
|
|
||
|
|
private static final String OP_WAREHOUSE_LIST = "warehouse.directory.warehouses";
|
||
|
|
private static final String OP_CARRIERS = "warehouse.directory.carriers";
|
||
|
|
private static final String OP_LOGISTICS_PRODUCTS = "warehouse.logistics.products";
|
||
|
|
|
||
|
|
private final HttpTransport transport;
|
||
|
|
private final JacksonJsonCodec json;
|
||
|
|
private final HengjianRequestFactory requestFactory;
|
||
|
|
private final HengjianRateLimiter rateLimiter;
|
||
|
|
|
||
|
|
public HengjianAdapter(HttpTransport transport, JacksonJsonCodec json) {
|
||
|
|
this.transport = transport;
|
||
|
|
this.json = json;
|
||
|
|
this.requestFactory = new HengjianRequestFactory(json);
|
||
|
|
this.rateLimiter = new HengjianRateLimiter();
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public String adapterId() {
|
||
|
|
return HengjianConstants.ADAPTER_ID;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public WarehouseProvider provider() {
|
||
|
|
return WarehouseProvider.HENGJIAN;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public EnumSet<WarehouseCapability> capabilities() {
|
||
|
|
return EnumSet.of(
|
||
|
|
WarehouseCapability.DIRECTORY,
|
||
|
|
WarehouseCapability.LOGISTICS_PRODUCT);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ------------------------------------------------------------------ 仓库目录
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 当前组织下的全部仓库({@code POST /v2/warehouse/getWarehouse},无参数)。
|
||
|
|
*
|
||
|
|
* <p>返回的 {@code warehouseCode} 可直接用于下单、库存查询等接口。</p>
|
||
|
|
*/
|
||
|
|
@Override
|
||
|
|
public List<WarehouseFacility> warehouses(WarehouseContext ctx) {
|
||
|
|
String raw = execute(ctx, HengjianConstants.PATH_WAREHOUSE_LIST, null, OP_WAREHOUSE_LIST);
|
||
|
|
List<HengjianWarehouseDtos.WarehouseRow> rows =
|
||
|
|
json.listAt(raw, "data", HengjianWarehouseDtos.WarehouseRow.class);
|
||
|
|
return HengjianDirectoryMapper.toWarehouses(rows);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 承运商 / 物流方式代码({@code POST /v2/carrierGroup/getCarrierGroup},无参数)。
|
||
|
|
*
|
||
|
|
* <p>返回物流产品代码(如 {@code UPS2DAM_HL}),可直接作为下单的 {@code carrierCode}。
|
||
|
|
* 按出现顺序去重。</p>
|
||
|
|
*/
|
||
|
|
@Override
|
||
|
|
public List<String> carriers(WarehouseContext ctx) {
|
||
|
|
return codesOf(fetchAllServices(ctx));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 物流产品清单({@code POST /v2/carrierGroup/getCarrierGroup})。
|
||
|
|
*
|
||
|
|
* <p>恒健这个接口<b>没有仓维度</b>,拿到的是组织级清单;{@code warehouseCode}
|
||
|
|
* 仅用于写进结果字段,便于调用方区分来源。需要「某仓支持哪些渠道」用
|
||
|
|
* {@link #productsOfWarehouse}。</p>
|
||
|
|
*/
|
||
|
|
@Override
|
||
|
|
public List<LogisticsProduct> products(WarehouseContext ctx, String warehouseCode) {
|
||
|
|
return HengjianDirectoryMapper.toProducts(fetchAllServices(ctx), resolveWarehouseCode(ctx, warehouseCode));
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 查询「某仓支持哪些物流渠道」({@code POST
|
||
|
|
* /v2/carrierGroup/getWarehouseCarrierGroupServices})。
|
||
|
|
*
|
||
|
|
* <p>返回形状与 {@link #products} 不同:{@code data} 是以仓代码为键的对象,
|
||
|
|
* 不是数组;只取请求的那个仓,混进别的仓的渠道会让「仓不支持该渠道」
|
||
|
|
* 变成仓方报错。</p>
|
||
|
|
*/
|
||
|
|
public List<LogisticsProduct> productsOfWarehouse(WarehouseContext ctx, String warehouseCode) {
|
||
|
|
String code = resolveWarehouseCode(ctx, warehouseCode);
|
||
|
|
if (code == null) {
|
||
|
|
throw new IllegalArgumentException(
|
||
|
|
"hengjian warehouse carrier services need a warehouseCode; none given and account has none");
|
||
|
|
}
|
||
|
|
List<String> codes = new ArrayList<String>();
|
||
|
|
codes.add(code);
|
||
|
|
Map<String, Object> body = new LinkedHashMap<String, Object>();
|
||
|
|
body.put("warehouseCodes", codes);
|
||
|
|
|
||
|
|
String raw = execute(ctx, HengjianConstants.PATH_WAREHOUSE_CARRIER_GROUP, body,
|
||
|
|
OP_LOGISTICS_PRODUCTS);
|
||
|
|
return HengjianDirectoryMapper.toProducts(servicesOfWarehouse(raw, code), code);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ------------------------------------------------------------------ 内部
|
||
|
|
|
||
|
|
private List<HengjianCarrierGroupDtos.CarrierServiceRow> fetchAllServices(WarehouseContext ctx) {
|
||
|
|
String raw = execute(ctx, HengjianConstants.PATH_CARRIER_GROUP, null, OP_CARRIERS);
|
||
|
|
return json.listAt(raw, "data", HengjianCarrierGroupDtos.CarrierServiceRow.class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** {@code data} 是 {@code 仓代码 → 服务数组} 的对象;只取指定仓。 */
|
||
|
|
private List<HengjianCarrierGroupDtos.CarrierServiceRow> servicesOfWarehouse(String raw, String warehouseCode) {
|
||
|
|
List<HengjianCarrierGroupDtos.CarrierServiceRow> rows =
|
||
|
|
new ArrayList<HengjianCarrierGroupDtos.CarrierServiceRow>();
|
||
|
|
JsonNode data = json.nodeAt(raw, "data");
|
||
|
|
if (data == null || !data.isObject()) {
|
||
|
|
return rows;
|
||
|
|
}
|
||
|
|
JsonNode node = data.get(warehouseCode);
|
||
|
|
if (node == null || !node.isArray()) {
|
||
|
|
return rows;
|
||
|
|
}
|
||
|
|
Iterator<JsonNode> it = node.elements();
|
||
|
|
while (it.hasNext()) {
|
||
|
|
rows.add(json.mapper().convertValue(it.next(),
|
||
|
|
HengjianCarrierGroupDtos.CarrierServiceRow.class));
|
||
|
|
}
|
||
|
|
return rows;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 显式仓代码优先,否则用账户配置的;都没有返回 null,由调用方决定是否拒绝。 */
|
||
|
|
private static String resolveWarehouseCode(WarehouseContext ctx, String warehouseCode) {
|
||
|
|
if (warehouseCode != null && !warehouseCode.trim().isEmpty()) {
|
||
|
|
return warehouseCode.trim();
|
||
|
|
}
|
||
|
|
String configured = ctx.providerWarehouseId();
|
||
|
|
return configured == null || configured.trim().isEmpty() ? null : configured.trim();
|
||
|
|
}
|
||
|
|
|
||
|
|
private static List<String> codesOf(List<HengjianCarrierGroupDtos.CarrierServiceRow> rows) {
|
||
|
|
List<String> out = new ArrayList<String>();
|
||
|
|
if (rows == null) {
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
for (HengjianCarrierGroupDtos.CarrierServiceRow row : rows) {
|
||
|
|
if (row == null || row.code == null || row.code.trim().isEmpty()) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
String code = row.code.trim();
|
||
|
|
if (!out.contains(code)) {
|
||
|
|
out.add(code);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 发请求并校验信封。
|
||
|
|
*
|
||
|
|
* @param body null 表示无参数接口,发送空 JSON 对象
|
||
|
|
*/
|
||
|
|
private String execute(WarehouseContext ctx, String path, Object body, String operation) {
|
||
|
|
rateLimiter.acquire(path, HengjianConstants.PERMITS_PER_SECOND);
|
||
|
|
HttpRequest req = requestFactory.post(ctx.endpointBaseUrl(), path, body, ctx.secret(), ctx, operation);
|
||
|
|
HttpResponse resp = transport.execute(req);
|
||
|
|
String raw = HengjianEnvelope.of(resp, json, operation, ctx.warehouseCode(), ctx.requestId()).body();
|
||
|
|
if (raw == null || raw.trim().isEmpty()) {
|
||
|
|
throw WarehouseException.builder()
|
||
|
|
.message("hengjian " + operation + " returned an empty body")
|
||
|
|
.operation(operation)
|
||
|
|
.warehouseCode(ctx.warehouseCode())
|
||
|
|
.provider(provider().name())
|
||
|
|
.requestId(ctx.requestId())
|
||
|
|
.retryable(true)
|
||
|
|
.build();
|
||
|
|
}
|
||
|
|
return raw;
|
||
|
|
}
|
||
|
|
}
|