BookAfterSpider.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return mixed
  36. */
  37. public function handle()
  38. {
  39. $all_bid = $this->argument('bid');
  40. $options = $this->option('scope');
  41. if ($options) {
  42. $bids = DB::table('books')->where('id', '>=', $all_bid[0])->select('id')->get();
  43. foreach ($bids as $key => $value) {
  44. $this->starts($value->id);
  45. }
  46. } else {
  47. foreach ($all_bid as $key => $value) {
  48. $this->starts($value);
  49. }
  50. }
  51. }
  52. private function starts($bid)
  53. {
  54. $this->bookChapterInfo($bid);
  55. $this->addbookConfig($bid);
  56. $this->adjustSequentOne($bid);
  57. }
  58. private function addproducts()
  59. {
  60. $res = ProductService::addProduct([
  61. 'price' => '8.99',
  62. 'type' => 'BOOK_ORDER',
  63. 'given' => 0,
  64. 'is_default' => 0,
  65. 'is_enabled' => 1,
  66. 'sequence' => 0
  67. ]);
  68. return $res->id;
  69. }
  70. private function addbookConfig($bid)
  71. {
  72. $sql = "UPDATE books set intro = REPLACE(intro,'&nbsp;','') where id = " . $bid;
  73. DB::update($sql);
  74. $book_info = DB::table('books')->where('id', $bid)->first();
  75. $book_config = DB::table('book_configs')->where('bid', $bid)->first();
  76. if ($book_config) {
  77. if ($book_info->ly_bid) {
  78. $cp = $this->getcp($book_info->ly_bid);
  79. DB::table('book_configs')->where('bid', $bid)->update(['cp_source' => $cp]);
  80. }
  81. } else {
  82. $cp = '';
  83. if ($book_info->ly_bid) {
  84. $cp = $this->getcp($book_info->ly_bid);
  85. }
  86. $product_id = $this->addproducts();
  87. $vip_seq = 0;
  88. $vip = DB::table('chapters')->where('bid', $bid)->where('is_vip', 1)->select('sequence')->orderBy('sequence')->first();
  89. if ($vip && isset($vip->sequence)) {
  90. $vip_seq = $vip->sequence;
  91. }
  92. DB::table('book_configs')->insert([
  93. 'bid' => $bid,
  94. 'force_subscribe_chapter_seq' => 10,
  95. 'book_name' => $book_info->name,
  96. 'price' => '8.99',
  97. 'cover' => $book_info->cover,
  98. 'charge_type' => 'CHAPTER',
  99. 'is_on_shelf' => 0,
  100. 'product_id' => $product_id,
  101. 'cp_source' => $cp,
  102. 'vip_seq' => $vip_seq,
  103. 'created_at' => date('Y-m-d H:i:s'),
  104. 'updated_at' => date('Y-m-d H:i:s')
  105. ]);
  106. }
  107. }
  108. public function bookChapterInfo($bid)
  109. {
  110. $res1 = DB::table('chapters')->where('bid', $bid)->orderBy('sequence', 'asc')->select('id')->first();
  111. $res2 = DB::table('chapters')->where('bid', $bid)->orderBy('sequence', 'desc')->select('id', 'name')->first();
  112. $res3 = DB::table('chapters')->where('bid', $bid)->count();
  113. $res4 = DB::table('chapters')->where('bid', $bid)->sum('size');
  114. DB::table('books')->where('id', $bid)->update(
  115. [
  116. 'chapter_count' => $res3,
  117. 'first_cid' => $res1->id,
  118. 'last_cid' => $res2->id,
  119. 'size' => $res4,
  120. 'last_chapter' => $res2->name
  121. ]
  122. );
  123. }
  124. public function getcp($ly_bid)
  125. {
  126. $url = "http://www.leyuee.com/services/zwfx.aspx?method=bookinfo&token=sefaf23h7face&bid=";
  127. $client = new Client(['timeout' => 8.0, 'allow_redirects' => true]);
  128. $book = $client->request('get', $url . $ly_bid)->getBody()->getContents();
  129. $book = json_decode($book, true);
  130. return $book['data']['cpname'];
  131. }
  132. /**
  133. * 调整单本书的顺序
  134. * @param $bid
  135. */
  136. public function adjustSequentOne($bid)
  137. {
  138. $chapter_list = DB::table('chapters')->orderBy('sequence')->where('bid', $bid)->select('id')->get();
  139. $prev = 0;
  140. foreach ($chapter_list as $chapter) {
  141. if ($prev) {
  142. DB::table('chapters')->where('id', $chapter->id)->update(['prev_cid' => $prev]);
  143. DB::table('chapters')->where('id', $prev)->update(['next_cid' => $chapter->id]);
  144. }
  145. $prev = $chapter->id;
  146. }
  147. }
  148. }