update entiity parse
This commit is contained in:
@@ -9,7 +9,7 @@ use App\Model\Company;
|
||||
use App\Model\Platform;
|
||||
use App\Model\Store;
|
||||
use App\Entity\Parse\Traits\EntityParseHelper;
|
||||
use Hyperf\Amqp\Message\ConsumerMessageInterface;
|
||||
use Hyperf\Collection\LazyCollection;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
@@ -18,13 +18,13 @@ use InvalidArgumentException;
|
||||
* 使用工厂方法模式 + 延迟初始化
|
||||
* 提供消息解析的通用框架和默认实现
|
||||
*
|
||||
* @method static static create(ConsumerMessageInterface $message)
|
||||
* @method static static create(array $data)
|
||||
*/
|
||||
abstract class EntityParse implements EntityParseInterface
|
||||
{
|
||||
use EntityParseHelper;
|
||||
|
||||
protected ConsumerMessageInterface $message;
|
||||
protected array $data;
|
||||
protected ?Platform $platform = null;
|
||||
protected ?Company $company = null;
|
||||
protected ?Store $store = null;
|
||||
@@ -41,30 +41,67 @@ abstract class EntityParse implements EntityParseInterface
|
||||
/**
|
||||
* 工厂方法:创建解析器实例
|
||||
*
|
||||
* @param ConsumerMessageInterface $message
|
||||
* @param array $data
|
||||
* @return static
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function create(ConsumerMessageInterface $message): static
|
||||
public static function create(array $data): static
|
||||
{
|
||||
$instance = new static();
|
||||
$instance->message = $message;
|
||||
$instance->data = $data;
|
||||
|
||||
// 在初始化前先验证数据
|
||||
if (!$instance->messageValidate($data)) {
|
||||
throw new InvalidArgumentException('Message validation failed: required fields missing');
|
||||
}
|
||||
|
||||
$instance->initialize();
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* 消息数据验证
|
||||
*
|
||||
* 默认实现:验证必需字段
|
||||
* 子类可以覆写以添加自定义验证逻辑
|
||||
*
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
public function messageValidate(array $data): bool
|
||||
{
|
||||
// 验证必需字段
|
||||
$requiredFields = ['company_id', 'platform_id', 'store_id', 'unique_id', 'raw_data'];
|
||||
|
||||
foreach ($requiredFields as $field) {
|
||||
if (!isset($data[$field])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟初始化
|
||||
* 在 message 设置后执行作用域匹配和验证
|
||||
* 在 data 设置后执行作用域匹配和验证
|
||||
*
|
||||
* @return void
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
protected function initialize(): void
|
||||
{
|
||||
$this->platform = $this->platformScopeMatch($this->message);
|
||||
$this->company = $this->companyScopeMatch($this->message);
|
||||
$this->store = $this->storeScopeMatch($this->message);
|
||||
// 提取 metadata
|
||||
$metadata = [
|
||||
'company_id' => $this->data['company_id'],
|
||||
'platform_id' => $this->data['platform_id'],
|
||||
'store_id' => $this->data['store_id'],
|
||||
'unique_id' => $this->data['unique_id'],
|
||||
];
|
||||
|
||||
$this->platform = $this->platformScopeMatch($metadata);
|
||||
$this->company = $this->companyScopeMatch($metadata);
|
||||
$this->store = $this->storeScopeMatch($metadata);
|
||||
|
||||
$this->validateScope();
|
||||
}
|
||||
@@ -93,47 +130,46 @@ abstract class EntityParse implements EntityParseInterface
|
||||
/**
|
||||
* 平台作用域匹配 - 提供默认实现
|
||||
*
|
||||
* 从 exchange 名称中提取平台信息
|
||||
* 从 metadata 中提取平台信息
|
||||
* 子类可以覆盖此方法以实现自定义逻辑
|
||||
*
|
||||
* @param ConsumerMessageInterface $message
|
||||
* @param array $metadata
|
||||
* @return Platform
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function platformScopeMatch(ConsumerMessageInterface $message): Platform
|
||||
public function platformScopeMatch(array $metadata): Platform
|
||||
{
|
||||
return $this->extractPlatformFromExchange($message);
|
||||
return $this->extractPlatformFromMetadata($metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* 实体匹配 - 提供默认实现
|
||||
*
|
||||
* 从 routing key 中提取实体类型
|
||||
* 从 metadata 中提取实体类型
|
||||
* 子类可以覆盖此方法以实现自定义逻辑
|
||||
*
|
||||
* @param ConsumerMessageInterface $message
|
||||
* @param array $metadata
|
||||
* @return Entity
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function entityMatch(ConsumerMessageInterface $message): Entity
|
||||
public function entityMatch(array $metadata): Entity
|
||||
{
|
||||
return $this->extractEntityFromRoutingKey($message);
|
||||
return $this->extractEntityFromMetadata($metadata);
|
||||
}
|
||||
|
||||
/**
|
||||
* 唯一标识符提取 - 提供默认实现
|
||||
*
|
||||
* 默认从消息体中提取 id 或 unique_id
|
||||
* 默认从 metadata 中提取 unique_id
|
||||
* 子类可以覆盖此方法以实现自定义逻辑
|
||||
*
|
||||
* @param ConsumerMessageInterface $message
|
||||
* @param array $metadata
|
||||
* @return string|int
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function entityUniqueIdentifierExtract(ConsumerMessageInterface $message): string|int
|
||||
public function entityUniqueIdentifierExtract(array $metadata): string|int
|
||||
{
|
||||
$data = $this->extractMessageData($message);
|
||||
return $this->extractUniqueIdentifier($data);
|
||||
return $metadata['unique_id'] ?? throw new InvalidArgumentException('unique_id not found in metadata');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -141,42 +177,41 @@ abstract class EntityParse implements EntityParseInterface
|
||||
*
|
||||
* 必须由子类实现,因为不同平台的公司识别逻辑不同
|
||||
*
|
||||
* @param ConsumerMessageInterface $message
|
||||
* @param array $metadata
|
||||
* @return Company
|
||||
*/
|
||||
abstract public function companyScopeMatch(ConsumerMessageInterface $message): Company;
|
||||
abstract public function companyScopeMatch(array $metadata): Company;
|
||||
|
||||
/**
|
||||
* 店铺作用域匹配 - 抽象方法
|
||||
*
|
||||
* 必须由子类实现,因为不同平台的店铺识别逻辑不同
|
||||
*
|
||||
* @param ConsumerMessageInterface $message
|
||||
* @param array $metadata
|
||||
* @return Store
|
||||
*/
|
||||
abstract public function storeScopeMatch(ConsumerMessageInterface $message): Store;
|
||||
abstract public function storeScopeMatch(array $metadata): Store;
|
||||
|
||||
/**
|
||||
* 实体数据映射 - 抽象方法
|
||||
*
|
||||
* 必须由子类实现,因为不同平台的数据结构不同
|
||||
*
|
||||
* @param array $data
|
||||
* @param Entity $entity
|
||||
* @return Entity
|
||||
* @param array $rawData
|
||||
* @return LazyCollection
|
||||
*/
|
||||
abstract public function entityMap(array $data, Entity $entity): Entity;
|
||||
abstract public function entityMap(array $rawData): LazyCollection;
|
||||
|
||||
// ==================== Getter 方法 ====================
|
||||
|
||||
/**
|
||||
* 获取消息对象
|
||||
* 获取消息数据
|
||||
*
|
||||
* @return ConsumerMessageInterface
|
||||
* @return array
|
||||
*/
|
||||
public function getMessage(): ConsumerMessageInterface
|
||||
public function getData(): array
|
||||
{
|
||||
return $this->message;
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user