InstallWordAll.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\WordList;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. class InstallWordAll extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'install:wordall {from?} {to?}';
  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. $this->info("instert word in palibook ");
  39. Log::info("instert word in palibook ");
  40. $_from = $this->argument('from');
  41. $_to = $this->argument('to');
  42. if(empty($_from) && empty($_to)){
  43. $_from = 1;
  44. $_to = 217;
  45. }else if(empty($_to)){
  46. $_to = $_from;
  47. }
  48. $bar = $this->output->createProgressBar($_to-$_from+1);
  49. for ($book=$_from; $book <= $_to; $book++) {
  50. Log::info("doing ".($book));
  51. DB::transaction(function ()use($book) {
  52. $fileSn = $book-1;
  53. if (($fpoutput = fopen(config("app.path.paliword_book") . "/{$fileSn}_words.csv", "r")) !== false){
  54. #删除目标数据库中数据
  55. WordList::where('book', $book)->delete();
  56. while (($data = fgetcsv($fpoutput, 0, ',')) !== false)
  57. {
  58. $newData = [
  59. 'sn'=>$data[0],
  60. 'book'=>$data[1],
  61. 'paragraph'=>$data[2],
  62. 'wordindex'=>$data[3],
  63. 'bold'=>$data[4],
  64. ];
  65. WordList::create($newData);
  66. }
  67. return 0;
  68. }else{
  69. Log::error("open csv fail");
  70. return 1;
  71. }
  72. });
  73. $bar->advance();
  74. }
  75. $bar->finish();
  76. $msg = "all done in ". time()-$startTime . "s";
  77. $this->info($msg.PHP_EOL);
  78. Log::info($msg);
  79. return 0;
  80. }
  81. }