CopyUserBook.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\CustomBookSentence;
  5. use App\Models\CustomBook;
  6. use App\Models\Channel;
  7. use App\Models\Sentence;
  8. use Illuminate\Support\Str;
  9. use Illuminate\Support\Facades\Log;
  10. class CopyUserBook extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. * php artisan copy:user.book
  15. * @var string
  16. */
  17. protected $signature = 'copy:user.book {--lang} {--book=}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '复制用户自定书到sentence表';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. //获取全部语言列表
  41. $lang = CustomBookSentence::select('lang')->groupBy('lang')->get();
  42. foreach ($lang as $key => $value) {
  43. $this->info('language:'.$value->lang);
  44. }
  45. if($this->option('lang')){
  46. return 0;
  47. }
  48. $this->info('给CustomBook 添加channel');
  49. foreach (CustomBook::get() as $key => $customBook) {
  50. $this->info('doing book='.$customBook->book_id);
  51. if(empty($customBook->channel_id)){
  52. $bookLang = $customBook->lang;
  53. if(empty($bookLang) || $bookLang === 'false' || $bookLang === 'null'){
  54. $this->info('language can not be empty change to pa, book='.$customBook->book_id);
  55. Log::warning('copy:user.book language can not be empty ,change to pa, book='.$customBook->book_id);
  56. $bookLang = 'pa';
  57. }
  58. $customBook->lang = $bookLang;
  59. $channelName = '_user_book_'.$bookLang;
  60. $channel = Channel::where('owner_uid',$customBook->owner)
  61. ->where('name',$channelName)->first();
  62. if($channel === null){
  63. $this->info('create new channel');
  64. $channelUuid = Str::uuid();
  65. $channel = new Channel;
  66. $channel->id = app('snowflake')->id();
  67. $channel->uid = $channelUuid;
  68. $channel->owner_uid = $customBook->owner;
  69. $channel->name = $channelName;
  70. $channel->type = 'original';
  71. $channel->lang = $bookLang;
  72. $channel->editor_id = 0;
  73. $channel->create_time = time()*1000;
  74. $channel->modify_time = time()*1000;
  75. $channel->status = $customBook->status;
  76. $saveOk = $channel->save();
  77. if($saveOk){
  78. Log::debug('copy user book : create channel success name='.$channelName);
  79. }else{
  80. Log::error('copy user book : create channel fail.',['channel'=>$channelName,'book'=>$book->book]);
  81. $this->error('copy user book : create channel fail. name='.$channelName);
  82. continue;
  83. }
  84. }
  85. if(!Str::isUuid($channel->uid)){
  86. Log::error('copy user book : channel id error.',['channel'=>$channelName,'book'=>$book->book]);
  87. $this->error('copy user book : channel id error. name='.$channelName);
  88. continue;
  89. }
  90. $customBook->channel_id = $channel->uid;
  91. $customBook->save();
  92. }
  93. }
  94. $this->info('给CustomBook 添加channel 结束');
  95. $userBooks = CustomBook::get();
  96. $this->info('book '. count($userBooks));
  97. foreach ($userBooks as $key => $book) {
  98. $queryBook = $this->option('book');
  99. if(!empty($queryBook)){
  100. if($book->book_id != $queryBook){
  101. continue;
  102. }
  103. }
  104. if(empty($book->channel_id)){
  105. $this->error('book channel is empty');
  106. continue;
  107. }
  108. $this->info('doing book '. $book->book_id);
  109. $bar = $this->output->createProgressBar(CustomBookSentence::where('book',$book->book_id)
  110. ->count());
  111. $bookSentence = CustomBookSentence::where('book',$book->book_id)->cursor();
  112. foreach ($bookSentence as $key => $sentence) {
  113. $bar->advance();
  114. $newRow = Sentence::firstOrNew(
  115. [
  116. "book_id" => $sentence->book,
  117. "paragraph" => $sentence->paragraph,
  118. "word_start" => $sentence->word_start,
  119. "word_end" => $sentence->word_end,
  120. "channel_uid" => $book->channel_id,
  121. ],
  122. [
  123. 'id' => app('snowflake')->id(),
  124. 'uid' => Str::uuid(),
  125. 'create_time' => $sentence->create_time,
  126. 'modify_time' => $sentence->modify_time,
  127. ]
  128. );
  129. $newRow->editor_uid = $sentence->owner;
  130. $newRow->content = $sentence->content;
  131. $newRow->strlen = mb_strlen($sentence->content,"UTF-8");
  132. $newRow->status = $sentence->status;
  133. $newRow->content_type = $sentence->content_type;
  134. $newRow->language = $book->lang;
  135. if(empty($newRow->channel_uid)){
  136. $this->error('channel uuid is null book='.$sentence->book .' para='.$sentence->paragraph);
  137. Log::error('channel uuid is null ',['sentence'=>$sentence->book]);
  138. }else{
  139. $newRow->save();
  140. }
  141. }
  142. $bar->finish();
  143. $this->info("book {$book->book} finished");
  144. }
  145. $this->info('all done');
  146. return 0;
  147. }
  148. }