Files
links/application/admin/command/SplitSyncTickets.php
T

89 lines
3.1 KiB
PHP
Raw Normal View History

2026-06-09 03:36:30 +08:00
<?php
declare(strict_types=1);
namespace app\admin\command;
2026-06-20 04:47:34 +08:00
use app\common\service\SplitCronLockService;
use app\common\service\SplitTicketSyncLogger;
2026-06-09 03:36:30 +08:00
use app\common\service\SplitTicketSyncService;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
/**
* 工单云控定时同步 CLI
*
* 用法:
* php think split:sync-tickets
* php think split:sync-tickets --ticket=12
* php think split:sync-tickets --ticket=12 --mode=auto
2026-06-09 03:36:30 +08:00
*/
class SplitSyncTickets extends Command
{
protected function configure(): void
{
$this->setName('split:sync-tickets')
->addOption('ticket', 't', Option::VALUE_OPTIONAL, '指定工单 ID(强制同步,忽略周期)', '')
->addOption('mode', 'm', Option::VALUE_OPTIONAL, '同步方式: auto=定时自动, manual=手动触发', 'manual')
2026-06-09 03:36:30 +08:00
->setDescription('同步分流工单云控数据');
}
protected function execute(Input $input, Output $output): void
{
set_time_limit(0);
$syncMode = $this->resolveSyncMode($input);
SplitTicketSyncLogger::log('cli', 'command start', [
'ticketOption' => trim((string) $input->getOption('ticket')),
'syncMode' => $syncMode,
'appDebug' => SplitTicketSyncLogger::isEnabled(),
]);
2026-06-09 03:36:30 +08:00
$service = new SplitTicketSyncService();
$ticketId = trim((string) $input->getOption('ticket'));
if ($ticketId !== '' && ctype_digit($ticketId)) {
$result = $service->syncOne((int) $ticketId, true, false, $syncMode);
2026-06-09 03:36:30 +08:00
if (!empty($result['skipped'])) {
$output->writeln('<comment>跳过: ' . ($result['message'] ?? '') . '</comment>');
return;
}
if ($result['success']) {
$output->writeln('<info>工单 #' . $ticketId . ' 同步成功</info>');
} else {
$output->writeln('<error>工单 #' . $ticketId . ' 同步失败: ' . ($result['message'] ?? '') . '</error>');
}
return;
}
2026-06-20 04:47:34 +08:00
$cronLock = new SplitCronLockService();
if (!$cronLock->acquire()) {
SplitTicketSyncLogger::log('cli', 'cron lock busy, skip');
$output->writeln('<comment>上一轮同步仍在执行,跳过本次</comment>');
return;
}
try {
$count = $service->syncDueTickets();
$output->writeln('<info>本次投递工单数: ' . $count . '</info>');
$output->writeln('<comment>同步在后台 CLI 进程中执行,汇总日志见 runtime/log/split_sync.log</comment>');
2026-06-20 04:47:34 +08:00
} finally {
$cronLock->release();
}
if (SplitTicketSyncLogger::isEnabled()) {
$output->writeln('<comment>详细调试日志已写入 runtime/log/split_sync.log</comment>');
}
2026-06-09 03:36:30 +08:00
}
/**
* 解析 CLI 同步方式,供 sync_success_mode 落库
*/
private function resolveSyncMode(Input $input): string
{
$mode = strtolower(trim((string) $input->getOption('mode')));
return in_array($mode, ['auto', 'manual'], true) ? $mode : 'manual';
}
2026-06-09 03:36:30 +08:00
}