161 lines
5.9 KiB
PHP
161 lines
5.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Platform\Tmall;
|
||
|
|
|
||
|
|
use App\Model\Company;
|
||
|
|
use App\Model\Model as Entity;
|
||
|
|
use App\Model\Store;
|
||
|
|
use App\Entity\Parse\EntityParse;
|
||
|
|
use Hyperf\Collection\LazyCollection;
|
||
|
|
use InvalidArgumentException;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Shopee订单解析器
|
||
|
|
*
|
||
|
|
* 展示如何继承 EntityParse 实现自定义解析逻辑
|
||
|
|
*/
|
||
|
|
class ShopeeOrderParser extends EntityParse
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* 公司作用域匹配
|
||
|
|
*
|
||
|
|
* 从 metadata 中提取 company_id,然后查询数据库获取公司对象
|
||
|
|
*
|
||
|
|
* @param array $metadata
|
||
|
|
* @return Company
|
||
|
|
* @throws InvalidArgumentException
|
||
|
|
*/
|
||
|
|
public function companyScopeMatch(array $metadata): Company
|
||
|
|
{
|
||
|
|
// 验证必需字段
|
||
|
|
if (!isset($metadata['company_id'])) {
|
||
|
|
throw new InvalidArgumentException('company_id is required in metadata');
|
||
|
|
}
|
||
|
|
|
||
|
|
$companyId = $metadata['company_id'];
|
||
|
|
|
||
|
|
$company = Company::find($companyId);
|
||
|
|
|
||
|
|
if (!$company) {
|
||
|
|
throw new InvalidArgumentException("Company with ID {$companyId} not found");
|
||
|
|
}
|
||
|
|
|
||
|
|
return $company;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 店铺作用域匹配
|
||
|
|
*
|
||
|
|
* 从 metadata 中提取 store_id,然后查询数据库获取店铺对象
|
||
|
|
*
|
||
|
|
* @param array $metadata
|
||
|
|
* @return Store
|
||
|
|
* @throws InvalidArgumentException
|
||
|
|
*/
|
||
|
|
public function storeScopeMatch(array $metadata): Store
|
||
|
|
{
|
||
|
|
// 验证必需字段
|
||
|
|
if (!isset($metadata['platform_store_id'])) {
|
||
|
|
throw new InvalidArgumentException('platform_store_id is required in metadata');
|
||
|
|
}
|
||
|
|
|
||
|
|
$platform_store_id = $metadata['platform_store_id'];
|
||
|
|
|
||
|
|
$shopee_platform_id = 25;
|
||
|
|
|
||
|
|
$store = Store::where('platform_id','=', $shopee_platform_id)
|
||
|
|
->where('platform_store_id', '=', $platform_store_id)
|
||
|
|
->first();
|
||
|
|
|
||
|
|
if (!$store) {
|
||
|
|
throw new InvalidArgumentException("Platform shopee store id {$shopee_platform_id} not found");
|
||
|
|
}
|
||
|
|
|
||
|
|
return $store;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 实体数据映射
|
||
|
|
*
|
||
|
|
* 将原始数据映射为可供 Model 使用的数组集合
|
||
|
|
*
|
||
|
|
* @param array $rawData 原始数据数组,通常来自 $data['raw_data']
|
||
|
|
* @return LazyCollection 返回 LazyCollection,每个元素为可供 Model::fill() 使用的数组
|
||
|
|
*/
|
||
|
|
public function entityMap(array $rawData): LazyCollection
|
||
|
|
{
|
||
|
|
// 使用 LazyCollection 进行延迟处理,提高性能
|
||
|
|
return LazyCollection::make(function () use ($rawData) {
|
||
|
|
// 如果 rawData 是单个记录,转换为数组
|
||
|
|
$records = isset($rawData[0]) ? $rawData : [$rawData];
|
||
|
|
|
||
|
|
foreach ($records as $record) {
|
||
|
|
|
||
|
|
// 映射每条原始数据到 Order Model 可用的数组格式
|
||
|
|
// @attention 此处业务决定了 电商平台数据 和 数据仓库 之间的映射关系
|
||
|
|
|
||
|
|
// "company_id" int4 NOT NULL,
|
||
|
|
// "platform_id" int4 NOT NULL,
|
||
|
|
// "store_id" int4 NOT NULL,
|
||
|
|
// "order_status_id" int4 NOT NULL,
|
||
|
|
// "platform_order_id" text COLLATE "pg_catalog"."default" NOT NULL,
|
||
|
|
// "payment_method_id" int4 NOT NULL DEFAULT 1,
|
||
|
|
// "presale" bool NOT NULL DEFAULT false,
|
||
|
|
// "total_amount" float8 NOT NULL DEFAULT '0'::double precision,
|
||
|
|
// "total_paid" float8 NOT NULL DEFAULT '0'::double precision,
|
||
|
|
// "total_discount" float8 NOT NULL DEFAULT '0'::double precision,
|
||
|
|
// "total_received" float8 NOT NULL DEFAULT '0'::double precision,
|
||
|
|
// "freight_fee" float8 NOT NULL DEFAULT '0'::double precision,
|
||
|
|
// "tax_fee" float8 NOT NULL DEFAULT '0'::double precision,
|
||
|
|
// "discount_fee" float8 NOT NULL DEFAULT '0'::double precision,
|
||
|
|
// "commission_fee" float8 NOT NULL DEFAULT '0'::double precision,
|
||
|
|
// "coupon_amount" float8 NOT NULL DEFAULT '0'::double precision,
|
||
|
|
// "voucher_amount" float8 NOT NULL DEFAULT '0'::double precision,
|
||
|
|
// "order_type_id" int4 NOT NULL DEFAULT 1,
|
||
|
|
// "created_date" timestamptz(0) NOT NULL,
|
||
|
|
// "updated_date" timestamptz(0),
|
||
|
|
// "paid_date" timestamptz(0),
|
||
|
|
// "shipping_date" timestamptz(0),
|
||
|
|
// "zipcode" text COLLATE "pg_catalog"."default",
|
||
|
|
// "city" text COLLATE "pg_catalog"."default",
|
||
|
|
// "province" text COLLATE "pg_catalog"."default",
|
||
|
|
// "country" text COLLATE "pg_catalog"."default",
|
||
|
|
// "raw" jsonb,
|
||
|
|
// "ext" jsonb,
|
||
|
|
// "created_at" timestamptz(0),
|
||
|
|
// "updated_at" timestamptz(0),
|
||
|
|
|
||
|
|
yield [
|
||
|
|
'company_id' => $this->getCompany()->id,
|
||
|
|
'platform_id' => 25,
|
||
|
|
'store_id' => $this->getStore()->id,
|
||
|
|
'unique_id' => $record['unique_id'] ?? $this->getData()['unique_id'] ?? null,
|
||
|
|
'raw_data' => json_encode($record['raw']),
|
||
|
|
// 根据实际业务需求映射其他字段
|
||
|
|
// 例如:
|
||
|
|
// 'order_id' => $record['order_id'] ?? null,
|
||
|
|
// 'order_status' => $record['order_status'] ?? null,
|
||
|
|
// 'order_amount' => $record['order_amount'] ?? 0,
|
||
|
|
// ...
|
||
|
|
];
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private function orderItemParse( array $order_items) : array {
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 可选:覆盖唯一标识符提取逻辑
|
||
|
|
*
|
||
|
|
* 如果使用默认的 unique_id 提取逻辑,则无需覆盖此方法
|
||
|
|
*/
|
||
|
|
// public function entityUniqueIdentifierExtract(array $metadata): string|int
|
||
|
|
// {
|
||
|
|
// return $metadata['custom_id_field'] ?? throw new InvalidArgumentException('custom_id_field not found');
|
||
|
|
// }
|
||
|
|
}
|