ImportPromptTemplates.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\PromptService;
  4. use Illuminate\Console\Command;
  5. class ImportPromptTemplates extends Command
  6. {
  7. protected $signature = 'prompt:import {file : 导出的提示词 JSON 文件路径}';
  8. protected $description = '从导出的 JSON 文件导入提示词到 MySQL';
  9. public function handle(): int
  10. {
  11. $path = (string) $this->argument('file');
  12. if (!is_file($path)) {
  13. $this->error('文件不存在: ' . $path);
  14. return self::FAILURE;
  15. }
  16. $content = file_get_contents($path);
  17. $payload = json_decode($content ?: '[]', true);
  18. if (!is_array($payload)) {
  19. $this->error('JSON 解析失败');
  20. return self::FAILURE;
  21. }
  22. $result = app(PromptService::class)->importFromArray($payload);
  23. $this->info(sprintf('导入 %d,更新 %d', $result['imported'] ?? 0, $result['updated'] ?? 0));
  24. if (!empty($result['errors'])) {
  25. $this->warn('部分失败:' . implode(';', $result['errors']));
  26. }
  27. return self::SUCCESS;
  28. }
  29. }