updateFromNewYunqi.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Modules\Book\Models\Chapter;
  5. use App\Modules\Book\Models\Book;
  6. use DB;
  7. use Log;
  8. class updateFromNewYunqi extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'updateyunqi {--bid=}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $bid = $this->option('bid');
  39. if($bid){
  40. $book = $this->getAllBid($bid);
  41. }else{
  42. $book = $this->getAllBid();
  43. }
  44. if($book){
  45. //Log::info($book);
  46. foreach ($book as $item){
  47. $this->updateOne($item->id,$item->chapter_count,$item->yq_bid,$item->name);
  48. }
  49. }
  50. }
  51. private function getAllBid($bid=0){
  52. if($bid){
  53. return DB::table('book_yunqi')->join('books','book_yunqi.bid','=','books.id')
  54. ->where('book_yunqi.type','NEW_YUNQI')
  55. ->where('books.status',0)
  56. ->where('books.id',$bid)
  57. ->select('books.id','books.name','book_yunqi.yq_bid','books.chapter_count','books.last_cid')
  58. ->get();
  59. }
  60. return DB::table('book_yunqi')->join('books','book_yunqi.bid','=','books.id')
  61. ->where('book_yunqi.type','NEW_YUNQI')
  62. ->where('books.status',0)
  63. ->select('books.id','books.name','book_yunqi.yq_bid','books.chapter_count','books.last_cid')
  64. ->get();
  65. }
  66. private function updateOne($zsy_bid,$zsy_count,$yq_bid,$name)
  67. {
  68. $yunqi_book_info = $this->yQbookInfo($yq_bid);
  69. if(!$yunqi_book_info) {
  70. Log::info( '$yq_bid is :'.$yq_bid.', not exists' );
  71. return ;
  72. }
  73. //Log::info($yunqi_book_info);
  74. if($yunqi_book_info->chapter_count <=$zsy_count) return ;
  75. $update_count = 0;
  76. $update_size = 0;
  77. for ($i = $zsy_count+1;$i<=$yunqi_book_info->chapter_count;$i++){
  78. $temp = DB::connection('new_yunqi')->table('chapters')->where('bid',$yq_bid)->where('sequence',$i)->select('name','content','is_vip','size')->first();
  79. if($temp){
  80. $update_count++;
  81. $update_size += $temp->size;
  82. Chapter::create([
  83. 'bid'=>$zsy_bid,
  84. 'name'=>$temp->name,
  85. 'sequence'=>$i,
  86. 'is_vip'=>$temp->is_vip,
  87. 'size'=>$temp->size,
  88. 'prev_cid'=>0,
  89. 'next_cid'=>0,
  90. 'recent_update_at'=>date('Y-m-d H:i:s'),
  91. 'content'=>$temp->content,
  92. 'ly_chapter_id'=>0
  93. ]);
  94. }
  95. }
  96. if($update_count >0){
  97. $this->recordUpdateInfo($zsy_bid,[
  98. 'book_name' => $name,
  99. 'channel_name' =>'女频',
  100. 'update_chapter_count' => $update_count,
  101. 'update_words' => $update_size,
  102. 'update_type'=>'add_chapter'
  103. ]);
  104. \Artisan::call('book:adjustone',['--bid'=>$zsy_bid]);
  105. $info = Chapter::join('books','books.id','=','chapters.bid')
  106. ->where('books.id',$zsy_bid)
  107. ->select('chapters.id','chapters.name','chapters.sequence','books.size')
  108. ->orderBy('chapters.sequence','desc')
  109. ->first();
  110. Book::where('id',$zsy_bid)->update([
  111. 'status'=>$yunqi_book_info->status,
  112. 'last_chapter'=>$info->name,
  113. 'size'=>$info->size+$update_size,
  114. 'last_cid'=>$info->id,
  115. 'chapter_count'=>$info->sequence
  116. ]);
  117. }
  118. }
  119. private function yQbookInfo($bid){
  120. $new_yunqi_book = DB::connection('new_yunqi')
  121. ->table('books')
  122. ->join('book_configs','books.id','=','book_configs.bid')
  123. ->select(
  124. 'books.id','books.author','books.author','books.intro','books.category_name','books.keyword',
  125. 'books.status','books.chapter_count','books.size','books.last_chapter','book_configs.book_name as name',
  126. 'book_configs.cover','book_configs.force_subscribe_chapter_seq','book_configs.charge_type','book_configs.roles'
  127. )
  128. ->where('books.id',$bid)
  129. ->first();
  130. if(!$new_yunqi_book){
  131. return false;
  132. }
  133. return $new_yunqi_book;
  134. }
  135. private function recordUpdateInfo($bid, $data)
  136. {
  137. // 2 book_updates 的更新记录
  138. DB::table('book_updates')->insert([
  139. 'bid' => $bid,
  140. 'book_name' => $data['book_name'],
  141. 'channel_name' => $data['channel_name'],
  142. 'update_date' => date('Y-m-d'),
  143. 'update_chapter_count' => $data['update_chapter_count'],
  144. 'update_words' => $data['update_words'],
  145. 'update_type' => $data['update_type'],
  146. 'created_at' => date('Y-m-d H:i:s'),
  147. 'updated_at' => date('Y-m-d H:i:s')
  148. ]);
  149. }
  150. }