152 lines
4.7 KiB
PHP
152 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Platform\Tmall\EntityParse;
|
|
|
|
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;
|
|
|
|
/**
|
|
* 天猫订单解析器示例
|
|
*
|
|
* 展示如何继承 EntityParse 实现自定义解析逻辑
|
|
*/
|
|
class Order 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['store_id'])) {
|
|
throw new InvalidArgumentException('store_id is required in metadata');
|
|
}
|
|
|
|
$storeId = $metadata['store_id'];
|
|
|
|
$store = Store::find($storeId);
|
|
|
|
if (!$store) {
|
|
throw new InvalidArgumentException("Store with ID {$storeId} 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) {
|
|
// 映射每条原始数据到 Model 可用的数组格式
|
|
yield [
|
|
'platform_id' => $this->getPlatform()->id,
|
|
'company_id' => $this->getCompany()->id,
|
|
'store_id' => $this->getStore()->id,
|
|
'unique_id' => $record['unique_id'] ?? $this->getData()['unique_id'] ?? null,
|
|
'raw_data' => json_encode($record),
|
|
// 根据实际业务需求映射其他字段
|
|
// 例如:
|
|
// 'order_id' => $record['order_id'] ?? null,
|
|
// 'order_status' => $record['order_status'] ?? null,
|
|
// 'order_amount' => $record['order_amount'] ?? 0,
|
|
// ...
|
|
];
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 获取实体的唯一键字段(用于 upsert 的 uniqueBy 参数)
|
|
*
|
|
* 定义订单的唯一约束字段组合
|
|
*
|
|
* @return array 唯一键字段名数组
|
|
*/
|
|
public function getUniqueBy(): array
|
|
{
|
|
// 天猫订单的唯一性由店铺 + 平台订单号确定
|
|
return ['store_id', 'platform_order_id'];
|
|
}
|
|
|
|
/**
|
|
* 获取可更新的字段列表(用于 upsert 的 update 参数)
|
|
*
|
|
* 明确定义哪些字段在更新时可以被修改
|
|
* 排除:主键、唯一键、创建时间、关联 ID 等不应变更的字段
|
|
*
|
|
* @return array 可更新字段名数组
|
|
*/
|
|
public function getUpdateFields(): array
|
|
{
|
|
// 方案1:手动指定可更新字段(推荐,最明确)
|
|
return [
|
|
'order_status_id',
|
|
'payment_method_id',
|
|
'buyer_user_id',
|
|
'total_amount',
|
|
'updated_date',
|
|
'raw',
|
|
// 根据实际业务需求添加其他可更新字段
|
|
];
|
|
|
|
// 方案2:动态计算(如果字段较多且经常变动)
|
|
// $entity = $this->entityMatch($this->getData());
|
|
// $excludeFields = array_merge(
|
|
// ['id', 'created_at', 'created_date', 'company_id', 'platform_id'],
|
|
// $this->getUniqueBy()
|
|
// );
|
|
// return $this->getTableColumnsExcept($entity, $excludeFields);
|
|
}
|
|
}
|