|
|
|
|
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.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 运去哪报文 → 统一模型。
|
|
|
|
|
*
|
|
|
|
|
* <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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 运去哪出库单 → 统一模型。 */
|
|
|
|
|
public static OutboundOrder toOrder(YqnDtos.OrderInfo info, String warehouseCode, ZoneId zone) {
|
|
|
|
|
return OutboundOrder.builder()
|
|
|
|
|
.warehouseCode(warehouseCode)
|
|
|
|
|
.outboundRequestNo(firstNonBlank(info.customerOrderNo, info.orderNo))
|
|
|
|
|
.providerOrderId(info.orderNo)
|
|
|
|
|
.orderRef(info.referenceNo)
|
|
|
|
|
.labelMode(null) // 查询结果不含面单模式,由 OMS 侧记录
|
|
|
|
|
.status(toStatus(info.status))
|
|
|
|
|
.rawStatus(info.status)
|
|
|
|
|
.carrier(info.carrier)
|
|
|
|
|
.trackingNumber(info.trackingNumber)
|
|
|
|
|
.requestedAt(parseTime(info.createTime, zone))
|
|
|
|
|
.shippedAt(parseTime(info.shipTime, zone))
|
|
|
|
|
.sourceProvider(WarehouseProvider.YQN)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 出库单 → 包裹列表;一单多包裹时逐个返回。 */
|
|
|
|
|
public static List<PackageResult> toPackages(YqnDtos.OrderInfo info) {
|
|
|
|
|
if (info.packages == null || info.packages.isEmpty()) {
|
|
|
|
|
if (info.trackingNumber == null) {
|
|
|
|
|
return Collections.emptyList();
|
|
|
|
|
}
|
|
|
|
|
return Collections.singletonList(PackageResult.builder()
|
|
|
|
|
.packageId(firstNonBlank(info.orderNo, info.customerOrderNo))
|
|
|
|
|
.outboundRequestNo(firstNonBlank(info.customerOrderNo, info.orderNo))
|
|
|
|
|
.trackingNumber(info.trackingNumber)
|
|
|
|
|
.carrier(info.carrier)
|
|
|
|
|
.labelRef(info.labelUrl)
|
|
|
|
|
.build());
|
|
|
|
|
}
|
|
|
|
|
List<PackageResult> out = new ArrayList<PackageResult>(info.packages.size());
|
|
|
|
|
for (YqnDtos.Package pkg : info.packages) {
|
|
|
|
|
out.add(PackageResult.builder()
|
|
|
|
|
.packageId(firstNonBlank(pkg.packageNo, info.orderNo))
|
|
|
|
|
.outboundRequestNo(firstNonBlank(info.customerOrderNo, info.orderNo))
|
|
|
|
|
.trackingNumber(pkg.trackingNumber)
|
|
|
|
|
.carrier(pkg.carrier)
|
|
|
|
|
.labelRef(pkg.labelUrl)
|
|
|
|
|
.build());
|
|
|
|
|
}
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** 运去哪状态 → 统一状态;未知按 {@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();
|
|
|
|
|
}
|
|
|
|
|
}
|