InstallWordIndex.php 1.9 KB

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