BookUpdateOne.php 8.8 KB

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