InstallWordBook.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. if(\App\Tools\Tools::isStop()){
  38. return 0;
  39. }
  40. $startTime = time();
  41. $this->info("instert word in palibook ");
  42. Log::info("instert word in palibook ");
  43. $_from = $this->argument('from');
  44. $_to = $this->argument('to');
  45. if(empty($_from) && empty($_to)){
  46. $_from = 1;
  47. $_to = 217;
  48. }else if(empty($_to)){
  49. $_to = $_from;
  50. }
  51. $bar = $this->output->createProgressBar($_to-$_from+1);
  52. for ($book=$_from; $book <= $_to; $book++) {
  53. Log::info("doing ".($book));
  54. #删除目标数据库中数据
  55. BookWord::where('book', $book)->delete();
  56. //分类汇总得到单词表
  57. $bookword = array();
  58. $fileId = $book-1;
  59. if (($fpoutput = fopen(config("mint.path.paliword_book") . "/{$fileId}_words.csv", "r")) !== false) {
  60. $count = 0;
  61. while (($data = fgetcsv($fpoutput, 0, ',')) !== false) {
  62. $book = $data[1];
  63. if (isset($bookword[$data[3]])) {
  64. $bookword[$data[3]]++;
  65. } else {
  66. $bookword[$data[3]] = 1;
  67. }
  68. $count++;
  69. }
  70. }else{
  71. Log::error("open csv fail");
  72. continue;
  73. }
  74. DB::transaction(function ()use($book,$bookword) {
  75. foreach ($bookword as $key => $value) {
  76. $newData = [
  77. 'book'=>$book,
  78. 'wordindex'=>$key,
  79. 'count'=>$value,
  80. ];
  81. BookWord::create($newData);
  82. }
  83. });
  84. $bar->advance();
  85. }
  86. $bar->finish();
  87. $msg = "all done in ". time()-$startTime . "s";
  88. $this->info($msg.PHP_EOL);
  89. Log::info($msg);
  90. return 0;
  91. }
  92. }