Browse Source
- 实时库存 GET api.stocklist:warehouse_code 必填,支持 product_no / page / limit - 库龄列表 GET api.stockagelist:bill_date 为「起 - 止」单值,支持 key / is_need_fee_rate core 只新增、不修改: - InventorySnapshot 追加 productName / pendingOutbound / alertQuantity / volumeCbm (信仓库存行有、原模型无处承载) - InventoryCapable 增加 default queryAges(ctx, InventoryAgeQuery), WarehouseOperations.InventoryView 增加 queryAges - 新增 InventoryAgeQuery / InventoryAgeRecord:库龄是计费视角数据 (按计费日 × 仓 × SKU × 柜号计费),与库存快照分开建模 信仓实现要点: - 实时库存只接受单个 warehouse_code;传多个显式拒绝,不静默只查第一个 - syncStrategy = FULL_PULL_DIFF:该接口没有时间增量参数 - 分页游标是字符串化页码,按「本页是否满页」判断下一页(count 不可靠) - 库龄行缺少计费日期时跳过,不猜日期;时间统一按北京时间解析成 UTC Co-Authored-By: Claude Code <noreply@anthropic.com>feature/supplier-lingxing
14 changed files with 1109 additions and 13 deletions
@ -0,0 +1,131 @@
@@ -0,0 +1,131 @@
|
||||
package common.ecommerce.warehouse.model; |
||||
|
||||
import java.time.LocalDate; |
||||
|
||||
/** |
||||
* 库龄查询条件。 |
||||
* |
||||
* <p>库龄按计费日期区间查,必须给区间;不给会拉到全部历史,对账时无从解释。</p> |
||||
*/ |
||||
public final class InventoryAgeQuery { |
||||
|
||||
private final LocalDate startDate; |
||||
private final LocalDate endDate; |
||||
/** 仓 code;null 表示用账户配置的仓。 */ |
||||
private final String warehouseCode; |
||||
/** 产品 SKU;null 表示不限。 */ |
||||
private final String providerSku; |
||||
private final int pageSize; |
||||
private final String cursor; |
||||
/** 是否返回费率与计费体积;null 表示按仓默认。 */ |
||||
private final Boolean includeFeeRate; |
||||
|
||||
private InventoryAgeQuery(Builder b) { |
||||
if (b.startDate == null || b.endDate == null) { |
||||
throw new IllegalArgumentException("startDate and endDate are required"); |
||||
} |
||||
if (b.endDate.isBefore(b.startDate)) { |
||||
throw new IllegalArgumentException("endDate must not be earlier than startDate"); |
||||
} |
||||
this.startDate = b.startDate; |
||||
this.endDate = b.endDate; |
||||
this.warehouseCode = b.warehouseCode; |
||||
this.providerSku = b.providerSku; |
||||
this.pageSize = b.pageSize; |
||||
this.cursor = b.cursor; |
||||
this.includeFeeRate = b.includeFeeRate; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
/** 计费日期区间起点(含)。 */ |
||||
public LocalDate startDate() { |
||||
return startDate; |
||||
} |
||||
|
||||
/** 计费日期区间终点(含)。 */ |
||||
public LocalDate endDate() { |
||||
return endDate; |
||||
} |
||||
|
||||
/** 仓 code;null 表示用账户配置的仓。 */ |
||||
public String warehouseCode() { |
||||
return warehouseCode; |
||||
} |
||||
|
||||
/** 产品 SKU;null 表示不限。 */ |
||||
public String providerSku() { |
||||
return providerSku; |
||||
} |
||||
|
||||
public int pageSize() { |
||||
return pageSize; |
||||
} |
||||
|
||||
public String cursor() { |
||||
return cursor; |
||||
} |
||||
|
||||
/** 是否返回费率;null 表示按仓默认。 */ |
||||
public Boolean includeFeeRate() { |
||||
return includeFeeRate; |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private LocalDate startDate; |
||||
private LocalDate endDate; |
||||
private String warehouseCode; |
||||
private String providerSku; |
||||
private int pageSize = 100; |
||||
private String cursor; |
||||
private Boolean includeFeeRate; |
||||
|
||||
public Builder startDate(LocalDate v) { |
||||
this.startDate = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder endDate(LocalDate v) { |
||||
this.endDate = v; |
||||
return this; |
||||
} |
||||
|
||||
/** 计费日期区间。 */ |
||||
public Builder dateRange(LocalDate start, LocalDate end) { |
||||
this.startDate = start; |
||||
this.endDate = end; |
||||
return this; |
||||
} |
||||
|
||||
public Builder warehouseCode(String v) { |
||||
this.warehouseCode = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder providerSku(String v) { |
||||
this.providerSku = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder pageSize(int v) { |
||||
this.pageSize = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder cursor(String v) { |
||||
this.cursor = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder includeFeeRate(Boolean v) { |
||||
this.includeFeeRate = v; |
||||
return this; |
||||
} |
||||
|
||||
public InventoryAgeQuery build() { |
||||
return new InventoryAgeQuery(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,189 @@
@@ -0,0 +1,189 @@
|
||||
package common.ecommerce.warehouse.model; |
||||
|
||||
import common.ecommerce.shared.support.Money; |
||||
|
||||
import java.time.Instant; |
||||
|
||||
/** |
||||
* 一条库龄记录(按计费日 × 仓库 × SKU × 入库柜号)。 |
||||
* |
||||
* <p>库龄是<b>计费视角</b>的数据,不是库存快照:同一 SKU 在不同入库批次 / 柜号下 |
||||
* 有不同的库龄天数与费率,按天累计出仓储费。所以单独建模,不塞进 |
||||
* {@link InventorySnapshot}——后者回答「现在有多少货」。</p> |
||||
* |
||||
* <p>只读查询结果,无幂等要求。</p> |
||||
*/ |
||||
public final class InventoryAgeRecord { |
||||
|
||||
private final String warehouseCode; |
||||
private final String providerSku; |
||||
/** 计费日期(仓方 {@code dt});仓方按天出账。 */ |
||||
private final Instant billingDate; |
||||
/** 入库柜号;无则为 null。 */ |
||||
private final String cabinetNo; |
||||
/** 实际入库时间(UTC)。 */ |
||||
private final Instant inStockAt; |
||||
/** 库龄天数;仓方原值。 */ |
||||
private final int ageDays; |
||||
/** 库存数量(该批次)。 */ |
||||
private final int quantity; |
||||
/** 总体积(立方米,仓方 {@code total_volume_cbm});可能为 null。 */ |
||||
private final java.math.BigDecimal totalVolumeCbm; |
||||
/** 计费数量。 */ |
||||
private final int billingQuantity; |
||||
/** 费率;仓方未给为 null。 */ |
||||
private final java.math.BigDecimal feeRate; |
||||
/** 当日费用;仓方未给为 null。 */ |
||||
private final Money fee; |
||||
|
||||
private InventoryAgeRecord(Builder b) { |
||||
this.warehouseCode = require(b.warehouseCode, "warehouseCode"); |
||||
this.providerSku = require(b.providerSku, "providerSku"); |
||||
this.billingDate = java.util.Objects.requireNonNull(b.billingDate, "billingDate"); |
||||
this.cabinetNo = b.cabinetNo; |
||||
this.inStockAt = b.inStockAt; |
||||
this.ageDays = b.ageDays; |
||||
this.quantity = b.quantity; |
||||
this.totalVolumeCbm = b.totalVolumeCbm; |
||||
this.billingQuantity = b.billingQuantity; |
||||
this.feeRate = b.feeRate; |
||||
this.fee = b.fee; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
public String warehouseCode() { |
||||
return warehouseCode; |
||||
} |
||||
|
||||
public String providerSku() { |
||||
return providerSku; |
||||
} |
||||
|
||||
/** 计费日期。 */ |
||||
public Instant billingDate() { |
||||
return billingDate; |
||||
} |
||||
|
||||
/** 入库柜号;无则为 null。 */ |
||||
public String cabinetNo() { |
||||
return cabinetNo; |
||||
} |
||||
|
||||
/** 实际入库时间;仓方未给为 null。 */ |
||||
public Instant inStockAt() { |
||||
return inStockAt; |
||||
} |
||||
|
||||
/** 库龄天数。 */ |
||||
public int ageDays() { |
||||
return ageDays; |
||||
} |
||||
|
||||
/** 库存数量(该批次)。 */ |
||||
public int quantity() { |
||||
return quantity; |
||||
} |
||||
|
||||
/** 总体积(立方米);未给为 null。 */ |
||||
public java.math.BigDecimal totalVolumeCbm() { |
||||
return totalVolumeCbm; |
||||
} |
||||
|
||||
/** 计费数量。 */ |
||||
public int billingQuantity() { |
||||
return billingQuantity; |
||||
} |
||||
|
||||
/** 费率;未给为 null。 */ |
||||
public java.math.BigDecimal feeRate() { |
||||
return feeRate; |
||||
} |
||||
|
||||
/** 当日费用;未给为 null。 */ |
||||
public Money fee() { |
||||
return fee; |
||||
} |
||||
|
||||
private static String require(String v, String n) { |
||||
if (v == null || v.trim().isEmpty()) { |
||||
throw new IllegalArgumentException(n + " must not be blank"); |
||||
} |
||||
return v.trim(); |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private String warehouseCode; |
||||
private String providerSku; |
||||
private Instant billingDate; |
||||
private String cabinetNo; |
||||
private Instant inStockAt; |
||||
private int ageDays; |
||||
private int quantity; |
||||
private java.math.BigDecimal totalVolumeCbm; |
||||
private int billingQuantity; |
||||
private java.math.BigDecimal feeRate; |
||||
private Money fee; |
||||
|
||||
public Builder warehouseCode(String v) { |
||||
this.warehouseCode = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder providerSku(String v) { |
||||
this.providerSku = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder billingDate(Instant v) { |
||||
this.billingDate = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder cabinetNo(String v) { |
||||
this.cabinetNo = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder inStockAt(Instant v) { |
||||
this.inStockAt = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder ageDays(int v) { |
||||
this.ageDays = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder quantity(int v) { |
||||
this.quantity = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder totalVolumeCbm(java.math.BigDecimal v) { |
||||
this.totalVolumeCbm = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder billingQuantity(int v) { |
||||
this.billingQuantity = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder feeRate(java.math.BigDecimal v) { |
||||
this.feeRate = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder fee(Money v) { |
||||
this.fee = v; |
||||
return this; |
||||
} |
||||
|
||||
public InventoryAgeRecord build() { |
||||
return new InventoryAgeRecord(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,268 @@
@@ -0,0 +1,268 @@
|
||||
package common.ecommerce.xincang.adapter; |
||||
|
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.shared.support.Page; |
||||
import common.ecommerce.warehouse.model.InventoryAgeQuery; |
||||
import common.ecommerce.warehouse.model.InventoryAgeRecord; |
||||
import common.ecommerce.warehouse.model.InventoryQuery; |
||||
import common.ecommerce.warehouse.model.InventorySnapshot; |
||||
import common.ecommerce.xincang.XincangConstants; |
||||
import common.ecommerce.xincang.dto.XincangDtos; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.time.Instant; |
||||
import java.time.LocalDate; |
||||
import java.time.LocalDateTime; |
||||
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; |
||||
|
||||
/** |
||||
* 库存与库龄报文映射。 |
||||
* |
||||
* <ul> |
||||
* <li>实时库存 {@code GET api.stocklist}</li> |
||||
* <li>库龄列表 {@code GET api.stockagelist}</li> |
||||
* </ul> |
||||
* |
||||
* <p>平台字段名只出现在本类与 {@link XincangDtos};时间按北京时间解析成 UTC。</p> |
||||
*/ |
||||
public final class XincangInventoryMapper { |
||||
|
||||
private static final DateTimeFormatter DATE_TIME = |
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
||||
private static final DateTimeFormatter DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
||||
private static final String CURRENCY_USD = "USD"; |
||||
/** 仓方分页上限文档未标明,先按 100 保守截断。 */ |
||||
static final int MAX_PAGE_SIZE = 100; |
||||
|
||||
private XincangInventoryMapper() { |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 实时库存
|
||||
|
||||
/** |
||||
* 库存查询条件 → query 参数。 |
||||
* |
||||
* <p>{@code warehouse_code} 是信仓必填项:调用方指定优先,否则用账户配置的仓; |
||||
* 两者都没有就拒绝,避免把整个租户的库存拉下来。</p> |
||||
*/ |
||||
public static Map<String, Object> toStockParams(InventoryQuery query, String defaultWarehouseCode) { |
||||
String warehouseCode = firstNonBlank( |
||||
query.warehouseCodes().isEmpty() ? null : query.warehouseCodes().get(0), |
||||
defaultWarehouseCode); |
||||
if (warehouseCode == null) { |
||||
throw new IllegalArgumentException( |
||||
"xincang stocklist requires warehouse_code; none given and account has none"); |
||||
} |
||||
// 信仓只支持单个 warehouse_code;给了多个仓时不能静默只查第一个
|
||||
if (query.warehouseCodes().size() > 1) { |
||||
throw new IllegalArgumentException( |
||||
"xincang stocklist accepts a single warehouse_code, got " |
||||
+ query.warehouseCodes()); |
||||
} |
||||
Map<String, Object> params = new LinkedHashMap<String, Object>(); |
||||
params.put("warehouse_code", warehouseCode); |
||||
if (!query.providerSkus().isEmpty()) { |
||||
params.put("product_no", query.providerSkus().get(0)); |
||||
} |
||||
params.put("page", pageNumber(query.cursor())); |
||||
params.put("limit", pageSize(query.pageSize())); |
||||
return params; |
||||
} |
||||
|
||||
/** 库存响应 → 快照分页。 */ |
||||
public static Page<InventorySnapshot> toSnapshots(XincangDtos.StockListResult rst, |
||||
String fallbackWarehouseCode, |
||||
Instant asOf, |
||||
int currentPage, |
||||
int pageSize) { |
||||
List<InventorySnapshot> out = new ArrayList<InventorySnapshot>(); |
||||
if (rst != null && rst.data != null) { |
||||
for (XincangDtos.StockListResult.StockRow row : rst.data) { |
||||
if (row == null || blank(row.productSku)) { |
||||
continue; |
||||
} |
||||
out.add(InventorySnapshot.builder() |
||||
.warehouseCode(firstNonBlank(row.warehouseCode, fallbackWarehouseCode)) |
||||
.providerSku(row.productSku.trim()) |
||||
.available(intOf(row.canSaleSkuCount)) |
||||
.onHand(intOf(row.realStock)) |
||||
.inTransit(intOf(row.shipSkuCount)) |
||||
.reserved(intOf(row.useSkuCount)) |
||||
// 信仓不区分可售 / 不可售库位,统一按可售
|
||||
.inventoryType(0) |
||||
.asOfUtc(asOf) |
||||
.productName(blankToNull(row.chnName)) |
||||
.pendingOutbound(intOf(row.waitSkuCount)) |
||||
.alertQuantity(intOf(row.alarmCount)) |
||||
.volumeCbm(decimalOf(row.volumeCm)) |
||||
.build()); |
||||
} |
||||
} |
||||
return Page.of(out, nextCursor(currentPage, pageSize, out.size())); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- 库龄
|
||||
|
||||
/** 库龄查询条件 → query 参数。 */ |
||||
public static Map<String, Object> toAgeParams(InventoryAgeQuery query, String defaultWarehouseCode) { |
||||
Map<String, Object> params = new LinkedHashMap<String, Object>(); |
||||
// bill_date 是「起 - 止」的单值字符串
|
||||
params.put("bill_date", query.startDate().format(DATE) + " - " + query.endDate().format(DATE)); |
||||
putIfPresent(params, "warehouse_code", firstNonBlank(query.warehouseCode(), defaultWarehouseCode)); |
||||
putIfPresent(params, "key", query.providerSku()); |
||||
params.put("page", pageNumber(query.cursor())); |
||||
params.put("limit", pageSize(query.pageSize())); |
||||
if (query.includeFeeRate() != null) { |
||||
params.put("is_need_fee_rate", query.includeFeeRate() ? 1 : 0); |
||||
} |
||||
return params; |
||||
} |
||||
|
||||
/** 库龄响应 → 记录分页。 */ |
||||
public static Page<InventoryAgeRecord> toAgeRecords(XincangDtos.StockAgeListResult rst, |
||||
String fallbackWarehouseCode, |
||||
int currentPage, |
||||
int pageSize) { |
||||
List<InventoryAgeRecord> out = new ArrayList<InventoryAgeRecord>(); |
||||
if (rst != null && rst.data != null) { |
||||
for (XincangDtos.StockAgeListResult.AgeRow row : rst.data) { |
||||
if (row == null || blank(row.productSku)) { |
||||
continue; |
||||
} |
||||
Instant billingDate = parseDate(row.dt); |
||||
if (billingDate == null) { |
||||
// 计费日期是必填主键,解析不出就没法定位这条记录,跳过而不是猜今天
|
||||
continue; |
||||
} |
||||
out.add(InventoryAgeRecord.builder() |
||||
.warehouseCode(firstNonBlank(row.warehouseCode, fallbackWarehouseCode)) |
||||
.providerSku(row.productSku.trim()) |
||||
.billingDate(billingDate) |
||||
.cabinetNo(blankToNull(row.cabinetNo)) |
||||
.inStockAt(parseTime(row.inStockTime)) |
||||
.ageDays(intOf(row.feeDays)) |
||||
.quantity(intOf(row.skuCount)) |
||||
.totalVolumeCbm(decimalOf(row.totalVolumeCbm)) |
||||
.billingQuantity(intOf(row.feeCount)) |
||||
.feeRate(decimalOf(row.configPrice)) |
||||
.fee(parseMoney(row.sumFee)) |
||||
.build()); |
||||
} |
||||
} |
||||
return Page.of(out, nextCursor(currentPage, pageSize, out.size())); |
||||
} |
||||
|
||||
// ---------------------------------------------------------------- internals
|
||||
|
||||
/** |
||||
* 有下一页时返回下一页页码。 |
||||
* |
||||
* <p>信仓分页是 {@code page} / {@code limit},游标存字符串化页码;按「本页是否满页」 |
||||
* 判断还有没有下一页——{@code count} 在部分响应里缺失,不能依赖。</p> |
||||
*/ |
||||
static String nextCursor(int currentPage, int pageSize, int fetched) { |
||||
return fetched >= pageSize ? String.valueOf(currentPage + 1) : null; |
||||
} |
||||
|
||||
static int pageNumber(String cursor) { |
||||
if (blank(cursor)) { |
||||
return 1; |
||||
} |
||||
try { |
||||
int v = Integer.parseInt(cursor.trim()); |
||||
return v > 0 ? v : 1; |
||||
} catch (NumberFormatException e) { |
||||
return 1; |
||||
} |
||||
} |
||||
|
||||
static int pageSize(int requested) { |
||||
if (requested <= 0) { |
||||
return 20; |
||||
} |
||||
return Math.min(requested, MAX_PAGE_SIZE); |
||||
} |
||||
|
||||
static Instant parseTime(String raw) { |
||||
if (blank(raw)) { |
||||
return null; |
||||
} |
||||
try { |
||||
return LocalDateTime.parse(raw.trim(), DATE_TIME) |
||||
.atZone(XincangConstants.SYSTEM_ZONE).toInstant(); |
||||
} catch (DateTimeParseException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
static Instant parseDate(String raw) { |
||||
if (blank(raw)) { |
||||
return null; |
||||
} |
||||
try { |
||||
return LocalDate.parse(raw.trim(), DATE) |
||||
.atStartOfDay(XincangConstants.SYSTEM_ZONE).toInstant(); |
||||
} catch (DateTimeParseException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
static Money parseMoney(String raw) { |
||||
if (blank(raw)) { |
||||
return null; |
||||
} |
||||
try { |
||||
return Money.of(new BigDecimal(raw.trim()), CURRENCY_USD); |
||||
} catch (RuntimeException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
static int intOf(String raw) { |
||||
if (blank(raw)) { |
||||
return 0; |
||||
} |
||||
try { |
||||
return (int) Math.round(Double.parseDouble(raw.trim())); |
||||
} catch (NumberFormatException e) { |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
static BigDecimal decimalOf(String raw) { |
||||
if (blank(raw)) { |
||||
return null; |
||||
} |
||||
try { |
||||
return new BigDecimal(raw.trim()); |
||||
} catch (NumberFormatException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
static String firstNonBlank(String a, String b) { |
||||
if (!blank(a)) { |
||||
return a.trim(); |
||||
} |
||||
return blank(b) ? null : b.trim(); |
||||
} |
||||
|
||||
static String blankToNull(String v) { |
||||
return blank(v) ? null : v.trim(); |
||||
} |
||||
|
||||
static boolean blank(String v) { |
||||
return v == null || v.trim().isEmpty(); |
||||
} |
||||
|
||||
private static void putIfPresent(Map<String, Object> params, String key, String value) { |
||||
if (!blank(value)) { |
||||
params.put(key, value.trim()); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,200 @@
@@ -0,0 +1,200 @@
|
||||
package common.ecommerce.xincang.adapter; |
||||
|
||||
import common.ecommerce.shared.error.ValidationException; |
||||
import common.ecommerce.shared.spi.ChannelSecret; |
||||
import common.ecommerce.shared.support.Page; |
||||
import common.ecommerce.shared.testkit.InMemoryLockProvider; |
||||
import common.ecommerce.shared.testkit.InMemoryTokenStore; |
||||
import common.ecommerce.shared.testkit.MutableClock; |
||||
import common.ecommerce.shared.testkit.RecordingTransport; |
||||
import common.ecommerce.shared.testkit.Responses; |
||||
import common.ecommerce.warehouse.enums.InventorySyncStrategy; |
||||
import common.ecommerce.warehouse.enums.WarehouseCapability; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.InventoryAgeQuery; |
||||
import common.ecommerce.warehouse.model.InventoryAgeRecord; |
||||
import common.ecommerce.warehouse.model.InventoryQuery; |
||||
import common.ecommerce.warehouse.model.InventorySnapshot; |
||||
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
import common.ecommerce.xincang.XincangConstants; |
||||
import org.junit.jupiter.api.BeforeEach; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.Clock; |
||||
import java.time.Duration; |
||||
import java.time.Instant; |
||||
import java.time.LocalDate; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.*; |
||||
|
||||
/** |
||||
* 实时库存({@code api.stocklist})与库龄列表({@code api.stockagelist})契约测试。 |
||||
*/ |
||||
class XincangInventoryTest { |
||||
|
||||
private static final String BASE = XincangConstants.TEST_BASE_URL; |
||||
private static final String APP_KEY = "ak-1"; |
||||
|
||||
private static final String STOCK_BODY = "{\"rst\":{\"code\":\"0\",\"msg\":\"\",\"data\":[" |
||||
+ "{\"warehouse_code\":\"NJ-2605\",\"product_sku\":\"LSS29ORANGE\"," |
||||
+ "\"real_stock\":\"1500\",\"cansale_sku_count\":\"1499\",\"use_sku_count\":\"1\"," |
||||
+ "\"wait_sku_count\":\"1\",\"ship_sku_count\":\"0\",\"alerm_count\":\"0\"," |
||||
+ "\"chn_name\":\"餐椅\",\"volume_cm\":\"136221.5274\"}" |
||||
+ "],\"count\":\"1\"},\"errno\":0,\"err\":\"\",\"trace_id\":\"t-stock\"}"; |
||||
|
||||
private static final String AGE_BODY = "{\"rst\":{\"code\":\"0\",\"msg\":\"\",\"data\":[" |
||||
+ "{\"dt\":\"2026-10-14\",\"warehouse_code\":\"NJ-2605\",\"product_sku\":\"LSS29ORANGE\"," |
||||
+ "\"cabinet_no\":\"KKKKK001\",\"sum_fee\":\"0.00\",\"total_volume_cbm\":\"204.3323\"," |
||||
+ "\"sku_count\":\"1500\",\"in_stock_time\":\"2026-10-14 08:00:00\",\"fee_days\":\"1\"," |
||||
+ "\"config_price\":\"0.50\",\"fee_count\":\"1500\"}" |
||||
+ "],\"count\":\"1\"},\"errno\":0,\"err\":\"\",\"trace_id\":\"t-age\"}"; |
||||
|
||||
private RecordingTransport transport; |
||||
private XincangAdapter adapter; |
||||
|
||||
@BeforeEach |
||||
void setUp() { |
||||
transport = RecordingTransport.create(); |
||||
MutableClock clock = MutableClock.atUtc("2026-11-10T00:00:00Z"); |
||||
transport.route(XincangConstants.PATH_LOGIN, req -> Responses.ok( |
||||
"{\"rst\":{\"id\":\"c-1\",\"token\":\"{T-1}\",\"token_expire_time\":\"" |
||||
+ java.time.LocalDateTime.ofInstant(clock.instant().plus(Duration.ofHours(2)), |
||||
XincangConstants.SYSTEM_ZONE) |
||||
.format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) |
||||
+ "\"},\"errno\":0,\"err\":\"\",\"trace_id\":\"t-login\"}")); |
||||
adapter = new XincangAdapter(transport, new common.ecommerce.http.JacksonJsonCodec(), |
||||
new InMemoryTokenStore(), new InMemoryLockProvider(), clock); |
||||
} |
||||
|
||||
private static WarehouseContext ctx() { |
||||
return WarehouseContext.of("XC-NJ-2605", WarehouseProvider.XINCANG, "NJ-2605", |
||||
WarehouseConnection.of(BASE, ChannelSecret.builder() |
||||
.appKey(APP_KEY).appSecret("sk").build()), |
||||
"req-1", Clock.systemUTC()); |
||||
} |
||||
|
||||
@Test |
||||
void stockQuerySendsWarehouseAndMapsSnapshot() { |
||||
transport.route(XincangConstants.PATH_STOCK_LIST, req -> Responses.ok(STOCK_BODY)); |
||||
|
||||
Page<InventorySnapshot> page = adapter.query(ctx(), |
||||
InventoryQuery.builder().warehouseCode("NJ-2605").pageSize(10).build()); |
||||
|
||||
String url = transport.lastDataRequest().url(); |
||||
assertTrue(url.contains("/api.stocklist?"), url); |
||||
assertTrue(url.contains("warehouse_code=NJ-2605"), url); |
||||
assertTrue(url.contains("page=1"), url); |
||||
assertTrue(url.contains("limit=10"), url); |
||||
assertTrue(url.contains("token=%7BT-1%7D"), url); |
||||
|
||||
assertEquals(1, page.size()); |
||||
InventorySnapshot snap = page.items().get(0); |
||||
assertEquals("NJ-2605", snap.warehouseCode()); |
||||
assertEquals("LSS29ORANGE", snap.providerSku()); |
||||
assertEquals(1500, snap.onHand()); |
||||
assertEquals(1499, snap.available()); |
||||
assertEquals(1, snap.reserved()); |
||||
assertEquals(1, snap.pendingOutbound()); |
||||
assertEquals(0, snap.inTransit()); |
||||
assertEquals(0, snap.inventoryType()); |
||||
assertEquals("餐椅", snap.productName()); |
||||
assertEquals(0, snap.volumeCbm().compareTo(new java.math.BigDecimal("136221.5274"))); |
||||
assertNotNull(snap.asOfUtc()); |
||||
} |
||||
|
||||
@Test |
||||
void stockFallsBackToAccountWarehouse() { |
||||
transport.route(XincangConstants.PATH_STOCK_LIST, req -> Responses.ok(STOCK_BODY)); |
||||
|
||||
adapter.query(ctx(), InventoryQuery.builder().build()); |
||||
|
||||
assertTrue(transport.lastDataRequest().url().contains("warehouse_code=NJ-2605")); |
||||
} |
||||
|
||||
@Test |
||||
void multipleWarehousesAreRejectedInsteadOfSilentlyUsingFirst() { |
||||
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, |
||||
() -> adapter.query(ctx(), InventoryQuery.builder() |
||||
.warehouseCode("A").warehouseCode("B").build())); |
||||
assertTrue(e.getMessage().contains("single warehouse_code"), e.getMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void stockSkuFilterIsSent() { |
||||
transport.route(XincangConstants.PATH_STOCK_LIST, req -> Responses.ok(STOCK_BODY)); |
||||
|
||||
adapter.query(ctx(), InventoryQuery.builder().sku("LSS29ORANGE").build()); |
||||
|
||||
String url = transport.lastDataRequest().url(); |
||||
assertTrue(url.contains("product_no=LSS29ORANGE"), url); |
||||
} |
||||
|
||||
@Test |
||||
void ageQuerySendsBillDateRangeAndMapsRecord() { |
||||
transport.route(XincangConstants.PATH_STOCK_AGE_LIST, req -> Responses.ok(AGE_BODY)); |
||||
|
||||
Page<InventoryAgeRecord> page = adapter.queryAges(ctx(), InventoryAgeQuery.builder() |
||||
.dateRange(LocalDate.parse("2026-10-01"), LocalDate.parse("2026-10-31")) |
||||
.includeFeeRate(true) |
||||
.build()); |
||||
|
||||
String url = transport.lastDataRequest().url(); |
||||
assertTrue(url.contains("/api.stockagelist?"), url); |
||||
// bill_date 是「起 - 止」单值,空格与连字符会被 URL 编码
|
||||
assertTrue(url.contains("bill_date=2026-10-01+-+2026-10-31"), url); |
||||
assertTrue(url.contains("is_need_fee_rate=1"), url); |
||||
assertTrue(url.contains("warehouse_code=NJ-2605"), "未指定仓时用账户配置: " + url); |
||||
|
||||
assertEquals(1, page.size()); |
||||
InventoryAgeRecord rec = page.items().get(0); |
||||
assertEquals("NJ-2605", rec.warehouseCode()); |
||||
assertEquals("LSS29ORANGE", rec.providerSku()); |
||||
// 计费日期按北京时间 0 点 → UTC 前一天 16:00
|
||||
assertEquals(Instant.parse("2026-10-13T16:00:00Z"), rec.billingDate()); |
||||
assertEquals("KKKKK001", rec.cabinetNo()); |
||||
assertEquals(1, rec.ageDays()); |
||||
assertEquals(1500, rec.quantity()); |
||||
assertEquals(1500, rec.billingQuantity()); |
||||
assertEquals(0, rec.feeRate().compareTo(new java.math.BigDecimal("0.50"))); |
||||
assertEquals(0, rec.fee().amount().compareTo(java.math.BigDecimal.ZERO)); |
||||
assertEquals("USD", rec.fee().currencyCode()); |
||||
assertEquals(Instant.parse("2026-10-14T00:00:00Z"), rec.inStockAt()); |
||||
} |
||||
|
||||
@Test |
||||
void ageQueryRejectsMissingDateRange() { |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> InventoryAgeQuery.builder().build()); |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> InventoryAgeQuery.builder() |
||||
.dateRange(LocalDate.parse("2026-10-31"), LocalDate.parse("2026-10-01")) |
||||
.build()); |
||||
} |
||||
|
||||
@Test |
||||
void ageQuerySkipsRowsWithoutBillingDate() { |
||||
String noDate = AGE_BODY.replace("\"dt\":\"2026-10-14\",", "\"dt\":\"\","); |
||||
transport.route(XincangConstants.PATH_STOCK_AGE_LIST, req -> Responses.ok(noDate)); |
||||
|
||||
Page<InventoryAgeRecord> page = adapter.queryAges(ctx(), InventoryAgeQuery.builder() |
||||
.dateRange(LocalDate.parse("2026-10-01"), LocalDate.parse("2026-10-31")).build()); |
||||
|
||||
assertTrue(page.items().isEmpty(), "计费日期解析不出时跳过,不猜日期"); |
||||
} |
||||
|
||||
@Test |
||||
void missingRstIsNotSilentEmpty() { |
||||
transport.route(XincangConstants.PATH_STOCK_LIST, req -> Responses.ok( |
||||
"{\"rst\":null,\"errno\":0,\"err\":\"\"}")); |
||||
|
||||
assertThrows(ValidationException.class, |
||||
() -> adapter.query(ctx(), InventoryQuery.builder().build())); |
||||
} |
||||
|
||||
@Test |
||||
void inventoryCapabilityAndStrategy() { |
||||
assertTrue(adapter.capabilities().contains(WarehouseCapability.INVENTORY_QUERY)); |
||||
assertEquals(InventorySyncStrategy.FULL_PULL_DIFF, adapter.syncStrategy()); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue