BookSpiderAfter.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Console\Book;
  3. use App\Libs\BatchUpdateTrait;
  4. use App\Models\Book\Book;
  5. use App\Models\Book\Chapter;
  6. use App\Models\Book\BookConfig;
  7. use Illuminate\Console\Command;
  8. class BookSpiderAfter extends Command
  9. {
  10. use BatchUpdateTrait;
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'book:after:spider {--bid=}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '书籍采集后续更新';
  23. public function handle()
  24. {
  25. // 传参
  26. $bid = (int)$this->option('bid');
  27. // 获取书籍列表
  28. $books = $this->getBooks($bid);
  29. // 开始更新
  30. $this->start($books);
  31. }
  32. /**
  33. * @param $books
  34. * @return void
  35. */
  36. private function start($books): void
  37. {
  38. if ($books->isEmpty()) {
  39. return;
  40. }
  41. foreach ($books as $book) {
  42. $bid = (int)getProp($book, 'id');
  43. dLog('sync')->info('book:after:spider-start', compact('bid'));
  44. $this->updateChapterPrevAndNextCid($bid);
  45. $this->updateBookData($bid);
  46. dLog('sync')->info('book:after:spider-end', compact('bid'));
  47. }
  48. }
  49. /**
  50. * 获取待更新书籍列表
  51. *
  52. * @param $bid
  53. * @return mixed
  54. */
  55. private function getBooks($bid)
  56. {
  57. // 组装查询条件
  58. $query = Book::select('id', 'zw_id', 'name')->where('zw_id', '>', '0');
  59. // 此处bid为原创小程序bid
  60. if ($bid) {
  61. $query->where('books.id', $bid);
  62. }
  63. // 查询
  64. return $query->get();
  65. }
  66. /**
  67. * 更新书籍数据
  68. *
  69. * @param $bid
  70. * @return bool
  71. */
  72. private function updateBookData($bid): bool
  73. {
  74. if (empty($bid)) {
  75. return false;
  76. }
  77. // 基本查询条件
  78. $where = ['bid' => $bid, 'is_check' => 1, 'is_draft' => 0, 'is_deleted' => 0];
  79. // 获取首个章节
  80. $firstChapter = Chapter::select('id')->where($where)->orderBy('sequence', 'asc')->first();
  81. // 获取首个vip章节
  82. $vipWhere = $where;
  83. $vipWhere['is_vip'] = 1;
  84. $firstVipChapter = Chapter::select('id', 'sequence')->where($vipWhere)->orderBy('sequence', 'asc')->first();
  85. // 获取最新章节
  86. $lastChapter = Chapter::select('id', 'name')->where($where)->orderBy('sequence', 'desc')->first();
  87. // 获取章节总数
  88. $count = Chapter::where($where)->count();
  89. // 更新书籍相关数据
  90. Book::where('id', $bid)->update([
  91. 'first_cid' => (int)getProp($firstChapter, 'id'),
  92. 'last_cid' => (int)getProp($lastChapter, 'id'),
  93. 'last_chapter' => getProp($lastChapter, 'name'),
  94. 'chapter_count' => $count,
  95. 'updated_at' => date('Y-m-d H:i:s')
  96. ]);
  97. // 更新书籍配置
  98. BookConfig::where('bid', $bid)->update([
  99. 'vip_seq' => (int)getProp($firstVipChapter, 'sequence'),
  100. 'updated_at' => date('Y-m-d H:i:s')
  101. ]);
  102. return true;
  103. }
  104. /**
  105. * 更新章节上下章节id
  106. *
  107. * @param $bid
  108. * @return bool
  109. */
  110. private function updateChapterPrevAndNextCid($bid): bool
  111. {
  112. if (empty($bid)) {
  113. return false;
  114. }
  115. // 基本查询条件
  116. $where = ['bid' => $bid, 'is_check' => 1, 'is_draft' => 0, 'is_deleted' => 0];
  117. $chapters = Chapter::select('id', 'sequence')->where($where)->orderBy('sequence', 'asc')->get();
  118. if (empty($chapters)) {
  119. return false;
  120. }
  121. // 组装待更新数据
  122. $seqData = [];
  123. foreach ($chapters as $chapter) {
  124. $sequence = (int)getProp($chapter, 'sequence');
  125. $seqData[$sequence] = ['id' => (int)getProp($chapter, 'id')];
  126. }
  127. // 批量更新
  128. $updateData = [];
  129. foreach ($seqData as $seq => $seqItem) {
  130. $prevSeqData = getProp($seqData,$seq - 1, []);
  131. $nextSeqData = getProp($seqData,$seq + 1, []);
  132. $updateData[] = [
  133. 'id' => $seqItem['id'],
  134. 'prev_cid' => (int)getProp($prevSeqData, 'id'),
  135. 'next_cid' => (int)getProp($nextSeqData, 'id'),
  136. ];
  137. }
  138. $this->batchUpdateDb('chapters', $updateData);
  139. return true;
  140. }
  141. }