update entity parse
This commit is contained in:
@@ -9,9 +9,9 @@ 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;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
|
||||
/**
|
||||
* EntityParse 抽象类
|
||||
@@ -19,14 +19,14 @@ use InvalidArgumentException;
|
||||
* 使用工厂方法模式 + 延迟初始化
|
||||
* 提供消息解析的通用框架和默认实现
|
||||
*
|
||||
* @method static static create(array $data, ConsumerMessageInterface $message)
|
||||
* @method static static create(array $data, AMQPMessage $message)
|
||||
*/
|
||||
abstract class EntityParse implements EntityParseInterface
|
||||
{
|
||||
use EntityParseHelper;
|
||||
|
||||
protected array $data;
|
||||
protected ConsumerMessageInterface $message;
|
||||
protected AMQPMessage $message;
|
||||
protected ?Platform $platform = null;
|
||||
protected ?Company $company = null;
|
||||
protected ?Store $store = null;
|
||||
@@ -52,11 +52,11 @@ abstract class EntityParse implements EntityParseInterface
|
||||
* 工厂方法:创建解析器实例
|
||||
*
|
||||
* @param array $data
|
||||
* @param ConsumerMessageInterface $message
|
||||
* @param AMQPMessage $message
|
||||
* @return static
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public static function create(array $data, ConsumerMessageInterface $message): static
|
||||
public static function create(array $data, AMQPMessage $message): static
|
||||
{
|
||||
$instance = new static();
|
||||
$instance->data = $data;
|
||||
@@ -82,15 +82,24 @@ abstract class EntityParse implements EntityParseInterface
|
||||
*/
|
||||
public function messageValidate(array $data): bool
|
||||
{
|
||||
// 验证必需字段
|
||||
$requiredFields = ['company_id', 'platform_id', 'store_id', 'unique_id', 'raw_data'];
|
||||
// 验证 meta 字段存在
|
||||
if (!isset($data['meta'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($requiredFields as $field) {
|
||||
if (!isset($data[$field])) {
|
||||
// 验证 meta 中的必需字段
|
||||
$requiredMetaFields = ['company_id', 'platform_id', 'store_id', 'unique_id'];
|
||||
foreach ($requiredMetaFields as $field) {
|
||||
if (!isset($data['meta'][$field])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 验证 data 字段存在
|
||||
if (!isset($data['data'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -103,12 +112,13 @@ abstract class EntityParse implements EntityParseInterface
|
||||
*/
|
||||
protected function initialize(): void
|
||||
{
|
||||
// 提取 metadata
|
||||
// 提取 metadata(从 meta 字段中获取)
|
||||
$meta = $this->data['meta'] ?? [];
|
||||
$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'],
|
||||
'company_id' => $meta['company_id'] ?? null,
|
||||
'platform_id' => $meta['platform_id'] ?? null,
|
||||
'store_id' => $meta['store_id'] ?? null,
|
||||
'unique_id' => $meta['unique_id'] ?? null,
|
||||
];
|
||||
|
||||
$this->platform = $this->platformScopeMatch($metadata);
|
||||
@@ -305,9 +315,9 @@ abstract class EntityParse implements EntityParseInterface
|
||||
/**
|
||||
* 获取消息对象
|
||||
*
|
||||
* @return ConsumerMessageInterface
|
||||
* @return AMQPMessage
|
||||
*/
|
||||
public function getMessage(): ConsumerMessageInterface
|
||||
public function getMessage(): AMQPMessage
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user