CopyUserBook.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\CustomBookSentence;
  5. use App\Models\Channel;
  6. use App\Models\Sentence;
  7. use Illuminate\Support\Str;
  8. use Illuminate\Support\Facades\Log;
  9. class CopyUserBook extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'copy:user.book {--lang}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '复制用户自定书到sentence表';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. //获取全部语言列表
  40. $lang = CustomBookSentence::select('lang')->groupBy('lang')->get();
  41. foreach ($lang as $key => $value) {
  42. $this->info('language:'.$value->lang);
  43. }
  44. if($this->option('lang')){
  45. return 0;
  46. }
  47. $userBooks = CustomBookSentence::select('book')->groupBy('book')->get();
  48. $this->info('book '. count($userBooks));
  49. foreach ($userBooks as $key => $book) {
  50. $this->info('doing book '. $book->book);
  51. $bookInfo = CustomBookSentence::where('book',$book->book)->first();
  52. $bookLang = $bookInfo->lang;
  53. if(empty($bookLang) || $bookLang === 'false' || $bookLang === 'null'){
  54. $this->error('language can not be empty book='.$book->book);
  55. Log::error('copy:user.book language can not be empty , book='.$book->book);
  56. continue;
  57. }
  58. $channelName = '_user_book_'.$bookInfo->lang;
  59. $channel = Channel::where('owner_uid',$bookInfo->owner)
  60. ->where('name',$channelName)->first();
  61. if($channel === null){
  62. $channel = new Channel;
  63. $channel->id = app('snowflake')->id();
  64. $channel->owner_uid = $bookInfo->owner;
  65. $channel->name = $channelName;
  66. $channel->type = 'original';
  67. $channel->lang = $bookInfo->lang;
  68. $channel->editor_id = 0;
  69. $channel->create_time = time()*1000;
  70. $channel->modify_time = time()*1000;
  71. $channel->status = $bookInfo->status;
  72. $channel->save();
  73. }
  74. $bar = $this->output->createProgressBar(CustomBookSentence::where('book',$book->book)
  75. ->count());
  76. $bookSentence = CustomBookSentence::where('book',$book->book)->cursor();
  77. foreach ($bookSentence as $key => $sentence) {
  78. $bar->advance();
  79. $newRow = Sentence::firstOrNew(
  80. [
  81. "book_id" => $sentence->book,
  82. "paragraph" => $sentence->paragraph,
  83. "word_start" => $sentence->word_start,
  84. "word_end" => $sentence->word_end,
  85. "channel_uid" => $channel->uid,
  86. ],
  87. [
  88. 'id' => app('snowflake')->id(),
  89. 'uid' => Str::uuid(),
  90. 'create_time' => $sentence->create_time,
  91. 'modify_time' => $sentence->modify_time,
  92. ]
  93. );
  94. $newRow->editor_uid = $sentence->owner;
  95. $newRow->content = $sentence->content;
  96. $newRow->strlen = mb_strlen($sentence->content,"UTF-8");
  97. $newRow->status = $sentence->status;
  98. $newRow->content_type = $sentence->content_type;
  99. $newRow->language = $sentence->lang;
  100. $newRow->save();
  101. }
  102. $bar->finish();
  103. $this->info("book {$book->book} finished");
  104. }
  105. return 0;
  106. }
  107. }