2
0

InstallWordAll.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\WordList;
  4. use App\Tools\Tools;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. class InstallWordAll extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'install:wordall {from?} {to?}';
  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. $this->info('instert word in palibook ');
  43. Log::info('instert word in palibook ');
  44. $_from = $this->argument('from');
  45. $_to = $this->argument('to');
  46. if (empty($_from) && empty($_to)) {
  47. $_from = 1;
  48. $_to = 217;
  49. } elseif (empty($_to)) {
  50. $_to = $_from;
  51. }
  52. $bar = $this->output->createProgressBar($_to - $_from + 1);
  53. for ($book = $_from; $book <= $_to; $book++) {
  54. Log::info('doing '.($book));
  55. DB::transaction(function () use ($book) {
  56. $fileSn = $book - 1;
  57. if (($fpoutput = fopen(config('mint.path.paliword_book')."/{$fileSn}_words.csv", 'r')) !== false) {
  58. // 删除目标数据库中数据
  59. WordList::where('book', $book)->delete();
  60. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  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. }