InstallWordBook.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\BookWord;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. class InstallWordBook extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'install:wordbook {from?} {to?}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'install palibook word list in each book';
  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. $this->info("instert word in palibook ");
  39. Log::info("instert word in palibook ");
  40. $_from = $this->argument('from');
  41. $_to = $this->argument('to');
  42. if(empty($_from) && empty($_to)){
  43. $_from = 1;
  44. $_to = 217;
  45. }else if(empty($_to)){
  46. $_to = $_from;
  47. }
  48. $bar = $this->output->createProgressBar($_to-$_from+1);
  49. for ($book=$_from; $book <= $_to; $book++) {
  50. Log::info("doing ".($book));
  51. #删除目标数据库中数据
  52. BookWord::where('book', $book)->delete();
  53. //分类汇总得到单词表
  54. $bookword = array();
  55. $fileId = $book-1;
  56. if (($fpoutput = fopen(config("app.path.paliword_book") . "/{$fileId}_words.csv", "r")) !== false) {
  57. $count = 0;
  58. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  59. $book = $data[1];
  60. if (isset($bookword[$data[3]])) {
  61. $bookword[$data[3]]++;
  62. } else {
  63. $bookword[$data[3]] = 1;
  64. }
  65. $count++;
  66. }
  67. }else{
  68. Log::error("open csv fail");
  69. continue;
  70. }
  71. DB::transaction(function ()use($book,$bookword) {
  72. foreach ($bookword as $key => $value) {
  73. $newData = [
  74. 'book'=>$book,
  75. 'wordindex'=>$key,
  76. 'count'=>$value,
  77. ];
  78. BookWord::create($newData);
  79. }
  80. });
  81. $bar->advance();
  82. }
  83. $bar->finish();
  84. $msg = "all done in ". time()-$startTime . "s";
  85. $this->info($msg.PHP_EOL);
  86. Log::info($msg);
  87. return 0;
  88. }
  89. }