CopyUserBook.php 6.1 KB

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