Files

155 lines
5.2 KiB
PHP
Raw Permalink 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;
2026-02-24 15:52:30 +08:00
use App\Platform\Tmall\Producer\TmallProductProducer;
use App\Platform\Tmall\Producer\TmallRefundProducer;
use App\Platform\Tmall\Producer\TmallInventoryProducer;
2025-11-26 09:45:30 +08:00
use Hyperf\Amqp\Producer;
2026-02-24 15:52:30 +08:00
use Symfony\Component\Console\Input\InputOption;
2025-11-26 09:45:30 +08:00
use Exception;
#[Command]
class AppMessageQueuePushTmall extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
parent::__construct('app:mq-push:tmall');
}
2026-02-24 15:52:30 +08:00
2025-11-26 09:45:30 +08:00
public function configure()
{
parent::configure();
$this->setDescription('Test push message with Tmall km data');
2026-02-24 15:52:30 +08:00
$this->addOption(
'queue-type',
't',
InputOption::VALUE_REQUIRED,
'Queue type: product, order, refund, inventory'
);
2026-02-25 14:32:09 +08:00
$this->addOption(
'ids',
null,
InputOption::VALUE_REQUIRED,
'Comma-separated primary key IDs to fetch specific records'
);
2026-02-24 15:52:30 +08:00
}
/**
* 获取队列类型配置
*/
protected function getQueueTypeConfig(string $type): array
{
$configs = [
'order' => [
'table' => 'wpic_taobao_order',
'column' => 'order_raw',
'producer' => TmallOrderProducer::class,
2026-02-25 14:32:09 +08:00
'primary' => 'tid'
2026-02-24 15:52:30 +08:00
],
'product' => [
'table' => 'wpic_taobao_item',
'column' => 'item_raw',
'producer' => TmallProductProducer::class,
2026-02-25 14:32:09 +08:00
'primary' => 'iid'
2026-02-24 15:52:30 +08:00
],
'refund' => [
'table' => 'wpic_taobao_return_item',
'column' => 'refund_raw',
'producer' => TmallRefundProducer::class,
2026-02-25 14:32:09 +08:00
'primary' => 'refund_id'
2026-02-24 15:52:30 +08:00
]
];
return $configs[$type] ?? throw new Exception("Invalid queue type: {$type}");
2025-11-26 09:45:30 +08:00
}
2026-02-24 15:52:30 +08:00
2025-11-27 15:03:25 +08:00
public function handle(): void
2025-11-26 09:45:30 +08:00
{
try {
2026-02-24 15:52:30 +08:00
$queueType = $this->input->getOption('queue-type');
$validTypes = ['product', 'order', 'refund', 'inventory'];
if (empty($queueType) || !in_array($queueType, $validTypes)) {
$this->error(sprintf('--queue-type is required. Must be one of: %s', implode(', ', $validTypes)));
return;
}
$config = $this->getQueueTypeConfig($queueType);
$this->info(sprintf('Processing queue type: %s, table: %s', $queueType, $config['table']));
2025-11-26 09:45:30 +08:00
// 从 raw 数据库连接获取数据
2026-02-25 14:32:09 +08:00
$query = Db::connection('raw')->table($config['table']);
$idsOption = $this->input->getOption('ids');
if (!empty($idsOption)) {
$ids = array_map('trim', explode(',', $idsOption));
$ids = array_unique(array_filter($ids, fn($id) => $id !== ''));
$query->whereIn('id', $ids);
$this->info(sprintf('Fetching records by %s: %s', $config['primary'], implode(', ', $ids)));
} else {
$query->orderBy('id', 'desc')->limit(4);
}
$records = $query->get([$config['column']])->lazy();
2026-02-24 15:52:30 +08:00
if ($records->isEmpty()) {
$this->warn(sprintf('No records found in %s table', $config['table']));
2025-11-27 15:03:25 +08:00
return;
2025-11-26 09:45:30 +08:00
}
2026-02-24 15:52:30 +08:00
$this->info(sprintf('Found %d records, processing...', $records->count()));
2025-11-26 09:45:30 +08:00
// 获取 Producer 实例
$producer = $this->container->get(Producer::class);
2026-02-24 15:52:30 +08:00
$producerClass = $config['producer'];
2025-11-26 09:45:30 +08:00
$messageCount = 0;
2026-02-24 15:52:30 +08:00
$records->chunk(2)->each(function (LazyCollection $collection) use ($producer, $producerClass, $config, &$messageCount) {
$raw_data = $collection->pluck($config['column'])->map(function ($item) {
2025-11-27 15:03:25 +08:00
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,
2026-02-24 15:52:30 +08:00
'unique_id' => time() . '_' . uniqid(),
'raw_data' => $raw_data, // 包含 2 条原始记录
2025-11-26 09:45:30 +08:00
];
// 创建并发送消息
2026-02-24 15:52:30 +08:00
$message = new $producerClass($messageData);
2025-11-26 09:45:30 +08:00
$producer->produce($message);
$messageCount++;
2025-11-27 15:03:25 +08:00
2026-02-24 15:52:30 +08:00
$this->line(sprintf('Sent message %d with unique ID: %s',
2025-11-26 09:45:30 +08:00
$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
});
2026-02-24 15:52:30 +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;
2026-02-24 15:52:30 +08:00
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
}
}
}