CopyUserBook.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. *
  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. $userBooks = CustomBookSentence::select('book')->groupBy('book')->get();
  49. $this->info('book '. count($userBooks));
  50. foreach ($userBooks as $key => $book) {
  51. $queryBook = $this->option('book');
  52. if(!empty($queryBook)){
  53. if($book->book != $queryBook){
  54. continue;
  55. }
  56. }
  57. $this->info('doing book '. $book->book);
  58. $bookInfo = CustomBookSentence::where('book',$book->book)->first();
  59. $bookLang = $bookInfo->lang;
  60. //|| $bookLang === 'false' || $bookLang === 'null'
  61. if(empty($bookLang) || $bookLang === 'false' || $bookLang === 'null'){
  62. $this->info('language can not be empty change to pa, book='.$book->book);
  63. Log::warning('copy:user.book language can not be empty ,change to pa, book='.$book->book);
  64. $bookLang = 'pa';
  65. }
  66. $channelName = '_user_book_'.$bookLang;
  67. $channel = Channel::where('owner_uid',$bookInfo->owner)
  68. ->where('name',$channelName)->first();
  69. if($channel === null){
  70. $channelUuid = Str::uuid();
  71. $channel = new Channel;
  72. $channel->id = app('snowflake')->id();
  73. $channel->uid = $channelUuid;
  74. $channel->owner_uid = $bookInfo->owner;
  75. $channel->name = $channelName;
  76. $channel->type = 'original';
  77. $channel->lang = $bookLang;
  78. $channel->editor_id = 0;
  79. $channel->create_time = time()*1000;
  80. $channel->modify_time = time()*1000;
  81. $channel->status = $bookInfo->status;
  82. $saveOk = $channel->save();
  83. if($saveOk){
  84. Log::debug('copy user book : create channel success name='.$channelName);
  85. }else{
  86. Log::error('copy user book : create channel fail.',['channel'=>$channelName,'book'=>$book->book]);
  87. $this->error('copy user book : create channel fail. name='.$channelName);
  88. continue;
  89. }
  90. }
  91. if(!Str::isUuid($channel->uid)){
  92. Log::error('copy user book : channel id error.',['channel'=>$channelName,'book'=>$book->book]);
  93. $this->error('copy user book : channel id error. name='.$channelName);
  94. continue;
  95. }
  96. CustomBook::where('book_id',$book->book)->update(['channel_id'=>$channel->uid]);
  97. $bar = $this->output->createProgressBar(CustomBookSentence::where('book',$book->book)
  98. ->count());
  99. $bookSentence = CustomBookSentence::where('book',$book->book)->cursor();
  100. foreach ($bookSentence as $key => $sentence) {
  101. $bar->advance();
  102. $newRow = Sentence::firstOrNew(
  103. [
  104. "book_id" => $sentence->book,
  105. "paragraph" => $sentence->paragraph,
  106. "word_start" => $sentence->word_start,
  107. "word_end" => $sentence->word_end,
  108. "channel_uid" => $channel->uid,
  109. ],
  110. [
  111. 'id' => app('snowflake')->id(),
  112. 'uid' => Str::uuid(),
  113. 'create_time' => $sentence->create_time,
  114. 'modify_time' => $sentence->modify_time,
  115. ]
  116. );
  117. $newRow->editor_uid = $sentence->owner;
  118. $newRow->content = $sentence->content;
  119. $newRow->strlen = mb_strlen($sentence->content,"UTF-8");
  120. $newRow->status = $sentence->status;
  121. $newRow->content_type = $sentence->content_type;
  122. $newRow->language = $channel->lang;
  123. if(empty($newRow->channel_uid)){
  124. $this->error('channel uuid is null book='.$sentence->book .' para='.$sentence->paragraph);
  125. Log::error('channel uuid is null ',['sentence'=>$sentence->book]);
  126. }else{
  127. $newRow->save();
  128. }
  129. }
  130. $bar->finish();
  131. $this->info("book {$book->book} finished");
  132. }
  133. return 0;
  134. }
  135. }