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.
179 lines
7.5 KiB
179 lines
7.5 KiB
package common.ecommerce.xincang.adapter; |
|
|
|
import common.ecommerce.http.JsonCodec; |
|
import common.ecommerce.shared.error.ChannelException; |
|
import common.ecommerce.shared.error.ValidationException; |
|
import common.ecommerce.shared.model.Platform; |
|
import common.ecommerce.warehouse.WarehouseProvider; |
|
import common.ecommerce.warehouse.model.OutboundCommand; |
|
import common.ecommerce.warehouse.model.OutboundLine; |
|
import common.ecommerce.warehouse.model.ShipTo; |
|
import common.ecommerce.warehouse.sdk.WarehouseContext; |
|
import common.ecommerce.xincang.XincangConstants; |
|
import common.ecommerce.xincang.dto.XincangDtos; |
|
|
|
import java.time.Instant; |
|
import java.time.LocalDateTime; |
|
import java.time.format.DateTimeFormatter; |
|
import java.util.ArrayList; |
|
import java.util.LinkedHashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
/** |
|
* 信仓出库下单报文映射({@code POST api.add_order_multiple})。 |
|
* |
|
* <p>该接口是 {@code multipart/form-data},{@code products} 字段本身是一段 JSON 字符串。 |
|
* 平台字段名只出现在本类与 {@link XincangDtos}。</p> |
|
* |
|
* <p>文档没有对应字段的就不发:申报信息(HS / 原产国 / 申报价值)、包裹尺寸等 |
|
* 统一模型里有,但信仓这版接口不收,硬塞只会被忽略或报错。</p> |
|
*/ |
|
public final class XincangOrderMapper { |
|
|
|
/** 信仓要求发货时间格式 {@code yyyy-MM-dd HH:mm:ss}(北京时间)。 */ |
|
private static final DateTimeFormatter REQUIRED_DELIVERY_TIME = |
|
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|
/** 地址 1 上限 32 字符,超出部分走 address2。 */ |
|
private static final int ADDRESS_MAX = 32; |
|
/** 订单类型:信仓只接受 common / wayfair。 */ |
|
static final String ORDER_TYPE_COMMON = "common"; |
|
static final String ORDER_TYPE_WAYFAIR = "wayfair"; |
|
/** 签名服务:信仓只收「是 / 否」,模型里没有该字段,默认否。 */ |
|
private static final String SIGN_NOT_REQUIRED = "否"; |
|
|
|
private XincangOrderMapper() { |
|
} |
|
|
|
/** |
|
* 统一命令 → 信仓一单多件下单表单。 |
|
* |
|
* @param ctx 本次调用上下文(取仓编码) |
|
* @param cmd 出库命令 |
|
* @param json 用于序列化 {@code products} |
|
* @throws ValidationException 缺少仓编码或收件人必填项 |
|
*/ |
|
public static Map<String, String> toForm(WarehouseContext ctx, OutboundCommand cmd, JsonCodec json) { |
|
ShipTo shipTo = cmd.shipTo(); |
|
requireText(shipTo.name(), "shipTo.name", ctx); |
|
requireText(shipTo.phone(), "shipTo.phone", ctx); |
|
requireText(shipTo.countryCode(), "shipTo.countryCode", ctx); |
|
requireText(shipTo.stateOrProvince(), "shipTo.stateOrProvince", ctx); |
|
requireText(shipTo.city(), "shipTo.city", ctx); |
|
requireText(shipTo.postalCode(), "shipTo.postalCode", ctx); |
|
requireText(shipTo.addressLine1(), "shipTo.addressLine1", ctx); |
|
|
|
String warehouseCode = firstNonBlank(cmd.warehouseId(), ctx.providerWarehouseId()); |
|
requireText(warehouseCode, "warehouseCode", ctx); |
|
// 信仓 logistics_type 是必填的物流方式字典值,这里用 carrierCode 承载 |
|
requireText(cmd.carrierCode(), "carrierCode (信仓 logistics_type)", ctx); |
|
|
|
Map<String, String> form = new LinkedHashMap<String, String>(); |
|
form.put("order_type", orderTypeOf(cmd.salesPlatform())); |
|
form.put("warehouse_code", warehouseCode); |
|
// ckh = 参考号,信仓要求唯一;用 OMS 出库单号,便于回查 |
|
form.put("ckh", cmd.outboundRequestNo()); |
|
|
|
Address address = splitAddress(shipTo.addressLine1()); |
|
form.put("country", shipTo.countryCode().trim()); |
|
form.put("province", shipTo.stateOrProvince().trim()); |
|
form.put("city", shipTo.city().trim()); |
|
form.put("address", address.line1); |
|
putIfPresent(form, "address2", address.line2); |
|
form.put("postcode", shipTo.postalCode().trim()); |
|
form.put("receive_user", shipTo.name().trim()); |
|
form.put("phone", shipTo.phone().trim()); |
|
putIfPresent(form, "email", shipTo.email()); |
|
form.put("logistics_type", cmd.carrierCode().trim()); |
|
form.put("is_sign", SIGN_NOT_REQUIRED); |
|
form.put("products", productsJson(cmd.lines(), json)); |
|
|
|
// 不传 is_split 时信仓默认 1(拆单);只有调用方显式给值才下发 |
|
if (cmd.splitOrder() != null) { |
|
form.put("is_split", cmd.splitOrder() ? "1" : "0"); |
|
} |
|
Instant latest = cmd.latestShipDate(); |
|
if (latest != null) { |
|
form.put("required_delivery_time", REQUIRED_DELIVERY_TIME.format( |
|
LocalDateTime.ofInstant(latest, XincangConstants.SYSTEM_ZONE))); |
|
} |
|
putIfPresent(form, "remark", cmd.remark()); |
|
return form; |
|
} |
|
|
|
/** 商品明细:{@code [{"product_sku":"...","sku_count":"1"}, ...]}。 */ |
|
static String productsJson(List<OutboundLine> lines, JsonCodec json) { |
|
List<XincangDtos.OrderProduct> products = new ArrayList<XincangDtos.OrderProduct>(lines.size()); |
|
for (OutboundLine line : lines) { |
|
XincangDtos.OrderProduct product = new XincangDtos.OrderProduct(); |
|
product.productSku = skuOf(line); |
|
product.skuCount = String.valueOf(line.quantity()); |
|
products.add(product); |
|
} |
|
return json.toJson(products); |
|
} |
|
|
|
/** 仓侧 SKU 优先;没有映射时用 OMS 主 SKU。 */ |
|
static String skuOf(OutboundLine line) { |
|
String providerSku = line.providerSku(); |
|
if (providerSku != null && !providerSku.trim().isEmpty()) { |
|
return providerSku.trim(); |
|
} |
|
return line.omsSku(); |
|
} |
|
|
|
/** |
|
* 信仓订单类型只接受 {@code common} / {@code wayfair}。 |
|
* |
|
* <p>Wayfair 渠道走 {@code wayfair};其余平台走 {@code common}。 |
|
* 信仓不区分其它平台,不做臆造映射。</p> |
|
*/ |
|
static String orderTypeOf(Platform platform) { |
|
return platform == Platform.WAYFAIR ? ORDER_TYPE_WAYFAIR : ORDER_TYPE_COMMON; |
|
} |
|
|
|
/** 地址超过 32 字符的部分截到 address2,避免被仓侧截断丢信息。 */ |
|
static Address splitAddress(String address) { |
|
String trimmed = address.trim(); |
|
if (trimmed.length() <= ADDRESS_MAX) { |
|
return new Address(trimmed, null); |
|
} |
|
return new Address(trimmed.substring(0, ADDRESS_MAX), trimmed.substring(ADDRESS_MAX)); |
|
} |
|
|
|
/** 拆好的地址。 */ |
|
static final class Address { |
|
final String line1; |
|
final String line2; |
|
|
|
Address(String line1, String line2) { |
|
this.line1 = line1; |
|
this.line2 = line2; |
|
} |
|
} |
|
|
|
private static void putIfPresent(Map<String, String> form, String key, String value) { |
|
if (value != null && !value.trim().isEmpty()) { |
|
form.put(key, value.trim()); |
|
} |
|
} |
|
|
|
private static String requireText(String value, String field, WarehouseContext ctx) { |
|
if (value == null || value.trim().isEmpty()) { |
|
throw new ValidationException(ChannelException.builder() |
|
.message("xincang outbound requires " + field) |
|
.operation("warehouse.outbound.create") |
|
.provider(WarehouseProvider.XINCANG.name()) |
|
.requestId(ctx.requestId()) |
|
.retryable(false)); |
|
} |
|
return value.trim(); |
|
} |
|
|
|
private static String firstNonBlank(String a, String b) { |
|
if (a != null && !a.trim().isEmpty()) { |
|
return a.trim(); |
|
} |
|
return b == null ? null : b.trim(); |
|
} |
|
}
|
|
|