BookAfterSpider.php 6.3 KB

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