diff --git a/core/src/main/java/common/ecommerce/model/Platform.java b/core/src/main/java/common/ecommerce/model/Platform.java index 2572ca5..4e04c23 100644 --- a/core/src/main/java/common/ecommerce/model/Platform.java +++ b/core/src/main/java/common/ecommerce/model/Platform.java @@ -24,6 +24,8 @@ public enum Platform { EBAY, /** SHEIN(首期经领星接入) */ SHEIN, + /** Shopify(首期经领星接入) */ + SHOPIFY, /** Mirakl 市场 */ MIRAKL, /** Target(领星平台码待确认) */ diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java b/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java index 2c081b2..90e05bd 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/LingxingApi.java @@ -35,6 +35,12 @@ public enum LingxingApi { /** 沃尔玛在线商品。POST,offset/length 分页(上限 200),返回 data.list。 */ LISTING_WALMART("/basicOpen/multiplatform/walmart/list", 1), + /** Temu 在线商品。POST,offset/length(上限 1000)或游标分页,返回 data.list。 */ + LISTING_TEMU("/basicOpen/multiplatform/temu/list", 1), + + /** Shopify 在线商品(变体维度)。POST,offset/length(上限 1000),返回 data.list。 */ + LISTING_SHOPIFY("/basicOpen/multiplatform/shopify/variantList", 1), + /** 多平台订单列表;当前实现使用的跨平台接口。 */ ORDER_LIST("/pb/mp/order/v2/list", 10); diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapter.java b/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapter.java index c17329e..00996d5 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapter.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapter.java @@ -104,7 +104,8 @@ public final class LingxingAdapter implements SalesOrderCapable, ProductCapable, @Override public Set platforms() { return EnumSet.of(Platform.AMAZON_SC, Platform.AMAZON_VC, Platform.WALMART_SC, Platform.TIKTOK, - Platform.TEMU, Platform.EBAY, Platform.SHEIN, Platform.MIRAKL, Platform.CUSTOM); + Platform.TEMU, Platform.EBAY, Platform.SHEIN, Platform.SHOPIFY, Platform.MIRAKL, + Platform.CUSTOM); } @Override diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapterFactory.java b/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapterFactory.java index 56c2f65..ef6ce2e 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapterFactory.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/adapter/LingxingAdapterFactory.java @@ -40,7 +40,8 @@ public final class LingxingAdapterFactory implements AdapterFactory { @Override public EnumSet platforms() { return EnumSet.of(Platform.AMAZON_SC, Platform.AMAZON_VC, Platform.WALMART_SC, Platform.TIKTOK, - Platform.TEMU, Platform.EBAY, Platform.SHEIN, Platform.MIRAKL, Platform.CUSTOM); + Platform.TEMU, Platform.EBAY, Platform.SHEIN, Platform.SHOPIFY, Platform.MIRAKL, + Platform.CUSTOM); } /** diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java index 826049c..e6c028f 100644 --- a/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/LingxingSubPlatforms.java @@ -34,6 +34,10 @@ public final class LingxingSubPlatforms { SHARED_TRANSPORT, SHARED_JSON)); register(map, new common.ecommerce.lingxing.platform.amazon.vc.AmazonVcSubPlatform( SHARED_TRANSPORT, SHARED_JSON)); + register(map, new common.ecommerce.lingxing.platform.temu.TemuSubPlatform( + SHARED_TRANSPORT, SHARED_JSON)); + register(map, new common.ecommerce.lingxing.platform.shopify.ShopifySubPlatform( + SHARED_TRANSPORT, SHARED_JSON)); // eBay、SHEIN… 落地后继续 register(...) BY_PLATFORM = Collections.unmodifiableMap(map); } diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/ShopifySubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/ShopifySubPlatform.java new file mode 100644 index 0000000..1da60df --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/ShopifySubPlatform.java @@ -0,0 +1,30 @@ +package common.ecommerce.lingxing.platform.shopify; + +import common.ecommerce.http.HttpTransport; +import common.ecommerce.http.JacksonJsonCodec; +import common.ecommerce.lingxing.platform.LingxingSubPlatform; +import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.shopify.product.ShopifyProductSource; +import common.ecommerce.model.Platform; + +/** + * 领星 · Shopify 子平台:只做能力组合。 + */ +public final class ShopifySubPlatform implements LingxingSubPlatform { + + private final LingxingProductSource products; + + public ShopifySubPlatform(HttpTransport transport, JacksonJsonCodec json) { + this.products = new ShopifyProductSource(); + } + + @Override + public Platform platform() { + return Platform.SHOPIFY; + } + + @Override + public LingxingProductSource productSource() { + return products; + } +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/dto/ShopifyListingDto.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/dto/ShopifyListingDto.java new file mode 100644 index 0000000..d1db7e4 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/dto/ShopifyListingDto.java @@ -0,0 +1,166 @@ +package common.ecommerce.lingxing.platform.shopify.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +/** + * 领星 · Shopify 在线商品({@code /basicOpen/multiplatform/shopify/variantList} 的 {@code data.list[]})。 + * + *

文档字段是 snake_case;这里用 camelCase 的 Java 字段 + {@code @JsonProperty} 映射。

+ * + *

Shopify 按变体(variant)返回,{@code msku} 是平台侧 SKU。

+ */ +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class ShopifyListingDto { + + /** 商品 ID。 */ + @JsonProperty("item_id") + private String itemId; + + /** 商品链接。 */ + @JsonProperty("item_url") + private String itemUrl; + + /** 产品 uid。 */ + @JsonProperty("product_unique_id") + private String productUniqueId; + + /** 子体 uid。 */ + @JsonProperty("variant_unique_id") + private String variantUniqueId; + + /** 企业 ID。 */ + @JsonProperty("company_id") + private String companyId; + + /** 父图链接。 */ + @JsonProperty("parent_picture_url") + private String parentPictureUrl; + + /** 图片链接。 */ + @JsonProperty("picture_url") + private String pictureUrl; + + /** 父 MSKU。 */ + @JsonProperty("parent_msku") + private String parentMsku; + + /** 平台侧 MSKU,参与 canonicalKey。 */ + @JsonProperty("msku") + private String msku; + + /** 母体属性,如 Size/Color。 */ + @JsonProperty("parent_attribute") + private String parentAttribute; + + /** 变体属性,如 42 / red。 */ + @JsonProperty("attribute") + private String attribute; + + /** 标题。 */ + @JsonProperty("title") + private String title; + + /** 本地 SKU。 */ + @JsonProperty("local_sku") + private String localSku; + + /** 本地商品 id。 */ + @JsonProperty("pid") + private String pid; + + /** 本地品名。 */ + @JsonProperty("local_name") + private String localName; + + /** 店铺 id。 */ + @JsonProperty("store_id") + private String storeId; + + /** 店铺名称。 */ + @JsonProperty("store_name") + private String storeName; + + /** 售价。 */ + @JsonProperty("price") + private String price; + + /** 库存。 */ + @JsonProperty("quantity") + private String quantity; + + /** 货币符号,如 ¥。 */ + @JsonProperty("currency_icon") + private String currencyIcon; + + /** 是否有子体。 */ + @JsonProperty("has_variant") + private Boolean hasVariant; + + /** 开始时间,Y-m-d H:i:s。 */ + @JsonProperty("listing_start_time") + private String listingStartTime; + + /** 结束时间,Y-m-d H:i:s。 */ + @JsonProperty("listing_end_time") + private String listingEndTime; + + /** 商品状态:1 active,2 draft,3 archived,4 deleted。 */ + @JsonProperty("status") + private Integer status; + + /** 商品状态名。 */ + @JsonProperty("status_name") + private String statusName; + + /** 库存策略:1 不跟踪,2 缺货停止销售,3 缺货继续销售。 */ + @JsonProperty("inventory_policy") + private Integer inventoryPolicy; + + /** 库存策略名。 */ + @JsonProperty("inventory_policy_name") + private String inventoryPolicyName; + + /** 商品分类 ID。 */ + @JsonProperty("type_id") + private String typeId; + + /** 商品分类名。 */ + @JsonProperty("type_name") + private String typeName; + + /** 是否需要运输。 */ + @JsonProperty("requires_shipping") + private Boolean requiresShipping; + + /** 是否要征税。 */ + @JsonProperty("taxable") + private Boolean taxable; + + /** 最小重量。 */ + @JsonProperty("min_weight") + private Integer minWeight; + + /** 最大重量。 */ + @JsonProperty("max_weight") + private Integer maxWeight; + + /** 重量单位。 */ + @JsonProperty("weight_unit") + private String weightUnit; + + /** 重量。 */ + @JsonProperty("weight") + private String weight; + + /** 各仓位信息。 */ + @JsonProperty("location_infos") + private List locationInfos = new ArrayList(); +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/dto/ShopifyLocationDto.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/dto/ShopifyLocationDto.java new file mode 100644 index 0000000..b94f00c --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/dto/ShopifyLocationDto.java @@ -0,0 +1,49 @@ +package common.ecommerce.lingxing.platform.shopify.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +/** Shopify 商品仓位信息({@code location_infos[]})。 */ +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class ShopifyLocationDto { + + /** 仓位 id。 */ + @JsonProperty("location_id") + private String locationId; + + /** 仓位名称。 */ + @JsonProperty("location_name") + private String locationName; + + /** 该仓位库存。 */ + @JsonProperty("location_quantity") + private String locationQuantity; + + /** 库存调整后的值。 */ + @JsonProperty("quantity_adjust_after") + private String quantityAdjustAfter; + + /** 库存调整前的值。 */ + @JsonProperty("quantity_adjust_before") + private String quantityAdjustBefore; + + /** 库存调整操作人 id。 */ + @JsonProperty("quantity_user_id") + private String quantityUserId; + + /** 库存调整操作人名字。 */ + @JsonProperty("quantity_user_id_name") + private String quantityUserIdName; + + /** 库存队列类型。 */ + @JsonProperty("quantity_queue_type") + private Integer quantityQueueType; + + /** 库存队列状态。 */ + @JsonProperty("quantity_queue_status") + private String quantityQueueStatus; +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/product/ShopifyProductSource.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/product/ShopifyProductSource.java new file mode 100644 index 0000000..3f14368 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/shopify/product/ShopifyProductSource.java @@ -0,0 +1,268 @@ +package common.ecommerce.lingxing.platform.shopify.product; + +import common.ecommerce.http.HttpRequest; +import common.ecommerce.http.HttpResponse; +import common.ecommerce.lingxing.LingxingApi; +import common.ecommerce.lingxing.http.LingxingEnvelope; +import common.ecommerce.lingxing.platform.LingxingCall; +import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.shopify.dto.ShopifyListingDto; +import common.ecommerce.model.ChannelProvider; +import common.ecommerce.model.product.ProductListing; +import common.ecommerce.model.product.ProductQuery; +import common.ecommerce.model.product.ProductStatus; +import common.ecommerce.model.support.Money; +import common.ecommerce.model.support.Page; +import common.ecommerce.model.support.PlatformTime; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; + +/** + * 领星 · Shopify 在线商品(变体维度)。 + * + *

接口 {@code /basicOpen/multiplatform/shopify/variantList}:POST,{@code offset}/{@code length} + * (上限 1000),返回 {@code data.list};令牌桶容量 1。

+ */ +public final class ShopifyProductSource implements LingxingProductSource { + + private static final int MAX_PAGE_SIZE = 1000; + + /** 时间字段格式,文档未给出示例值格式,按 Y-m-d H:i:s 发送。 */ + private static final DateTimeFormatter DATE_TIME = + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + @Override + public Page products(LingxingCall call, ProductQuery query) { + int length = Math.min(query.pageSize() <= 0 ? 100 : query.pageSize(), MAX_PAGE_SIZE); + int offset = query.cursor() == null ? 0 : Integer.parseInt(query.cursor()); + + Map body = new LinkedHashMap(); + body.put("offset", offset); + body.put("length", length); + body.put("store_ids", + Collections.singletonList(call.ctx().account().externalStoreId())); + applyTimeWindow(body, query); + + HttpRequest req = call.signed(LingxingApi.LISTING_SHOPIFY, body, "products.pull"); + HttpResponse resp = call.execute(req); + LingxingEnvelope envelope = call.envelope(resp, "products.pull"); + + List dtos = call.json().listAt(envelope.body(), "data.list", ShopifyListingDto.class); + List out = new ArrayList(dtos.size()); + for (ShopifyListingDto dto : dtos) { + out.add(toProductListing(dto, call)); + } + + int total = totalOf(call, envelope.body()); + int nextOffset = offset + dtos.size(); + boolean hasMore = dtos.size() >= length && (total < 0 || nextOffset < total); + return Page.of(out, hasMore ? String.valueOf(nextOffset) : null); + } + + /** 时间窗:按 UTC 发送,未给终点时不发终点。 */ + private static void applyTimeWindow(Map body, ProductQuery query) { + Instant from = query.updatedAfter(); + Instant to = query.updatedBefore(); + if (from == null && to == null) { + return; + } + ZoneId utc = ZoneId.of("UTC"); + if (from != null) { + body.put("listing_start_time", DATE_TIME.format(from.atZone(utc))); + } + if (to != null) { + body.put("listing_end_time", DATE_TIME.format(to.atZone(utc))); + } + } + + /** 总数取 data.count;没有时用顶层 total;都没有返回 -1。 */ + private static int totalOf(LingxingCall call, String body) { + String raw = call.json().stringAt(body, "data.count"); + if (raw == null) { + raw = call.json().stringAt(body, "total"); + } + if (raw == null) { + return -1; + } + try { + return Integer.parseInt(raw.trim()); + } catch (NumberFormatException e) { + return -1; + } + } + + /** + * 领星 Shopify 商品 → 统一模型。 + * + *

统一字段来源:

+ *
    + *
  • platformSku ← {@code msku}
  • + *
  • listingId ← {@code product_unique_id}
  • + *
  • gtin ← 无
  • + *
  • title ← {@code title}
  • + *
  • imageUrls ← {@code picture_url},为空时用 {@code parent_picture_url}
  • + *
  • status ← {@code status}(1 active / 2 draft / 3 archived / 4 deleted)
  • + *
  • rawStatus ← {@code status_name} 原值
  • + *
  • price ← {@code price};currency 由 {@code currency_icon} 反推
  • + *
  • listedAt ← {@code listing_start_time}
  • + *
  • updatedAt ← 无(接口没有内容更新时间)
  • + *
  • attributes ← 其余全部原始字段
  • + *
+ */ + private ProductListing toProductListing(ShopifyListingDto dto, LingxingCall call) { + ZoneId zone = ZoneId.of(call.ctx().account().marketplaceZone()); + String currency = currencyOf(dto.getCurrencyIcon()); + String image = firstNonBlank(dto.getPictureUrl(), dto.getParentPictureUrl()); + + return ProductListing.builder() + .platform(call.ctx().platform()) + .omsStoreId(call.ctx().omsStoreId()) + .platformSku(dto.getMsku()) + .listingId(trimToNull(dto.getProductUniqueId())) + .title(dto.getTitle()) + .imageUrls(image == null + ? Collections.emptyList() + : Collections.singletonList(image)) + .status(statusOf(dto)) + .rawStatus(dto.getStatusName()) + .price(moneyOrNull(dto.getPrice(), currency)) + .currency(currency) + .listedAt(dateTime(dto.getListingStartTime(), zone)) + .attributes(attributesOf(dto)) + .sourceProvider(ChannelProvider.LINGXING) + .build(); + } + + /** Shopify:1 active / 2 draft / 3 archived / 4 deleted。 */ + private static ProductStatus statusOf(ShopifyListingDto dto) { + if (dto.getStatus() == null) { + String name = dto.getStatusName(); + if (name != null) { + String s = name.trim().toUpperCase(Locale.ROOT); + if ("ACTIVE".equals(s)) { + return ProductStatus.ACTIVE; + } + if ("DELETED".equals(s)) { + return ProductStatus.DELETED; + } + if ("DRAFT".equals(s) || "ARCHIVED".equals(s)) { + return ProductStatus.INACTIVE; + } + } + return ProductStatus.UNKNOWN; + } + switch (dto.getStatus()) { + case 1: + return ProductStatus.ACTIVE; + case 2: + case 3: + return ProductStatus.INACTIVE; + case 4: + return ProductStatus.DELETED; + default: + return ProductStatus.UNKNOWN; + } + } + + private static Map attributesOf(ShopifyListingDto dto) { + Map attrs = new LinkedHashMap(); + attrs.put("item_id", dto.getItemId()); + attrs.put("item_url", dto.getItemUrl()); + attrs.put("variant_unique_id", dto.getVariantUniqueId()); + attrs.put("company_id", dto.getCompanyId()); + attrs.put("parent_picture_url", dto.getParentPictureUrl()); + attrs.put("parent_msku", dto.getParentMsku()); + attrs.put("parent_attribute", dto.getParentAttribute()); + attrs.put("attribute", dto.getAttribute()); + attrs.put("local_sku", dto.getLocalSku()); + attrs.put("pid", dto.getPid()); + attrs.put("local_name", dto.getLocalName()); + attrs.put("store_id", dto.getStoreId()); + attrs.put("store_name", dto.getStoreName()); + attrs.put("quantity", dto.getQuantity()); + attrs.put("currency_icon", dto.getCurrencyIcon()); + attrs.put("has_variant", dto.getHasVariant()); + attrs.put("listing_end_time", dto.getListingEndTime()); + attrs.put("inventory_policy", dto.getInventoryPolicy()); + attrs.put("inventory_policy_name", dto.getInventoryPolicyName()); + attrs.put("type_id", dto.getTypeId()); + attrs.put("type_name", dto.getTypeName()); + attrs.put("requires_shipping", dto.getRequiresShipping()); + attrs.put("taxable", dto.getTaxable()); + attrs.put("min_weight", dto.getMinWeight()); + attrs.put("max_weight", dto.getMaxWeight()); + attrs.put("weight", dto.getWeight()); + attrs.put("weight_unit", dto.getWeightUnit()); + attrs.put("location_infos", dto.getLocationInfos()); + attrs.values().removeAll(Collections.singleton(null)); + return attrs; + } + + /** 货币符号 → ISO;¥/¥ 无法区分人民币与日元,返回 null。 */ + private static String currencyOf(String icon) { + if (icon == null) { + return null; + } + String s = icon.trim(); + if ("$".equals(s) || "US$".equals(s)) { + return "USD"; + } + if ("€".equals(s)) { + return "EUR"; + } + if ("£".equals(s)) { + return "GBP"; + } + return null; + } + + private static PlatformTime dateTime(String raw, ZoneId zone) { + if (raw == null || raw.trim().isEmpty()) { + return null; + } + try { + Instant utc = LocalDateTime.parse(raw.trim(), DATE_TIME).atZone(zone).toInstant(); + return PlatformTime.of(utc, zone); + } catch (RuntimeException e) { + return null; + } + } + + private static Money moneyOrNull(String value, String currency) { + if (value == null || value.trim().isEmpty() || currency == null) { + return null; + } + try { + return Money.of(value.trim(), currency); + } catch (RuntimeException e) { + return null; + } + } + + private static String firstNonBlank(String... values) { + for (String v : values) { + String trimmed = trimToNull(v); + if (trimmed != null) { + return trimmed; + } + } + return null; + } + + private static String trimToNull(String v) { + if (v == null) { + return null; + } + String trimmed = v.trim(); + return trimmed.isEmpty() ? null : trimmed; + } +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/TemuSubPlatform.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/TemuSubPlatform.java new file mode 100644 index 0000000..32de4fe --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/TemuSubPlatform.java @@ -0,0 +1,32 @@ +package common.ecommerce.lingxing.platform.temu; + +import common.ecommerce.http.HttpTransport; +import common.ecommerce.http.JacksonJsonCodec; +import common.ecommerce.lingxing.platform.LingxingSubPlatform; +import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.temu.product.TemuProductSource; +import common.ecommerce.model.Platform; + +/** + * 领星 · Temu 子平台:只做能力组合。 + * + *

10022 / 10024 都映射到 {@link Platform#TEMU},本实现同时服务两者。

+ */ +public final class TemuSubPlatform implements LingxingSubPlatform { + + private final LingxingProductSource products; + + public TemuSubPlatform(HttpTransport transport, JacksonJsonCodec json) { + this.products = new TemuProductSource(); + } + + @Override + public Platform platform() { + return Platform.TEMU; + } + + @Override + public LingxingProductSource productSource() { + return products; + } +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuListingDto.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuListingDto.java new file mode 100644 index 0000000..c18c44c --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuListingDto.java @@ -0,0 +1,169 @@ +package common.ecommerce.lingxing.platform.temu.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; + +/** + * 领星 · Temu 在线商品({@code /basicOpen/multiplatform/temu/list} 的 {@code data.list[]})。 + * + *

字段名以官方文档为准(camelCase)。响应结构:{@code data.count} + {@code data.list}, + * 顶层另有 {@code total}(文档示例为 0,实际以 data.count 为准)。

+ * + *

SKU 语义:{@code msku} 是平台侧 MSKU,{@code sku} / {@code pmsku} 是本地 SKU, + * 对齐时应优先用 {@code msku}。

+ */ +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class TemuListingDto { + + /** 领星内部 id。 */ + @JsonProperty("id") + private String id; + + /** 商品 uid。 */ + @JsonProperty("productUniqueId") + private String productUniqueId; + + /** 负责人 uid 列表。 */ + @JsonProperty("principalUids") + private List principalUids = new ArrayList(); + + /** 负责人名称。 */ + @JsonProperty("principalName") + private String principalName; + + /** 企业 id。 */ + @JsonProperty("companyId") + private String companyId; + + /** 店铺 id。 */ + @JsonProperty("storeId") + private String storeId; + + /** 店铺名称。 */ + @JsonProperty("storeName") + private String storeName; + + /** 平台代码,如 10024。 */ + @JsonProperty("platformCode") + private String platformCode; + + /** MSKU ID。 */ + @JsonProperty("mskuId") + private String mskuId; + + /** 平台侧 SKU(MSKU),参与 canonicalKey。 */ + @JsonProperty("msku") + private String msku; + + /** 品名。 */ + @JsonProperty("pname") + private String pname; + + /** 本地 SKU(配对的)。 */ + @JsonProperty("pmsku") + private String pmsku; + + /** 本地 SKU。 */ + @JsonProperty("sku") + private String sku; + + /** 本地商品 id。 */ + @JsonProperty("pid") + private String pid; + + /** 标题。 */ + @JsonProperty("title") + private String title; + + /** 分类 id。 */ + @JsonProperty("cid") + private String cid; + + /** 品牌 id。 */ + @JsonProperty("bid") + private String bid; + + /** 7 天销量。 */ + @JsonProperty("day7SaleCnt") + private Integer day7SaleCnt; + + /** 30 天销量。 */ + @JsonProperty("day30SaleCnt") + private Integer day30SaleCnt; + + /** 各仓库存。 */ + @JsonProperty("wareHouseDataList") + private List wareHouseDataList = new ArrayList(); + + /** 备注。 */ + @JsonProperty("notes") + private String notes; + + /** 图片链接。 */ + @JsonProperty("imgUrl") + private String imgUrl; + + /** SKC ID。 */ + @JsonProperty("skcId") + private String skcId; + + /** SKC。 */ + @JsonProperty("skc") + private String skc; + + /** SPU ID。 */ + @JsonProperty("spuId") + private String spuId; + + /** 上架状态:0 删除,2 正常。 */ + @JsonProperty("status") + private Integer status; + + /** SKU 规格。 */ + @JsonProperty("attribute") + private String attribute; + + /** JIT 发货模式:0 无,1 Jit,2 非 Jit。 */ + @JsonProperty("jitMode") + private Integer jitMode; + + /** 叶子类目。 */ + @JsonProperty("catName") + private String catName; + + /** 商品状态枚举(生命周期),取值见文档附加说明。 */ + @JsonProperty("selectStatus") + private String selectStatus; + + /** 虚拟库存。 */ + @JsonProperty("virtualQuantity") + private Integer virtualQuantity; + + /** 可用库存。 */ + @JsonProperty("usableInventory") + private String usableInventory; + + /** 品牌名称。 */ + @JsonProperty("bname") + private String bname; + + /** 分类名称。 */ + @JsonProperty("cname") + private String cname; + + /** 申报价列表(按生效区间)。 */ + @JsonProperty("supplyPriceList") + private List supplyPriceList = new ArrayList(); + + /** 活动价详情。 */ + @JsonProperty("promotionalPriceDataList") + private List promotionalPriceDataList = + new ArrayList(); +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuPromotionalPriceDto.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuPromotionalPriceDto.java new file mode 100644 index 0000000..374fbd3 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuPromotionalPriceDto.java @@ -0,0 +1,37 @@ +package common.ecommerce.lingxing.platform.temu.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +/** Temu 活动价({@code promotionalPriceDataList[]})。 */ +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class TemuPromotionalPriceDto { + + /** 活动状态:0 报名失败,1 报名成功待开始,2 进行中,3 活动已结束。 */ + @JsonProperty("activeStatus") + private String activeStatus; + + /** Temu 活动主题名。 */ + @JsonProperty("activeType") + private String activeType; + + /** 币种,文档示例为 ISO 代码(CNY)。 */ + @JsonProperty("currency") + private String currency; + + /** 活动价。 */ + @JsonProperty("promotionalPrice") + private String promotionalPrice; + + /** 报名结果:0 失败,1 成功待开始,2 进行中,3 已结束。 */ + @JsonProperty("result") + private String result; + + /** 活动价唯一键。 */ + @JsonProperty("uniqueId") + private String uniqueId; +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuSupplyPriceDto.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuSupplyPriceDto.java new file mode 100644 index 0000000..f933170 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuSupplyPriceDto.java @@ -0,0 +1,31 @@ +package common.ecommerce.lingxing.platform.temu.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +import java.math.BigDecimal; + +/** Temu 申报价({@code supplyPriceList[]}),按生效区间分段。 */ +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class TemuSupplyPriceDto { + + /** 申报价。 */ + @JsonProperty("price") + private BigDecimal price; + + /** 货币,文档示例为符号「¥」,不是 ISO 代码。 */ + @JsonProperty("currency") + private String currency; + + /** 执行开始时间,Y-m-d。 */ + @JsonProperty("beginStr") + private String beginStr; + + /** 执行结束时间,Y-m-d;为空表示当前生效。 */ + @JsonProperty("endStr") + private String endStr; +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuWarehouseDto.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuWarehouseDto.java new file mode 100644 index 0000000..ecb9247 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/dto/TemuWarehouseDto.java @@ -0,0 +1,25 @@ +package common.ecommerce.lingxing.platform.temu.dto; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; +import lombok.Setter; + +/** Temu 商品各仓库存({@code wareHouseDataList[]})。 */ +@Getter +@Setter +@JsonIgnoreProperties(ignoreUnknown = true) +public class TemuWarehouseDto { + + /** 仓库 id。 */ + @JsonProperty("warehouseId") + private String warehouseId; + + /** 仓库名称。 */ + @JsonProperty("warehouseName") + private String warehouseName; + + /** 库存数量。 */ + @JsonProperty("quantity") + private Integer quantity; +} diff --git a/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/product/TemuProductSource.java b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/product/TemuProductSource.java new file mode 100644 index 0000000..b34ed72 --- /dev/null +++ b/lingxing/src/main/java/common/ecommerce/lingxing/platform/temu/product/TemuProductSource.java @@ -0,0 +1,264 @@ +package common.ecommerce.lingxing.platform.temu.product; + +import common.ecommerce.http.HttpRequest; +import common.ecommerce.http.HttpResponse; +import common.ecommerce.lingxing.LingxingApi; +import common.ecommerce.lingxing.http.LingxingEnvelope; +import common.ecommerce.lingxing.platform.LingxingCall; +import common.ecommerce.lingxing.platform.product.LingxingProductSource; +import common.ecommerce.lingxing.platform.temu.dto.TemuListingDto; +import common.ecommerce.lingxing.platform.temu.dto.TemuSupplyPriceDto; +import common.ecommerce.model.ChannelProvider; +import common.ecommerce.model.product.ProductListing; +import common.ecommerce.model.product.ProductQuery; +import common.ecommerce.model.product.ProductStatus; +import common.ecommerce.model.support.Money; +import common.ecommerce.model.support.Page; + +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * 领星 · Temu 在线商品。 + * + *

接口 {@code /basicOpen/multiplatform/temu/list}:POST,{@code offset}/{@code length} + * (上限 1000)或 {@code cursorId} 游标分页,返回 {@code data.list};令牌桶容量 1。

+ * + *

本实现固定走 offset 分页({@code useCursor=false}),游标以 offset 字符串表示。

+ */ +public final class TemuProductSource implements LingxingProductSource { + + private static final int MAX_PAGE_SIZE = 1000; + + /** 更新时间区间格式。 */ + private static final DateTimeFormatter DATE_TIME = + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + @Override + public Page products(LingxingCall call, ProductQuery query) { + int length = Math.min(query.pageSize() <= 0 ? 100 : query.pageSize(), MAX_PAGE_SIZE); + int offset = query.cursor() == null ? 0 : Integer.parseInt(query.cursor()); + + Map body = new LinkedHashMap(); + body.put("offset", offset); + body.put("length", length); + body.put("useCursor", Boolean.FALSE); + body.put("storeIds", + Collections.singletonList(call.ctx().account().externalStoreId())); + applyTimeWindow(body, query); + + HttpRequest req = call.signed(LingxingApi.LISTING_TEMU, body, "products.pull"); + HttpResponse resp = call.execute(req); + LingxingEnvelope envelope = call.envelope(resp, "products.pull"); + + List dtos = call.json().listAt(envelope.body(), "data.list", TemuListingDto.class); + List out = new ArrayList(dtos.size()); + for (TemuListingDto dto : dtos) { + out.add(toProductListing(dto, call)); + } + + // Temu 的 total 在 data.count(游标模式下不返回) + int total = totalOf(call, envelope.body()); + int nextOffset = offset + dtos.size(); + boolean hasMore = dtos.size() >= length && (total < 0 || nextOffset < total); + return Page.of(out, hasMore ? String.valueOf(nextOffset) : null); + } + + /** 时间窗:接口精度到秒,未说明时区,统一按 UTC 发送。 */ + private static void applyTimeWindow(Map body, ProductQuery query) { + Instant from = query.updatedAfter(); + Instant to = query.updatedBefore(); + if (from == null && to == null) { + return; + } + ZoneId utc = ZoneId.of("UTC"); + if (from != null) { + body.put("updateStartTime", DATE_TIME.format(from.atZone(utc))); + } + if (to != null) { + body.put("updateEndTime", DATE_TIME.format(to.atZone(utc))); + } + } + + /** 总数取 data.count;没有时返回 -1。 */ + private static int totalOf(LingxingCall call, String body) { + String raw = call.json().stringAt(body, "data.count"); + if (raw == null) { + raw = call.json().stringAt(body, "total"); + } + if (raw == null) { + return -1; + } + try { + return Integer.parseInt(raw.trim()); + } catch (NumberFormatException e) { + return -1; + } + } + + /** + * 领星 Temu 商品 → 统一模型。 + * + *

统一字段来源:

+ *
    + *
  • platformSku ← {@code msku}
  • + *
  • listingId ← {@code productUniqueId}
  • + *
  • title ← {@code title}
  • + *
  • imageUrls ← {@code imgUrl}(单张,可能为空串)
  • + *
  • status ← {@code status}(0 删除 / 2 正常)
  • + *
  • rawStatus ← 原始 {@code status} 值
  • + *
  • price / currency ← 当前生效的 {@code supplyPriceList} 条目(见下)
  • + *
  • listedAt / updatedAt ← 无(接口不返回时间字段)
  • + *
  • attributes ← 其余全部原始字段(SKC/SPU、仓库库存、活动价、JIT 模式等)
  • + *
+ * + *

价格说明:Temu 要求上报「申报价」,{@code supplyPriceList} 按时间区间分段, + * 取 {@code endStr} 为空(当前生效)或开始时间最晚的一条。币种字段是符号「¥」, + * 无法区分人民币/日元,按符号映射为 null 时不填金额。

+ */ + private ProductListing toProductListing(TemuListingDto dto, LingxingCall call) { + TemuSupplyPriceDto price = currentSupplyPrice(dto.getSupplyPriceList()); + String currency = price == null ? null : currencyOf(price.getCurrency()); + + return ProductListing.builder() + .platform(call.ctx().platform()) + .omsStoreId(call.ctx().omsStoreId()) + .platformSku(dto.getMsku()) + .listingId(trimToNull(dto.getProductUniqueId())) + .title(dto.getTitle()) + .imageUrls(isBlank(dto.getImgUrl()) + ? Collections.emptyList() + : Collections.singletonList(dto.getImgUrl().trim())) + .status(statusOf(dto.getStatus())) + .rawStatus(dto.getStatus() == null ? null : String.valueOf(dto.getStatus())) + .price(price == null ? null : moneyOrNull(price.getPrice(), currency)) + .currency(currency) + .attributes(attributesOf(dto)) + .sourceProvider(ChannelProvider.LINGXING) + .build(); + } + + /** 当前生效的申报价:优先 endStr 为空,否则取 beginStr 最大的一条。 */ + private static TemuSupplyPriceDto currentSupplyPrice(List prices) { + if (prices == null || prices.isEmpty()) { + return null; + } + TemuSupplyPriceDto best = null; + for (TemuSupplyPriceDto p : prices) { + if (p == null || p.getPrice() == null) { + continue; + } + if (isBlank(p.getEndStr())) { + return p; + } + if (best == null || compareBegin(p.getBeginStr(), best.getBeginStr()) > 0) { + best = p; + } + } + return best; + } + + private static int compareBegin(String a, String b) { + if (a == null) { + return b == null ? 0 : -1; + } + if (b == null) { + return 1; + } + return a.compareTo(b); + } + + /** Temu 状态:0 删除,2 正常。 */ + private static ProductStatus statusOf(Integer status) { + if (status == null) { + return ProductStatus.UNKNOWN; + } + if (status == 2) { + return ProductStatus.ACTIVE; + } + if (status == 0) { + return ProductStatus.DELETED; + } + return ProductStatus.UNKNOWN; + } + + private static Map attributesOf(TemuListingDto dto) { + Map attrs = new LinkedHashMap(); + attrs.put("id", dto.getId()); + attrs.put("storeId", dto.getStoreId()); + attrs.put("storeName", dto.getStoreName()); + attrs.put("platformCode", dto.getPlatformCode()); + attrs.put("mskuId", dto.getMskuId()); + attrs.put("sku", dto.getSku()); + attrs.put("pmsku", dto.getPmsku()); + attrs.put("pid", dto.getPid()); + attrs.put("pname", dto.getPname()); + attrs.put("skcId", dto.getSkcId()); + attrs.put("skc", dto.getSkc()); + attrs.put("spuId", dto.getSpuId()); + attrs.put("cid", dto.getCid()); + attrs.put("cname", dto.getCname()); + attrs.put("catName", dto.getCatName()); + attrs.put("bid", dto.getBid()); + attrs.put("bname", dto.getBname()); + attrs.put("attribute", dto.getAttribute()); + attrs.put("jitMode", dto.getJitMode()); + attrs.put("selectStatus", dto.getSelectStatus()); + attrs.put("virtualQuantity", dto.getVirtualQuantity()); + attrs.put("usableInventory", dto.getUsableInventory()); + attrs.put("wareHouseDataList", dto.getWareHouseDataList()); + attrs.put("supplyPriceList", dto.getSupplyPriceList()); + attrs.put("promotionalPriceDataList", dto.getPromotionalPriceDataList()); + attrs.put("day7SaleCnt", dto.getDay7SaleCnt()); + attrs.put("day30SaleCnt", dto.getDay30SaleCnt()); + attrs.put("principalUids", dto.getPrincipalUids()); + attrs.put("principalName", dto.getPrincipalName()); + attrs.put("companyId", dto.getCompanyId()); + attrs.put("notes", dto.getNotes()); + attrs.values().removeAll(Collections.singleton(null)); + return attrs; + } + + /** 货币符号 → ISO;¥/¥ 人民币与日元无法区分,返回 null。 */ + private static String currencyOf(String icon) { + if (icon == null) { + return null; + } + String s = icon.trim(); + if ("$".equals(s) || "US$".equals(s)) { + return "USD"; + } + if ("€".equals(s)) { + return "EUR"; + } + if ("£".equals(s)) { + return "GBP"; + } + return null; + } + + private static Money moneyOrNull(java.math.BigDecimal amount, String currency) { + if (amount == null || currency == null) { + return null; + } + return Money.of(amount, currency); + } + + private static boolean isBlank(String v) { + return v == null || v.trim().isEmpty(); + } + + private static String trimToNull(String v) { + if (v == null) { + return null; + } + String trimmed = v.trim(); + return trimmed.isEmpty() ? null : trimmed; + } +}