CopyUserBook.php 6.9 KB

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