fix order item fail to save

This commit is contained in:
2026-02-02 15:13:46 +08:00
parent 033bebe11e
commit 39ef3acc1f
2 changed files with 85 additions and 25 deletions
@@ -266,10 +266,16 @@ class Order extends EntityParse
*
* orderItems 的输出结果为 \App\Model\OrderItem::fill() 可以直接使用的数据格式
*
* @param array $raw_data
* @return array
* @param array $raw_data
* @param array $platform_orders_id_to_local_db_order_id_map
*
*
* @param array $order_items_to_product_id_map = [
* num_iid => local_db_product_id
* ]
* @return array
*/
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 $order_items_to_product_id_map = []): array
{
// 由于 $raw_data 是批量数据,包含多条订单结果,因此订单子项需要从集合中提取
// Tmall 平台响应体的数据格式为: collection->payload->orders
@@ -278,10 +284,12 @@ class Order extends EntityParse
$items = [];
foreach ($raw_data as $raw_orders) {
foreach ($raw_orders['orders'] as $raw_order_item) {
foreach ($raw_orders['orders']['order'] as $raw_order_item) {
// 数据库中父订单 id
$db_order_id = $platform_orders_id_to_local_db_order_id_map[$raw_orders['tid']];
$items[] = $this->tmallOrderItemMap($raw_order_item, $raw_orders['tid'], $db_order_id, $raw_orders['created']);
$platform_order_id = strval($raw_orders['tid']);
$local_product_id = empty($order_items_to_product_id_map) || !isset($order_items_to_product_id_map[$raw_order_item['num_iid']]) ? 0 : $order_items_to_product_id_map[$raw_order_item['num_iid']];
$items[] = $this->tmallOrderItemMap($raw_order_item, $platform_order_id, $db_order_id, $raw_orders['created'], $local_product_id);
}
}
@@ -292,20 +300,65 @@ class Order extends EntityParse
* Tmall 订单子项映射转换
* @return void
*/
private function tmallOrderItemMap(array $item, string $platform_order_id, int $parent_order_id, string $parent_order_created_date): array
private function tmallOrderItemMap(array $item, string $platform_order_id, int $parent_order_id, string $parent_order_created_date, int $local_product_id = 0): array
{
// $item = [
// "adjust_fee" => "0.00",
// "buyer_rate" => false,
// "cid" => 50026872,
// "consign_due_time" => "2_5",
// "discount_fee" => "231.98",
// "divide_order_fee" => "504.00",
// "is_bybt_order" => false,
// "is_daixiao" => false,
// "is_idle" => "0",
// "is_jzfx" => false,
// "is_oversold" => false,
// "nr_outer_iid" => "768990037900",
// "num" => 1,
// "num_iid" => 637901668632,
// "oid" => 4932549972242808538,
// "oid_str" => "4932549972242808538",
// "order_attr" => "{"esDate":"2025-12-14","pushTime":"2025-12-09 11:30:00"}",
// "order_from" => "WAP,WAP",
// "outer_iid" => "768990037900",
// "outer_sku_id" => "768990037900",
// "part_mjz_discount" => "15.00",
// "payment" => "504.00",
// "pic_path" => "https://img.alicdn.com/bao/uploaded/i4/2413132940/O1CN01duCukP1XaZLX2jVQy_!!2413132940.jpg",
// "price" => "693.00",
// "refund_status" => "NO_REFUND",
// "s_tariff_fee" => "0.00",
// "seller_rate" => false,
// "seller_type" => "B",
// "sku_id" => "6123547511321",
// "sku_properties_name" => "颜色分类:高倍Omega鱼油软胶囊 180粒/瓶",
// "snapshot_url" => "za:4932549972242808538_1",
// "status" => "WAIT_SELLER_SEND_GOODS",
// "store_code" => "STORE_11388627",
// "sub_order_tax_fee" => "57.98",
// "sub_order_tax_promotion_fee" => "0.00",
// "sub_order_tax_rate" => "0",
// "tax_coupon_discount" => "57.98",
// "tax_free" => true,
// "title" => "【优惠价】NordicNaturals挪威小鱼dha深海鱼油Omega3成人rTG高纯度epa胶囊",
// "total_fee" => "461.02",
// ]
//@TODO order item 的业务映射需进一步补全
return [
'company_id' => $this->getCompany()->id,
'platform_id' => $this->getPlatform()->id,
'store_id' => $this->getStore()->id,
'order_id' => $parent_order_id,
'platform_order_id' => $platform_order_id,
'platform_order_id' => strval($platform_order_id),
'sub_order_id' => $item['oid'],
// @attention sku 的处理需要仅以规范和约束,确保 sku 准确
'sub_order_type_id' => '',
'product_id' => '',
'sub_order_type_id' => null,
// @attention 表示未找到产品 id
'product_id' => $local_product_id,
'platform_product_id' =>$item['num_iid'],
// @attention @TODO 需要对 运营侧的产品维护做进一步规范
'product_sku' => $item['outer_sku_id'] ?? null,
@@ -316,7 +369,7 @@ class Order extends EntityParse
'discount' => $item['discount_fee'],
'total' => $item['divide_order_fee'],
// @attention 扩展字段,用来存储其他信息
'ext' => '',
'ext' => null,
'created_date' => $parent_order_created_date,
];
}