Files
datahub/backend/app/Platform/Tmall/EntityParse/Order.php
T

152 lines
4.7 KiB
PHP
Raw Normal View History

2025-11-27 13:40:58 +08:00
<?php
declare(strict_types=1);
2025-12-12 13:15:02 +08:00
namespace App\Platform\Tmall\EntityParse;
2025-11-27 13:40:58 +08:00
use App\Model\Company;
use App\Model\Model as Entity;
use App\Model\Store;
use App\Entity\Parse\EntityParse;
2025-12-01 11:12:33 +08:00
use Hyperf\Collection\LazyCollection;
2025-11-27 13:40:58 +08:00
use InvalidArgumentException;
/**
* 天猫订单解析器示例
*
* 展示如何继承 EntityParse 实现自定义解析逻辑
*/
2025-12-12 13:15:02 +08:00
class Order extends EntityParse
2025-11-27 13:40:58 +08:00
{
/**
* 公司作用域匹配
*
2025-12-01 11:12:33 +08:00
* 从 metadata 中提取 company_id,然后查询数据库获取公司对象
2025-11-27 13:40:58 +08:00
*
2025-12-01 11:12:33 +08:00
* @param array $metadata
2025-11-27 13:40:58 +08:00
* @return Company
* @throws InvalidArgumentException
*/
2025-12-01 11:12:33 +08:00
public function companyScopeMatch(array $metadata): Company
2025-11-27 13:40:58 +08:00
{
// 验证必需字段
2025-12-01 11:12:33 +08:00
if (!isset($metadata['company_id'])) {
throw new InvalidArgumentException('company_id is required in metadata');
}
2025-11-27 13:40:58 +08:00
2025-12-01 11:12:33 +08:00
$companyId = $metadata['company_id'];
2025-11-27 13:40:58 +08:00
$company = Company::find($companyId);
if (!$company) {
throw new InvalidArgumentException("Company with ID {$companyId} not found");
}
return $company;
}
/**
* 店铺作用域匹配
*
2025-12-01 11:12:33 +08:00
* 从 metadata 中提取 store_id,然后查询数据库获取店铺对象
2025-11-27 13:40:58 +08:00
*
2025-12-01 11:12:33 +08:00
* @param array $metadata
2025-11-27 13:40:58 +08:00
* @return Store
* @throws InvalidArgumentException
*/
2025-12-01 11:12:33 +08:00
public function storeScopeMatch(array $metadata): Store
2025-11-27 13:40:58 +08:00
{
// 验证必需字段
2025-12-01 11:12:33 +08:00
if (!isset($metadata['store_id'])) {
throw new InvalidArgumentException('store_id is required in metadata');
}
2025-11-27 13:40:58 +08:00
2025-12-01 11:12:33 +08:00
$storeId = $metadata['store_id'];
2025-11-27 13:40:58 +08:00
$store = Store::find($storeId);
if (!$store) {
throw new InvalidArgumentException("Store with ID {$storeId} not found");
}
return $store;
}
/**
* 实体数据映射
*
2025-12-01 11:12:33 +08:00
* 将原始数据映射为可供 Model 使用的数组集合
2025-11-27 13:40:58 +08:00
*
2025-12-01 11:12:33 +08:00
* @param array $rawData 原始数据数组,通常来自 $data['raw_data']
* @return LazyCollection 返回 LazyCollection,每个元素为可供 Model::fill() 使用的数组
2025-11-27 13:40:58 +08:00
*/
2025-12-01 11:12:33 +08:00
public function entityMap(array $rawData): LazyCollection
2025-11-27 13:40:58 +08:00
{
2025-12-01 11:12:33 +08:00
// 使用 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,
// ...
];
}
});
2025-11-27 13:40:58 +08:00
}
/**
2025-12-12 13:15:02 +08:00
* 获取实体的唯一键字段(用于 upsert 的 uniqueBy 参数)
2025-11-27 13:40:58 +08:00
*
2025-12-12 13:15:02 +08:00
* 定义订单的唯一约束字段组合
*
* @return array 唯一键字段名数组
*/
public function getUniqueBy(): array
{
// 天猫订单的唯一性由店铺 + 平台订单号确定
return ['store_id', 'platform_order_id'];
}
/**
* 获取可更新的字段列表(用于 upsert 的 update 参数)
*
* 明确定义哪些字段在更新时可以被修改
* 排除:主键、唯一键、创建时间、关联 ID 等不应变更的字段
*
* @return array 可更新字段名数组
2025-11-27 13:40:58 +08:00
*/
2025-12-12 13:15:02 +08:00
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);
}
2025-11-27 13:40:58 +08:00
}