update entiity parse

This commit is contained in:
2025-11-27 15:03:25 +08:00
parent 0d634a2ca9
commit e9068ac73d
7 changed files with 569 additions and 122 deletions
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Command;
use Hyperf\Collection\LazyCollection;
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
@@ -19,46 +20,52 @@ class AppMessageQueuePushTmall extends HyperfCommand
{
parent::__construct('app:mq-push:tmall');
}
public function configure()
{
parent::configure();
$this->setDescription('Test push message with Tmall km data');
}
public function handle()
public function handle(): void
{
try {
// 从 raw 数据库连接获取数据
$orders = Db::connection('raw')
->table('wpic_taobao_order')
->orderBy('id', 'desc')
->limit(10)
->get()
->toArray();
if (empty($orders)) {
->table('wpic_taobao_order')->orderBy('id', 'desc')
->limit(10)->get('order_raw')->lazy();
// dump($orders->first());
// return;
if ($orders->isEmpty()) {
$this->warn('No orders found in wpic_taobao_order table');
return 0;
return;
}
$this->info(sprintf('Found %d orders, processing...', count($orders)));
$this->info(sprintf('Found %d orders, processing...', $orders->count()));
// 获取 Producer 实例
$producer = $this->container->get(Producer::class);
// 每 2 条记录组成一条消息
$chunks = array_chunk($orders, 2);
// 每 2 条记录组成一条消息 - 实际生产环境需要增大这个值
// $orders->chunk(2)->each(function($collection) use ($producer) {
$messageCount = 0;
$orders->chunk(2)->each(function (LazyCollection $collection) use ($producer, &$messageCount) {
foreach ($chunks as $index => $chunk) {
// 构造消息数据(根据实际表结构调整字段映射)
$order_data = $collection->pluck('order_raw')->map(function ($item) {
return json_decode($item, true);
})->toArray();
//@ATTENTION 生产环境需要注意, 暂时使用 KM 进行测试
$messageData = [
'company_id' => $chunk[0]->company_id ?? 'default_company',
'platform_id' => $chunk[0]->platform_id ?? 'tmall',
'store_id' => $chunk[0]->store_id ?? 'default_store',
'unique_id' => implode('_', array_column($chunk, 'id')),
'raw_data' => $chunk, // 包含 2 条原始记录
'company_id' => 188,
'platform_id' => 2,
'store_id' => 292,
'unique_id' => uniqid() . '_' . time(),
'raw_data' => $order_data, // 包含 2 条原始记录
];
// 创建并发送消息
@@ -66,19 +73,21 @@ class AppMessageQueuePushTmall extends HyperfCommand
$producer->produce($message);
$messageCount++;
$this->line(sprintf('Sent message %d with order IDs: %s',
$messageCount,
$messageData['unique_id']
$messageData['unique_id'],
));
}
});
$this->info(sprintf('Successfully sent %d messages to RabbitMQ', $messageCount));
return 0;
return;
} catch (Exception $e) {
$this->error('Error pushing messages: ' . $e->getMessage());
$this->error($e->getTraceAsString());
return 1;
return;
}
}
}