CopyUserBook.php 6.8 KB

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