argument('file'); if (! is_string($path) || ! File::isFile($path)) { $this->error('文件不存在:'.$path); return self::FAILURE; } try { $rawRows = $service->loadRowsFromFile($path); } catch (\Throwable $e) { $this->error($e->getMessage()); return self::FAILURE; } if ($rawRows === []) { $this->warn('未解析到任何题目行。'); return self::FAILURE; } $rows = []; foreach ($rawRows as $i => $raw) { if (! is_array($raw)) { continue; } $rows[] = $service->normalizeImportRow($raw, $i + 1); } $includeId = (bool) $this->option('with-id'); $sql = $service->renderMysqlScript($rows, $includeId); $outPath = $this->option('output'); if (! is_string($outPath) || $outPath === '') { $outPath = storage_path('app/exports/questions_import_'.date('YmdHis').'.sql'); } File::ensureDirectoryExists(dirname($outPath)); File::put($outPath, $sql); $this->info('SQL 已写入:'.$outPath); $dryRun = (bool) $this->option('dry-run'); $sqlOnly = (bool) $this->option('sql-only'); if ($sqlOnly) { $this->line('已使用 --sql-only,未写入本地 questions 表。'); return self::SUCCESS; } $result = $service->importToDatabase($rows, $dryRun); $this->info(sprintf( '本地库:新建 %d 条,更新 %d 条,跳过 %d 条(dry-run=%s)', $result['created'], $result['updated'], $result['skipped'], $dryRun ? 'true' : 'false' )); foreach ($result['errors'] as $err) { $this->warn($err); } return self::SUCCESS; } }