package common.ecommerce.edayun.adapter; import common.ecommerce.edayun.dto.EdayunInboundDtos; import common.ecommerce.warehouse.model.InboundCarton; import common.ecommerce.warehouse.model.InboundCommand; import java.time.LocalDate; import java.time.ZoneOffset; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * 易达云自发入库单映射 * ({@code POST /webApi/entryInfo/v1/saveEntrySpontaneousOrder})。 * *

一个接口吃下新增 / 更新 / 发布,靠 {@code saveType} 区分:

* * *

只下发统一模型有的三组:{@code basicInfo} / {@code logisticsInfo} / * {@code cargoInfos}。头程拖车、报关、清关、海外拖车在标准模型里没有对应字段, * 不下发——这些服务范围本来就由仓方按物流产品判定,缺字段时仓方会报错。

*/ public final class EdayunInboundMapper { /** 保存类型:新增。 */ static final int SAVE_NEW = 0; /** 保存类型:更新。 */ static final int SAVE_UPDATE = 1; /** 保存类型:保存并发布。 */ static final int SAVE_PUBLISH = 2; /** 客户单号长度上限。 */ static final int MAX_CUSTOMER_ORDER_NO = 30; private static final double GRAMS_PER_KG = 1000.0; private EdayunInboundMapper() { } /** * 统一命令 → 保存入库单请求。 * * @param saveType {@link #SAVE_PUBLISH} / {@link #SAVE_UPDATE} * @param entryCode 仓侧入库单号;仅更新时必填 * @param warehouseId 仓方仓标识;数字进 {@code shelfWarehouseId},非数字进 * {@code shelfWarehouseCode} */ public static EdayunInboundDtos.SaveEntryRequest toRequest(InboundCommand cmd, int saveType, String entryCode, String warehouseId) { EdayunInboundDtos.SaveEntryRequest req = new EdayunInboundDtos.SaveEntryRequest(); req.saveType = saveType; String customerOrderNo = cmd.inboundRequestNo(); if (customerOrderNo.length() > MAX_CUSTOMER_ORDER_NO) { throw new IllegalArgumentException( "edayun customerOrderNo must not exceed " + MAX_CUSTOMER_ORDER_NO + " chars, got " + customerOrderNo.length() + ": " + customerOrderNo); } req.customerOrderNo = customerOrderNo; if (saveType == SAVE_UPDATE) { if (!hasText(entryCode)) { throw new IllegalArgumentException( "edayun updating an inbound order requires entryCode (provider entry no)"); } req.entryCode = entryCode.trim(); } req.basicInfo = toBasicInfo(cmd, warehouseId); req.logisticsInfo = toLogisticsInfo(cmd); req.cargoInfos = toCargoInfos(cmd.cartons()); return req; } /** * 基础信息:仓 id 或编码二选一。 * *

易达云的 {@code shelfWarehouseId} 是数字、{@code shelfWarehouseCode} 是编码(如 * {@code USLA})。配成 {@code EDY-WH-1} 这类 OMS 编码时走 code 分支。

*/ private static EdayunInboundDtos.BasicInfo toBasicInfo(InboundCommand cmd, String warehouseId) { EdayunInboundDtos.BasicInfo info = new EdayunInboundDtos.BasicInfo(); String raw = hasText(cmd.warehouseId()) ? cmd.warehouseId().trim() : (hasText(warehouseId) ? warehouseId.trim() : null); Long id = numericOrNull(raw); if (id != null) { info.shelfWarehouseId = id; } else if (raw != null) { info.shelfWarehouseCode = raw; } info.intShippingMethod = cmd.transportType(); info.cabinetType = intOrNull(cmd.cabinetType()); // shippingProductId 是「查询入库物流产品」接口给的 id,标准模型没有对应字段 return info; } /** * 物流信息:只填标准模型有的。 * *

{@code waybill}(快递单号)与 {@code shipCompany}(承运商名)标准模型没有, * 不下发——快递类发布时仓方会报缺字段,由宿主补。不要把柜号之类的 * {@code transportOrderNos} 塞进 {@code waybill},那是另一回事。

*/ private static EdayunInboundDtos.LogisticsInfo toLogisticsInfo(InboundCommand cmd) { EdayunInboundDtos.LogisticsInfo info = new EdayunInboundDtos.LogisticsInfo(); info.cabinetCode = blankToNull(cmd.containerNo()); // 预计离港 ≈ 预计发运日期;预计到港 ≈ 预计到仓日期(口径待仓方确认) info.estimatedDepartureTime = millisOf(cmd.predictionShipDate()); info.estimatedArrivalDate = millisOf(cmd.estimateArriveDate()); return info; } /** * 装箱 → 箱货信息。 * *

统一模型一行是「一个 SKU 在一箱里的数量」,一箱多 SKU 时同一 {@code sid} * 会拆成多行。这里按 {@code sid}(缺失时用箱码)归并成「一箱 = 一个 cargoInfo + * 多条 boxSkuInfoList」,与文档结构对齐。

* *

{@code cargoInfos} 是必填的,而箱规只有 {@code InboundCarton} 才有—— * 调用方只给了 SKU 明细时本地拒绝,不凭空编一个箱子。

*/ static List toCargoInfos(List cartons) { if (cartons == null || cartons.isEmpty()) { throw new IllegalArgumentException( "edayun spontaneous entry requires carton info (cartonNo + box size/weight);" + " product lines alone cannot fill cargoInfos"); } Map byBox = new LinkedHashMap(); for (InboundCarton carton : cartons) { if (carton == null) { continue; } String key = hasText(carton.sid()) ? carton.sid().trim() : carton.cartonNo(); EdayunInboundDtos.CargoInfo box = byBox.get(key); if (box == null) { box = new EdayunInboundDtos.CargoInfo(); box.outsideBoxCode = carton.cartonNo(); box.boxLength = positiveOrNull(carton.lengthCm()); box.boxWidth = positiveOrNull(carton.widthCm()); box.boxHeight = positiveOrNull(carton.heightCm()); // 我们的模型是克,易达云这组字段按 kg(与创建产品口径一致) box.weight = positiveOrNull(carton.weightGrams() / GRAMS_PER_KG); box.boxSkuInfoList = new ArrayList(); byBox.put(key, box); } EdayunInboundDtos.BoxSku sku = new EdayunInboundDtos.BoxSku(); sku.skuCode = carton.providerSku(); sku.skuForecastQuantity = carton.quantity(); box.boxSkuInfoList.add(sku); } return new ArrayList(byBox.values()); } /** 本地日期 → 毫秒时间戳(按 UTC 起算,易达云时间戳不带时区)。 */ static Long millisOf(LocalDate date) { return date == null ? null : date.atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli(); } static Long numericOrNull(String v) { if (!hasText(v)) { return null; } try { return Long.valueOf(v.trim()); } catch (NumberFormatException e) { return null; } } /** 字符串码 → 数字;非数字视为未给。 */ static Integer intOrNull(String v) { if (!hasText(v)) { return null; } try { return Integer.valueOf(v.trim()); } catch (NumberFormatException e) { return null; } } private static Double positiveOrNull(double v) { return v > 0 ? v : null; } private static String blankToNull(String v) { return hasText(v) ? v.trim() : null; } private static boolean hasText(String v) { return v != null && !v.trim().isEmpty(); } }