2
0

InstallWordStatistics.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\WordStatistic;
  4. use App\Tools\Tools;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. class InstallWordStatistics extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'install:wordstatistics';
  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. $info = 'instert wordstatistics ';
  43. $this->info($info.PHP_EOL);
  44. Log::info($info);
  45. // 删除目标数据库中数据
  46. WordStatistic::where('id', '>', -1)->delete();
  47. $scan = scandir(config('mint.path.word_statistics'));
  48. $bar = $this->output->createProgressBar(count($scan));
  49. foreach ($scan as $filename) {
  50. $bar->advance();
  51. $filename = config('mint.path.word_statistics').'/'.$filename;
  52. if (is_file($filename)) {
  53. Log::info('doing '.$filename);
  54. DB::transaction(function () use ($filename) {
  55. if (($fpoutput = fopen($filename, 'r')) !== false) {
  56. $count = 0;
  57. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  58. $newData = [
  59. 'bookid' => $data[0],
  60. 'word' => $data[1],
  61. 'count' => $data[2],
  62. 'base' => $data[3],
  63. 'end1' => $data[4],
  64. 'end2' => $data[5],
  65. 'type' => $data[6],
  66. 'length' => $data[7],
  67. ];
  68. WordStatistic::create($newData);
  69. $count++;
  70. }
  71. Log::info('insert '.$count);
  72. }
  73. });
  74. }
  75. }
  76. $bar->finish();
  77. $msg = 'all done in '.time() - $startTime.'s';
  78. Log::info($msg);
  79. $this->info($msg);
  80. return 0;
  81. return 0;
  82. }
  83. }