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.
420 lines
17 KiB
420 lines
17 KiB
package common.ecommerce.yqn.adapter; |
|
|
|
import common.ecommerce.shared.support.Money; |
|
import common.ecommerce.warehouse.model.CancelEligibility; |
|
import common.ecommerce.warehouse.model.OutboundCommand; |
|
import common.ecommerce.warehouse.model.OutboundLine; |
|
import common.ecommerce.warehouse.model.OutboundOrder; |
|
import common.ecommerce.warehouse.model.OutboundPackageSpec; |
|
import common.ecommerce.warehouse.model.OutboundStatus; |
|
import common.ecommerce.warehouse.model.PackageResult; |
|
import common.ecommerce.warehouse.model.ShipTo; |
|
import common.ecommerce.shared.model.Platform; |
|
import common.ecommerce.warehouse.WarehouseProvider; |
|
import common.ecommerce.yqn.dto.YqnDtos; |
|
|
|
import java.time.Instant; |
|
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><b>平台字段名只出现在本类与 {@link YqnDtos}</b>;门面与领域模型不感知运去哪。 |
|
* 字段语义待打样核对(Spec §8.6),核对后只改这里。</p> |
|
*/ |
|
public final class YqnOutboundMapper { |
|
|
|
/** 运去哪时间格式;待核对。 */ |
|
private static final DateTimeFormatter TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
|
|
|
/** 运去哪时间按仓所在地解释;账户未配时回退北京时区。 */ |
|
private static final ZoneId DEFAULT_ZONE = ZoneId.of("Asia/Shanghai"); |
|
|
|
/** 运去哪下单币种默认 USD。 */ |
|
private static final String DEFAULT_CURRENCY = "USD"; |
|
|
|
private YqnOutboundMapper() { |
|
} |
|
|
|
/** 统一命令 → 运去哪创建销售订单请求(API 123)。 */ |
|
public static YqnDtos.CreateOrderRequest toRequest(OutboundCommand cmd, String warehouseCode) { |
|
YqnDtos.CreateOrderRequest req = new YqnDtos.CreateOrderRequest(); |
|
req.orderNo = cmd.outboundRequestNo(); |
|
req.poNumber = cmd.orderRef(); |
|
req.warehouseCode = warehouseCode; |
|
req.carrier = cmd.carrier(); |
|
req.carrierCode = cmd.carrierCode(); |
|
req.currency = cmd.currency() == null ? DEFAULT_CURRENCY : cmd.currency(); |
|
req.shipServiceLevel = cmd.shipServiceLevel(); |
|
req.latestShipDate = formatTime(cmd.latestShipDate()); |
|
req.estimatedDeliveryTime = formatTime(cmd.estimatedDeliveryTime()); |
|
req.salesPlatform = salesPlatformOf(cmd.salesPlatform()); |
|
req.remark = cmd.remark(); |
|
|
|
ShipTo shipTo = cmd.shipTo(); |
|
req.shipToCountry = shipTo.countryCode(); |
|
req.shipToState = shipTo.stateOrProvince(); |
|
req.shipToCity = shipTo.city(); |
|
req.shipToPostal = shipTo.postalCode(); |
|
req.shipToAddress1 = shipTo.addressLine1(); |
|
req.shipToAddress2 = shipTo.addressLine2(); |
|
req.shipToAddress3 = shipTo.addressLine3(); |
|
req.shipToContact = shipTo.name(); |
|
req.shipToTelephone = shipTo.phone(); |
|
req.buyerName = shipTo.name(); |
|
req.buyerEmail = shipTo.email(); |
|
|
|
req.productItems = toProductItems(cmd.lines()); |
|
req.packageItems = toPackageItems(cmd.packages()); |
|
return req; |
|
} |
|
|
|
/** 仓库域 Platform → 运去哪 salesPlatform 码;无映射按「其他」99。 */ |
|
static int salesPlatformOf(Platform platform) { |
|
if (platform == null) { |
|
throw new IllegalArgumentException( |
|
"salesPlatform is required; warehouse routes and validates by platform"); |
|
} |
|
switch (platform) { |
|
case AMAZON_SC: |
|
case AMAZON_VC: |
|
return 1; |
|
case WALMART_SC: |
|
case WALMART_1P: |
|
return 2; |
|
case BESTBUY: |
|
return 3; |
|
case WAYFAIR: |
|
return 5; |
|
case TARGET: |
|
return 6; |
|
case TEMU: |
|
return 9; |
|
case TIKTOK: |
|
return 10; |
|
case EBAY: |
|
return 11; |
|
case SHEIN: |
|
return 14; |
|
case ALIEXPRESS: |
|
return 15; |
|
default: |
|
return 99; |
|
} |
|
} |
|
|
|
private static List<YqnDtos.CreateOrderRequest.ProductItem> toProductItems(List<OutboundLine> lines) { |
|
List<YqnDtos.CreateOrderRequest.ProductItem> items = |
|
new ArrayList<YqnDtos.CreateOrderRequest.ProductItem>(lines.size()); |
|
for (OutboundLine line : lines) { |
|
YqnDtos.CreateOrderRequest.ProductItem item = new YqnDtos.CreateOrderRequest.ProductItem(); |
|
item.sku = line.providerSku() != null ? line.providerSku() : line.omsSku(); |
|
item.quantity = line.quantity(); |
|
item.lineNo = line.lineNo(); |
|
Money value = line.declareValue(); |
|
if (value != null) { |
|
item.skuAmount = value.amount().doubleValue(); |
|
} |
|
items.add(item); |
|
} |
|
return items; |
|
} |
|
|
|
private static List<YqnDtos.CreateOrderRequest.PackageItem> toPackageItems( |
|
List<OutboundPackageSpec> packages) { |
|
if (packages == null || packages.isEmpty()) { |
|
return null; |
|
} |
|
List<YqnDtos.CreateOrderRequest.PackageItem> out = |
|
new ArrayList<YqnDtos.CreateOrderRequest.PackageItem>(packages.size()); |
|
for (OutboundPackageSpec spec : packages) { |
|
YqnDtos.CreateOrderRequest.PackageItem item = new YqnDtos.CreateOrderRequest.PackageItem(); |
|
item.refNo = spec.refNo(); |
|
item.length = spec.lengthCm(); |
|
item.width = spec.widthCm(); |
|
item.height = spec.heightCm(); |
|
item.weight = spec.weightGrams(); |
|
if (!spec.materialTypes().isEmpty()) { |
|
item.materialType = new ArrayList<Integer>(spec.materialTypes()); |
|
} |
|
if (!spec.skuInfos().isEmpty()) { |
|
List<YqnDtos.CreateOrderRequest.PackageSku> skus = |
|
new ArrayList<YqnDtos.CreateOrderRequest.PackageSku>(spec.skuInfos().size()); |
|
for (OutboundPackageSpec.SkuInfo sku : spec.skuInfos()) { |
|
YqnDtos.CreateOrderRequest.PackageSku one = |
|
new YqnDtos.CreateOrderRequest.PackageSku(); |
|
one.sku = sku.sku(); |
|
one.qty = sku.qty(); |
|
skus.add(one); |
|
} |
|
item.skuInfos = skus; |
|
} |
|
out.add(item); |
|
} |
|
return out; |
|
} |
|
|
|
/** 运去哪时间格式 {@code yyyy-MM-dd HH:mm:ss}。 */ |
|
static String formatTime(Instant instant) { |
|
return instant == null ? null : TIME_FORMAT.format(instant.atZone(DEFAULT_ZONE)); |
|
} |
|
|
|
/** 订单详情 → 统一出库单(API 128)。 */ |
|
public static OutboundOrder toOrder(YqnDtos.OrderDetail detail, String warehouseCode, ZoneId zone) { |
|
List<YqnDtos.OrderDetail.ShipmentPackage> shippedPackages = shippedPackagesOf(detail); |
|
return OutboundOrder.builder() |
|
.warehouseCode(firstNonBlank(detail.warehouseCode, warehouseCode)) |
|
.outboundRequestNo(firstNonBlank(detail.orderNo, null)) |
|
.orderRef(null) // API 128 不返回平台单号 |
|
.status(toStatus(detail.orderStatusEnum)) |
|
.rawStatus(detail.orderStatusEnum) |
|
.carrier(packagesCarrierOf(shippedPackages, detail.carrierCode)) |
|
.trackingNumber(firstTrackingOf(shippedPackages)) |
|
.carrierCode(detail.carrierCode) |
|
.currency(detail.currency) |
|
.paidAt(parseTime(detail.paidAt, zone)) |
|
.shippedAt(firstShipmentTime(shippedPackages, zone)) |
|
.lines(toLines(detail.productItems)) |
|
.shipTo(toShipTo(detail)) |
|
.exceptionCode(exceptionOf(detail.errorCode)) |
|
.estimatedShippingFee(moneyOrNull(detail.estimatedShippingFee, detail.currency)) |
|
.sourceProvider(WarehouseProvider.YQN) |
|
.build(); |
|
} |
|
|
|
/** |
|
* 发货包裹 → 统一包裹列表(API 128)。 |
|
* |
|
* <p>{@code shipmentPackages} 里 {@code packageFlag=1} 是面单信息行, |
|
* 同一个 trackingNo 可能与实际包裹重复出现;按 trackingNo 去重, |
|
* 面单行没有 trackingNo 时跳过,避免虚增包裹数。</p> |
|
*/ |
|
public static List<PackageResult> toPackages(YqnDtos.OrderDetail detail) { |
|
if (detail.shipmentPackages == null || detail.shipmentPackages.isEmpty()) { |
|
return Collections.emptyList(); |
|
} |
|
// 两遍:先放实际包裹(packageFlag=0),再补面单信息行(packageFlag=1), |
|
// 同一个 trackingNo 只保留一条,且以实际包裹为准 |
|
Map<String, PackageResult> byKey = new LinkedHashMap<String, PackageResult>(); |
|
for (YqnDtos.OrderDetail.ShipmentPackage pkg : detail.shipmentPackages) { |
|
if (isActualPackage(pkg)) { |
|
putPackage(byKey, pkg, detail.orderNo); |
|
} |
|
} |
|
for (YqnDtos.OrderDetail.ShipmentPackage pkg : detail.shipmentPackages) { |
|
if (!isActualPackage(pkg)) { |
|
putPackage(byKey, pkg, detail.orderNo); |
|
} |
|
} |
|
return new ArrayList<PackageResult>(byKey.values()); |
|
} |
|
|
|
private static void putPackage(Map<String, PackageResult> byKey, |
|
YqnDtos.OrderDetail.ShipmentPackage pkg, String orderNo) { |
|
String tracking = pkg.trackingNo == null ? null : pkg.trackingNo.trim(); |
|
String key = hasText(tracking) ? tracking : "SKU-" + pkg.erpSku; |
|
if (byKey.containsKey(key)) { |
|
return; |
|
} |
|
byKey.put(key, PackageResult.builder() |
|
.packageId(key) |
|
.outboundRequestNo(orderNo) |
|
.trackingNumber(tracking) |
|
.carrier(pkg.carrier) |
|
.carrierServiceCode(pkg.carrierServiceCode) |
|
.erpSku(pkg.erpSku) |
|
.shippedQuantity(pkg.shipedQuantity) |
|
.shippedAt(parseTime(pkg.shipmentDate, DEFAULT_ZONE)) |
|
.build()); |
|
} |
|
|
|
/** 是否实际包裹(packageFlag=0 或缺失)。 */ |
|
private static boolean isActualPackage(YqnDtos.OrderDetail.ShipmentPackage pkg) { |
|
return pkg.packageFlag == null || pkg.packageFlag == 0; |
|
} |
|
|
|
/** 实际包裹优先;只用于取顶层承运商与跟踪号。 */ |
|
static List<YqnDtos.OrderDetail.ShipmentPackage> shippedPackagesOf(YqnDtos.OrderDetail detail) { |
|
if (detail.shipmentPackages == null) { |
|
return Collections.emptyList(); |
|
} |
|
List<YqnDtos.OrderDetail.ShipmentPackage> actual = |
|
new ArrayList<YqnDtos.OrderDetail.ShipmentPackage>(); |
|
for (YqnDtos.OrderDetail.ShipmentPackage pkg : detail.shipmentPackages) { |
|
if (isActualPackage(pkg)) { |
|
actual.add(pkg); |
|
} |
|
} |
|
return actual.isEmpty() ? detail.shipmentPackages : actual; |
|
} |
|
|
|
private static String packagesCarrierOf(List<YqnDtos.OrderDetail.ShipmentPackage> packages, |
|
String fallbackCarrierCode) { |
|
for (YqnDtos.OrderDetail.ShipmentPackage pkg : packages) { |
|
if (hasText(pkg.carrier)) { |
|
return pkg.carrier; |
|
} |
|
} |
|
return fallbackCarrierCode; |
|
} |
|
|
|
private static String firstTrackingOf(List<YqnDtos.OrderDetail.ShipmentPackage> packages) { |
|
for (YqnDtos.OrderDetail.ShipmentPackage pkg : packages) { |
|
if (hasText(pkg.trackingNo)) { |
|
return pkg.trackingNo; |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
private static Instant firstShipmentTime(List<YqnDtos.OrderDetail.ShipmentPackage> packages, ZoneId zone) { |
|
for (YqnDtos.OrderDetail.ShipmentPackage pkg : packages) { |
|
Instant time = parseTime(pkg.shipmentDate, zone); |
|
if (time != null) { |
|
return time; |
|
} |
|
} |
|
return null; |
|
} |
|
|
|
private static List<OutboundLine> toLines(List<YqnDtos.OrderDetail.DetailProductItem> items) { |
|
if (items == null || items.isEmpty()) { |
|
return Collections.emptyList(); |
|
} |
|
List<OutboundLine> lines = new ArrayList<OutboundLine>(items.size()); |
|
for (YqnDtos.OrderDetail.DetailProductItem item : items) { |
|
if (!hasText(item.sku)) { |
|
continue; |
|
} |
|
lines.add(OutboundLine.builder() |
|
.lineNo(firstNonBlank(item.lineNo, item.sku)) |
|
.omsSku(item.sku) |
|
.providerSku(item.sku) |
|
.quantity(item.quantity == null ? 0 : item.quantity) |
|
.build()); |
|
} |
|
return lines; |
|
} |
|
|
|
/** 详情里没有任何地址字段时返回 null,避免把「空地址」当成真实地址回传给业务。 */ |
|
private static ShipTo toShipTo(YqnDtos.OrderDetail detail) { |
|
if (!hasText(detail.shipToCountry) && !hasText(detail.shipToAddress1) |
|
&& !hasText(detail.shipToCity) && !hasText(detail.shipToContact)) { |
|
return null; |
|
} |
|
return ShipTo.builder() |
|
.name(detail.shipToContact) |
|
.phone(detail.shipToTelephone) |
|
.countryCode(detail.shipToCountry) |
|
.stateOrProvince(detail.shipToState) |
|
.city(detail.shipToCity) |
|
.addressLine1(detail.shipToAddress1) |
|
.addressLine2(detail.shipToAddress2) |
|
.addressLine3(detail.shipToAddress3) |
|
.postalCode(detail.shipToPostal) |
|
.build(); |
|
} |
|
|
|
/** {@code unexception} 或空视为无异常。 */ |
|
private static String exceptionOf(String errorCode) { |
|
if (!hasText(errorCode) || "unexception".equalsIgnoreCase(errorCode.trim())) { |
|
return null; |
|
} |
|
return errorCode.trim(); |
|
} |
|
|
|
private static Money moneyOrNull(Double amount, String currency) { |
|
if (amount == null) { |
|
return null; |
|
} |
|
return Money.of(java.math.BigDecimal.valueOf(amount), |
|
currency == null || currency.trim().isEmpty() ? "USD" : currency.trim()); |
|
} |
|
|
|
private static boolean hasText(String v) { |
|
return v != null && !v.trim().isEmpty(); |
|
} |
|
|
|
/** 运去哪状态 → 统一状态;未知按 {@link OutboundStatus#SUBMITTED}。待打样核对映射表。 */ |
|
public static OutboundStatus toStatus(String rawStatus) { |
|
if (rawStatus == null) { |
|
return OutboundStatus.SUBMITTED; |
|
} |
|
String s = rawStatus.trim().toUpperCase(); |
|
if (s.isEmpty()) { |
|
return OutboundStatus.SUBMITTED; |
|
} |
|
if (s.contains("CANCEL")) { |
|
return OutboundStatus.CANCELLED; |
|
} |
|
if (s.contains("SHIP") || s.contains("SENT") || s.contains("DELIVER")) { |
|
return OutboundStatus.SHIPPED; |
|
} |
|
if (s.contains("LABEL")) { |
|
return OutboundStatus.LABEL_READY; |
|
} |
|
if (s.contains("PACK")) { |
|
return OutboundStatus.PACKED; |
|
} |
|
if (s.contains("PICK")) { |
|
return OutboundStatus.PICKING; |
|
} |
|
if (s.contains("ACCEPT")) { |
|
return OutboundStatus.ACCEPTED; |
|
} |
|
if (s.contains("ERROR") || s.contains("EXCEPTION") || s.contains("FAIL")) { |
|
return OutboundStatus.EXCEPTION; |
|
} |
|
return OutboundStatus.SUBMITTED; |
|
} |
|
|
|
/** 运去哪取消类型 → 统一三态;未知按 BLOCKED,宁可拦住也不误放行。 */ |
|
public static CancelEligibility toEligibility(String cancelType) { |
|
if (cancelType == null) { |
|
return CancelEligibility.BLOCKED; |
|
} |
|
String s = cancelType.trim().toUpperCase(); |
|
if (s.contains("FREE")) { |
|
return CancelEligibility.FREE; |
|
} |
|
if (s.contains("CHARGE")) { |
|
return CancelEligibility.CHARGED; |
|
} |
|
if (s.contains("BLOCK")) { |
|
return CancelEligibility.BLOCKED; |
|
} |
|
return CancelEligibility.BLOCKED; |
|
} |
|
|
|
/** 解析运去哪时间;失败返回 null,不因时间格式阻断业务。 */ |
|
static Instant parseTime(String value, ZoneId zone) { |
|
if (value == null || value.trim().isEmpty()) { |
|
return null; |
|
} |
|
String v = value.trim(); |
|
try { |
|
return java.time.LocalDateTime.parse(v, TIME_FORMAT) |
|
.atZone(zone == null ? DEFAULT_ZONE : zone).toInstant(); |
|
} catch (DateTimeParseException e) { |
|
try { |
|
return Instant.parse(v); |
|
} catch (DateTimeParseException ignored) { |
|
return null; |
|
} |
|
} |
|
} |
|
|
|
private static String firstNonBlank(String a, String b) { |
|
if (a != null && !a.trim().isEmpty()) { |
|
return a.trim(); |
|
} |
|
return b == null ? null : b.trim(); |
|
} |
|
}
|
|
|