Files
datahub/backend/app/Command/AppMessageQueuePushTmall.php
T

94 lines
3.0 KiB
PHP
Raw Normal View History

2025-11-26 09:45:30 +08:00
<?php
declare(strict_types=1);
namespace App\Command;
2025-11-27 15:03:25 +08:00
use Hyperf\Collection\LazyCollection;
2025-11-26 09:45:30 +08:00
use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Hyperf\DbConnection\Db;
use App\Platform\Tmall\Producer\TmallOrderProducer;
use Hyperf\Amqp\Producer;
use Exception;
#[Command]
class AppMessageQueuePushTmall extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
parent::__construct('app:mq-push:tmall');
}
2025-11-27 15:03:25 +08:00
2025-11-26 09:45:30 +08:00
public function configure()
{
parent::configure();
$this->setDescription('Test push message with Tmall km data');
}
2025-11-27 15:03:25 +08:00
public function handle(): void
2025-11-26 09:45:30 +08:00
{
try {
// 从 raw 数据库连接获取数据
$orders = Db::connection('raw')
2025-11-27 15:03:25 +08:00
->table('wpic_taobao_order')->orderBy('id', 'desc')
2025-12-01 11:12:33 +08:00
->limit(4)->get('order_raw')->lazy();
2025-11-27 15:03:25 +08:00
// dump($orders->first());
// return;
if ($orders->isEmpty()) {
2025-11-26 09:45:30 +08:00
$this->warn('No orders found in wpic_taobao_order table');
2025-11-27 15:03:25 +08:00
return;
2025-11-26 09:45:30 +08:00
}
2025-11-27 15:03:25 +08:00
$this->info(sprintf('Found %d orders, processing...', $orders->count()));
2025-11-26 09:45:30 +08:00
// 获取 Producer 实例
$producer = $this->container->get(Producer::class);
2025-11-27 15:03:25 +08:00
// 每 2 条记录组成一条消息 - 实际生产环境需要增大这个值
// $orders->chunk(2)->each(function($collection) use ($producer) {
2025-11-26 09:45:30 +08:00
$messageCount = 0;
2025-11-27 15:03:25 +08:00
$orders->chunk(2)->each(function (LazyCollection $collection) use ($producer, &$messageCount) {
2025-11-26 09:45:30 +08:00
2025-11-27 15:03:25 +08:00
$order_data = $collection->pluck('order_raw')->map(function ($item) {
return json_decode($item, true);
})->toArray();
//@ATTENTION 生产环境需要注意, 暂时使用 KM 进行测试
2025-11-26 09:45:30 +08:00
$messageData = [
2025-11-27 15:03:25 +08:00
'company_id' => 188,
'platform_id' => 2,
'store_id' => 292,
'unique_id' => uniqid() . '_' . time(),
'raw_data' => $order_data, // 包含 2 条原始记录
2025-11-26 09:45:30 +08:00
];
// 创建并发送消息
$message = new TmallOrderProducer($messageData);
$producer->produce($message);
$messageCount++;
2025-11-27 15:03:25 +08:00
2025-11-26 09:45:30 +08:00
$this->line(sprintf('Sent message %d with order IDs: %s',
$messageCount,
2025-11-27 15:03:25 +08:00
$messageData['unique_id'],
2025-11-26 09:45:30 +08:00
));
2025-11-27 15:03:25 +08:00
});
2025-11-26 09:45:30 +08:00
$this->info(sprintf('Successfully sent %d messages to RabbitMQ', $messageCount));
2025-11-27 15:03:25 +08:00
return;
2025-11-26 09:45:30 +08:00
} catch (Exception $e) {
$this->error('Error pushing messages: ' . $e->getMessage());
$this->error($e->getTraceAsString());
2025-11-27 15:03:25 +08:00
return;
2025-11-26 09:45:30 +08:00
}
}
}