Browse Source
- 路径 /inventory/BatchQueryInventory → /v2/inventory/get_inventory_by_page - 方法是 GET,但文档把参数写在 Body 里;按重复 query 参数下发 (query.warehouseCodes=a&query.warehouseCodes=b),待联调核对 - 响应是 Spring Data 分页结构,翻页以 data.last 为准 - 该接口的 code 是字符串,其它接口是数字;信封按数值解析,两种都能过 库存模型补齐 API 107 的维度: - InventorySnapshot 补 inventoryType(可售/不可售) / parentSku / saleType / standardQuantity / 箱套四个库存字段 - InventoryQuery 补 warehouseCodes / inventoryType 两条防护: - warehouseCodes 必填;没给时用账户配置的仓,都没有直接拒绝, 空列表会把整个租户的库存都拉下来 - inventory_current 文档标为 string,按 BigDecimal 解析,脏值归 0 同步策略固定 FULL_PULL_DIFF:运去哪没有按时间增量查的接口。 Co-Authored-By: Claude <noreply@anthropic.com>feature/supplier-lingxing
7 changed files with 487 additions and 4 deletions
@ -0,0 +1,180 @@ |
|||||||
|
package common.ecommerce.yqn.adapter; |
||||||
|
|
||||||
|
import common.ecommerce.shared.support.Page; |
||||||
|
import common.ecommerce.warehouse.model.InventoryQuery; |
||||||
|
import common.ecommerce.warehouse.model.InventorySnapshot; |
||||||
|
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.LinkedHashMap; |
||||||
|
import java.util.List; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* 运去哪库存报文映射(API 107)。 |
||||||
|
*/ |
||||||
|
public final class YqnInventoryMapper { |
||||||
|
|
||||||
|
/** 页面大小上限;文档写「默认最高 500」。 */ |
||||||
|
static final int MAX_ROWS = 500; |
||||||
|
|
||||||
|
private static final DateTimeFormatter TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
||||||
|
private static final ZoneId DEFAULT_ZONE = ZoneId.of("Asia/Shanghai"); |
||||||
|
|
||||||
|
private YqnInventoryMapper() { |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 统一查询条件 → GET 参数。 |
||||||
|
* |
||||||
|
* <p>运去哪要求 {@code warehouseCodes} 必填;没给时用账户配置的仓, |
||||||
|
* 两者都没有就拒绝——空列表会把整个租户的库存都拉下来。</p> |
||||||
|
*/ |
||||||
|
public static Map<String, List<String>> toParams(InventoryQuery query, String defaultWarehouseCode) { |
||||||
|
List<String> codes = new ArrayList<String>(); |
||||||
|
if (query.warehouseCodes() != null && !query.warehouseCodes().isEmpty()) { |
||||||
|
codes.addAll(query.warehouseCodes()); |
||||||
|
} else if (defaultWarehouseCode != null && !defaultWarehouseCode.trim().isEmpty()) { |
||||||
|
codes.add(defaultWarehouseCode.trim()); |
||||||
|
} |
||||||
|
if (codes.isEmpty()) { |
||||||
|
throw new IllegalArgumentException( |
||||||
|
"yqn inventory query requires warehouseCodes; refusing to query without a warehouse"); |
||||||
|
} |
||||||
|
|
||||||
|
Map<String, List<String>> params = new LinkedHashMap<String, List<String>>(); |
||||||
|
params.put("query.warehouseCodes", codes); |
||||||
|
if (query.inventoryType() != null) { |
||||||
|
params.put("query.inventoryType", single(String.valueOf(query.inventoryType()))); |
||||||
|
} |
||||||
|
if (query.pageSize() > 0) { |
||||||
|
params.put("query.rows", single(String.valueOf(Math.min(query.pageSize(), MAX_ROWS)))); |
||||||
|
} |
||||||
|
if (query.cursor() != null && !query.cursor().trim().isEmpty()) { |
||||||
|
params.put("query.page", single(query.cursor().trim())); |
||||||
|
} |
||||||
|
return params; |
||||||
|
} |
||||||
|
|
||||||
|
/** 一条库存记录 → 统一快照。 */ |
||||||
|
public static InventorySnapshot toSnapshot(YqnDtos.InventoryRow row, String fallbackWarehouseCode, |
||||||
|
ZoneId zone) { |
||||||
|
String warehouseCode = hasText(row.warehouseCode) ? row.warehouseCode.trim() |
||||||
|
: fallbackWarehouseCode; |
||||||
|
String sku = row.sku == null ? null : row.sku.trim(); |
||||||
|
if (!hasText(sku)) { |
||||||
|
// 没有 SKU 的库存行无法落库,跳过比抛异常更合适:整页不该被一行脏数据废掉
|
||||||
|
return null; |
||||||
|
} |
||||||
|
Instant updatedAt = parseTime(row.updatedAt, zone); |
||||||
|
return InventorySnapshot.builder() |
||||||
|
.warehouseCode(warehouseCode) |
||||||
|
.providerSku(sku) |
||||||
|
.omsSku(sku) |
||||||
|
.available(intOf(row.inventoryAvailable)) |
||||||
|
.onHand(intOf(row.inventoryCurrent)) |
||||||
|
.inTransit(intOf(row.inventoryTransport)) |
||||||
|
.reserved(intOf(row.inventoryReserved)) |
||||||
|
.inventoryType(intOf(row.inventoryType)) |
||||||
|
.parentSku(row.parentSku) |
||||||
|
.saleType(intOf(row.saleType)) |
||||||
|
.standardQuantity(intOf(row.standardQuantity)) |
||||||
|
.packageAvailable(intOf(row.inventoryPcsAvailable)) |
||||||
|
.packageReserved(intOf(row.inventoryPcsReserved)) |
||||||
|
.packageInTransit(intOf(row.inventoryPcsTransport)) |
||||||
|
.packageCurrent(intOf(row.inventoryPcsCurrent)) |
||||||
|
// 没有更新时间时用查询时刻兜底:快照必须带 asOfUtc 才能与逻辑库存对齐
|
||||||
|
.asOfUtc(updatedAt == null ? Instant.now() : updatedAt) |
||||||
|
.build(); |
||||||
|
} |
||||||
|
|
||||||
|
/** 解析一整页。 */ |
||||||
|
public static Page<InventorySnapshot> toPage(YqnDtos.InventoryPage page, String fallbackWarehouseCode, |
||||||
|
ZoneId zone, String requestedPage) { |
||||||
|
if (page == null || page.data == null || page.data.content == null) { |
||||||
|
return Page.empty(); |
||||||
|
} |
||||||
|
List<InventorySnapshot> items = new ArrayList<InventorySnapshot>(page.data.content.size()); |
||||||
|
for (YqnDtos.InventoryRow row : page.data.content) { |
||||||
|
InventorySnapshot snapshot = toSnapshot(row, fallbackWarehouseCode, zone); |
||||||
|
if (snapshot != null) { |
||||||
|
items.add(snapshot); |
||||||
|
} |
||||||
|
} |
||||||
|
String next = nextCursor(page, requestedPage); |
||||||
|
return next == null ? Page.last(items) : Page.of(items, next); |
||||||
|
} |
||||||
|
|
||||||
|
/** 翻页:以仓返回的 {@code last} 为准;缺失时用页码与总页数比较。 */ |
||||||
|
private static String nextCursor(YqnDtos.InventoryPage page, String requestedPage) { |
||||||
|
if (page.data.last != null && page.data.last) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
int currentPage = pageOf(requestedPage); |
||||||
|
if (page.data.totalPages != null) { |
||||||
|
return currentPage >= page.data.totalPages ? null : String.valueOf(currentPage + 1); |
||||||
|
} |
||||||
|
// 没有分页元数据时按「本页是否满页」推断,避免死循环
|
||||||
|
int size = page.data.content == null ? 0 : page.data.content.size(); |
||||||
|
int pageSize = page.data.size == null ? size : page.data.size; |
||||||
|
return size == 0 || size < pageSize ? null : String.valueOf(currentPage + 1); |
||||||
|
} |
||||||
|
|
||||||
|
static int pageOf(String cursor) { |
||||||
|
if (cursor == null || cursor.trim().isEmpty()) { |
||||||
|
return 1; |
||||||
|
} |
||||||
|
try { |
||||||
|
return Integer.parseInt(cursor.trim()); |
||||||
|
} catch (NumberFormatException e) { |
||||||
|
return 1; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** {@code inventory_current} 在文档里是 string,可能是小数或带单位,按数值截断。 */ |
||||||
|
static int intOf(String value) { |
||||||
|
if (value == null || value.trim().isEmpty()) { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
try { |
||||||
|
return new java.math.BigDecimal(value.trim()).intValue(); |
||||||
|
} catch (NumberFormatException e) { |
||||||
|
return 0; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static int intOf(Integer value) { |
||||||
|
return value == null ? 0 : value; |
||||||
|
} |
||||||
|
|
||||||
|
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) |
||||||
|
.atZone(zone == null ? DEFAULT_ZONE : zone).toInstant(); |
||||||
|
} catch (DateTimeParseException e) { |
||||||
|
try { |
||||||
|
return Instant.parse(v); |
||||||
|
} catch (DateTimeParseException ignored) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private static List<String> single(String v) { |
||||||
|
List<String> list = new ArrayList<String>(1); |
||||||
|
list.add(v); |
||||||
|
return list; |
||||||
|
} |
||||||
|
|
||||||
|
private static boolean hasText(String v) { |
||||||
|
return v != null && !v.trim().isEmpty(); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue