add order item constraint to order parse
This commit is contained in:
@@ -7,7 +7,7 @@ namespace App\Platform\Shopee\EntityParse;
|
||||
use App\Model\Company;
|
||||
use App\Model\Model as Entity;
|
||||
use App\Model\Store;
|
||||
use App\Entity\Parse\EntityParse;
|
||||
use App\Platform\AbstractOrderParse;
|
||||
use App\Platform\Shopee\Constants\OrderStatus;
|
||||
use App\Platform\Shopee\Constants\PaymentMethod;
|
||||
use Carbon\Carbon;
|
||||
@@ -17,11 +17,12 @@ use Psr\Log\LoggerInterface;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Shopee订单解析器
|
||||
* Shopee 订单解析器
|
||||
*
|
||||
* 展示如何继承 EntityParse 实现自定义解析逻辑
|
||||
* 继承 AbstractOrderParse,实现 Shopee 平台特定的订单解析逻辑
|
||||
* 实现 OrderContract 契约中定义的 parseOrderItems 方法
|
||||
*/
|
||||
class Order extends EntityParse
|
||||
class Order extends AbstractOrderParse
|
||||
{
|
||||
/**
|
||||
* 公司作用域匹配
|
||||
@@ -97,50 +98,100 @@ class Order extends EntityParse
|
||||
$records = isset($rawData[0]) ? $rawData : [$rawData];
|
||||
|
||||
foreach ($records as $record) {
|
||||
|
||||
$_origin_order_item = $record['order_list']; // 'order_list' 为来自 shopee 原始数据的 订单子项信息
|
||||
|
||||
// 解析订单子项数据,传入 platform_order_id(实现 OrderContract 契约方法)
|
||||
$order_items = $this->parseOrderItems($_origin_order_item, $record['order_sn']);
|
||||
|
||||
// 根据实际业务需求映射其他字段
|
||||
// 映射每条原始数据到 Order Model 可用的数组格式
|
||||
// @attention 此处业务决定了 电商平台数据 和 数据仓库 之间的映射关系
|
||||
|
||||
// 返回包含订单数据和子项数据的结构
|
||||
yield [
|
||||
'company_id' => $this->getCompany()->id,
|
||||
'platform_id' => 25,
|
||||
'store_id' => $this->getStore()->id,
|
||||
'order_status_id'=> OrderStatus::{$record['order_status']}->value,
|
||||
'platform_order_id' => $record['order_sn'],
|
||||
'payment_method_id' => $this->parsePaymentMethod($record['payment_method']),
|
||||
'buyer_user_id' => $record['buyer_user_id'] ?? null,
|
||||
'presale' => false, // shopee 平台暂不存类型为 presales 的订单
|
||||
'total_amount' => $record['total_amount'] ?? 0,
|
||||
// @link https://open.shopee.com/documents/v2/v2.order.get_order_detail?module=94&type=1
|
||||
// shopee 定义 $record['total_amount'] 为买家付款金额字段
|
||||
// The total amount paid by the buyer for the order. This amount includes the total sale price of items,
|
||||
// shipping cost beared by buyer; and offset by Shopee promotions if applicable. This value
|
||||
// will only return after the buyer has completed payment for the order.
|
||||
'total_paid' => $record['total_amount'] ?? 0,
|
||||
'total_discount' => 0,
|
||||
'total_received' => 0,
|
||||
// @link https://open.shopee.com/documents/v2/v2.order.get_order_detail?module=94&type=1
|
||||
// $record['actual_shipping_fee'] - The actual shipping fee of the order if available from external logistics partners.
|
||||
'freight_fee' => $record['actual_shipping_fee'] ?? 0,
|
||||
'tax_fee' => 0,
|
||||
'discount_fee' => 0,
|
||||
'commission_fee' => 0,
|
||||
'coupon_amount' => 0,
|
||||
'voucher_amount' => 0,
|
||||
'order_type_id' => 0,
|
||||
'created_date' => Carbon::createFromTimestamp($record['create_time'], $this->getStore()->getTimezoneString()), // 订单的创建时间
|
||||
'updated_date' => Carbon::createFromTimestamp($record['update_time'], $this->getStore()->getTimezoneString()), // 最近的一次订单更新时间
|
||||
'paid_date' => $record['pay_time'] ? Carbon::createFromTimestamp($record['pay_time'], $this->getStore()->getTimezoneString()) : null,
|
||||
'shipping_date' => $record['pickup_done_time'] > 0 ? Carbon::createFromTimestamp($record['pickup_done_time'], $this->getStore()->getTimezoneString()) : null,
|
||||
'country' => $record['region'], // address 等地理位置信息因 shopee 数据保护协议已被隐藏, 目前仅提供 ISO 格式国家代码
|
||||
'raw' => json_encode($record['raw']),
|
||||
'order' => [
|
||||
'company_id' => $this->getCompany()->id,
|
||||
'platform_id' => 25,
|
||||
'store_id' => $this->getStore()->id,
|
||||
'order_status_id'=> OrderStatus::{$record['order_status']}->value,
|
||||
'platform_order_id' => $record['order_sn'],
|
||||
'payment_method_id' => $this->parsePaymentMethod($record['payment_method']),
|
||||
'buyer_user_id' => $record['buyer_user_id'] ?? null,
|
||||
'presale' => false, // shopee 平台暂不存类型为 presales 的订单
|
||||
'total_amount' => $record['total_amount'] ?? 0,
|
||||
// @link https://open.shopee.com/documents/v2/v2.order.get_order_detail?module=94&type=1
|
||||
// shopee 定义 $record['total_amount'] 为买家付款金额字段
|
||||
// The total amount paid by the buyer for the order. This amount includes the total sale price of items,
|
||||
// shipping cost beared by buyer; and offset by Shopee promotions if applicable. This value
|
||||
// will only return after the buyer has completed payment for the order.
|
||||
'total_paid' => $record['total_amount'] ?? 0,
|
||||
'total_discount' => 0,
|
||||
'total_received' => 0,
|
||||
// @link https://open.shopee.com/documents/v2/v2.order.get_order_detail?module=94&type=1
|
||||
// $record['actual_shipping_fee'] - The actual shipping fee of the order if available from external logistics partners.
|
||||
'freight_fee' => $record['actual_shipping_fee'] ?? 0,
|
||||
'tax_fee' => 0,
|
||||
'discount_fee' => 0,
|
||||
'commission_fee' => 0,
|
||||
'coupon_amount' => 0,
|
||||
'voucher_amount' => 0,
|
||||
'order_type_id' => 0,
|
||||
'created_date' => Carbon::createFromTimestamp($record['create_time'], $this->getStore()->getTimezoneString()), // 订单的创建时间
|
||||
'updated_date' => Carbon::createFromTimestamp($record['update_time'], $this->getStore()->getTimezoneString()), // 最近的一次订单更新时间
|
||||
'paid_date' => $record['pay_time'] ? Carbon::createFromTimestamp($record['pay_time'], $this->getStore()->getTimezoneString()) : null,
|
||||
'shipping_date' => $record['pickup_done_time'] > 0 ? Carbon::createFromTimestamp($record['pickup_done_time'], $this->getStore()->getTimezoneString()) : null,
|
||||
'country' => $record['region'], // address 等地理位置信息因 shopee 数据保护协议已被隐藏, 目前仅提供 ISO 格式国家代码
|
||||
'raw' => json_encode($record['raw']),
|
||||
],
|
||||
'items' => $order_items,
|
||||
];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private function orderItemParse( array $order_items) : array {
|
||||
return [];
|
||||
/**
|
||||
* 解析订单子项数据(实现 OrderContract 契约方法)
|
||||
*
|
||||
* Shopee API 文档:https://open.shopee.com/documents/v2/v2.order.get_order_detail?module=94&type=1
|
||||
*
|
||||
* @param array $orderItems Shopee 原始订单子项数组
|
||||
* @param string $platformOrderId 平台订单 ID (order_sn)
|
||||
* @return array 解析后的订单子项数据数组
|
||||
*/
|
||||
public function parseOrderItems(array $orderItems, string $platformOrderId): array
|
||||
{
|
||||
$parsedItems = [];
|
||||
|
||||
foreach ($orderItems as $item) {
|
||||
// 价格和数量
|
||||
$originalPrice = (float)($item['model_original_price'] ?? 0);
|
||||
$discountedPrice = (float)($item['model_discounted_price'] ?? $originalPrice);
|
||||
$quantity = (int)($item['model_quantity_purchased'] ?? 1);
|
||||
|
||||
// SKU: 优先使用 model_sku(变体SKU),如果为空则使用 item_sku(商品SKU)
|
||||
$sku = !empty($item['model_sku']) ? $item['model_sku'] : ($item['item_sku'] ?? '');
|
||||
|
||||
$parsedItems[] = [
|
||||
'company_id' => $this->getCompany()->id,
|
||||
'platform_id' => 25,
|
||||
'store_id' => $this->getStore()->id,
|
||||
// order_id 将在 Consumer 中补充
|
||||
'platform_order_id' => $platformOrderId, // 平台订单 ID(从 order_sn 传入)
|
||||
'sub_order_id' => (string)$item['order_item_id'], // 使用 order_item_id 作为订单子项的唯一标识
|
||||
'sub_order_type_id' => 0,
|
||||
'product_id' => 0, // 需要根据 platform_product_id 匹配,暂设为 0
|
||||
'platform_product_id' => (string)$item['item_id'], // Shopee 商品 ID
|
||||
'product_sku' => $sku,
|
||||
'product_barcode' => '',
|
||||
'unit_price' => $discountedPrice, // 实际成交单价(折扣后)
|
||||
'quantity' => $quantity,
|
||||
'discount' => ($originalPrice - $discountedPrice) * $quantity, // 总折扣金额
|
||||
'total' => $discountedPrice * $quantity, // 总金额 = 折扣价 × 数量
|
||||
'ext' => '', // 原始信息已在 Order.raw 中记录,此处留空
|
||||
];
|
||||
}
|
||||
|
||||
return $parsedItems;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user