update shopee order parse

This commit is contained in:
2026-02-05 10:38:11 +08:00
parent c188368483
commit fccc80761c
@@ -6,6 +6,7 @@ namespace App\Platform\Shopee\EntityParse;
use App\Model\Company; use App\Model\Company;
use App\Model\Model as Entity; use App\Model\Model as Entity;
use App\Model\Product;
use App\Model\Store; use App\Model\Store;
use App\Platform\AbstractOrderParse; use App\Platform\AbstractOrderParse;
use App\Platform\Shopee\Constants\OrderStatus; use App\Platform\Shopee\Constants\OrderStatus;
@@ -200,7 +201,7 @@ class Order extends AbstractOrderParse
'quantity' => $quantity, 'quantity' => $quantity,
'discount' => ($original_price - $discounted_price) * $quantity, // 总折扣金额 'discount' => ($original_price - $discounted_price) * $quantity, // 总折扣金额
'total' => $discounted_price * $quantity, // 总金额 = 折扣价 × 数量 'total' => $discounted_price * $quantity, // 总金额 = 折扣价 × 数量
'ext' => '', // 原始信息已在 Order.raw 中记录,此处留空 'ext' => null, // 原始信息已在 Order.raw 中记录,此处留空
]; ];
} }
@@ -358,34 +359,111 @@ class Order extends AbstractOrderParse
* *
* @param array $raw_data 原始数据数组 * @param array $raw_data 原始数据数组
* @param array $platform_orders_id_to_local_db_order_id_map 平台订单 ID 到本地数据库订单 ID 的映射 [platform_order_id => local_db_order_id] * @param array $platform_orders_id_to_local_db_order_id_map 平台订单 ID 到本地数据库订单 ID 的映射 [platform_order_id => local_db_order_id]
* @return array 格式化后的订单子项数据,可供 OrderItem::fill() 使用 *
*
* @return array 格式化后的订单子项数据,可供 OrderItem::fill() 使用]
*
*/ */
public function formatOrderItemsFromRaw(array $raw_data, array $platform_orders_id_to_local_db_order_id_map): array public function formatOrderItemsFromRaw(array $raw_data, array $platform_orders_id_to_local_db_order_id_map): array
{ {
// 1. 从 $rawData 中提取订单子项信息(Shopee 的 item_list 字段)
// 2. 使用 $platformOrderIdToLocalDbOrderIdMap 获取本地 order_id
// 3. 调用 shopeeOrderItemMap() 进行单个子项的映射转换
// 4. 返回可供 OrderItem::fill() 使用的数组
// 由于 $rawData 是批量数据,包含多条订单结果,因此订单子项需要从集合中提取
// Shopee 平台响应体的数据格式为: collection->payload->item_list
// $platform_orders_id_to_local_db_order_id_map 内部元素数据格式为 [platform_order_id => local db order id]
$items = []; $items = [];
$product_keys = []; // 索引 => 查询键
foreach ($raw_data as $raw_orders) { foreach ($raw_data as $raw_orders) {
$parent_order_created_date = Carbon::createFromTimestamp($raw_orders['create_time'], $this->getStore()->getTimezoneString()); $parent_order_created_date = Carbon::createFromTimestamp($raw_orders['create_time'], $this->getStore()->getTimezoneString());
foreach ($raw_orders['item_list'] as $raw_order_item) { foreach ($raw_orders['item_list'] as $raw_order_item) {
// 数据库中父订单 id
$db_order_id = $platform_orders_id_to_local_db_order_id_map[$raw_orders['order_sn']]; $db_order_id = $platform_orders_id_to_local_db_order_id_map[$raw_orders['order_sn']];
$items[] = $this->shopeeOrderItemMap($raw_order_item, $raw_orders['order_sn'], $db_order_id, $parent_order_created_date);
$index = count($items);
$items[] = $this->orderItemMap($raw_order_item, $raw_orders['order_sn'], $db_order_id, $parent_order_created_date);
// 记录索引对应的产品查询键
$model_id = !empty($raw_order_item['model_id']) ? (string)$raw_order_item['model_id'] : null;
$product_keys[$index] = $this->buildProductMapKey((string)$raw_order_item['item_id'], $model_id);
}
}
// 批量查询产品
if (!empty($product_keys)) {
$products_id_map = $this->batchQueryProducts($product_keys);
// 直接用索引更新 product_id
foreach ($product_keys as $index => $key) {
if (isset($products_id_map[$key])) {
$items[$index]['product_id'] = $products_id_map[$key];
}
} }
} }
return $items; return $items;
} }
/**
* 构建产品映射键
*
* @param string $item_id 平台商品 ID
* @param string|null $model_id 平台规格 ID
* @return string
*/
private function buildProductMapKey(string $item_id, ?string $model_id): string
{
return $item_id . ':' . ($model_id ?? 'null');
}
/**
* 批量查询产品ID
*
* 使用 PostgreSQL VALUES + JOIN 语法优化,避免生成冗长的 OR 条件链
*
* @param array $product_keys 索引 => "item_id:model_id" 格式的键
* @return array "item_id:model_id" => product_id 的映射
*/
private function batchQueryProducts(array $product_keys): array
{
$store_id = $this->getStore()->id;
$unique_keys = array_unique(array_values($product_keys));
if (empty($unique_keys)) {
return [];
}
// 构建 VALUES 子句和绑定参数
$values = [];
$bindings = [];
foreach ($unique_keys as $key) {
[$item_id, $model_id] = explode(':', $key, 2);
$values[] = '(?::text, ?::text)';
$bindings[] = $item_id;
$bindings[] = $model_id === 'null' ? null : $model_id;
}
$valuesClause = implode(', ', $values);
$bindings[] = $store_id;
// PostgreSQL 原生 SQLVALUES 作为驱动表(小表在左侧)
// store_id 条件移入 JOIN ON 子句,让索引过滤更早介入
$sql = "
SELECT p.id, p.platform_item_id, p.platform_model_id
FROM (VALUES {$valuesClause}) AS v(item_id, model_id)
INNER JOIN products p
ON p.store_id = ?
AND p.platform_item_id = v.item_id
AND (p.platform_model_id = v.model_id OR (v.model_id IS NULL AND p.platform_model_id IS NULL))
";
$products = \Hyperf\DbConnection\Db::select($sql, $bindings);
// 构建映射
$map = [];
foreach ($products as $product) {
$key = $this->buildProductMapKey($product->platform_item_id, $product->platform_model_id);
$map[$key] = $product->id;
}
return $map;
}
/** /**
* Shopee 订单子项映射转换 * Shopee 订单子项映射转换
* *
@@ -395,7 +473,7 @@ class Order extends AbstractOrderParse
* @param string $parentOrderCreatedDate 父订单创建时间 * @param string $parentOrderCreatedDate 父订单创建时间
* @return array 映射后的订单子项数据 * @return array 映射后的订单子项数据
*/ */
private function shopeeOrderItemMap(array $item, string $platform_order_id, int $parent_order_id, string $parent_order_created_date): array private function orderItemMap(array $item, string $platform_order_id, int $parent_order_id, Carbon $parent_order_created_date): array
{ {
// @TODO 实现 Shopee 订单子项字段映射 // @TODO 实现 Shopee 订单子项字段映射
// 参考现有的 parseOrderItems() 方法中的映射逻辑 // 参考现有的 parseOrderItems() 方法中的映射逻辑
@@ -416,7 +494,7 @@ class Order extends AbstractOrderParse
'sub_order_id' => $item['order_item_id'], 'sub_order_id' => $item['order_item_id'],
// @attention sku 的处理需要仅以规范和约束,确保 sku 准确 // @attention sku 的处理需要仅以规范和约束,确保 sku 准确
'sub_order_type_id' => '', 'sub_order_type_id' => '',
'product_id' => '', 'product_id' => 0,
'platform_product_id' =>$item['item_id'], 'platform_product_id' =>$item['item_id'],
// @attention @TODO 需要对 运营侧的产品维护做进一步规范 // @attention @TODO 需要对 运营侧的产品维护做进一步规范
'product_sku' => $item['model_sku'] ?? $item['item_sku'] ?? null, 'product_sku' => $item['model_sku'] ?? $item['item_sku'] ?? null,
@@ -427,7 +505,7 @@ class Order extends AbstractOrderParse
'discount' => $item['model_original_price'] - $item['model_quantity_purchased'] ?? 0, 'discount' => $item['model_original_price'] - $item['model_quantity_purchased'] ?? 0,
'total' => $item['model_quantity_purchased'], 'total' => $item['model_quantity_purchased'],
// @attention 扩展字段,用来存储其他信息 // @attention 扩展字段,用来存储其他信息
'ext' => '', 'ext' => null,
'created_date' => $parent_order_created_date, 'created_date' => $parent_order_created_date,
]; ];
} }