InstallWordAll.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. if(\App\Tools\Tools::isStop()){
  38. return 0;
  39. }
  40. $startTime = time();
  41. $this->info("instert word in palibook ");
  42. Log::info("instert word in palibook ");
  43. $_from = $this->argument('from');
  44. $_to = $this->argument('to');
  45. if(empty($_from) && empty($_to)){
  46. $_from = 1;
  47. $_to = 217;
  48. }else if(empty($_to)){
  49. $_to = $_from;
  50. }
  51. $bar = $this->output->createProgressBar($_to-$_from+1);
  52. for ($book=$_from; $book <= $_to; $book++) {
  53. Log::info("doing ".($book));
  54. DB::transaction(function ()use($book) {
  55. $fileSn = $book-1;
  56. if (($fpoutput = fopen(config("mint.path.paliword_book") . "/{$fileSn}_words.csv", "r")) !== false){
  57. #删除目标数据库中数据
  58. WordList::where('book', $book)->delete();
  59. while (($data = fgetcsv($fpoutput, 0, ',')) !== false)
  60. {
  61. $newData = [
  62. 'sn'=>$data[0],
  63. 'book'=>$data[1],
  64. 'paragraph'=>$data[2],
  65. 'wordindex'=>$data[3],
  66. 'bold'=>$data[4],
  67. ];
  68. WordList::create($newData);
  69. }
  70. return 0;
  71. }else{
  72. Log::error("open csv fail");
  73. return 1;
  74. }
  75. });
  76. $bar->advance();
  77. }
  78. $bar->finish();
  79. $msg = "all done in ". time()-$startTime . "s";
  80. $this->info($msg.PHP_EOL);
  81. Log::info($msg);
  82. return 0;
  83. }
  84. }