InstallWordStatistics.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\WordStatistic;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. class InstallWordStatistics extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'install:wordstatistics';
  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 wordstatistics ";
  39. $this->info($info.PHP_EOL);
  40. Log::info($info);
  41. #删除目标数据库中数据
  42. WordStatistic::where('id', '>',-1)->delete();
  43. $scan = scandir(config("app.path.word_statistics"));
  44. $bar = $this->output->createProgressBar(count($scan));
  45. foreach($scan as $filename) {
  46. $bar->advance();
  47. $filename = config("app.path.word_statistics")."/".$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. 'bookid'=>$data[0],
  56. 'word'=>$data[1],
  57. 'count'=>$data[2],
  58. 'base'=>$data[3],
  59. 'end1'=>$data[4],
  60. 'end2'=>$data[5],
  61. 'type'=>$data[6],
  62. 'length'=>$data[7],
  63. ];
  64. WordStatistic::create($newData);
  65. $count++;
  66. }
  67. Log::info("insert ".$count);
  68. }
  69. });
  70. }
  71. }
  72. $bar->finish();
  73. $msg = "all done in ". time()-$startTime . "s";
  74. Log::info($msg);
  75. $this->info($msg);
  76. return 0;
  77. return 0;
  78. }
  79. }