<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Modules\Book\Models\Chapter;
use App\Modules\Book\Models\Book;
use DB;
use Log;

class updateFromNewYunqi extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'updateyunqi {--bid=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $bid = $this->option('bid');
        if($bid){
            $book = $this->getAllBid($bid);
        }else{
            $book = $this->getAllBid();
        }
        if($book){
            //Log::info($book);
            foreach ($book as $item){
                $this->updateOne($item->id,$item->chapter_count,$item->yq_bid,$item->name);
            }
        }
    }

    private function getAllBid($bid=0){
        if($bid){
            return DB::table('book_yunqi')->join('books','book_yunqi.bid','=','books.id')
                ->where('book_yunqi.type','NEW_YUNQI')
                ->where('books.status',0)
                ->where('books.id',$bid)
                ->select('books.id','books.name','book_yunqi.yq_bid','books.chapter_count','books.last_cid')
                ->get();
        }
        return DB::table('book_yunqi')->join('books','book_yunqi.bid','=','books.id')
            ->where('book_yunqi.type','NEW_YUNQI')
            ->where('books.status',0)
            ->select('books.id','books.name','book_yunqi.yq_bid','books.chapter_count','books.last_cid')
            ->get();
    }

    private function updateOne($zsy_bid,$zsy_count,$yq_bid,$name)
    {
        $yunqi_book_info = $this->yQbookInfo($yq_bid);
        if(!$yunqi_book_info) {
            Log::info( '$yq_bid is :'.$yq_bid.', not exists' );
            return ;
        }
        //Log::info($yunqi_book_info);
        if($yunqi_book_info->chapter_count <=$zsy_count) return ;
        $update_count = 0;
        $update_size = 0;
        for ($i = $zsy_count+1;$i<=$yunqi_book_info->chapter_count;$i++){
            $temp = DB::connection('new_yunqi')->table('chapters')->where('bid',$yq_bid)->where('sequence',$i)->select('name','content','is_vip','size')->first();
            if($temp){
                $update_count++;
                $update_size += $temp->size;
                Chapter::create([
                    'bid'=>$zsy_bid,
                    'name'=>$temp->name,
                    'sequence'=>$i,
                    'is_vip'=>$temp->is_vip,
                    'size'=>$temp->size,
                    'prev_cid'=>0,
                    'next_cid'=>0,
                    'recent_update_at'=>date('Y-m-d H:i:s'),
                    'content'=>$temp->content,
                    'ly_chapter_id'=>0
                ]);
            }
        }

        if($update_count >0){
            $this->recordUpdateInfo($zsy_bid,[
                'book_name' => $name,
                'channel_name' =>'女频',
                'update_chapter_count' => $update_count,
                'update_words' => $update_size,
                'update_type'=>'add_chapter'
            ]);
            \Artisan::call('book:adjustone',['--bid'=>$zsy_bid]);
            $info = Chapter::join('books','books.id','=','chapters.bid')
                ->where('books.id',$zsy_bid)
                ->select('chapters.id','chapters.name','chapters.sequence','books.size')
                ->orderBy('chapters.sequence','desc')
                ->first();

            Book::where('id',$zsy_bid)->update([
                'status'=>$yunqi_book_info->status,
                'last_chapter'=>$info->name,
                'size'=>$info->size+$update_size,
                'last_cid'=>$info->id,
                'chapter_count'=>$info->sequence
            ]);
        }
    }

    private function yQbookInfo($bid){
        $new_yunqi_book = DB::connection('new_yunqi')
            ->table('books')
            ->join('book_configs','books.id','=','book_configs.bid')
            ->select(
                'books.id','books.author','books.author','books.intro','books.category_name','books.keyword',
                'books.status','books.chapter_count','books.size','books.last_chapter','book_configs.book_name as name',
                'book_configs.cover','book_configs.force_subscribe_chapter_seq','book_configs.charge_type','book_configs.roles'
            )
            ->where('books.id',$bid)
            ->first();
        if(!$new_yunqi_book){
            return false;
        }
        return $new_yunqi_book;
    }


    private function recordUpdateInfo($bid, $data)
    {
        // 2 book_updates 的更新记录
        DB::table('book_updates')->insert([
            'bid' => $bid,
            'book_name' => $data['book_name'],
            'channel_name' => $data['channel_name'],
            'update_date' => date('Y-m-d'),
            'update_chapter_count' => $data['update_chapter_count'],
            'update_words' => $data['update_words'],
            'update_type' => $data['update_type'],
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ]);
    }

}
