2
0

InstallWordStatistics.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. if(\App\Tools\Tools::isStop()){
  38. return 0;
  39. }
  40. $startTime = time();
  41. $info = "instert wordstatistics ";
  42. $this->info($info.PHP_EOL);
  43. Log::info($info);
  44. #删除目标数据库中数据
  45. WordStatistic::where('id', '>',-1)->delete();
  46. $scan = scandir(config("mint.path.word_statistics"));
  47. $bar = $this->output->createProgressBar(count($scan));
  48. foreach($scan as $filename) {
  49. $bar->advance();
  50. $filename = config("mint.path.word_statistics")."/".$filename;
  51. if (is_file($filename)) {
  52. Log::info("doing ".$filename);
  53. DB::transaction(function ()use($filename) {
  54. if (($fpoutput = fopen($filename, "r")) !== false) {
  55. $count = 0;
  56. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  57. $newData = [
  58. 'bookid'=>$data[0],
  59. 'word'=>$data[1],
  60. 'count'=>$data[2],
  61. 'base'=>$data[3],
  62. 'end1'=>$data[4],
  63. 'end2'=>$data[5],
  64. 'type'=>$data[6],
  65. 'length'=>$data[7],
  66. ];
  67. WordStatistic::create($newData);
  68. $count++;
  69. }
  70. Log::info("insert ".$count);
  71. }
  72. });
  73. }
  74. }
  75. $bar->finish();
  76. $msg = "all done in ". time()-$startTime . "s";
  77. Log::info($msg);
  78. $this->info($msg);
  79. return 0;
  80. return 0;
  81. }
  82. }