zz 6 gadi atpakaļ
vecāks
revīzija
72bda881d5
1 mainītis faili ar 97 papildinājumiem un 61 dzēšanām
  1. 97 61
      app/Console/Commands/BookUpdate.php

+ 97 - 61
app/Console/Commands/BookUpdate.php

@@ -8,6 +8,7 @@ use App\Modules\Book\Models\Book;
 use App\Modules\Book\Models\Chapter;
 use App\Modules\Book\Models\Chapter;
 use Log;
 use Log;
 use App\Modules\Book\Services\BookConfigService;
 use App\Modules\Book\Services\BookConfigService;
+
 class BookUpdate extends Command
 class BookUpdate extends Command
 {
 {
     /**
     /**
@@ -24,6 +25,8 @@ class BookUpdate extends Command
      */
      */
     protected $description = '更新所有图书章节内容';
     protected $description = '更新所有图书章节内容';
 
 
+    private $client;
+
     /**
     /**
      * Create a new command instance.
      * Create a new command instance.
      *
      *
@@ -32,7 +35,7 @@ class BookUpdate extends Command
     public function __construct()
     public function __construct()
     {
     {
         parent::__construct();
         parent::__construct();
-        $this->client = new Client(['timeout'  => 10.0,'allow_redirects'=>true]);
+        $this->client = new Client(['timeout' => 10.0, 'allow_redirects' => true]);
     }
     }
 
 
     /**
     /**
@@ -48,12 +51,13 @@ class BookUpdate extends Command
     /**
     /**
      * 更新所有书的章节
      * 更新所有书的章节
      */
      */
-    public function updateAll(){
+    public function updateAll()
+    {
         set_time_limit(0);
         set_time_limit(0);
-        $exist_book = Book::where('ly_bid','!=','0')->where('status',0)->select('id','ly_bid')->get();
-        foreach ($exist_book as $v){
+        $exist_book = Book::where('ly_bid', '!=', '0')->where('status', 0)->select('id', 'ly_bid')->get();
+        foreach ($exist_book as $v) {
             //Log::info('updateAll bid is: '.$v->id.'$ly_bid is :'.$v->ly_bid.'----------------------------');
             //Log::info('updateAll bid is: '.$v->id.'$ly_bid is :'.$v->ly_bid.'----------------------------');
-            $this->updateOne($v->id,$v->ly_bid);
+            $this->updateOne($v->id, $v->ly_bid);
         }
         }
     }
     }
 
 
@@ -62,14 +66,27 @@ class BookUpdate extends Command
      * @param $bid
      * @param $bid
      * @return int|string
      * @return int|string
      */
      */
-    public function updateOne($bid,$ly_bid)
+    public function updateOne($bid, $ly_bid)
     {
     {
         set_time_limit(0);
         set_time_limit(0);
-        $max_sequence = Chapter::where('bid', $bid)->max('sequence');
-        $max_sequence = $max_sequence ? $max_sequence : -1;
-        $max_sequence_chapter = Chapter::where('bid', $bid)->where('sequence', $max_sequence)->select('id')->first();
-        $max_sequence_chapter_id = isset($max_sequence_chapter->id) ? $max_sequence_chapter->id : 0;
-        $this->bookStatus($bid,$ly_bid);
+        //$max_sequence = Chapter::where('bid', $bid)->max('sequence');
+        //$max_sequence = $max_sequence ? $max_sequence : -1;
+        //$max_sequence_chapter = Chapter::where('bid', $bid)->where('sequence', $max_sequence)->select('id')->first();
+        //$max_sequence_chapter_id = isset($max_sequence_chapter->id) ? $max_sequence_chapter->id : 0;
+        $this->bookStatus($bid, $ly_bid);
+
+        $last_chapter = Chapter::where('bid', $bid)
+            ->select('id', 'name', 'sequence', 'prev_cid', 'next_cid')
+            ->orderBy('sequence', 'desc')
+            ->first();
+        $last_chapter_from_third = Chapter::where('bid', $bid)
+            ->where('ly_chapter_id', '>', 0)
+            ->select('id', 'name', 'sequence', 'prev_cid', 'next_cid', 'ly_chapter_id')
+            ->orderBy('sequence', 'desc')
+            ->first();
+        $max_sequence_chapter_id = isset($last_chapter->id) ? $last_chapter->id : 0;
+        $max_sequence = isset($last_chapter->sequence) ? $last_chapter->sequence : 0;
+
         try {
         try {
             $chapter_list_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapterlist&bid=%s&token=sefaf23h7face";
             $chapter_list_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapterlist&bid=%s&token=sefaf23h7face";
             $res = $this->client->get(sprintf($chapter_list_fromat, $ly_bid));
             $res = $this->client->get(sprintf($chapter_list_fromat, $ly_bid));
@@ -80,18 +97,18 @@ class BookUpdate extends Command
         }
         }
         if (!$res) return '';
         if (!$res) return '';
         $res = json_decode($res, true);
         $res = json_decode($res, true);
-        if($res && isset($res['code']) && $res['code'] == 200){
+        if ($res && isset($res['code']) && $res['code'] == 200) {
 
 
-        }else{
+        } else {
             return '';
             return '';
         }
         }
         $j = 0;
         $j = 0;
 
 
-        $local_count = Chapter::where('bid', $bid)->count();
+        $local_count = Chapter::where('bid', $bid)->where('ly_chapter_id', '>', 0)->count();
         $remote_count = 0;
         $remote_count = 0;
-        if(isset($res['data']) && $res['data'] && is_array($res['data'])){
+        if (isset($res['data']) && $res['data'] && is_array($res['data'])) {
 
 
-        }else{
+        } else {
             return '';
             return '';
         }
         }
         foreach ($res['data'] as $item1) {
         foreach ($res['data'] as $item1) {
@@ -101,71 +118,90 @@ class BookUpdate extends Command
         Log::info('$remote_count is: ' . $remote_count . '====$local_count :' . $local_count . '----');
         Log::info('$remote_count is: ' . $remote_count . '====$local_count :' . $local_count . '----');
         if ($remote_count == $local_count) return '';
         if ($remote_count == $local_count) return '';
         Log::info('$bid =: ' . $bid . '---is lack---');
         Log::info('$bid =: ' . $bid . '---is lack---');
+        $start = false;
+        if (!$last_chapter) {
+            $start = true;
+        }
+        $ly_last_chapter_id = 0;
+        if ($last_chapter_from_third && isset($last_chapter_from_third->ly_chapter_id)) {
+            $ly_last_chapter_id = $last_chapter_from_third->ly_chapter_id;
+        }
+
         foreach ($res['data'] as $v1) {
         foreach ($res['data'] as $v1) {
             foreach ($v1['chapters'] as $v) {
             foreach ($v1['chapters'] as $v) {
 
 
-            if ($v['chapter_order_number'] <= $max_sequence-1) {
-                continue;
-            }
+                /*if ($v['chapter_order_number'] <= $max_sequence-1) {
+                    continue;
+                }
 
 
-            if (Chapter::where('bid', $bid)->where('ly_chapter_id', $v['chapter_id'])->count()) {
-                continue;
-            }
+                if (Chapter::where('bid', $bid)->where('ly_chapter_id', $v['chapter_id'])->count()) {
+                    continue;
+                }*/
+                if (!$start) {
+                    if ($v['chapter_id'] != $ly_last_chapter_id) {
+                        continue;
+                    } else {
+                        \Log::info($v1);
+                        $start = true;
+                        continue;
+                    }
+                }
 
 
-            Log::info('bid: ' . $bid . '----ly_chapter_id: ' . $v['chapter_id']);
-            $chapter_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapter&bid=%s&cid=%s&token=sefaf23h7face";
-            $cahpter_content_res = $this->client->get(sprintf($chapter_fromat, $ly_bid, $v['chapter_id']));
-            $cahpter_content = $cahpter_content_res->getBody()->getContents();
-
-            $temp = [
-                'bid' => $bid,
-                'name' => $v['chapter_name'],
-                'sequence' => $v['chapter_order_number']+1,
-                'is_vip' => $v['chapter_need_pay'],
-                'prev_cid' => $max_sequence_chapter_id,
-                'next_cid' => '',
-                'recent_update_at' => date('Y-m-d H:i:s', $v['chapter_last_update_time']),
-                'ly_chapter_id' => $v['chapter_id']
-            ];
-            if ($cahpter_content) {
-                $cahpter_content = json_decode($cahpter_content, true);
-                $temp['size'] = ceil(strlen($cahpter_content['data']['chapter_content']) / 3);
-                $temp['content'] = $cahpter_content['data']['chapter_content'];
+                Log::info('bid: ' . $bid . '----ly_chapter_id: ' . $v['chapter_id']);
+                $chapter_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapter&bid=%s&cid=%s&token=sefaf23h7face";
+                $cahpter_content_res = $this->client->get(sprintf($chapter_fromat, $ly_bid, $v['chapter_id']));
+                $cahpter_content = $cahpter_content_res->getBody()->getContents();
+
+                $temp = [
+                    'bid' => $bid,
+                    'name' => $v['chapter_name'],
+                    'sequence' => ++$max_sequence,
+                    'is_vip' => $v['chapter_need_pay'],
+                    'prev_cid' => $max_sequence_chapter_id,
+                    'next_cid' => '',
+                    'recent_update_at' => date('Y-m-d H:i:s', $v['chapter_last_update_time']),
+                    'ly_chapter_id' => $v['chapter_id']
+                ];
+                if ($cahpter_content) {
+                    $cahpter_content = json_decode($cahpter_content, true);
+                    $temp['size'] = ceil(strlen($cahpter_content['data']['chapter_content']) / 3);
+                    $temp['content'] = $cahpter_content['data']['chapter_content'];
+                }
+                $insert_res = Chapter::create($temp);
+                Chapter::where('id', $max_sequence_chapter_id)->update(['next_cid' => $insert_res->id]);
+                $max_sequence_chapter_id = $insert_res->id;
+                $temp = null;
+                $j++;
             }
             }
-            $insert_res = Chapter::create($temp);
-            Chapter::where('id', $max_sequence_chapter_id)->update(['next_cid' => $insert_res->id]);
-            $max_sequence_chapter_id = $insert_res->id;
-            $temp = null;
-            $j++;
         }
         }
-    }
 
 
-        $chapter_count = Chapter::where('bid',$bid)->count();
-        $chapter_size = Chapter::where('bid',$bid)->sum('size');
-        if($j>0){
-            Book::where('id',$bid)->update(['chapter_count'=>$chapter_count,'last_cid'=>$max_sequence_chapter_id,'size'=>$chapter_size,'last_chapter'=>$v['chapter_name']]);
+        $chapter_count = Chapter::where('bid', $bid)->count();
+        $chapter_size = Chapter::where('bid', $bid)->sum('size');
+        if ($j > 0) {
+            Book::where('id', $bid)->update(['chapter_count' => $chapter_count, 'last_cid' => $max_sequence_chapter_id, 'size' => $chapter_size, 'last_chapter' => $v['chapter_name']]);
         }
         }
         return $j;
         return $j;
     }
     }
 
 
 
 
-    private function bookStatus($bid,$ly_bid){
+    private function bookStatus($bid, $ly_bid)
+    {
         $status = 0;
         $status = 0;
-        try{
-            $book_info_url_format =   "http://www.leyuee.com/services/zwfx.aspx?method=bookinfo&token=sefaf23h7face&bid=%s";
-            $res = $this->client->get(sprintf($book_info_url_format,$ly_bid));
+        try {
+            $book_info_url_format = "http://www.leyuee.com/services/zwfx.aspx?method=bookinfo&token=sefaf23h7face&bid=%s";
+            $res = $this->client->get(sprintf($book_info_url_format, $ly_bid));
             $res = $res->getBody()->getContents();
             $res = $res->getBody()->getContents();
-            if($res){
-                $res = json_decode($res,true);
-                if(isset($res['data']) && isset($res['data']['book_state'])){
+            if ($res) {
+                $res = json_decode($res, true);
+                if (isset($res['data']) && isset($res['data']['book_state'])) {
                     $status = $res['data']['book_state'];
                     $status = $res['data']['book_state'];
                 }
                 }
             }
             }
-        }catch (\Exception $e){
+        } catch (\Exception $e) {
 
 
         }
         }
-        if($status){
-            Book::where('id',$bid)->update(['status'=>$status]);
+        if ($status) {
+            Book::where('id', $bid)->update(['status' => $status]);
         }
         }
     }
     }
 }
 }