2
0

InstallWordIndex.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\WordIndex;
  4. use App\Tools\Tools;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. class InstallWordIndex extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'install:wordindex';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. if (Tools::isStop()) {
  39. return 0;
  40. }
  41. $startTime = time();
  42. $info = 'instert word in palibook ';
  43. $this->info($info);
  44. Log::info($info);
  45. // 删除目标数据库中数据
  46. WordIndex::where('id', '>', -1)->delete();
  47. $scan = scandir(config('mint.path.paliword_index'));
  48. $bar = $this->output->createProgressBar(count($scan));
  49. foreach ($scan as $filename) {
  50. $bar->advance();
  51. $filename = config('mint.path.paliword_index').'/'.$filename;
  52. if (is_file($filename)) {
  53. Log::info('doing '.$filename);
  54. DB::transaction(function () use ($filename) {
  55. if (($fpoutput = fopen($filename, 'r')) !== false) {
  56. $count = 0;
  57. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  58. $newData = [
  59. 'id' => $data[0],
  60. 'word' => $data[1],
  61. 'word_en' => $data[2],
  62. 'normal' => $data[3],
  63. 'bold' => $data[4],
  64. 'is_base' => $data[5],
  65. 'len' => $data[6],
  66. ];
  67. WordIndex::create($newData);
  68. $count++;
  69. }
  70. Log::info('insert '.$count);
  71. }
  72. });
  73. }
  74. }
  75. $bar->finish();
  76. $msg = 'all done in '.time() - $startTime.'s';
  77. Log::info($msg);
  78. $this->info($msg);
  79. return 0;
  80. }
  81. }