CopyUserBook.php 4.7 KB

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