SyncBooks.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace App\Console\Sync;
  3. use App\Libs\BatchUpdateTrait;
  4. use App\Models\Book\Book;
  5. use App\Models\Book\BookConfig;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. class SyncBooks extends Command
  9. {
  10. use BatchUpdateTrait;
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'sync:books {--zw_ids=}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '从内容平台同步书籍';
  23. private $zwBooks = [];
  24. public function handle()
  25. {
  26. // 传参
  27. $idsStr = $this->option('zw_ids');
  28. $passZwIds = filterValidIds(explode(',', $idsStr));
  29. // 获取书籍列表
  30. $existedZwIds = $this->getAllBookZwIds($passZwIds);
  31. // 合并
  32. $zwIds = array_unique(array_merge($passZwIds, $existedZwIds));
  33. // 执行更新
  34. $this->runSync($zwIds);
  35. }
  36. /**
  37. * @param $zwIds
  38. * @return array
  39. */
  40. private function getAllBookZwIds($zwIds): array
  41. {
  42. // 查询当前所有书
  43. $query = Book::select('id', 'zw_id');
  44. if ($zwIds) {
  45. $query->whereIn('zw_id', $zwIds);
  46. } else {
  47. $query->where('zw_id', '>', 0);
  48. }
  49. $books = $query->get();
  50. if ($books->isEmpty()) {
  51. return [];
  52. }
  53. // 标记已存在书籍
  54. $result = [];
  55. foreach ($books as $book) {
  56. $zwId = (int)getProp($book, 'zw_id');
  57. if (!isset($this->zwBooks[$zwId])) {
  58. $this->zwBooks[$zwId] = (int)getProp($book, 'id');
  59. }
  60. $result[] = $zwId;
  61. }
  62. return $result;
  63. }
  64. /**
  65. * @param $zwIds
  66. * @return void
  67. */
  68. private function runSync($zwIds): void
  69. {
  70. // 判空
  71. if (empty($zwIds)) {
  72. return;
  73. }
  74. // 获取内容平台书籍
  75. $zwBooks = $this->getZwBooksByIds($zwIds);
  76. if (empty($zwBooks)) {
  77. return;
  78. }
  79. // 组装书籍数据
  80. $now = date('Y-m-d H:i:s');
  81. $zwIds = $upBooks = $inBooks = $initConfigs = [];
  82. foreach ($zwBooks as $zwBook) {
  83. $zwId = (int)getProp($zwBook, 'id');
  84. $cover = getProp($zwBook, 'cover') ?: getProp($zwBook, 'book_config_cover');
  85. $intro = getProp($zwBook, 'intro');
  86. $intro = str_replace("&nbsp;", "", $intro);
  87. $intro = trim($intro);
  88. $zwIds[] = $zwId;
  89. $bookItem = [
  90. 'zw_id' => $zwId,
  91. 'name' => getProp($zwBook, 'name'),
  92. 'author' => getProp($zwBook, 'author'),
  93. 'intro' => $intro,
  94. 'cover' => $cover,
  95. 'keyword' => getProp($zwBook, 'keyword'),
  96. 'gender' => getProp($zwBook, 'gender'), # 1男 2女
  97. 'category_id' => getProp($zwBook, 'category_id'),
  98. 'category_name' => getProp($zwBook, 'category_name'),
  99. 'ncategory_id' => getProp($zwBook, 'ncategory_id'),
  100. 'size' => getProp($zwBook, 'size'),
  101. 'status' => getProp($zwBook, 'status'),
  102. 'chapter_count' => getProp($zwBook, 'chapter_count'),
  103. 'updated_at' => $now,
  104. ];
  105. // 书籍配置表
  106. $initConfigs[$zwId] = [
  107. 'cover' => $cover,
  108. 'book_name' => getProp($zwBook, 'name'),
  109. 'source_domain' => getProp($zwBook, 'gender') == 1 ? 'fanqqe.com' : 'fanqrj.com',
  110. 'cp_source' => getProp($zwBook, 'source'),
  111. 'updated_at' => $now,
  112. ];
  113. // 区分更新还是写入
  114. $bid = (int)getProp($this->zwBooks, $zwId);
  115. if ($bid) {
  116. // 书籍数据
  117. $bookItem['id'] = $bid;
  118. $upBooks[] = $bookItem;
  119. } else {
  120. $bookItem['created_at'] = $now;
  121. $inBooks[] = $bookItem;
  122. }
  123. }
  124. Book::insert($inBooks);
  125. $this->batchUpdateDb('books', $upBooks);
  126. // 获取这批书籍
  127. $books = Book::select('id', 'zw_id')->whereIn('zw_id', $zwIds)->get();
  128. if ($books->isEmpty()) {
  129. return;
  130. }
  131. $bids = collect($books)->pluck('id')->all();
  132. $bookConfigs = BookConfig::select('id', 'bid')->whereIn('bid', $bids)->get();
  133. $configBids = collect($bookConfigs)->pluck('bid')->all();
  134. // 区分更新or新增
  135. $inConfigs = $upConfigs = [];
  136. foreach ($books as $book) {
  137. $bid = (int)getProp($book, 'id');
  138. $zwId = (int)getProp($book, 'zw_id');
  139. $bookConfig = getProp($initConfigs, $zwId, []);
  140. $bookConfig['bid'] = $bid;
  141. if ($bookConfig && in_array($bid, $configBids)) {
  142. // 书籍数据
  143. $upConfigs[] = $bookConfig;
  144. } else {
  145. $bookConfig['copyright_limit_data'] = '0000-00-00';
  146. $bookConfig['charge_type'] = 'CHAPTER';
  147. $bookConfig['shelf_time'] = $now;
  148. $bookConfig['created_at'] = $now;
  149. $inConfigs[] = $bookConfig;
  150. }
  151. }
  152. BookConfig::insert($inConfigs);
  153. $this->batchUpdateDb('book_configs', $upConfigs, 'bid');
  154. }
  155. /**
  156. * 获取内容平台书籍
  157. *
  158. * @param $ids
  159. * @return array
  160. */
  161. private function getZwBooksByIds($ids): array
  162. {
  163. if (empty($ids)) {
  164. return [];
  165. }
  166. $result = DB::connection('zw_content_mysql')
  167. ->table('books')
  168. ->leftjoin('book_configs', 'book_configs.bid', '=', 'books.id')->whereIn('books.id', $ids)
  169. ->select('books.id', 'books.name', 'books.out_bid', 'books.source', 'books.source_name', 'books.author', 'books.source_bid',
  170. 'books.intro', 'books.gender', 'books.cover', 'books.size', 'books.keyword', 'books.category_name', 'books.status', 'books.chapter_count',
  171. 'books.updated_at', 'books.primary_cover', 'books.category_id', 'books.last_chapter_update_time', 'books.series', 'books.labels',
  172. 'books.styles', 'books.man_type', 'books.woman_type', 'books.time_back', 'books.directional', 'books.is_app', 'books.end_time', 'books.post_time',
  173. 'books.ncategory_id', 'books.roles', 'books.authorization_status', 'books.is_vip', 'books.is_first', 'books.is_check', 'books.is_contract',
  174. 'books.author_id', 'book_configs.is_on_shelf', 'book_configs.cover as book_config_cover', 'books.group_id'
  175. )
  176. ->get();
  177. return $result ? $result->toArray() : [];
  178. }
  179. }