UpdateTaskCommand.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\DrugReportInfo;
  4. use App\Services\SyncDrugReportService;
  5. use Carbon\Carbon;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. class UpdateTaskCommand extends Command
  9. {
  10. protected $signature = 'update-task';
  11. protected $description = '批量更新药检报告域名';
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. }
  16. public function handle()
  17. {
  18. $oldDomain = 'https://drugreport.oss-cn-shenzhen.aliyuncs.com';
  19. $newDomain = 'https://file.snowkirin.com.cn';
  20. $batchSize = 5000; // 每批 5000 条
  21. $totalUpdated = 0;
  22. // 先获取符合条件的总记录数
  23. $totalCount = DB::table('drug_report_ass')
  24. ->where('report_url', 'like', "{$oldDomain}%")
  25. ->count();
  26. var_dump($totalCount);exit();
  27. if ($totalCount === 0) {
  28. $this->info('没有需要更新的记录');
  29. return 0;
  30. }
  31. $this->info("共发现 {$totalCount} 条需要更新的记录");
  32. $this->info("开始分批更新,每批 {$batchSize} 条...");
  33. // 创建进度条
  34. $bar = $this->output->createProgressBar($totalCount);
  35. $bar->start();
  36. try {
  37. // 使用分块更新,避免一次性锁表
  38. do {
  39. $affected = DB::table('drug_report_ass')
  40. ->where('report_url', 'like', "{$oldDomain}%")
  41. ->limit($batchSize)
  42. ->update([
  43. 'report_url' => DB::raw(
  44. "REPLACE(report_url, '{$oldDomain}', '{$newDomain}')"
  45. )
  46. ]);
  47. $totalUpdated += $affected;
  48. $bar->advance($affected);
  49. // 可选:每批之间短暂休眠,减轻数据库压力
  50. // usleep(100000); // 0.1 秒
  51. } while ($affected > 0);
  52. $bar->finish();
  53. $this->newLine();
  54. $this->info("✅ 更新完成,共处理 {$totalUpdated} 条记录");
  55. } catch (\Exception $e) {
  56. $bar->finish();
  57. $this->error("❌ 更新失败:{$e->getMessage()}");
  58. return 1;
  59. }
  60. return 0;
  61. }
  62. }