BookAfterSpider.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Modules\Book\Services\ChapterService;
  4. use Illuminate\Console\Command;
  5. use App\Modules\Product\Services\ProductService;
  6. use App\Modules\Book\Models\BookConfig;
  7. use GuzzleHttp\Client;
  8. use DB;
  9. use App\Modules\Book\Services\BookConfigService;
  10. class BookAfterSpider extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'book:afs {bid*} {--scope}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '采集之后';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. private $update_info = [];
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return mixed
  38. */
  39. public function handle()
  40. {
  41. $all_bid = $this->argument('bid');
  42. $options = $this->option('scope');
  43. if ($options) {
  44. $bids = DB::table('books')->where('id', '>=', $all_bid[0])->select('id')->get();
  45. foreach ($bids as $key => $value) {
  46. $this->starts($value->id);
  47. }
  48. } else {
  49. foreach ($all_bid as $key => $value) {
  50. $this->starts($value);
  51. }
  52. }
  53. $this->recordUpdateInfo();
  54. }
  55. private function starts($bid)
  56. {
  57. $this->addbookConfig($bid);
  58. $this->bookChapterInfo($bid);
  59. $this->adjustSequentOne($bid);
  60. //ChapterService::splitContentAll($bid);
  61. }
  62. private function addproducts()
  63. {
  64. $res = ProductService::addProduct([
  65. 'price' => '8.99',
  66. 'type' => 'BOOK_ORDER',
  67. 'given' => 0,
  68. 'is_default' => 0,
  69. 'is_enabled' => 1,
  70. 'sequence' => 0
  71. ]);
  72. return $res->id;
  73. }
  74. private function addbookConfig($bid)
  75. {
  76. $sql = "UPDATE books set intro = REPLACE(intro,'&nbsp;','') where id = " . $bid;
  77. DB::update($sql);
  78. $book_info = DB::table('books')->where('id', $bid)->first();
  79. $book_config = DB::table('book_configs')->where('bid', $bid)->first();
  80. if ($book_config) {
  81. if ($book_info->ly_bid) {
  82. $cp = $this->getcp($book_info->ly_bid);
  83. DB::table('book_configs')->where('bid', $bid)->update(['cp_source' => $cp]);
  84. }
  85. } else {
  86. $cp = '';
  87. if ($book_info->ly_bid) {
  88. $cp = $this->getcp($book_info->ly_bid);
  89. }
  90. $product_id = $this->addproducts();
  91. $vip_seq = 0;
  92. $vip = DB::table('chapters')->where('bid', $bid)->where('is_vip', 1)->select('sequence')->orderBy('sequence')->first();
  93. if ($vip && isset($vip->sequence)) {
  94. $vip_seq = $vip->sequence;
  95. }
  96. $this->update_info[$bid] = [];
  97. $this->update_info[$bid]['add'] = true;
  98. $this->update_info[$bid]['channel_name'] = $book_info->category_id >=13 ? '女频':'男频';
  99. $this->update_info[$bid]['update_type'] = 'add_book';
  100. $this->update_info[$bid]['book_name'] = $book_info->name;
  101. DB::table('book_configs')->insert([
  102. 'bid' => $bid,
  103. 'force_subscribe_chapter_seq' => 10,
  104. 'book_name' => $book_info->name,
  105. 'price' => '8.99',
  106. 'cover' => $book_info->cover,
  107. 'charge_type' => 'CHAPTER',
  108. 'is_on_shelf' => 0,
  109. 'product_id' => $product_id,
  110. 'cp_source' => $cp,
  111. 'vip_seq' => $vip_seq,
  112. 'created_at' => date('Y-m-d H:i:s'),
  113. 'updated_at' => date('Y-m-d H:i:s')
  114. ]);
  115. }
  116. }
  117. public function bookChapterInfo($bid)
  118. {
  119. $res1 = DB::table('chapters')->where('bid', $bid)->orderBy('sequence', 'asc')->select('id')->first();
  120. $res2 = DB::table('chapters')->where('bid', $bid)->orderBy('sequence', 'desc')->select('id', 'name')->first();
  121. $res3 = DB::table('chapters')->where('bid', $bid)->count();
  122. $res4 = DB::table('chapters')->where('bid', $bid)->sum('size');
  123. if(isset($this->update_info[$bid])){
  124. $this->update_info[$bid]['update_words'] = $res4;
  125. $this->update_info[$bid]['update_chapter_count'] = $res3;
  126. }
  127. DB::table('books')->where('id', $bid)->update(
  128. [
  129. 'chapter_count' => $res3,
  130. 'first_cid' => $res1->id,
  131. 'last_cid' => $res2->id,
  132. 'size' => $res4,
  133. 'last_chapter' => $res2->name
  134. ]
  135. );
  136. }
  137. public function getcp($ly_bid)
  138. {
  139. return '';
  140. }
  141. /**
  142. * 调整单本书的顺序
  143. * @param $bid
  144. */
  145. public function adjustSequentOne($bid)
  146. {
  147. $chapter_list = DB::table('chapters')->orderBy('sequence')->where('bid', $bid)->select('id')->get();
  148. $prev = 0;
  149. foreach ($chapter_list as $chapter) {
  150. if ($prev) {
  151. DB::table('chapters')->where('id', $chapter->id)->update(['prev_cid' => $prev]);
  152. DB::table('chapters')->where('id', $prev)->update(['next_cid' => $chapter->id]);
  153. }
  154. $prev = $chapter->id;
  155. }
  156. }
  157. private function recordUpdateInfo()
  158. {
  159. if(!$this->update_info) {
  160. return;
  161. }
  162. foreach ($this->update_info as $k=>$v){
  163. if(!$v['add']) continue;
  164. // 2 book_updates 的更新记录
  165. DB::table('book_updates')->insert([
  166. 'bid' => $k,
  167. 'book_name' => $v['book_name'],
  168. 'channel_name' => $v['channel_name'],
  169. 'update_date' => date('Y-m-d'),
  170. 'update_chapter_count' => $v['update_chapter_count'],
  171. 'update_words' => $v['update_words'],
  172. 'update_type' => $v['update_type'],
  173. 'created_at' => date('Y-m-d H:i:s'),
  174. 'updated_at' => date('Y-m-d H:i:s')
  175. ]);
  176. }
  177. }
  178. }