update entity map

This commit is contained in:
2025-12-01 11:12:33 +08:00
parent 674f3857ba
commit dcca6a109c
4 changed files with 329 additions and 131 deletions
+43 -49
View File
@@ -8,7 +8,7 @@ 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 Hyperf\Collection\LazyCollection;
use InvalidArgumentException;
/**
@@ -21,20 +21,20 @@ class TmallOrderParser extends EntityParse
/**
* 公司作用域匹配
*
* 从消息体中提取 company_id,然后查询数据库获取公司对象
* 从 metadata 中提取 company_id,然后查询数据库获取公司对象
*
* @param ConsumerMessageInterface $message
* @param array $metadata
* @return Company
* @throws InvalidArgumentException
*/
public function companyScopeMatch(ConsumerMessageInterface $message): Company
public function companyScopeMatch(array $metadata): Company
{
$data = $this->extractMessageData($message);
// 验证必需字段
$this->validateRequiredFields($data, ['company_id']);
if (!isset($metadata['company_id'])) {
throw new InvalidArgumentException('company_id is required in metadata');
}
$companyId = $data['company_id'];
$companyId = $metadata['company_id'];
$company = Company::find($companyId);
@@ -48,20 +48,20 @@ class TmallOrderParser extends EntityParse
/**
* 店铺作用域匹配
*
* 从消息体中提取 store_id,然后查询数据库获取店铺对象
* 从 metadata 中提取 store_id,然后查询数据库获取店铺对象
*
* @param ConsumerMessageInterface $message
* @param array $metadata
* @return Store
* @throws InvalidArgumentException
*/
public function storeScopeMatch(ConsumerMessageInterface $message): Store
public function storeScopeMatch(array $metadata): Store
{
$data = $this->extractMessageData($message);
// 验证必需字段
$this->validateRequiredFields($data, ['store_id']);
if (!isset($metadata['store_id'])) {
throw new InvalidArgumentException('store_id is required in metadata');
}
$storeId = $data['store_id'];
$storeId = $metadata['store_id'];
$store = Store::find($storeId);
@@ -75,50 +75,44 @@ class TmallOrderParser extends EntityParse
/**
* 实体数据映射
*
* 将原始数据映射到实体对象
* 将原始数据映射为可供 Model 使用的数组集合
*
* @param array $data
* @param Entity $entity
* @return Entity
* @param array $rawData 原始数据数组,通常来自 $data['raw_data']
* @return LazyCollection 返回 LazyCollection,每个元素为可供 Model::fill() 使用的数组
*/
public function entityMap(array $data, Entity $entity): Entity
public function entityMap(array $rawData): LazyCollection
{
// 假设数据结构为:
// {
// "platform_id": 2,
// "company_id": 188,
// "store_id": 292,
// "unique_id": "abc123",
// "raw_data": [...]
// }
// 使用 LazyCollection 进行延迟处理,提高性能
return LazyCollection::make(function () use ($rawData) {
// 如果 rawData 是单个记录,转换为数组
$records = isset($rawData[0]) ? $rawData : [$rawData];
$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;
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,
// ...
];
}
});
}
/**
* 可选:覆盖唯一标识符提取逻辑
*
* 如果使用默认的 id/unique_id 提取逻辑,则无需覆盖此方法
* 如果使用默认的 unique_id 提取逻辑,则无需覆盖此方法
*/
// public function entityUniqueIdentifierExtract(ConsumerMessageInterface $message): string|int
// public function entityUniqueIdentifierExtract(array $metadata): string|int
// {
// $data = $this->extractMessageData($message);
// return $this->extractUniqueIdentifier($data, 'custom_id_field');
// return $metadata['custom_id_field'] ?? throw new InvalidArgumentException('custom_id_field not found');
// }
}