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.
213 lines
8.5 KiB
213 lines
8.5 KiB
|
2 weeks ago
|
package common.ecommerce.yqn.adapter;
|
||
|
|
|
||
|
|
import common.ecommerce.shared.support.Money;
|
||
|
|
import common.ecommerce.warehouse.model.CancelEligibility;
|
||
|
|
import common.ecommerce.warehouse.model.LabelMode;
|
||
|
|
import common.ecommerce.warehouse.model.OutboundCommand;
|
||
|
|
import common.ecommerce.warehouse.model.OutboundLine;
|
||
|
|
import common.ecommerce.warehouse.model.OutboundOrder;
|
||
|
|
import common.ecommerce.warehouse.model.OutboundStatus;
|
||
|
|
import common.ecommerce.warehouse.model.PackageResult;
|
||
|
|
import common.ecommerce.warehouse.model.ShipTo;
|
||
|
|
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");
|
||
|
|
|
||
|
|
private YqnOutboundMapper() {
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 统一命令 → 运去哪下单请求。 */
|
||
|
|
public static YqnDtos.CreateOrderRequest toRequest(OutboundCommand cmd, String warehouseId) {
|
||
|
|
YqnDtos.CreateOrderRequest req = new YqnDtos.CreateOrderRequest();
|
||
|
|
req.warehouseCode = warehouseId;
|
||
|
|
req.customerOrderNo = cmd.outboundRequestNo();
|
||
|
|
req.referenceNo = cmd.orderRef();
|
||
|
|
req.labelMode = cmd.labelMode().name();
|
||
|
|
req.shippingMethod = cmd.carrier();
|
||
|
|
req.remark = cmd.remark();
|
||
|
|
req.receiver = toReceiver(cmd.shipTo());
|
||
|
|
req.items = toItems(cmd.lines());
|
||
|
|
return req;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static YqnDtos.CreateOrderRequest.Receiver toReceiver(ShipTo shipTo) {
|
||
|
|
YqnDtos.CreateOrderRequest.Receiver r = new YqnDtos.CreateOrderRequest.Receiver();
|
||
|
|
r.name = shipTo.name();
|
||
|
|
r.phone = shipTo.phone();
|
||
|
|
r.email = shipTo.email();
|
||
|
|
r.country = shipTo.countryCode();
|
||
|
|
r.state = shipTo.stateOrProvince();
|
||
|
|
r.city = shipTo.city();
|
||
|
|
r.address1 = shipTo.addressLine1();
|
||
|
|
r.address2 = shipTo.addressLine2();
|
||
|
|
r.zipCode = shipTo.postalCode();
|
||
|
|
return r;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static List<YqnDtos.CreateOrderRequest.OrderItem> toItems(List<OutboundLine> lines) {
|
||
|
|
List<YqnDtos.CreateOrderRequest.OrderItem> items =
|
||
|
|
new ArrayList<YqnDtos.CreateOrderRequest.OrderItem>(lines.size());
|
||
|
|
for (OutboundLine line : lines) {
|
||
|
|
YqnDtos.CreateOrderRequest.OrderItem item = new YqnDtos.CreateOrderRequest.OrderItem();
|
||
|
|
item.sku = line.providerSku() != null ? line.providerSku() : line.omsSku();
|
||
|
|
item.quantity = line.quantity();
|
||
|
|
item.declareName = line.declareName();
|
||
|
|
item.declareNameEn = line.declareNameEn();
|
||
|
|
Money value = line.declareValue();
|
||
|
|
item.declareValue = value == null ? null : value.amount().toPlainString();
|
||
|
|
item.currency = value == null ? null : value.currencyCode();
|
||
|
|
item.hsCode = line.hsCode();
|
||
|
|
item.countryOfOrigin = line.countryOfOrigin();
|
||
|
|
items.add(item);
|
||
|
|
}
|
||
|
|
return items;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 运去哪出库单 → 统一模型。 */
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 面单模式 → 能力要求的模式名。 */
|
||
|
|
public static String labelModeName(LabelMode mode) {
|
||
|
|
return mode == null ? LabelMode.WAREHOUSE_LABEL.name() : mode.name();
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 解析运去哪时间;失败返回 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();
|
||
|
|
}
|
||
|
|
}
|