BookUpdateOne.php 10 KB

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