Compare commits
8 Commits
93e85bfeb7
...
635fc09883
| Author | SHA1 | Date |
|---|---|---|
|
|
635fc09883 | 3 days ago |
|
|
5bbfb33261 | 3 days ago |
|
|
b5e94c1157 | 3 days ago |
|
|
efc248c27d | 3 days ago |
|
|
7629a52ab8 | 3 days ago |
|
|
8dabf9fb1b | 3 days ago |
|
|
97be2aebe9 | 3 days ago |
|
|
5dce79ea32 | 3 days ago |
43 changed files with 2573 additions and 74 deletions
@ -0,0 +1,423 @@
@@ -0,0 +1,423 @@
|
||||
package common.ecommerce.warehouse.model; |
||||
|
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
|
||||
import java.time.Instant; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 仓侧商品资料(查询结果)。 |
||||
* |
||||
* <p>与 {@link common.ecommerce.warehouse.capability.WarehouseProduct} 的区别: |
||||
* 那个是<b>建档入参</b>(我们下发给仓方),这个是<b>查询结果</b>(仓方存着什么)。 |
||||
* 两者字段高度重叠但语义不同——入参里没有 id / 状态 / 创建时间, |
||||
* 结果里也没有「要求仓方必填」这类约束,所以不复用同一个类。</p> |
||||
* |
||||
* <p>单位与入参一致:尺寸 cm、重量 g。</p> |
||||
*/ |
||||
public final class ProductInfo { |
||||
|
||||
/** 仓侧 SKU(建档时下发的那串)。 */ |
||||
private final String providerSku; |
||||
/** OMS 侧 SKU;仓方不回时为 null(SDK 不做反查)。 */ |
||||
private final String omsSku; |
||||
/** 仓方内部商品 id;不同仓给不给不一。 */ |
||||
private final String providerProductId; |
||||
/** 自定义产品编码。 */ |
||||
private final String customItemNo; |
||||
private final String barcode; |
||||
private final List<String> extraBarcodes; |
||||
|
||||
private final String name; |
||||
private final String nameEn; |
||||
private final String description; |
||||
|
||||
/** 产品类型:1 成品 / 2 配件 / 3 耗材;未给为 -1。 */ |
||||
private final int productType; |
||||
|
||||
/** 外箱 / 整箱尺寸重量。 */ |
||||
private final double lengthCm; |
||||
private final double widthCm; |
||||
private final double heightCm; |
||||
private final double weightGrams; |
||||
|
||||
/** 单品尺寸;未给为 null。 */ |
||||
private final Double itemLengthCm; |
||||
private final Double itemWidthCm; |
||||
private final Double itemHeightCm; |
||||
|
||||
/** 装箱数量;未给为 null。 */ |
||||
private final Integer packedQuantity; |
||||
private final Double packedLengthCm; |
||||
private final Double packedWidthCm; |
||||
private final Double packedHeightCm; |
||||
private final Double packedWeightGrams; |
||||
|
||||
/** 是否需要 SN;未给为 null。 */ |
||||
private final Boolean requireSn; |
||||
/** 成本;未给为 null。 */ |
||||
private final Money cost; |
||||
|
||||
/** 仓方状态原值(如 edayun 的 0 草稿 / 1 已发布);未给为 null。 */ |
||||
private final Integer rawStatus; |
||||
/** 仓方状态名原值。 */ |
||||
private final String rawStatusName; |
||||
|
||||
/** 创建时间;未给为 null。 */ |
||||
private final Instant createdAt; |
||||
private final String remark; |
||||
/** 该商品来自哪家仓。 */ |
||||
private final WarehouseProvider sourceProvider; |
||||
|
||||
private ProductInfo(Builder b) { |
||||
this.providerSku = require(b.providerSku, "providerSku"); |
||||
this.omsSku = b.omsSku; |
||||
this.providerProductId = b.providerProductId; |
||||
this.customItemNo = b.customItemNo; |
||||
this.barcode = b.barcode; |
||||
this.extraBarcodes = Collections.unmodifiableList( |
||||
new ArrayList<String>(b.extraBarcodes)); |
||||
this.name = b.name; |
||||
this.nameEn = b.nameEn; |
||||
this.description = b.description; |
||||
this.productType = b.productType; |
||||
this.lengthCm = b.lengthCm; |
||||
this.widthCm = b.widthCm; |
||||
this.heightCm = b.heightCm; |
||||
this.weightGrams = b.weightGrams; |
||||
this.itemLengthCm = b.itemLengthCm; |
||||
this.itemWidthCm = b.itemWidthCm; |
||||
this.itemHeightCm = b.itemHeightCm; |
||||
this.packedQuantity = b.packedQuantity; |
||||
this.packedLengthCm = b.packedLengthCm; |
||||
this.packedWidthCm = b.packedWidthCm; |
||||
this.packedHeightCm = b.packedHeightCm; |
||||
this.packedWeightGrams = b.packedWeightGrams; |
||||
this.requireSn = b.requireSn; |
||||
this.cost = b.cost; |
||||
this.rawStatus = b.rawStatus; |
||||
this.rawStatusName = b.rawStatusName; |
||||
this.createdAt = b.createdAt; |
||||
this.remark = b.remark; |
||||
this.sourceProvider = b.sourceProvider; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
public String providerSku() { |
||||
return providerSku; |
||||
} |
||||
|
||||
/** OMS SKU;仓方不回时为 null。 */ |
||||
public String omsSku() { |
||||
return omsSku; |
||||
} |
||||
|
||||
public String providerProductId() { |
||||
return providerProductId; |
||||
} |
||||
|
||||
public String customItemNo() { |
||||
return customItemNo; |
||||
} |
||||
|
||||
public String barcode() { |
||||
return barcode; |
||||
} |
||||
|
||||
/** 额外条码(仓方支持多个时);不可变。 */ |
||||
public List<String> extraBarcodes() { |
||||
return extraBarcodes; |
||||
} |
||||
|
||||
public String name() { |
||||
return name; |
||||
} |
||||
|
||||
public String nameEn() { |
||||
return nameEn; |
||||
} |
||||
|
||||
public String description() { |
||||
return description; |
||||
} |
||||
|
||||
/** 产品类型:1 成品 / 2 配件 / 3 耗材;未给为 -1。 */ |
||||
public int productType() { |
||||
return productType; |
||||
} |
||||
|
||||
public double lengthCm() { |
||||
return lengthCm; |
||||
} |
||||
|
||||
public double widthCm() { |
||||
return widthCm; |
||||
} |
||||
|
||||
public double heightCm() { |
||||
return heightCm; |
||||
} |
||||
|
||||
public double weightGrams() { |
||||
return weightGrams; |
||||
} |
||||
|
||||
public Double itemLengthCm() { |
||||
return itemLengthCm; |
||||
} |
||||
|
||||
public Double itemWidthCm() { |
||||
return itemWidthCm; |
||||
} |
||||
|
||||
public Double itemHeightCm() { |
||||
return itemHeightCm; |
||||
} |
||||
|
||||
public Integer packedQuantity() { |
||||
return packedQuantity; |
||||
} |
||||
|
||||
public Double packedLengthCm() { |
||||
return packedLengthCm; |
||||
} |
||||
|
||||
public Double packedWidthCm() { |
||||
return packedWidthCm; |
||||
} |
||||
|
||||
public Double packedHeightCm() { |
||||
return packedHeightCm; |
||||
} |
||||
|
||||
public Double packedWeightGrams() { |
||||
return packedWeightGrams; |
||||
} |
||||
|
||||
public Boolean requireSn() { |
||||
return requireSn; |
||||
} |
||||
|
||||
public Money cost() { |
||||
return cost; |
||||
} |
||||
|
||||
/** 仓方状态原值。 */ |
||||
public Integer rawStatus() { |
||||
return rawStatus; |
||||
} |
||||
|
||||
/** 仓方状态名原值。 */ |
||||
public String rawStatusName() { |
||||
return rawStatusName; |
||||
} |
||||
|
||||
public Instant createdAt() { |
||||
return createdAt; |
||||
} |
||||
|
||||
public String remark() { |
||||
return remark; |
||||
} |
||||
|
||||
public WarehouseProvider sourceProvider() { |
||||
return sourceProvider; |
||||
} |
||||
|
||||
private static String require(String v, String name) { |
||||
if (v == null || v.trim().isEmpty()) { |
||||
throw new IllegalArgumentException(name + " must not be blank"); |
||||
} |
||||
return v.trim(); |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private String providerSku; |
||||
private String omsSku; |
||||
private String providerProductId; |
||||
private String customItemNo; |
||||
private String barcode; |
||||
private final List<String> extraBarcodes = new ArrayList<String>(); |
||||
private String name; |
||||
private String nameEn; |
||||
private String description; |
||||
private int productType = -1; |
||||
private double lengthCm; |
||||
private double widthCm; |
||||
private double heightCm; |
||||
private double weightGrams; |
||||
private Double itemLengthCm; |
||||
private Double itemWidthCm; |
||||
private Double itemHeightCm; |
||||
private Integer packedQuantity; |
||||
private Double packedLengthCm; |
||||
private Double packedWidthCm; |
||||
private Double packedHeightCm; |
||||
private Double packedWeightGrams; |
||||
private Boolean requireSn; |
||||
private Money cost; |
||||
private Integer rawStatus; |
||||
private String rawStatusName; |
||||
private Instant createdAt; |
||||
private String remark; |
||||
private WarehouseProvider sourceProvider; |
||||
|
||||
public Builder providerSku(String v) { |
||||
this.providerSku = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder omsSku(String v) { |
||||
this.omsSku = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder providerProductId(String v) { |
||||
this.providerProductId = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder customItemNo(String v) { |
||||
this.customItemNo = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder barcode(String v) { |
||||
this.barcode = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder extraBarcode(String v) { |
||||
if (v != null && !v.trim().isEmpty()) { |
||||
this.extraBarcodes.add(v.trim()); |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
public Builder name(String v) { |
||||
this.name = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder nameEn(String v) { |
||||
this.nameEn = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder description(String v) { |
||||
this.description = v; |
||||
return this; |
||||
} |
||||
|
||||
/** 产品类型:1 成品 / 2 配件 / 3 耗材;-1 表示未给。 */ |
||||
public Builder productType(int v) { |
||||
this.productType = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder lengthCm(double v) { |
||||
this.lengthCm = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder widthCm(double v) { |
||||
this.widthCm = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder heightCm(double v) { |
||||
this.heightCm = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder weightGrams(double v) { |
||||
this.weightGrams = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder itemLengthCm(Double v) { |
||||
this.itemLengthCm = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder itemWidthCm(Double v) { |
||||
this.itemWidthCm = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder itemHeightCm(Double v) { |
||||
this.itemHeightCm = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder packedQuantity(Integer v) { |
||||
this.packedQuantity = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder packedLengthCm(Double v) { |
||||
this.packedLengthCm = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder packedWidthCm(Double v) { |
||||
this.packedWidthCm = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder packedHeightCm(Double v) { |
||||
this.packedHeightCm = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder packedWeightGrams(Double v) { |
||||
this.packedWeightGrams = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder requireSn(Boolean v) { |
||||
this.requireSn = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder cost(Money v) { |
||||
this.cost = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder rawStatus(Integer v) { |
||||
this.rawStatus = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder rawStatusName(String v) { |
||||
this.rawStatusName = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder createdAt(Instant v) { |
||||
this.createdAt = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder remark(String v) { |
||||
this.remark = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder sourceProvider(WarehouseProvider v) { |
||||
this.sourceProvider = v; |
||||
return this; |
||||
} |
||||
|
||||
public ProductInfo build() { |
||||
return new ProductInfo(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,147 @@
@@ -0,0 +1,147 @@
|
||||
package common.ecommerce.warehouse.model; |
||||
|
||||
import java.time.Instant; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 商品查询条件(批量 / 分页)。 |
||||
* |
||||
* <p>与 {@link InventoryQuery} 的口径一致:{@code providerSkus} 是<b>仓侧 SKU</b>, |
||||
* 即建档时下发给仓方的那一串(通常等于 {@code WarehouseProduct.providerSku()}, |
||||
* 没给映射时就是 OMS SKU)。<b>SDK 不做 OMS→仓侧 SKU 的转换</b>—— |
||||
* 那张映射表在宿主手里,这里静默转换会让查回来的东西对不上。</p> |
||||
* |
||||
* <p>各仓支持的过滤维度不同,不支持的由适配器<b>显式拒绝</b>, |
||||
* 不静默忽略——忽略一个过滤条件会让结果对不上。</p> |
||||
*/ |
||||
public final class ProductQuery { |
||||
|
||||
/** 单次最多查多少个 SKU(各仓不同,由适配器按自己的上限再校验)。 */ |
||||
public static final int MAX_SKUS = 100; |
||||
|
||||
private final List<String> providerSkus; |
||||
/** 条码(UPC / 仓方条码)。 */ |
||||
private final String barcode; |
||||
private final Instant createdAfter; |
||||
private final Instant createdBefore; |
||||
private final int pageSize; |
||||
private final String cursor; |
||||
|
||||
public ProductQuery(Builder b) { |
||||
this.providerSkus = Collections.unmodifiableList( |
||||
new ArrayList<String>(b.providerSkus)); |
||||
if (this.providerSkus.size() > MAX_SKUS) { |
||||
throw new IllegalArgumentException( |
||||
"at most " + MAX_SKUS + " skus per query, got " + this.providerSkus.size()); |
||||
} |
||||
if (b.createdAfter != null && b.createdBefore != null |
||||
&& !b.createdAfter.isBefore(b.createdBefore)) { |
||||
throw new IllegalArgumentException("createdAfter must be before createdBefore"); |
||||
} |
||||
this.barcode = b.barcode; |
||||
this.createdAfter = b.createdAfter; |
||||
this.createdBefore = b.createdBefore; |
||||
this.pageSize = b.pageSize; |
||||
this.cursor = b.cursor; |
||||
} |
||||
|
||||
public static Builder builder() { |
||||
return new Builder(); |
||||
} |
||||
|
||||
/** |
||||
* 仓侧 SKU 列表;空表示不按 SKU 过滤。 |
||||
* |
||||
* <p>这串必须等于<b>建档时下发给仓方的 SKU</b>({@code WarehouseProduct.providerSku()}, |
||||
* 未给映射时即 {@code omsSku()})。各仓入库字段不同:易达云 {@code skuCode} |
||||
* (逗号拼接,≤50)/ 运去哪 {@code skuList}(JSON 数组,≤100)。 |
||||
* 传错不会报错,只会查不到——SDK 不做 OMS→仓侧转换。</p> |
||||
*/ |
||||
public List<String> providerSkus() { |
||||
return providerSkus; |
||||
} |
||||
|
||||
/** 条码;null 表示不按条码过滤。 */ |
||||
public String barcode() { |
||||
return barcode; |
||||
} |
||||
|
||||
public Instant createdAfter() { |
||||
return createdAfter; |
||||
} |
||||
|
||||
public Instant createdBefore() { |
||||
return createdBefore; |
||||
} |
||||
|
||||
/** 是否给了完整的创建时间窗。 */ |
||||
public boolean hasCreatedRange() { |
||||
return createdAfter != null && createdBefore != null; |
||||
} |
||||
|
||||
public int pageSize() { |
||||
return pageSize; |
||||
} |
||||
|
||||
/** 分页游标;null 表示第一页。 */ |
||||
public String cursor() { |
||||
return cursor; |
||||
} |
||||
|
||||
public static final class Builder { |
||||
private final List<String> providerSkus = new ArrayList<String>(); |
||||
private String barcode; |
||||
private Instant createdAfter; |
||||
private Instant createdBefore; |
||||
private int pageSize = 100; |
||||
private String cursor; |
||||
|
||||
/** 一个仓侧 SKU;空串忽略。 */ |
||||
public Builder providerSku(String v) { |
||||
if (v != null && !v.trim().isEmpty()) { |
||||
this.providerSkus.add(v.trim()); |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
public Builder providerSkus(List<String> v) { |
||||
if (v != null) { |
||||
for (String s : v) { |
||||
providerSku(s); |
||||
} |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
public Builder barcode(String v) { |
||||
this.barcode = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder createdAfter(Instant v) { |
||||
this.createdAfter = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder createdBefore(Instant v) { |
||||
this.createdBefore = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder pageSize(int v) { |
||||
this.pageSize = v; |
||||
return this; |
||||
} |
||||
|
||||
public Builder cursor(String v) { |
||||
this.cursor = v; |
||||
return this; |
||||
} |
||||
|
||||
public ProductQuery build() { |
||||
return new ProductQuery(this); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,153 @@
@@ -0,0 +1,153 @@
|
||||
package common.ecommerce.edayun.adapter; |
||||
|
||||
import common.ecommerce.edayun.dto.EdayunProductQueryDtos; |
||||
import common.ecommerce.shared.support.Page; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.ProductInfo; |
||||
import common.ecommerce.warehouse.model.ProductQuery; |
||||
|
||||
import java.time.Instant; |
||||
import java.time.ZoneId; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 易达云商品查询映射({@code /webApi/skuInfo/v1/listProductPage})。 |
||||
* |
||||
* <p><b>SKU 过滤用逗号拼接的单个字符串</b>({@code skuCode}), |
||||
* 且一次最多 50 个——与统一模型的 {@code providerSkus} 列表不同,这里要拼。</p> |
||||
* |
||||
* <p>页码从 1 开始,单页最多 200。</p> |
||||
*/ |
||||
public final class EdayunProductQueryMapper { |
||||
|
||||
static final int MAX_PAGE_SIZE = 200; |
||||
/** skuCode 一次最多查多少个(文档:50)。 */ |
||||
static final int MAX_SKUS = 50; |
||||
|
||||
private EdayunProductQueryMapper() { |
||||
} |
||||
|
||||
/** |
||||
* 统一查询条件 → 仓方请求。 |
||||
* |
||||
* @param zone 解释创建时间窗的时区(仓方口径) |
||||
*/ |
||||
public static EdayunProductQueryDtos.ListProductRequest toRequest(ProductQuery query, |
||||
ZoneId zone, |
||||
int pageNum) { |
||||
EdayunProductQueryDtos.ListProductRequest req = |
||||
new EdayunProductQueryDtos.ListProductRequest(); |
||||
req.pageNum = pageNum; |
||||
req.pageSize = pageSize(query.pageSize()); |
||||
|
||||
if (!query.providerSkus().isEmpty()) { |
||||
if (query.providerSkus().size() > MAX_SKUS) { |
||||
throw new IllegalArgumentException( |
||||
"edayun product query accepts at most " + MAX_SKUS + " skus, got " |
||||
+ query.providerSkus().size() |
||||
+ "; split into multiple queries"); |
||||
} |
||||
req.skuCode = String.join(",", query.providerSkus()); |
||||
} |
||||
if (query.barcode() != null && !query.barcode().trim().isEmpty()) { |
||||
req.barcode = query.barcode().trim(); |
||||
} |
||||
if (query.hasCreatedRange()) { |
||||
req.startCreateTime = query.createdAfter().toEpochMilli(); |
||||
req.endCreateTime = query.createdBefore().toEpochMilli(); |
||||
} |
||||
return req; |
||||
} |
||||
|
||||
/** 每页条数:默认 100、上限 200。 */ |
||||
static int pageSize(int requested) { |
||||
if (requested <= 0) { |
||||
return 100; |
||||
} |
||||
return Math.min(requested, MAX_PAGE_SIZE); |
||||
} |
||||
|
||||
/** 游标是字符串化的页码(从 1 开始)。 */ |
||||
static int pageNumber(String cursor) { |
||||
if (cursor == null || cursor.trim().isEmpty()) { |
||||
return 1; |
||||
} |
||||
try { |
||||
int v = Integer.parseInt(cursor.trim()); |
||||
return v > 0 ? v : 1; |
||||
} catch (NumberFormatException e) { |
||||
return 1; |
||||
} |
||||
} |
||||
|
||||
/** 响应 → 分页结果。 */ |
||||
public static Page<ProductInfo> toPage(EdayunProductQueryDtos.ListProductResponse response, |
||||
int currentPage, int pageSize) { |
||||
if (response == null || response.rows == null || response.rows.isEmpty()) { |
||||
return Page.empty(); |
||||
} |
||||
List<ProductInfo> out = new ArrayList<ProductInfo>(response.rows.size()); |
||||
for (EdayunProductQueryDtos.ProductRow row : response.rows) { |
||||
ProductInfo info = toInfo(row); |
||||
if (info != null) { |
||||
out.add(info); |
||||
} |
||||
} |
||||
return Page.of(out, nextCursor(response.total, currentPage, pageSize, out.size())); |
||||
} |
||||
|
||||
/** 有下一页时返回下一页页码;优先用 {@code total}(总行数)。 */ |
||||
static String nextCursor(Long total, int currentPage, int pageSize, int fetched) { |
||||
boolean more = total != null |
||||
? (long) currentPage * pageSize < total |
||||
: fetched >= pageSize; |
||||
return more ? String.valueOf(currentPage + 1) : null; |
||||
} |
||||
|
||||
/** |
||||
* 一行 → 统一商品资料。 |
||||
* |
||||
* <p>尺寸取<b>实际</b>({@code length/width/height/weight}),缺失才回落 |
||||
* 预报值({@code forecast*})——查询商品资料的目的就是拿到仓方当前存的值。</p> |
||||
*/ |
||||
static ProductInfo toInfo(EdayunProductQueryDtos.ProductRow row) { |
||||
if (row == null || row.skuCode == null || row.skuCode.trim().isEmpty()) { |
||||
return null; |
||||
} |
||||
ProductInfo.Builder b = ProductInfo.builder() |
||||
.sourceProvider(WarehouseProvider.EDAYUN) |
||||
.providerSku(row.skuCode.trim()) |
||||
.providerProductId(trimToNull(row.skuId)) |
||||
.barcode(trimToNull(row.barcode)) |
||||
.name(trimToNull(row.skuName)) |
||||
.nameEn(trimToNull(row.skuNameEn)) |
||||
.lengthCm(numberOf(row.length, row.forecastLength)) |
||||
.widthCm(numberOf(row.width, row.forecastWidth)) |
||||
.heightCm(numberOf(row.height, row.forecastHeight)) |
||||
.weightGrams(numberOf(row.weight, row.forecastWeight)) |
||||
.remark(trimToNull(row.remark)) |
||||
.createdAt(instantOf(row.createTime)); |
||||
b.extraBarcode(row.extraBarcode1); |
||||
b.extraBarcode(row.extraBarcode2); |
||||
b.extraBarcode(row.extraBarcode3); |
||||
return b.build(); |
||||
} |
||||
|
||||
/** 优先实际值,缺失回落预报值;都缺为 0。 */ |
||||
private static double numberOf(Double actual, Double forecast) { |
||||
if (actual != null) { |
||||
return actual; |
||||
} |
||||
return forecast == null ? 0 : forecast; |
||||
} |
||||
|
||||
private static Instant instantOf(Long millis) { |
||||
return millis == null || millis <= 0 ? null : Instant.ofEpochMilli(millis); |
||||
} |
||||
|
||||
private static String trimToNull(String v) { |
||||
return v == null || v.trim().isEmpty() ? null : v.trim(); |
||||
} |
||||
} |
||||
@ -0,0 +1,96 @@
@@ -0,0 +1,96 @@
|
||||
package common.ecommerce.edayun.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 易达云报文结构:批量查询产品分页 |
||||
* ({@code POST /webApi/skuInfo/v1/listProductPage})。 |
||||
* |
||||
* <p>一个文件对应一个接口。查询条件全空时仓方默认按创建时间查<b>最近一个月</b>, |
||||
* 页码从 1 开始,单页最多 200 条。</p> |
||||
*/ |
||||
public final class EdayunProductQueryDtos { |
||||
|
||||
private EdayunProductQueryDtos() { |
||||
} |
||||
|
||||
/** 查询请求。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class ListProductRequest { |
||||
/** 产品状态:0 草稿 / 1 已发布。 */ |
||||
public String status; |
||||
/** 每页条数,最多 200。 */ |
||||
public Integer pageSize; |
||||
/** 页码,从 1 开始。 */ |
||||
public Integer pageNum; |
||||
|
||||
public String barcode; |
||||
/** SKU 编码,多个用英文逗号拼接,<b>一次最多 50 个</b>。 */ |
||||
public String skuCode; |
||||
public String extraBarcode; |
||||
/** 中文品名。 */ |
||||
public String skuName; |
||||
|
||||
/** 创建时间窗(毫秒)。 */ |
||||
public Long startCreateTime; |
||||
public Long endCreateTime; |
||||
/** 审核时间窗(毫秒)。 */ |
||||
public Long startAuditTime; |
||||
public Long endAuditTime; |
||||
} |
||||
|
||||
/** 查询响应。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class ListProductResponse { |
||||
public Long total; |
||||
public List<ProductRow> rows; |
||||
public Integer code; |
||||
public String msg; |
||||
} |
||||
|
||||
/** 一行商品。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class ProductRow { |
||||
/** 创建时间(毫秒)。 */ |
||||
public Long createTime; |
||||
public String remark; |
||||
/** 仓方商品 id。 */ |
||||
public String skuId; |
||||
public String skuCode; |
||||
/** 中文品名。 */ |
||||
public String skuName; |
||||
public String skuNameEn; |
||||
public String barcode; |
||||
/** 外部条码 1/2/3。 */ |
||||
public String extraBarcode1; |
||||
public String extraBarcode2; |
||||
public String extraBarcode3; |
||||
|
||||
/** 预报尺寸(cm)与重量;量级像克。 */ |
||||
public Double forecastLength; |
||||
public Double forecastWidth; |
||||
public Double forecastHeight; |
||||
public Double forecastWeight; |
||||
|
||||
/** 实际尺寸(cm)与重量;量级像克。 */ |
||||
public Double length; |
||||
public Double width; |
||||
public Double height; |
||||
public Double weight; |
||||
|
||||
/** 带电标志:<b>0 是 / 1 否</b>(同其它接口的反向布尔)。 */ |
||||
public Integer electricFlag; |
||||
public Integer electricType; |
||||
public Integer electricPackage; |
||||
public Integer electricPower; |
||||
public String electricUrl; |
||||
|
||||
/** 商品图片。 */ |
||||
public String skuUrl; |
||||
/** 品牌标志:0 是 / 1 否。 */ |
||||
public Integer brandFlag; |
||||
public String brandName; |
||||
} |
||||
} |
||||
@ -0,0 +1,215 @@
@@ -0,0 +1,215 @@
|
||||
package common.ecommerce.edayun.adapter; |
||||
|
||||
import common.ecommerce.edayun.dto.EdayunProductQueryDtos; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.shared.spi.ChannelSecret; |
||||
import common.ecommerce.shared.support.Page; |
||||
import common.ecommerce.shared.testkit.RecordingTransport; |
||||
import common.ecommerce.shared.testkit.Responses; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.ProductInfo; |
||||
import common.ecommerce.warehouse.model.ProductQuery; |
||||
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.Clock; |
||||
import java.time.Instant; |
||||
import java.time.ZoneOffset; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertNull; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 商品查询({@code /webApi/skuInfo/v1/listProductPage})映射。 |
||||
* |
||||
* <p>重点:SKU 是逗号拼接的单个字符串、<b>一次最多 50 个</b>、页码从 1 开始、 |
||||
* 实际尺寸优先于预报尺寸。</p> |
||||
*/ |
||||
class EdayunProductQueryTest { |
||||
|
||||
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-11-17T00:00:00Z"), ZoneOffset.UTC); |
||||
|
||||
private static WarehouseContext ctx() { |
||||
return WarehouseContext.of("EDY-WH", WarehouseProvider.EDAYUN, "2", |
||||
WarehouseConnection.of("https://test.example/openapi", |
||||
ChannelSecret.builder().appKey("k").appSecret("s") |
||||
.clientId("c").clientSecret("cs").build()), |
||||
"req-product-query", CLOCK); |
||||
} |
||||
|
||||
private static RecordingTransport stub(String body) { |
||||
return RecordingTransport.create() |
||||
.route(common.ecommerce.edayun.EdayunConstants.PATH_TOKEN, req -> Responses.ok( |
||||
"{\"access_token\":\"T1\",\"token_type\":\"bearer\"}")) |
||||
.route(common.ecommerce.edayun.EdayunConstants.PATH_PRODUCT_LIST, |
||||
req -> Responses.ok(body)); |
||||
} |
||||
|
||||
// ------------------------------------------------------------ 请求
|
||||
|
||||
/** SKU 列表要拼成逗号分隔的单个字符串。 */ |
||||
@Test |
||||
void skusAreJoinedWithComma() { |
||||
EdayunProductQueryDtos.ListProductRequest req = |
||||
EdayunProductQueryMapper.toRequest( |
||||
ProductQuery.builder().providerSku("A").providerSku("B").build(), |
||||
ZoneOffset.UTC, 1); |
||||
|
||||
assertEquals("A,B", req.skuCode); |
||||
} |
||||
|
||||
/** 一次最多 50 个 SKU,超了本地拒绝(不截断)。 */ |
||||
@Test |
||||
void moreThan50SkusRejected() { |
||||
ProductQuery.Builder b = ProductQuery.builder(); |
||||
for (int i = 0; i < 51; i++) { |
||||
b.providerSku("SKU-" + i); |
||||
} |
||||
|
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, |
||||
() -> EdayunProductQueryMapper.toRequest(b.build(), ZoneOffset.UTC, 1)); |
||||
|
||||
assertTrue(ex.getMessage().contains("at most 50"), ex.getMessage()); |
||||
} |
||||
|
||||
@Test |
||||
void pageSizeCappedAt200() { |
||||
assertEquals(200, EdayunProductQueryMapper.pageSize(1000)); |
||||
assertEquals(100, EdayunProductQueryMapper.pageSize(0)); |
||||
} |
||||
|
||||
/** 页码从 1 开始。 */ |
||||
@Test |
||||
void pageNumberStartsAtOne() { |
||||
assertEquals(1, EdayunProductQueryMapper.pageNumber(null)); |
||||
assertEquals(1, EdayunProductQueryMapper.pageNumber("0")); |
||||
assertEquals(3, EdayunProductQueryMapper.pageNumber("3")); |
||||
} |
||||
|
||||
/** 创建时间窗按毫秒下发;只给一端时不下发(避免半开区间语义不明)。 */ |
||||
@Test |
||||
void createdRangeIsMillis() { |
||||
EdayunProductQueryDtos.ListProductRequest req = EdayunProductQueryMapper.toRequest( |
||||
ProductQuery.builder() |
||||
.createdAfter(Instant.parse("2026-01-01T00:00:00Z")) |
||||
.createdBefore(Instant.parse("2026-02-01T00:00:00Z")) |
||||
.build(), ZoneOffset.UTC, 1); |
||||
|
||||
assertEquals(Long.valueOf(Instant.parse("2026-01-01T00:00:00Z").toEpochMilli()), |
||||
req.startCreateTime); |
||||
assertEquals(Long.valueOf(Instant.parse("2026-02-01T00:00:00Z").toEpochMilli()), |
||||
req.endCreateTime); |
||||
|
||||
assertNull(EdayunProductQueryMapper.toRequest( |
||||
ProductQuery.builder().createdAfter(Instant.parse("2026-01-01T00:00:00Z")).build(), |
||||
ZoneOffset.UTC, 1).startCreateTime); |
||||
} |
||||
|
||||
// ------------------------------------------------------------ 响应
|
||||
|
||||
private static EdayunProductQueryDtos.ProductRow row() { |
||||
EdayunProductQueryDtos.ProductRow r = new EdayunProductQueryDtos.ProductRow(); |
||||
r.skuCode = "ybtest"; |
||||
r.skuId = "1001"; |
||||
r.skuName = "苹果14"; |
||||
r.skuNameEn = "iphone14"; |
||||
r.barcode = "N94902313123"; |
||||
r.extraBarcode1 = "E1"; |
||||
r.extraBarcode3 = "E3"; |
||||
r.length = 12.0; |
||||
r.width = 11.0; |
||||
r.height = 10.0; |
||||
r.weight = 600.0; |
||||
r.forecastLength = 99.0; |
||||
r.createTime = 1745473090263L; |
||||
return r; |
||||
} |
||||
|
||||
/** 实际尺寸优先于预报尺寸。 */ |
||||
@Test |
||||
void actualSizeWinsOverForecast() { |
||||
ProductInfo info = EdayunProductQueryMapper.toInfo(row()); |
||||
|
||||
assertEquals(12.0, info.lengthCm()); |
||||
assertEquals(11.0, info.widthCm()); |
||||
assertEquals(600.0, info.weightGrams()); |
||||
assertEquals("ybtest", info.providerSku()); |
||||
assertEquals("1001", info.providerProductId()); |
||||
assertEquals(2, info.extraBarcodes().size()); |
||||
assertEquals(WarehouseProvider.EDAYUN, info.sourceProvider()); |
||||
assertEquals(Instant.ofEpochMilli(1745473090263L), info.createdAt()); |
||||
} |
||||
|
||||
/** 实际尺寸缺失才回落预报值。 */ |
||||
@Test |
||||
void fallsBackToForecastSize() { |
||||
EdayunProductQueryDtos.ProductRow r = row(); |
||||
r.length = null; |
||||
r.weight = null; |
||||
|
||||
ProductInfo info = EdayunProductQueryMapper.toInfo(r); |
||||
|
||||
assertEquals(99.0, info.lengthCm()); |
||||
assertEquals(0.0, info.weightGrams()); |
||||
} |
||||
|
||||
/** 没有 SKU 的行跳过。 */ |
||||
@Test |
||||
void rowsWithoutSkuAreSkipped() { |
||||
EdayunProductQueryDtos.ProductRow bad = row(); |
||||
bad.skuCode = " "; |
||||
|
||||
assertNull(EdayunProductQueryMapper.toInfo(bad)); |
||||
assertNull(EdayunProductQueryMapper.toInfo(null)); |
||||
} |
||||
|
||||
/** 「查询无数据」是空结果,不抛异常(同轨迹接口的处理)。 */ |
||||
@Test |
||||
void noDataYieldsEmptyPage() { |
||||
RecordingTransport transport = stub( |
||||
"{\"total\":0,\"rows\":null,\"code\":500,\"msg\":\"查询无数据\"}"); |
||||
|
||||
Page<ProductInfo> page = new EdayunAdapter(transport, new JacksonJsonCodec()) |
||||
.query(ctx(), ProductQuery.builder().providerSku("NOPE").build()); |
||||
|
||||
assertTrue(page.items().isEmpty()); |
||||
assertTrue(!page.hasMore()); |
||||
} |
||||
|
||||
/** 其它业务错误仍然抛。 */ |
||||
@Test |
||||
void otherBusinessErrorsStillThrow() { |
||||
RecordingTransport transport = stub("{\"code\":500,\"msg\":\"系统异常\"}"); |
||||
|
||||
assertThrows(RuntimeException.class, |
||||
() -> new EdayunAdapter(transport, new JacksonJsonCodec()) |
||||
.query(ctx(), ProductQuery.builder().build())); |
||||
} |
||||
|
||||
@Test |
||||
void nextCursorUsesTotal() { |
||||
assertEquals("2", EdayunProductQueryMapper.nextCursor(250L, 1, 100, 100)); |
||||
assertNull(EdayunProductQueryMapper.nextCursor(250L, 3, 100, 50)); |
||||
// total 缺失时按满页判断
|
||||
assertEquals("2", EdayunProductQueryMapper.nextCursor(null, 1, 100, 100)); |
||||
} |
||||
|
||||
@Test |
||||
void adapterQueriesProducts() { |
||||
RecordingTransport transport = stub("{\"total\":1,\"code\":200,\"msg\":\"成功\",\"rows\":[{" |
||||
+ "\"skuCode\":\"ybtest\",\"skuName\":\"苹果14\",\"length\":12,\"width\":11," |
||||
+ "\"height\":10,\"weight\":600,\"createTime\":1745473090263}]}"); |
||||
|
||||
Page<ProductInfo> page = new EdayunAdapter(transport, new JacksonJsonCodec()) |
||||
.query(ctx(), ProductQuery.builder().providerSku("ybtest").build()); |
||||
|
||||
assertEquals(1, page.size()); |
||||
assertEquals("ybtest", page.items().get(0).providerSku()); |
||||
String body = transport.lastDataRequest().body(); |
||||
assertTrue(body.contains("\"skuCode\":\"ybtest\""), body); |
||||
assertTrue(body.contains("\"pageNum\":1"), body); |
||||
} |
||||
} |
||||
@ -0,0 +1,106 @@
@@ -0,0 +1,106 @@
|
||||
package common.ecommerce.edayun.probe; |
||||
|
||||
import common.ecommerce.edayun.EdayunConstants; |
||||
import common.ecommerce.edayun.adapter.EdayunAdapter; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.http.OkHttpTransport; |
||||
import common.ecommerce.shared.error.ChannelException; |
||||
import common.ecommerce.shared.spi.ChannelSecret; |
||||
import common.ecommerce.shared.support.Page; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.ProductInfo; |
||||
import common.ecommerce.warehouse.model.ProductQuery; |
||||
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
import org.junit.jupiter.api.Assumptions; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.junit.jupiter.api.condition.EnabledIfSystemProperty; |
||||
|
||||
import java.time.Clock; |
||||
import java.util.Arrays; |
||||
import java.util.UUID; |
||||
|
||||
/** |
||||
* 商品查询联调探针:<b>只读</b>。 |
||||
* |
||||
* <pre> |
||||
* # 只看报文(不联网) |
||||
* mvn -pl warehouse-sdk-edayun -DfailIfNoTests=false \ |
||||
* -Dedayun.probe.productQuery=true -Dtest=EdayunProductQueryProbeTest test |
||||
* |
||||
* # 真查(只读) |
||||
* ... -Dedayun.probe.productQuery=true -Dedayun.probe.call=true \ |
||||
* -Dedayun.probe.clientId=... -Dedayun.probe.clientSecret=... \ |
||||
* -Dedayun.probe.appKey=... -Dedayun.probe.appSecret=... \ |
||||
* [-Dedayun.probe.sku=ybtest] [-Dedayun.probe.days=30] |
||||
* </pre> |
||||
*/ |
||||
@EnabledIfSystemProperty(named = "edayun.probe.productQuery", matches = "true") |
||||
class EdayunProductQueryProbeTest { |
||||
|
||||
private static final String BASE_URL = opt("edayun.probe.baseUrl", EdayunConstants.PR_BASE_URL); |
||||
private static final String CLIENT_ID = System.getProperty("edayun.probe.clientId", ""); |
||||
private static final String CLIENT_SECRET = System.getProperty("edayun.probe.clientSecret", ""); |
||||
private static final String APP_KEY = System.getProperty("edayun.probe.appKey", ""); |
||||
private static final String APP_SECRET = System.getProperty("edayun.probe.appSecret", ""); |
||||
private static final String SKU = System.getProperty("edayun.probe.sku", "ybtest"); |
||||
private static final int DAYS = Integer.getInteger("edayun.probe.days", 30); |
||||
private static final boolean CALL = Boolean.getBoolean("edayun.probe.call"); |
||||
|
||||
@Test |
||||
void probeProductQuery() { |
||||
ProductQuery query = ProductQuery.builder() |
||||
.providerSku(SKU) |
||||
.createdAfter(java.time.Instant.now().minus(java.time.Duration.ofDays(DAYS))) |
||||
.createdBefore(java.time.Instant.now()) |
||||
.build(); |
||||
|
||||
System.out.println("\n================ edayun / POST " |
||||
+ EdayunConstants.PATH_PRODUCT_LIST + " ================"); |
||||
System.out.println("[probe] baseUrl=" + BASE_URL + " sku=" + SKU + " days=" + DAYS); |
||||
|
||||
Assumptions.assumeTrue(CALL, "dry-run:未加 -Dedayun.probe.call=true,不实际调用"); |
||||
Assumptions.assumeTrue(!CLIENT_ID.trim().isEmpty() && !CLIENT_SECRET.trim().isEmpty() |
||||
&& !APP_KEY.trim().isEmpty() && !APP_SECRET.trim().isEmpty(), |
||||
"真查需要 -Dedayun.probe.clientId / clientSecret / appKey / appSecret"); |
||||
|
||||
try { |
||||
WarehouseContext ctx = WarehouseContext.of("EDY-PROBE", WarehouseProvider.EDAYUN, "2", |
||||
WarehouseConnection.of(BASE_URL, |
||||
ChannelSecret.builder() |
||||
.appKey(APP_KEY).appSecret(APP_SECRET) |
||||
.clientId(CLIENT_ID).clientSecret(CLIENT_SECRET) |
||||
.build()), |
||||
"probe-" + UUID.randomUUID(), Clock.systemUTC()); |
||||
EdayunAdapter adapter = new EdayunAdapter( |
||||
new OkHttpTransport("common-ecommerce/1.0.0 (edayun-probe)", 10_000, 60_000), |
||||
new JacksonJsonCodec()); |
||||
|
||||
Page<ProductInfo> page = adapter.query(ctx, query); |
||||
System.out.println("[OK] rows=" + page.size() + " hasMore=" + page.hasMore() |
||||
+ " nextCursor=" + page.nextCursor()); |
||||
for (ProductInfo p : page.items()) { |
||||
System.out.println(" " + p.providerSku() + " " + p.name() |
||||
+ " lwh=" + p.lengthCm() + "x" + p.widthCm() + "x" + p.heightCm() |
||||
+ " weight=" + p.weightGrams() |
||||
+ " barcode=" + p.barcode() |
||||
+ " createdAt=" + p.createdAt()); |
||||
} |
||||
} catch (RuntimeException e) { |
||||
String msg = e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage(); |
||||
System.out.println("[ERROR] " + e.getClass().getSimpleName() + ": " |
||||
+ (msg.length() <= 800 ? msg : msg.substring(0, 800) + "…")); |
||||
if (e instanceof ChannelException) { |
||||
ChannelException ce = (ChannelException) e; |
||||
System.out.println(" platformCode=" + ce.platformCode() |
||||
+ " retryable=" + ce.retryable()); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private static String opt(String name, String fallback) { |
||||
String value = System.getProperty(name, ""); |
||||
String trimmed = value == null ? "" : value.trim(); |
||||
return trimmed.isEmpty() ? fallback : trimmed; |
||||
} |
||||
} |
||||
@ -0,0 +1,436 @@
@@ -0,0 +1,436 @@
|
||||
package common.ecommerce.edayun.sdk; |
||||
|
||||
import common.ecommerce.edayun.EdayunConstants; |
||||
import common.ecommerce.edayun.adapter.EdayunAdapter; |
||||
import common.ecommerce.edayun.adapter.EdayunAdapterFactory; |
||||
import common.ecommerce.shared.model.Platform; |
||||
import common.ecommerce.shared.spi.ChannelSecret; |
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.shared.testkit.InMemoryIdempotencyStore; |
||||
import common.ecommerce.shared.testkit.InMemoryLockProvider; |
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.shared.testkit.InMemoryTokenStore; |
||||
import common.ecommerce.shared.testkit.RecordingTransport; |
||||
import common.ecommerce.shared.testkit.Responses; |
||||
import common.ecommerce.warehouse.enums.CaseMarkPaperSize; |
||||
import common.ecommerce.warehouse.enums.CancelEligibility; |
||||
import common.ecommerce.warehouse.enums.ReturnDealMethod; |
||||
import common.ecommerce.warehouse.enums.WarehouseCapability; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.CaseMarkPrintCommand; |
||||
import common.ecommerce.warehouse.model.BolDocument; |
||||
import common.ecommerce.warehouse.model.CancelCommand; |
||||
import common.ecommerce.warehouse.model.CancelResult; |
||||
import common.ecommerce.warehouse.model.FeeEstimateQuery; |
||||
import common.ecommerce.warehouse.model.FeeEstimateResult; |
||||
import common.ecommerce.warehouse.model.InventoryQuery; |
||||
import common.ecommerce.warehouse.model.InventorySnapshot; |
||||
import common.ecommerce.warehouse.model.LogisticsProduct; |
||||
import common.ecommerce.warehouse.model.OutboundCommand; |
||||
import common.ecommerce.warehouse.model.OutboundLine; |
||||
import common.ecommerce.warehouse.model.OutboundOrder; |
||||
import common.ecommerce.warehouse.model.OutboundReceipt; |
||||
import common.ecommerce.warehouse.model.OutboundRef; |
||||
import common.ecommerce.warehouse.model.ReturnCommand; |
||||
import common.ecommerce.warehouse.model.ReturnQuery; |
||||
import common.ecommerce.warehouse.model.ShipTo; |
||||
import common.ecommerce.warehouse.model.TrackEvent; |
||||
import common.ecommerce.warehouse.model.TrackingInfo; |
||||
import common.ecommerce.warehouse.model.WarehouseCarrier; |
||||
import common.ecommerce.warehouse.model.WarehouseFacility; |
||||
import common.ecommerce.warehouse.model.WarehouseLabel; |
||||
import common.ecommerce.warehouse.model.WarehouseReturn; |
||||
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||
import common.ecommerce.warehouse.sdk.WarehouseSdk; |
||||
import common.ecommerce.warehouse.spi.WarehouseAccount; |
||||
import common.ecommerce.warehouse.testkit.InMemoryWarehouses; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.math.BigDecimal; |
||||
import java.time.Clock; |
||||
import java.time.Instant; |
||||
import java.time.ZoneOffset; |
||||
import java.util.List; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertNotNull; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 易达云 SDK 装配链路冒烟。 |
||||
* |
||||
* <p><b>不联网</b>:走 {@link WarehouseSdk} 门面 → 工厂 → 适配器 → 假传输层, |
||||
* 用真实报文样本验一遍「能不能装起来、能力位对不对、字段有没有搬对」。</p> |
||||
* |
||||
* <p>覆盖的业务场景:</p> |
||||
* <ol> |
||||
* <li>主数据:仓库 / 承运商 / 物流产品</li> |
||||
* <li>发货:试算 → 建单 → 查单 → 面单 → 取消</li> |
||||
* <li>库存:查询</li> |
||||
* <li>退货:建单 → 查详情 → 列表 → 取消</li> |
||||
* <li>单据 / 轨迹 / 打印</li> |
||||
* </ol> |
||||
*/ |
||||
class EdayunSdkSmokeTest { |
||||
|
||||
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-11-16T00:00:00Z"), ZoneOffset.UTC); |
||||
private static final String WH = "EDY-USLA"; |
||||
private static final String BASE = "https://test.omniselling.com.cn/saas-service/openapi/microService"; |
||||
|
||||
private static WarehouseSdk sdk(RecordingTransport transport) { |
||||
InMemoryWarehouses repo = new InMemoryWarehouses().put(WarehouseAccount.builder() |
||||
.warehouseCode(WH) |
||||
.provider(WarehouseProvider.EDAYUN.name()) |
||||
.providerWarehouseId("2") |
||||
.credentialRef("kms://test/edayun") |
||||
.timeZone("America/Los_Angeles") |
||||
.build()); |
||||
// 直接注册适配器实例:把传输层换成假的,其余装配(token 缓存 / 锁 / 时钟)与
|
||||
// 生产走同一套构造参数。工厂路径由 factoryAssemblesAdapter 单独验证。
|
||||
EdayunAdapter adapter = new EdayunAdapter(transport, new JacksonJsonCodec(), |
||||
new InMemoryTokenStore(), new InMemoryLockProvider(), CLOCK); |
||||
return WarehouseSdk.builder() |
||||
.warehouseRepository(repo) |
||||
.idempotencyStore(new InMemoryIdempotencyStore()) |
||||
.clock(CLOCK) |
||||
.register(adapter) |
||||
.build(); |
||||
} |
||||
|
||||
private static WarehouseConnection conn() { |
||||
return WarehouseConnection.of(BASE, ChannelSecret.builder() |
||||
.appKey("ak").appSecret("sk") |
||||
.clientId("cid").clientSecret("cs") |
||||
.build()); |
||||
} |
||||
|
||||
/** 所有业务接口共用的假传输层:token + 按路径路由。 */ |
||||
private static RecordingTransport transport() { |
||||
return RecordingTransport.create() |
||||
.route(EdayunConstants.PATH_TOKEN, req -> Responses.ok( |
||||
"{\"access_token\":\"T1\",\"token_type\":\"bearer\"}")) |
||||
.route(EdayunConstants.PATH_WAREHOUSE_PERMITTED, req -> Responses.ok( |
||||
"{\"code\":200,\"data\":[{\"warehouseId\":\"2\",\"warehouseName\":\"美西1仓\"," |
||||
+ "\"warehouseCode\":\"USLA\",\"countryCode\":\"US\"}]}")) |
||||
.route(EdayunConstants.PATH_CARRIERS, req -> Responses.ok( |
||||
"{\"code\":200,\"data\":[{\"carrierId\":\"1\",\"carrierNameEn\":\"FedEx\"," |
||||
+ "\"carrierNameCn\":\"FedEx\",\"ownLogisticList\":[" |
||||
+ "{\"logisticId\":\"250\",\"logisticName\":\"FedEx-express\"}]}]}")) |
||||
.route(EdayunConstants.PATH_LOGISTICS_PRODUCTS, req -> Responses.ok( |
||||
"{\"code\":200,\"data\":[{\"productId\":\"133\",\"productName\":\"USPS\"}," |
||||
+ "{\"productId\":\"-998\",\"productName\":\"使用分物流规则\"}]}")) |
||||
.route(EdayunConstants.PATH_FEE_ESTIMATE, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":{\"successItems\":[{" |
||||
+ "\"logisticProductId\":\"80\",\"logisticProductName\":\"美国经济派送\"," |
||||
+ "\"currencyCode\":\"USD\",\"estimatedPrice\":5.315," |
||||
+ "\"detailList\":[{\"billingItemName\":\"尾程配送费\"," |
||||
+ "\"estimatedPrice\":3.315,\"currencyCode\":\"USD\"}]}]," |
||||
+ "\"failItems\":[]}}")) |
||||
.route(EdayunConstants.PATH_ORDER_CREATE, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":{\"orderNo\":\"ORD1001\"," |
||||
+ "\"poNumber\":\"SO-1\"}}")) |
||||
.route(EdayunConstants.PATH_ORDER_GET, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":{\"orderNo\":\"ORD1001\"," |
||||
+ "\"poNumber\":\"SO-1\",\"orderStatus\":\"Shipped\"," |
||||
+ "\"trackingNo\":\"1Z111\"}}")) |
||||
.route(EdayunConstants.PATH_ORDER_LABEL, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":{\"orderNo\":\"ORD1001\"," |
||||
+ "\"trackingNo\":\"1Z111\",\"labels\":\"https://oss/l1.pdf\"}}")) |
||||
.route(EdayunConstants.PATH_ORDER_CHECK_CANCEL, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":\"Published\"}")) |
||||
.route(EdayunConstants.PATH_ORDER_CANCEL, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":true}")) |
||||
.route(EdayunConstants.PATH_INVENTORY, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"success\",\"total\":1,\"rows\":[{" |
||||
+ "\"skuCode\":\"ybtest\",\"stock\":\"4\",\"totalStock\":\"10\"," |
||||
+ "\"inTransitStock\":\"20\",\"frozenStock\":\"1\"," |
||||
+ "\"toDeliverStock\":\"2\",\"safeStock\":null," |
||||
+ "\"warehouseCode\":\"USLA\"}]}")) |
||||
.route(EdayunConstants.PATH_RETURN_CREATE, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":\"RMA1001\"}")) |
||||
.route(EdayunConstants.PATH_RETURN_DETAIL, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":{\"basicInfo\":{" |
||||
+ "\"rmaCode\":\"RMA1001\",\"status\":5,\"statusName\":\"已处理\"," |
||||
+ "\"relationNo\":\"SO-1\",\"warehouseName\":\"美西1仓\"}," |
||||
+ "\"checkList\":[{\"skuCode\":\"ybtest\",\"skuNum\":1," |
||||
+ "\"commandName\":\"上架\"}]}}")) |
||||
.route(EdayunConstants.PATH_RETURN_LIST, req -> Responses.ok( |
||||
"{\"total\":1,\"code\":200,\"rows\":[{\"rmaCode\":\"RMA1001\"," |
||||
+ "\"status\":5,\"statusName\":\"已处理\",\"warehouseName\":\"美西1仓\"," |
||||
+ "\"createTime\":1790568000000}]}")) |
||||
.route(EdayunConstants.PATH_RETURN_CANCEL, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":true}")) |
||||
.route(EdayunConstants.PATH_BOL_LIST, req -> Responses.ok( |
||||
"{\"code\":200,\"data\":{\"bolList\":[{\"bolCode\":\"cp005\"," |
||||
+ "\"status\":11,\"statusName\":\"已妥投\",\"bolFileUrl\":\"https://oss/b.pdf\"}]}}")) |
||||
.route(EdayunConstants.PATH_TRACK, req -> Responses.ok( |
||||
"{\"code\":200,\"data\":[{\"trackingNo\":\"1Z111\",\"status\":1," |
||||
+ "\"carrierName\":\"fedex\",\"onlineTime\":\"2025-08-19 08:17:00\"," |
||||
+ "\"deliveredTime\":\"2025-08-20 10:00:00\",\"timeZone\":\"GMT-04:00\"," |
||||
+ "\"trackDetaillVoList\":[{\"recentDescribe\":\"上网\"," |
||||
+ "\"recentDescribeTime\":1741145821000}]}]}")) |
||||
.route(EdayunConstants.PATH_CASE_MARK_PRINT, req -> Responses.ok( |
||||
"{\"code\":200,\"msg\":\"操作成功\",\"data\":\"https://oss/case.pdf\"}")) |
||||
.route(EdayunConstants.PATH_INBOUND_SKU_DETAIL, req -> Responses.ok( |
||||
"{\"total\":1,\"code\":200,\"rows\":[{\"entryCode\":\"REV1\"," |
||||
+ "\"skuCode\":\"ybtest\",\"forecastQuantity\":10," |
||||
+ "\"actualCheckedQuantity\":10,\"shelvesQuantity\":10," |
||||
+ "\"status\":4,\"statusName\":\"已上架\"}]}")); |
||||
} |
||||
|
||||
// ================================================================ 装配
|
||||
|
||||
/** |
||||
* 工厂装配:不联网,只验证 {@code build()} 能把适配器建出来、 |
||||
* 能力位与 provider 对得上。 |
||||
*/ |
||||
@Test |
||||
void factoryAssemblesAdapter() { |
||||
InMemoryWarehouses repo = new InMemoryWarehouses().put(WarehouseAccount.builder() |
||||
.warehouseCode(WH) |
||||
.provider(WarehouseProvider.EDAYUN.name()) |
||||
.providerWarehouseId("2") |
||||
.credentialRef("kms://test/edayun") |
||||
.timeZone("America/Los_Angeles") |
||||
.build()); |
||||
WarehouseSdk built = WarehouseSdk.builder() |
||||
.warehouseRepository(repo) |
||||
.clock(CLOCK) |
||||
.register(new EdayunAdapterFactory( |
||||
new InMemoryTokenStore(), new InMemoryLockProvider(), CLOCK)) |
||||
.build(); |
||||
|
||||
assertTrue(built.capabilities(WH).supported().contains(WarehouseCapability.OUTBOUND)); |
||||
// 能按仓解析到适配器:仓库清单会真的打一次仓方,用假传输层接
|
||||
assertNotNull(built.warehouses(WH, conn())); |
||||
} |
||||
|
||||
// ================================================================ 能力位
|
||||
|
||||
@Test |
||||
void declaresAllImplementedCapabilities() { |
||||
WarehouseSdk sdk = sdk(transport()); |
||||
java.util.EnumSet<WarehouseCapability> caps = sdk.capabilities(WH).supported(); |
||||
|
||||
// 已实现
|
||||
assertTrue(caps.contains(WarehouseCapability.PRODUCT), "商品"); |
||||
assertTrue(caps.contains(WarehouseCapability.OUTBOUND), "出库"); |
||||
assertTrue(caps.contains(WarehouseCapability.INVENTORY_QUERY), "库存"); |
||||
assertTrue(caps.contains(WarehouseCapability.INBOUND), "入库"); |
||||
assertTrue(caps.contains(WarehouseCapability.RETURN), "退货"); |
||||
assertTrue(caps.contains(WarehouseCapability.FEE_SETTLEMENT), "费用"); |
||||
assertTrue(caps.contains(WarehouseCapability.FEE_ESTIMATE), "试算"); |
||||
assertTrue(caps.contains(WarehouseCapability.BOL), "BOL"); |
||||
assertTrue(caps.contains(WarehouseCapability.TRACKING), "轨迹"); |
||||
assertTrue(caps.contains(WarehouseCapability.LOGISTICS_PRODUCT), "物流产品"); |
||||
assertTrue(caps.contains(WarehouseCapability.CASE_MARK_PRINT), "箱唛"); |
||||
assertTrue(caps.contains(WarehouseCapability.DIRECTORY), "基础资料"); |
||||
|
||||
// 未实现——调了必须抛,不能返回空
|
||||
assertTrue(!caps.contains(WarehouseCapability.TRANSFER), "易达云没有调拨"); |
||||
assertTrue(!caps.contains(WarehouseCapability.WEBHOOK), "回调文档还没给"); |
||||
assertTrue(!caps.contains(WarehouseCapability.RETURN_DISPOSITION), "缺行 id"); |
||||
} |
||||
|
||||
// ================================================================ 场景 1:主数据
|
||||
|
||||
@Test |
||||
void scenario1_masterData() { |
||||
WarehouseSdk sdk = sdk(transport()); |
||||
|
||||
List<WarehouseFacility> warehouses = sdk.warehouses(WH, conn()).warehouses(); |
||||
assertEquals(1, warehouses.size()); |
||||
assertEquals("USLA", warehouses.get(0).warehouseCode()); |
||||
assertEquals("2", warehouses.get(0).providerWarehouseId()); |
||||
|
||||
List<WarehouseCarrier> carriers = sdk.warehouses(WH, conn()).carrierDirectory(); |
||||
assertEquals(1, carriers.size()); |
||||
assertEquals("FedEx", carriers.get(0).nameEn()); |
||||
assertEquals(1, carriers.get(0).logists().size()); |
||||
|
||||
List<LogisticsProduct> products = sdk.warehouses(WH, conn()).logisticsProducts(null); |
||||
// -998 已跳过
|
||||
assertEquals(1, products.size()); |
||||
assertEquals("133", products.get(0).logisticsProductCode()); |
||||
} |
||||
|
||||
// ================================================================ 场景 2:发货
|
||||
|
||||
@Test |
||||
void scenario2_shipping() { |
||||
RecordingTransport transport = transport(); |
||||
WarehouseSdk sdk = sdk(transport); |
||||
|
||||
// 2.1 发货前试算
|
||||
List<FeeEstimateResult> quotes = sdk.warehouses(WH, conn()).fees().estimate( |
||||
FeeEstimateQuery.builder() |
||||
.lengthCm(new BigDecimal("22")).widthCm(new BigDecimal("22")) |
||||
.heightCm(new BigDecimal("22")).weightGrams(new BigDecimal("1000")) |
||||
.receiveCountry("US").receivePostal("91761") |
||||
.logisticsServiceCode("80") |
||||
.build()); |
||||
assertEquals(1, quotes.size()); |
||||
assertEquals(1, quotes.get(0).carriers().size()); |
||||
assertEquals(new BigDecimal("5.315"), quotes.get(0).carriers().get(0).totalRate().amount()); |
||||
|
||||
// 2.2 建单
|
||||
OutboundReceipt receipt = sdk.warehouses(WH, conn()).outbound().create(outbound()); |
||||
assertEquals("ORD1001", receipt.providerOrderId()); |
||||
|
||||
// 2.3 查单
|
||||
OutboundOrder order = sdk.warehouses(WH, conn()).outbound() |
||||
.get(OutboundRef.ofRequestNo("SO-1")); |
||||
assertNotNull(order); |
||||
assertEquals("ORD1001", order.providerOrderId()); |
||||
|
||||
// 2.4 面单
|
||||
assertEquals(1, sdk.warehouses(WH, conn()).outbound() |
||||
.packages(OutboundRef.ofRequestNo("SO-1")).size()); |
||||
|
||||
// 2.5 取消前校验 → 免费
|
||||
assertEquals(CancelEligibility.FREE, sdk.warehouses(WH, conn()).outbound() |
||||
.checkCancel(OutboundRef.ofRequestNo("SO-1"))); |
||||
|
||||
// 2.6 取消
|
||||
CancelResult cancelled = sdk.warehouses(WH, conn()).outbound().cancel( |
||||
CancelCommand.builder().outboundRequestNo("SO-1").providerOrderId("ORD1001").build()); |
||||
assertTrue(cancelled.cancelled()); |
||||
} |
||||
|
||||
/** 建单幂等:同一个 outboundRequestNo 调两次只发一次请求。 */ |
||||
@Test |
||||
void scenario2_createIsIdempotent() { |
||||
RecordingTransport transport = transport(); |
||||
WarehouseSdk sdk = sdk(transport); |
||||
|
||||
sdk.warehouses(WH, conn()).outbound().create(outbound()); |
||||
sdk.warehouses(WH, conn()).outbound().create(outbound()); |
||||
|
||||
long creates = transport.dataRequests().stream() |
||||
.filter(r -> r.url().endsWith(EdayunConstants.PATH_ORDER_CREATE)) |
||||
.count(); |
||||
assertEquals(1, creates, "第二次应命中幂等,不再调仓"); |
||||
} |
||||
|
||||
private static OutboundCommand outbound() { |
||||
return OutboundCommand.builder() |
||||
.outboundRequestNo("OB-1") |
||||
.orderRef("SO-1") |
||||
.warehouseId("2") |
||||
.currency("USD") |
||||
.salesPlatform(Platform.AMAZON_SC) |
||||
.shipTo(ShipTo.builder() |
||||
.name("Buyer").phone("+1-555-0100") |
||||
.countryCode("US").stateOrProvince("California") |
||||
.city("Escondido").addressLine1("1308 E Washington Ave") |
||||
.postalCode("92027") |
||||
.build()) |
||||
.line(OutboundLine.builder() |
||||
.lineNo("1").omsSku("ybtest").quantity(1) |
||||
.declareValue(Money.of("19.99", "USD")) |
||||
.build()) |
||||
.build(); |
||||
} |
||||
|
||||
// ================================================================ 场景 3:库存
|
||||
|
||||
@Test |
||||
void scenario3_inventory() { |
||||
WarehouseSdk sdk = sdk(transport()); |
||||
|
||||
List<InventorySnapshot> rows = sdk.warehouses(WH, conn()).inventory() |
||||
.query(InventoryQuery.builder().sku("ybtest").build()).items(); |
||||
|
||||
assertEquals(1, rows.size()); |
||||
InventorySnapshot s = rows.get(0); |
||||
assertEquals("ybtest", s.providerSku()); |
||||
assertEquals(4, s.available()); |
||||
assertEquals(10, s.onHand()); |
||||
assertEquals(20, s.inTransit()); |
||||
// safeStock 为 null → -1(统一模型约定)
|
||||
assertEquals(-1, s.alertQuantity()); |
||||
} |
||||
|
||||
// ================================================================ 场景 4:退货
|
||||
|
||||
@Test |
||||
void scenario4_returns() { |
||||
WarehouseSdk sdk = sdk(transport()); |
||||
|
||||
// 4.1 建退货单(自行退回)
|
||||
WarehouseReturn created = sdk.warehouses(WH, conn()).returns().create( |
||||
ReturnCommand.builder() |
||||
.returnRequestNo("RT-1") |
||||
.saleOrderNo("SO-1") |
||||
.returnTrackingNo("1Z999") |
||||
.carrierCode("FedEx") |
||||
.warehouseId("2") |
||||
.line("ybtest", 1, ReturnDealMethod.WAREHOUSE_CONFIRM) |
||||
.build()); |
||||
assertEquals("RMA1001", created.providerReturnId()); |
||||
|
||||
// 4.2 查详情
|
||||
WarehouseReturn detail = sdk.warehouses(WH, conn()).returns().get("RMA1001"); |
||||
assertNotNull(detail); |
||||
assertEquals("RMA1001", detail.providerReturnId()); |
||||
assertEquals(1, detail.lines().size()); |
||||
|
||||
// 4.3 列表
|
||||
assertEquals(1, sdk.warehouses(WH, conn()).returns() |
||||
.query(ReturnQuery.builder().pageSize(100).build()).size()); |
||||
|
||||
// 4.4 取消
|
||||
assertTrue(sdk.warehouses(WH, conn()).returns().cancel("RMA1001")); |
||||
} |
||||
|
||||
// ================================================================ 场景 5:单据 / 轨迹 / 打印
|
||||
|
||||
@Test |
||||
void scenario5_documentsTrackingAndPrinting() { |
||||
WarehouseSdk sdk = sdk(transport()); |
||||
|
||||
// BOL
|
||||
List<BolDocument> bols = sdk.warehouses(WH, conn()).orderBols("FBA1"); |
||||
assertEquals(1, bols.size()); |
||||
assertEquals("cp005", bols.get(0).bolCode()); |
||||
|
||||
// 轨迹:节点
|
||||
List<TrackEvent> events = sdk.warehouses(WH, conn()).track("1Z111"); |
||||
assertEquals(1, events.size()); |
||||
// 轨迹:运单级,带上网 / 妥投时间
|
||||
TrackingInfo info = sdk.warehouses(WH, conn()).trackShipment("1Z111"); |
||||
assertNotNull(info.firstOnlineAt()); |
||||
assertNotNull(info.deliveredAt()); |
||||
|
||||
// 箱唛(同步 PDF)
|
||||
WarehouseLabel label = sdk.warehouses(WH, conn()).printCaseMarks( |
||||
CaseMarkPrintCommand.builder() |
||||
.paperSize(CaseMarkPaperSize.SIZE_70_30) |
||||
.item("ybtest", 1) |
||||
.build()); |
||||
assertEquals("https://oss/case.pdf", label.url()); |
||||
|
||||
// 入库单
|
||||
assertNotNull(sdk.warehouses(WH, conn()).inbound().get("REV1")); |
||||
} |
||||
|
||||
// ================================================================ 未实现能力
|
||||
|
||||
@Test |
||||
void unimplementedCapabilitiesThrow() { |
||||
WarehouseSdk sdk = sdk(transport()); |
||||
|
||||
// 调拨
|
||||
assertThrows(RuntimeException.class, |
||||
() -> sdk.warehouses(WH, conn()).getTransfer("T-1")); |
||||
// 退货检验后处理(缺行 id)
|
||||
assertThrows(RuntimeException.class, |
||||
() -> sdk.warehouses(WH, conn()).returns().process( |
||||
common.ecommerce.warehouse.model.ReturnProcessCommand.builder() |
||||
.providerReturnNo("RMA1").lineId(1) |
||||
.disposition(common.ecommerce.warehouse.enums |
||||
.ReturnProcessDisposition.RESTOCK) |
||||
.build())); |
||||
} |
||||
} |
||||
@ -0,0 +1,175 @@
@@ -0,0 +1,175 @@
|
||||
package common.ecommerce.yqn.adapter; |
||||
|
||||
import common.ecommerce.shared.support.Money; |
||||
import common.ecommerce.shared.support.Page; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.ProductInfo; |
||||
import common.ecommerce.warehouse.model.ProductQuery; |
||||
import common.ecommerce.yqn.dto.YqnProductQueryDtos; |
||||
|
||||
import java.time.Instant; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 运去哪商品查询映射(API 113,{@code /v1/product/findPage})。 |
||||
* |
||||
* <p><b>页码从 0 开始</b>——统一模型的游标语义是「下一页页码」, |
||||
* 这里用字符串页码原样透传,不做 ±1 换算,免得调用方和仓方口径对不上。</p> |
||||
*/ |
||||
public final class YqnProductQueryMapper { |
||||
|
||||
/** 单页上限(文档:默认 100、最大 100)。 */ |
||||
static final int MAX_PAGE_SIZE = 100; |
||||
|
||||
/** 一次最多查多少个 SKU(文档:最大 100)。 */ |
||||
static final int MAX_SKUS = 100; |
||||
|
||||
private YqnProductQueryMapper() { |
||||
} |
||||
|
||||
/** 统一查询条件 → 仓方请求。 */ |
||||
public static YqnProductQueryDtos.FindPageRequest toRequest(ProductQuery query) { |
||||
YqnProductQueryDtos.FindPageRequest req = new YqnProductQueryDtos.FindPageRequest(); |
||||
if (!query.providerSkus().isEmpty()) { |
||||
if (query.providerSkus().size() > MAX_SKUS) { |
||||
throw new IllegalArgumentException( |
||||
"yqn product query accepts at most " + MAX_SKUS + " skus, got " |
||||
+ query.providerSkus().size()); |
||||
} |
||||
req.skuList = new ArrayList<String>(query.providerSkus()); |
||||
} |
||||
req.pageSize = pageSize(query.pageSize()); |
||||
req.pageNum = pageNum(query.cursor()); |
||||
return req; |
||||
} |
||||
|
||||
/** 每页条数:默认 100、上限 100。 */ |
||||
static int pageSize(int requested) { |
||||
if (requested <= 0) { |
||||
return MAX_PAGE_SIZE; |
||||
} |
||||
return Math.min(requested, MAX_PAGE_SIZE); |
||||
} |
||||
|
||||
/** 游标 → 页码;<b>从 0 开始</b>,空游标即第 0 页。 */ |
||||
static int pageNum(String cursor) { |
||||
if (cursor == null || cursor.trim().isEmpty()) { |
||||
return 0; |
||||
} |
||||
try { |
||||
int v = Integer.parseInt(cursor.trim()); |
||||
return Math.max(v, 0); |
||||
} catch (NumberFormatException e) { |
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
/** 响应 → 分页结果。 */ |
||||
public static Page<ProductInfo> toPage(YqnProductQueryDtos.FindPageResponse response) { |
||||
if (response == null || response.data == null |
||||
|| response.data.content == null || response.data.content.isEmpty()) { |
||||
return Page.empty(); |
||||
} |
||||
List<ProductInfo> out = new ArrayList<ProductInfo>(response.data.content.size()); |
||||
for (YqnProductQueryDtos.ProductRow row : response.data.content) { |
||||
ProductInfo info = toInfo(row); |
||||
if (info != null) { |
||||
out.add(info); |
||||
} |
||||
} |
||||
return Page.of(out, nextCursor(response.data)); |
||||
} |
||||
|
||||
/** |
||||
* 下一页游标。 |
||||
* |
||||
* <p>优先用 {@code last}(仓方明确给了是否末页);缺失时用 |
||||
* {@code number+1 < totalPages} 推断。两者都给不出时按「本页是否满页」兜底。</p> |
||||
*/ |
||||
static String nextCursor(YqnProductQueryDtos.ProductPage page) { |
||||
if (page == null) { |
||||
return null; |
||||
} |
||||
if (page.last != null) { |
||||
if (page.last) { |
||||
return null; |
||||
} |
||||
int current = page.number == null ? 0 : page.number; |
||||
return String.valueOf(current + 1); |
||||
} |
||||
if (page.number != null && page.totalPages != null) { |
||||
return page.number + 1 < page.totalPages |
||||
? String.valueOf(page.number + 1) : null; |
||||
} |
||||
if (page.size != null && page.numberOfElements != null) { |
||||
return page.numberOfElements < page.size |
||||
? null : String.valueOf((page.number == null ? 0 : page.number) + 1); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** 一行 → 统一商品资料;没有 SKU 的行跳过。 */ |
||||
static ProductInfo toInfo(YqnProductQueryDtos.ProductRow row) { |
||||
if (row == null || row.sku == null || row.sku.trim().isEmpty()) { |
||||
return null; |
||||
} |
||||
ProductInfo.Builder b = ProductInfo.builder() |
||||
.sourceProvider(WarehouseProvider.YQN) |
||||
.providerSku(row.sku.trim()) |
||||
.customItemNo(trimToNull(row.customItemNo)) |
||||
.barcode(trimToNull(row.upc)) |
||||
.name(trimToNull(row.name)) |
||||
.nameEn(trimToNull(row.nameEn)) |
||||
.description(trimToNull(row.description)) |
||||
// 文档:1 成品 / 2 配件 / 3 耗材;未给保持 -1
|
||||
.productType(row.productType == null ? -1 : row.productType) |
||||
.lengthCm(numberOf(row.length)) |
||||
.widthCm(numberOf(row.width)) |
||||
.heightCm(numberOf(row.height)) |
||||
.weightGrams(numberOf(row.weight)) |
||||
.itemLengthCm(row.itemLength) |
||||
.itemWidthCm(row.itemWidth) |
||||
.itemHeightCm(row.itemHeight) |
||||
.packedQuantity(row.packedQty) |
||||
.packedLengthCm(row.packedLength) |
||||
.packedWidthCm(row.packedWidth) |
||||
.packedHeightCm(row.packedHeight) |
||||
.packedWeightGrams(row.packedWeight) |
||||
.requireSn(flagOf(row.isRequireSn)) |
||||
.remark(trimToNull(row.remark)); |
||||
|
||||
// 成本:文档标 integer,但货币是另一列;没有币种时不造 Money
|
||||
if (row.cost != null && trimToNull(row.currency) != null) { |
||||
b.cost(Money.of(java.math.BigDecimal.valueOf(row.cost), row.currency.trim())); |
||||
} |
||||
return b.build(); |
||||
} |
||||
|
||||
/** |
||||
* 文档把 {@code isRequireSn} 标成 string,真机可能是数字。 |
||||
* 统一按「是不是 1 / "1" / true」判,认不出返回 null(不猜)。 |
||||
*/ |
||||
static Boolean flagOf(String raw) { |
||||
if (raw == null) { |
||||
return null; |
||||
} |
||||
String v = raw.trim(); |
||||
if ("1".equals(v) || "true".equalsIgnoreCase(v) || "Y".equalsIgnoreCase(v)) { |
||||
return Boolean.TRUE; |
||||
} |
||||
if ("0".equals(v) || "false".equalsIgnoreCase(v) || "N".equalsIgnoreCase(v)) { |
||||
return Boolean.FALSE; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
private static double numberOf(Double v) { |
||||
return v == null ? 0 : v; |
||||
} |
||||
|
||||
private static String trimToNull(String v) { |
||||
return v == null || v.trim().isEmpty() ? null : v.trim(); |
||||
} |
||||
} |
||||
@ -0,0 +1,112 @@
@@ -0,0 +1,112 @@
|
||||
package common.ecommerce.yqn.dto; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 运去哪报文结构:批量查询 SKU 分页(API 113,{@code /v1/product/findPage})。 |
||||
* |
||||
* <p>一个文件对应一个接口,只放该接口的请求 / 响应与内嵌子结构。</p> |
||||
* |
||||
* <p><b>页码从 0 开始</b>(文档:「起始页为从0开始」),与其它分页接口不同。 |
||||
* {@code pageSize} 默认 100、最大 100。</p> |
||||
*/ |
||||
public final class YqnProductQueryDtos { |
||||
|
||||
private YqnProductQueryDtos() { |
||||
} |
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class FindPageRequest { |
||||
|
||||
/** 仓侧 SKU 列表,最大 100 个。 */ |
||||
public List<String> skuList; |
||||
/** 每页大小,默认 100、最大 100。 */ |
||||
public Integer pageSize; |
||||
/** 页码,<b>从 0 开始</b>。 */ |
||||
public Integer pageNum; |
||||
} |
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class FindPageResponse { |
||||
public Integer code; |
||||
public String message; |
||||
public Integer statusCode; |
||||
public Integer status; |
||||
public ProductPage data; |
||||
} |
||||
|
||||
/** Spring Data 风格的分页载荷。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class ProductPage { |
||||
public Pageable pageable; |
||||
public Integer totalElements; |
||||
public Integer totalPages; |
||||
public Boolean last; |
||||
public Boolean first; |
||||
public Integer numberOfElements; |
||||
public Integer size; |
||||
public Integer number; |
||||
public Boolean empty; |
||||
public List<ProductRow> content; |
||||
} |
||||
|
||||
/** 分页元信息(只取用得到的几个)。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class Pageable { |
||||
public Integer pageNumber; |
||||
public Integer pageSize; |
||||
public Integer offset; |
||||
public Boolean paged; |
||||
public Boolean unpaged; |
||||
} |
||||
|
||||
/** 一行商品。 */ |
||||
@JsonInclude(JsonInclude.Include.NON_NULL) |
||||
public static final class ProductRow { |
||||
public String sku; |
||||
public String customItemNo; |
||||
public String upc; |
||||
/** 1 成品 / 2 配件 / 3 耗材。 */ |
||||
public Integer productType; |
||||
public String name; |
||||
public String nameEn; |
||||
public String description; |
||||
|
||||
/** 整箱尺寸(cm)与重量(g)。 */ |
||||
public Double length; |
||||
public Double width; |
||||
public Double height; |
||||
public Double weight; |
||||
|
||||
/** 单品尺寸(cm)。 */ |
||||
public Double itemLength; |
||||
public Double itemWidth; |
||||
public Double itemHeight; |
||||
|
||||
/** 装箱数量 / 外箱尺寸重量。 */ |
||||
public Integer packedQty; |
||||
public Double packedLength; |
||||
public Double packedWidth; |
||||
public Double packedHeight; |
||||
public Double packedWeight; |
||||
|
||||
public Double cost; |
||||
public String currency; |
||||
public String remark; |
||||
|
||||
/** 是否需要 SN;文档标 string({@code "0"} / {@code "1"})。 */ |
||||
public String isRequireSn; |
||||
|
||||
/** 商品形态。 */ |
||||
public Integer saleType; |
||||
public String parentErpsku; |
||||
public Integer standardQuantity; |
||||
|
||||
public String customName; |
||||
public String customNameEn; |
||||
/** 是否启用。 */ |
||||
public Integer enable; |
||||
} |
||||
} |
||||
@ -0,0 +1,190 @@
@@ -0,0 +1,190 @@
|
||||
package common.ecommerce.yqn.adapter; |
||||
|
||||
import common.ecommerce.http.JacksonJsonCodec; |
||||
import common.ecommerce.shared.spi.ChannelSecret; |
||||
import common.ecommerce.shared.support.Page; |
||||
import common.ecommerce.shared.testkit.RecordingTransport; |
||||
import common.ecommerce.shared.testkit.Responses; |
||||
import common.ecommerce.warehouse.enums.WarehouseProvider; |
||||
import common.ecommerce.warehouse.model.ProductInfo; |
||||
import common.ecommerce.warehouse.model.ProductQuery; |
||||
import common.ecommerce.warehouse.sdk.WarehouseConnection; |
||||
import common.ecommerce.warehouse.sdk.WarehouseContext; |
||||
import common.ecommerce.yqn.YqnConstants; |
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import java.time.Clock; |
||||
import java.time.Instant; |
||||
import java.time.ZoneOffset; |
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||
import static org.junit.jupiter.api.Assertions.assertFalse; |
||||
import static org.junit.jupiter.api.Assertions.assertNull; |
||||
import static org.junit.jupiter.api.Assertions.assertThrows; |
||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||
|
||||
/** |
||||
* 商品查询(API 113,{@code /v1/product/findPage})映射。 |
||||
* |
||||
* <p>重点:<b>页码从 0 开始</b>、单页上限 100、{@code last} 优先判断翻页。</p> |
||||
*/ |
||||
class YqnProductQueryTest { |
||||
|
||||
private static final Clock CLOCK = Clock.fixed(Instant.parse("2026-11-17T00:00:00Z"), ZoneOffset.UTC); |
||||
|
||||
private static WarehouseContext ctx() { |
||||
return WarehouseContext.of("YQN-WH", WarehouseProvider.YQN, "RF_NY_US", |
||||
WarehouseConnection.of(YqnConstants.PR_BASE_URL, |
||||
ChannelSecret.builder().appSecret("key-1").build()), |
||||
"req-product-query", CLOCK); |
||||
} |
||||
|
||||
private static RecordingTransport stub(String body) { |
||||
return RecordingTransport.create() |
||||
.route(YqnConstants.PATH_PRODUCT_FIND_PAGE, req -> Responses.ok(body)); |
||||
} |
||||
|
||||
// ------------------------------------------------------------ 请求
|
||||
|
||||
/** 游标为空 → 第 0 页(不是第 1 页)。 */ |
||||
@Test |
||||
void emptyCursorMeansPageZero() { |
||||
assertEquals(0, YqnProductQueryMapper.pageNum(null)); |
||||
assertEquals(0, YqnProductQueryMapper.pageNum(" ")); |
||||
assertEquals(0, YqnProductQueryMapper.pageNum("abc")); |
||||
assertEquals(3, YqnProductQueryMapper.pageNum("3")); |
||||
// 负数归 0
|
||||
assertEquals(0, YqnProductQueryMapper.pageNum("-1")); |
||||
} |
||||
|
||||
/** 单页上限 100。 */ |
||||
@Test |
||||
void pageSizeCappedAt100() { |
||||
assertEquals(100, YqnProductQueryMapper.pageSize(0)); |
||||
assertEquals(100, YqnProductQueryMapper.pageSize(500)); |
||||
assertEquals(50, YqnProductQueryMapper.pageSize(50)); |
||||
} |
||||
|
||||
@Test |
||||
void mapsSkusAndPaging() { |
||||
common.ecommerce.yqn.dto.YqnProductQueryDtos.FindPageRequest req = |
||||
YqnProductQueryMapper.toRequest( |
||||
ProductQuery.builder().providerSku("SKU-1").providerSku("SKU-2") |
||||
.pageSize(20).cursor("2").build()); |
||||
|
||||
assertEquals(java.util.Arrays.asList("SKU-1", "SKU-2"), req.skuList); |
||||
assertEquals(Integer.valueOf(20), req.pageSize); |
||||
assertEquals(Integer.valueOf(2), req.pageNum); |
||||
} |
||||
|
||||
@Test |
||||
void moreThan100SkusRejected() { |
||||
ProductQuery.Builder b = ProductQuery.builder(); |
||||
for (int i = 0; i < 101; i++) { |
||||
b.providerSku("SKU-" + i); |
||||
} |
||||
assertThrows(IllegalArgumentException.class, () -> b.build()); |
||||
} |
||||
|
||||
/** 运去哪这个接口没有条码 / 时间窗过滤,传了要本地拒绝。 */ |
||||
@Test |
||||
void unsupportedFiltersAreRejected() { |
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> new YqnAdapter(stub("{}"), new JacksonJsonCodec()).query(ctx(), |
||||
ProductQuery.builder().barcode("UPC1").build())); |
||||
|
||||
assertThrows(IllegalArgumentException.class, |
||||
() -> new YqnAdapter(stub("{}"), new JacksonJsonCodec()).query(ctx(), |
||||
ProductQuery.builder() |
||||
.createdAfter(Instant.parse("2026-01-01T00:00:00Z")) |
||||
.createdBefore(Instant.parse("2026-02-01T00:00:00Z")) |
||||
.build())); |
||||
} |
||||
|
||||
// ------------------------------------------------------------ 响应
|
||||
|
||||
private static String pageBody(boolean last, int number, Integer totalPages) { |
||||
return "{\"code\":200,\"message\":\"ok\",\"data\":{" |
||||
+ "\"pageable\":{\"pageNumber\":" + number + ",\"pageSize\":100}," |
||||
+ "\"numberOfElements\":1,\"size\":100,\"number\":" + number + "," |
||||
+ "\"last\":" + last + "," |
||||
+ (totalPages == null ? "" : "\"totalPages\":" + totalPages + ",") |
||||
+ "\"content\":[{\"sku\":\"SKU-1\",\"name\":\"商品1\",\"nameEn\":\"P1\"," |
||||
+ "\"upc\":\"UPC1\",\"productType\":1,\"length\":10,\"width\":8,\"height\":6," |
||||
+ "\"weight\":500,\"packedQty\":12,\"isRequireSn\":\"0\"," |
||||
+ "\"cost\":9.5,\"currency\":\"USD\",\"enable\":1}]}}"; |
||||
} |
||||
|
||||
@Test |
||||
void mapsRow() { |
||||
Page<ProductInfo> page = YqnProductQueryMapper.toPage( |
||||
new JacksonJsonCodec().fromJson(pageBody(true, 0, null), |
||||
common.ecommerce.yqn.dto.YqnProductQueryDtos.FindPageResponse.class)); |
||||
|
||||
assertEquals(1, page.size()); |
||||
ProductInfo p = page.items().get(0); |
||||
assertEquals("SKU-1", p.providerSku()); |
||||
assertEquals("商品1", p.name()); |
||||
assertEquals("UPC1", p.barcode()); |
||||
assertEquals(1, p.productType()); |
||||
assertEquals(10.0, p.lengthCm()); |
||||
assertEquals(500.0, p.weightGrams()); |
||||
assertEquals(Integer.valueOf(12), p.packedQuantity()); |
||||
assertEquals(Boolean.FALSE, p.requireSn()); |
||||
assertEquals(WarehouseProvider.YQN, p.sourceProvider()); |
||||
// 成本带币种才成 Money
|
||||
assertEquals(0, new java.math.BigDecimal("9.5").compareTo(p.cost().amount())); |
||||
assertEquals("USD", p.cost().currencyCode()); |
||||
assertFalse(page.hasMore()); |
||||
} |
||||
|
||||
/** {@code last=false} → 游标 = number + 1。 */ |
||||
@Test |
||||
void nextCursorUsesLast() { |
||||
assertEquals("1", YqnProductQueryMapper.nextCursor(page(0, false, null))); |
||||
assertNull(YqnProductQueryMapper.nextCursor(page(3, true, null))); |
||||
} |
||||
|
||||
/** {@code last} 缺失时用 number + 1 < totalPages 推断。 */ |
||||
@Test |
||||
void nextCursorFallsBackToTotalPages() { |
||||
assertEquals("2", YqnProductQueryMapper.nextCursor(page(1, null, 3))); |
||||
assertNull(YqnProductQueryMapper.nextCursor(page(2, null, 3))); |
||||
} |
||||
|
||||
private static common.ecommerce.yqn.dto.YqnProductQueryDtos.ProductPage page( |
||||
int number, Boolean last, Integer totalPages) { |
||||
common.ecommerce.yqn.dto.YqnProductQueryDtos.ProductPage p = |
||||
new common.ecommerce.yqn.dto.YqnProductQueryDtos.ProductPage(); |
||||
p.number = number; |
||||
p.last = last; |
||||
p.totalPages = totalPages; |
||||
p.size = 100; |
||||
p.numberOfElements = 100; |
||||
return p; |
||||
} |
||||
|
||||
/** 没有 SKU 的行跳过,不整页失败。 */ |
||||
@Test |
||||
void rowsWithoutSkuAreSkipped() { |
||||
common.ecommerce.yqn.dto.YqnProductQueryDtos.ProductRow bad = |
||||
new common.ecommerce.yqn.dto.YqnProductQueryDtos.ProductRow(); |
||||
bad.sku = " "; |
||||
assertNull(YqnProductQueryMapper.toInfo(bad)); |
||||
assertNull(YqnProductQueryMapper.toInfo(null)); |
||||
} |
||||
|
||||
@Test |
||||
void adapterQueriesProducts() { |
||||
RecordingTransport transport = stub(pageBody(true, 0, null)); |
||||
|
||||
Page<ProductInfo> page = new YqnAdapter(transport, new JacksonJsonCodec()) |
||||
.query(ctx(), ProductQuery.builder().providerSku("SKU-1").build()); |
||||
|
||||
assertEquals(1, page.size()); |
||||
String body = transport.lastDataRequest().body(); |
||||
assertTrue(body.contains("\"skuList\":[\"SKU-1\"]"), body); |
||||
// 首页必须是 pageNum=0
|
||||
assertTrue(body.contains("\"pageNum\":0"), body); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue