InstallWordIndex.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. $startTime = time();
  38. $info = "instert word in palibook ";
  39. $this->info($info);
  40. Log::info($info);
  41. #删除目标数据库中数据
  42. WordIndex::where('id', '>',-1)->delete();
  43. $scan = scandir(config("app.path.paliword_index"));
  44. $bar = $this->output->createProgressBar(count($scan));
  45. foreach($scan as $filename) {
  46. $bar->advance();
  47. $filename = config("app.path.paliword_index")."/".$filename;
  48. if (is_file($filename)) {
  49. Log::info("doing ".$filename);
  50. DB::transaction(function ()use($filename) {
  51. if (($fpoutput = fopen($filename, "r")) !== false) {
  52. $count = 0;
  53. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  54. $newData = [
  55. 'id'=>$data[0],
  56. 'word'=>$data[1],
  57. 'word_en'=>$data[2],
  58. 'normal'=>$data[3],
  59. 'bold'=>$data[4],
  60. 'is_base'=>$data[5],
  61. 'len'=>$data[6],
  62. ];
  63. WordIndex::create($newData);
  64. $count++;
  65. }
  66. Log::info("insert ".$count);
  67. }
  68. });
  69. }
  70. }
  71. $bar->finish();
  72. $msg = "all done in ". time()-$startTime . "s";
  73. Log::info($msg);
  74. $this->info($msg);
  75. return 0;
  76. }
  77. }