125 lines
3.2 KiB
PHP
125 lines
3.2 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\Amqp\Message\ConsumerMessageInterface;
|
|
use InvalidArgumentException;
|
|
|
|
/**
|
|
* 天猫订单解析器示例
|
|
*
|
|
* 展示如何继承 EntityParse 实现自定义解析逻辑
|
|
*/
|
|
class TmallOrderParser extends EntityParse
|
|
{
|
|
/**
|
|
* 公司作用域匹配
|
|
*
|
|
* 从消息体中提取 company_id,然后查询数据库获取公司对象
|
|
*
|
|
* @param ConsumerMessageInterface $message
|
|
* @return Company
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
public function companyScopeMatch(ConsumerMessageInterface $message): Company
|
|
{
|
|
$data = $this->extractMessageData($message);
|
|
|
|
// 验证必需字段
|
|
$this->validateRequiredFields($data, ['company_id']);
|
|
|
|
$companyId = $data['company_id'];
|
|
|
|
$company = Company::find($companyId);
|
|
|
|
if (!$company) {
|
|
throw new InvalidArgumentException("Company with ID {$companyId} not found");
|
|
}
|
|
|
|
return $company;
|
|
}
|
|
|
|
/**
|
|
* 店铺作用域匹配
|
|
*
|
|
* 从消息体中提取 store_id,然后查询数据库获取店铺对象
|
|
*
|
|
* @param ConsumerMessageInterface $message
|
|
* @return Store
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
public function storeScopeMatch(ConsumerMessageInterface $message): Store
|
|
{
|
|
$data = $this->extractMessageData($message);
|
|
|
|
// 验证必需字段
|
|
$this->validateRequiredFields($data, ['store_id']);
|
|
|
|
$storeId = $data['store_id'];
|
|
|
|
$store = Store::find($storeId);
|
|
|
|
if (!$store) {
|
|
throw new InvalidArgumentException("Store with ID {$storeId} not found");
|
|
}
|
|
|
|
return $store;
|
|
}
|
|
|
|
/**
|
|
* 实体数据映射
|
|
*
|
|
* 将原始数据映射到实体对象
|
|
*
|
|
* @param array $data
|
|
* @param Entity $entity
|
|
* @return Entity
|
|
*/
|
|
public function entityMap(array $data, Entity $entity): Entity
|
|
{
|
|
// 假设数据结构为:
|
|
// {
|
|
// "platform_id": 2,
|
|
// "company_id": 188,
|
|
// "store_id": 292,
|
|
// "unique_id": "abc123",
|
|
// "raw_data": [...]
|
|
// }
|
|
|
|
$rawData = $data['raw_data'] ?? [];
|
|
|
|
// 映射基本信息
|
|
$entity->platform_id = $this->getPlatform()->id;
|
|
$entity->company_id = $this->getCompany()->id;
|
|
$entity->store_id = $this->getStore()->id;
|
|
$entity->unique_id = $data['unique_id'] ?? null;
|
|
|
|
// 映射原始数据
|
|
if (!empty($rawData)) {
|
|
$entity->raw_data = json_encode($rawData);
|
|
}
|
|
|
|
// 映射其他字段(根据实际业务需求)
|
|
// ...
|
|
|
|
return $entity;
|
|
}
|
|
|
|
/**
|
|
* 可选:覆盖唯一标识符提取逻辑
|
|
*
|
|
* 如果使用默认的 id/unique_id 提取逻辑,则无需覆盖此方法
|
|
*/
|
|
// public function entityUniqueIdentifierExtract(ConsumerMessageInterface $message): string|int
|
|
// {
|
|
// $data = $this->extractMessageData($message);
|
|
// return $this->extractUniqueIdentifier($data, 'custom_id_field');
|
|
// }
|
|
}
|