BookAfterSpider.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. }
  61. private function addproducts()
  62. {
  63. $res = ProductService::addProduct([
  64. 'price' => '8.99',
  65. 'type' => 'BOOK_ORDER',
  66. 'given' => 0,
  67. 'is_default' => 0,
  68. 'is_enabled' => 1,
  69. 'sequence' => 0
  70. ]);
  71. return $res->id;
  72. }
  73. private function addbookConfig($bid)
  74. {
  75. $sql = "UPDATE books set intro = REPLACE(intro,'&nbsp;','') where id = " . $bid;
  76. DB::update($sql);
  77. $book_info = DB::table('books')->where('id', $bid)->first();
  78. $book_config = DB::table('book_configs')->where('bid', $bid)->first();
  79. if ($book_config) {
  80. return ;
  81. } else {
  82. $cp = '';
  83. $product_id = 0;
  84. $vip_seq = 0;
  85. $vip = DB::table('chapters')->where('bid', $bid)->where('is_vip', 1)->select('sequence')->orderBy('sequence')->first();
  86. if ($vip && isset($vip->sequence)) {
  87. $vip_seq = $vip->sequence;
  88. }
  89. $this->update_info[$bid] = [];
  90. $this->update_info[$bid]['add'] = true;
  91. $this->update_info[$bid]['channel_name'] = $book_info->category_id >=13 ? '女频':'男频';
  92. $this->update_info[$bid]['update_type'] = 'add_book';
  93. $this->update_info[$bid]['book_name'] = $book_info->name;
  94. DB::table('book_configs')->insert([
  95. 'bid' => $bid,
  96. 'force_subscribe_chapter_seq' => 10,
  97. 'book_name' => $book_info->name,
  98. 'price' => '8.99',
  99. 'cover' => $book_info->cover,
  100. 'charge_type' => 'CHAPTER',
  101. 'is_on_shelf' => 0,
  102. 'product_id' => $product_id,
  103. 'cp_source' => $cp,
  104. 'vip_seq' => $vip_seq,
  105. 'created_at' => date('Y-m-d H:i:s'),
  106. 'updated_at' => date('Y-m-d H:i:s')
  107. ]);
  108. }
  109. }
  110. public function bookChapterInfo($bid)
  111. {
  112. $res1 = DB::table('chapters')->where('bid', $bid)->orderBy('sequence', 'asc')->select('id')->first();
  113. $res2 = DB::table('chapters')->where('bid', $bid)->orderBy('sequence', 'desc')->select('id', 'name')->first();
  114. $res3 = DB::table('chapters')->where('bid', $bid)->count();
  115. $res4 = DB::table('chapters')->where('bid', $bid)->sum('size');
  116. if(isset($this->update_info[$bid])){
  117. $this->update_info[$bid]['update_words'] = $res4;
  118. $this->update_info[$bid]['update_chapter_count'] = $res3;
  119. }
  120. DB::table('books')->where('id', $bid)->update(
  121. [
  122. 'chapter_count' => $res3,
  123. 'first_cid' => $res1->id,
  124. 'last_cid' => $res2->id,
  125. 'size' => $res4,
  126. 'last_chapter' => $res2->name
  127. ]
  128. );
  129. }
  130. public function getcp($ly_bid)
  131. {
  132. return '';
  133. }
  134. /**
  135. * 调整单本书的顺序
  136. * @param $bid
  137. */
  138. public function adjustSequentOne($bid)
  139. {
  140. $chapter_list = DB::table('chapters')->orderBy('sequence')->where('bid', $bid)->select('id')->get();
  141. $prev = 0;
  142. foreach ($chapter_list as $chapter) {
  143. if ($prev) {
  144. DB::table('chapters')->where('id', $chapter->id)->update(['prev_cid' => $prev]);
  145. DB::table('chapters')->where('id', $prev)->update(['next_cid' => $chapter->id]);
  146. }
  147. $prev = $chapter->id;
  148. }
  149. }
  150. private function recordUpdateInfo()
  151. {
  152. if(!$this->update_info) {
  153. return;
  154. }
  155. foreach ($this->update_info as $k=>$v){
  156. if(!$v['add']) continue;
  157. // 2 book_updates 的更新记录
  158. DB::table('book_updates')->insert([
  159. 'bid' => $k,
  160. 'book_name' => $v['book_name'],
  161. 'channel_name' => $v['channel_name'],
  162. 'update_date' => date('Y-m-d'),
  163. 'update_chapter_count' => $v['update_chapter_count'],
  164. 'update_words' => $v['update_words'],
  165. 'update_type' => $v['update_type'],
  166. 'created_at' => date('Y-m-d H:i:s'),
  167. 'updated_at' => date('Y-m-d H:i:s')
  168. ]);
  169. }
  170. }
  171. }