BookUpdateOne.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use GuzzleHttp\Client;
  5. use App\Modules\Book\Models\Book;
  6. use App\Modules\Book\Models\Chapter;
  7. use App\Modules\Book\Services\ChapterService;
  8. use App\Modules\Book\Services\BookConfigService;
  9. use DB;
  10. class BookUpdateOne extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'book:updateone
  18. {--bid= : the id of a book}
  19. {--content}
  20. {--start=}
  21. {--end=}
  22. {--withname}';
  23. /**
  24. * The console command description.
  25. *
  26. * @var string
  27. */
  28. protected $description = '更新图书章节内容';
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. $this->client = new Client(['timeout' => 8.0,'allow_redirects'=>true]);
  38. }
  39. /**
  40. * Execute the console command.
  41. *
  42. * @return mixed
  43. */
  44. public function handle()
  45. {
  46. $bid = $this->option('bid');
  47. if(empty($bid)) return 0;
  48. $content = $this->option('content');
  49. if($content){
  50. $start = $this->option('start');
  51. $end = $this->option('end');
  52. $withname = $this->option('withname');
  53. return $this->updateExistContent($bid,$start,$end,$withname);
  54. }
  55. return $this->updateOne($bid);
  56. }
  57. /**
  58. * 更新章节内容 一本书的
  59. * @param $bid
  60. * @return int|string
  61. */
  62. public function updateOne($bid){
  63. set_time_limit(0);
  64. $book_info = Book::find($bid);
  65. if(empty($book_info) || empty($book_info->ly_bid)) return -1;
  66. $this->bookStatus($bid,$book_info->ly_bid);
  67. $max_sequence = Chapter::where('bid',$bid)->max('sequence');
  68. $max_sequence = $max_sequence?$max_sequence:-1;
  69. $max_sequence_chapter = Chapter::where('bid',$bid)->where('sequence',$max_sequence)->select('id')->first();
  70. $max_sequence_chapter_id = isset($max_sequence_chapter->id)?$max_sequence_chapter->id:0;
  71. $chapter_list_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapterlist&bid=%s&token=sefaf23h7face";
  72. $res = $this->client->get(sprintf($chapter_list_fromat,$book_info->ly_bid));
  73. $res = $res->getBody()->getContents();
  74. if(!$res) return '';
  75. $book_config_info = DB::table('book_configs')->where('bid',$bid)->first();
  76. $res = json_decode($res,true);
  77. $j = 0;
  78. $size = 0;
  79. $local_count = Chapter::where('bid',$bid)->count();
  80. $remote_count = 0;
  81. foreach ($res['data'] as $item1) {
  82. $remote_count += count($item1['chapters']);
  83. $item1 = null;
  84. }
  85. if($remote_count == $local_count) return '';
  86. foreach ($res['data'] as $v1) {
  87. foreach ($v1['chapters'] as $v) {
  88. if ($v['chapter_order_number'] <= $max_sequence-1) {
  89. continue;
  90. }
  91. if (Chapter::where('bid', $bid)->where('ly_chapter_id', $v['chapter_id'])->count()) {
  92. continue;
  93. }
  94. $chapter_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapter&bid=%s&cid=%s&token=sefaf23h7face";
  95. $cahpter_content_res = $this->client->get(sprintf($chapter_fromat, $book_info->ly_bid, $v['chapter_id']));
  96. $cahpter_content = $cahpter_content_res->getBody()->getContents();
  97. $temp = [
  98. 'bid' => $bid,
  99. 'name' => $v['chapter_name'],
  100. 'sequence' => $v['chapter_order_number']+1,
  101. 'is_vip' => $v['chapter_need_pay'],
  102. 'prev_cid' => $max_sequence_chapter_id,
  103. 'next_cid' => '',
  104. 'recent_update_at' => date('Y-m-d H:i:s', $v['chapter_last_update_time']),
  105. 'ly_chapter_id' => $v['chapter_id']
  106. ];
  107. if ($cahpter_content) {
  108. $cahpter_content = json_decode($cahpter_content, true);
  109. $temp['size'] = ceil(strlen($cahpter_content['data']['chapter_content']) / 3);
  110. $temp['content'] = $cahpter_content['data']['chapter_content'];
  111. $size += $temp['size'];
  112. }
  113. $insert_res = Chapter::create($temp);
  114. Chapter::where('id', $max_sequence_chapter_id)->update(['next_cid' => $insert_res->id]);
  115. $max_sequence_chapter_id = $insert_res->id;
  116. $temp = null;
  117. $j++;
  118. }
  119. }
  120. $chapter_count = Chapter::where('bid',$bid)->count();
  121. $chapter_size = Chapter::where('bid',$bid)->sum('size');
  122. if($j>0){
  123. Book::where('id',$bid)->update(['chapter_count'=>$chapter_count,'last_cid'=>$max_sequence_chapter_id,'size'=>$chapter_size,'last_chapter'=>$v['chapter_name']]);
  124. $this->recordUpdateInfo($bid,[
  125. 'book_name'=>$book_config_info->book_name,
  126. 'update_chapter_count'=>$j,
  127. 'update_words'=>$size,
  128. 'update_type'=>'add_chapter',
  129. 'channel_name'=>$book_info->category_id >=13 ? '女频':'男频'
  130. ]);
  131. }
  132. return $j;
  133. }
  134. private function bookStatus($bid,$ly_bid){
  135. $status = 0;
  136. try{
  137. $book_info_url_format = "http://www.leyuee.com/services/zwfx.aspx?method=bookinfo&token=sefaf23h7face&bid=%s";
  138. $res = $this->client->get(sprintf($book_info_url_format,$ly_bid));
  139. $res = $res->getBody()->getContents();
  140. if($res){
  141. $res = json_decode($res,true);
  142. if(isset($res['data']) && isset($res['data']['book_state'])){
  143. $status = $res['data']['book_state'];
  144. }
  145. }
  146. }catch (\Exception $e){
  147. }
  148. if($status){
  149. Book::where('id',$bid)->update(['status'=>$status]);
  150. }
  151. }
  152. private function updateExistContent($bid,$start=0,$end=0,$name=false){
  153. $book_info = Book::find($bid);
  154. if(empty($book_info) || empty($book_info->ly_bid)) return -1;
  155. $chapter_list_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapterlist&bid=%s&token=sefaf23h7face";
  156. $res = $this->client->get(sprintf($chapter_list_fromat,$book_info->ly_bid));
  157. $res = $res->getBody()->getContents();
  158. if(!$res) return '';
  159. $res = json_decode($res,true);
  160. if(!isset($res['data']))
  161. return '';
  162. foreach ($res['data'] as $v1){
  163. foreach ($v1['chapters'] as $v){
  164. if($start && ($v['chapter_order_number']+1<$start) ){
  165. continue;
  166. }
  167. if($end && ($v['chapter_order_number']+1>$end)){
  168. break;
  169. }
  170. $chapter_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapter&bid=%s&cid=%s&token=sefaf23h7face";
  171. $cahpter_content_res = $this->client->get(sprintf($chapter_fromat, $book_info->ly_bid, $v['chapter_id']));
  172. $cahpter_content = $cahpter_content_res->getBody()->getContents();
  173. $cahpter_content = json_decode($cahpter_content, true);
  174. if($cahpter_content && isset( $cahpter_content['data']['chapter_content']) && !empty( $cahpter_content['data']['chapter_content'])){
  175. //$temp['size'] = ceil(strlen($cahpter_content['data']['chapter_content']) / 3);
  176. //$temp['content'] = $cahpter_content['data']['chapter_content'];
  177. $chapter = Chapter::where('bid', $bid)->where('sequence', $v['chapter_order_number']+1)->first();
  178. if($chapter){
  179. $chapter->content = $cahpter_content['data']['chapter_content'];
  180. if($name){
  181. $chapter->name =$v['chapter_name'];
  182. }
  183. $chapter->size = ceil(strlen($cahpter_content['data']['chapter_content']) / 3);
  184. ChapterService::editChapter($chapter);
  185. }
  186. }
  187. }
  188. }
  189. }
  190. private function recordUpdateInfo($bid, $data)
  191. {
  192. // 2 book_updates 的更新记录
  193. DB::table('book_updates')->insert([
  194. 'bid' => $bid,
  195. 'book_name' => $data['book_name'],
  196. 'channel_name' => $data['channel_name'],
  197. 'update_date' => date('Y-m-d'),
  198. 'update_chapter_count' => $data['update_chapter_count'],
  199. 'update_words' => $data['update_words'],
  200. 'update_type' => $data['update_type'],
  201. 'created_at' => date('Y-m-d H:i:s'),
  202. 'updated_at' => date('Y-m-d H:i:s')
  203. ]);
  204. }
  205. }