<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use GuzzleHttp\Client;
use DB;
use Redis;

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

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

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->client = new Client(['timeout'  => 8.0,'allow_redirects'=>true]);
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //32401811
        //$this->cpc();
        $type = $this->option('type');
        if($type == 'yunqi'){
            $bids = $this->option('bid');
            $bids = explode(',',$bids);
            foreach ($bids as $bid){
                $this->newYunQi($bid);
            }
        }
    }


    public function cleanRedis()
    {
        //accurateSendOrder
        //$result = Redis::scan($flag,['match'=>'wap:string:chapter:{*','count'=>1000]);
        $flag = 0;
        /*while (true){
            list($flag,$list) = Redis::hscan('push:inner_send_order_id:pv',$flag,['match'=>'*','count'=>100]);
            $data = [];
            if($list){
                foreach ($list as $k=>$item){
                    $uv = Redis::scard('push:inner_send_order_id:uv:' . $k);
                    $data[] =
                        ['stats_type'=>'inner_send_order_id',
                            'param'=>$k,'pv'=>$item,'uv'=>$uv,
                            'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
                }
                DB::table('wap_long_visit_stats')->insert($data);
            }

            if(!$flag){
                break;
            }
        }*/
        /*while (true){
            list($flag,$list) = Redis::scan($flag,['match'=>'accurateSendOrder*','count'=>1000]);
            if($list){
                foreach ($list as $item){
                    Redis::del($item);
                }
            }
            if(!$flag){
                break;
            }
        }*/

        /*while (true){
            list($flag,$list) = Redis::hscan('specialChannelIdStatsMarkUser:hash:',$flag,['match'=>'*','count'=>100]);
            if($list){
                $data = [];
                foreach ($list as $k=>$send_order_id){
                    list($uid,$bid) = explode('-',$k);
                    //$data[] = ['uid'=>$uid,'bid'=>$bid,'send_order_id'=>$send_order_id,
                     //   'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
                    Redis::del('specialChannelIdStatsMarkUser:set:'.$uid);
                }

                //DB::table('special_channel_stats_mark_user')->insert($data);
            }
            if(!$flag){
                break;
            }
        }*/
        ///131359
        //$uv = Redis::scard('push:distribution_channel_id:alluv:from:' . $from);
        /*$flag = 0;
        while (true){
            if($flag*1000 >=131359){
                break;
                //$result = DB::table('wap_long_visit_stats')->where('id','>=',$flag*1000)->select('param')->get();
            }
            $result = DB::table('wap_long_visit_stats')->where('id','>=',$flag*1000)->select('param')->limit(1000)->get();
            foreach ($result as $v){
                Redis::del('push:distribution_channel_id:alluv:from:' . $v->param);
            }
            $flag++;
        }*/
        $flag = 0;
        while (true){
            //smartPushTestBookPayPageUv:bid:%s
            list($flag,$list) = Redis::scan($flag,['match'=>'smartPushTestBookPaidUv:bid:*','count'=>100]);
            if($list){
                foreach ($list as $k){
                    $bid = str_replace('smartPushTestBookPaidUv:bid:','',$k);
                    //Redis::del($k);
                    DB::table('test_book_smart_push_stats20190709')->where('bid',$bid)->increment('paid_user_num',Redis::scard($k));
                    echo $bid .PHP_EOL;
                }
            }
            if(!$flag){
                break;
            }
        }
    }

    private function newYunQi($bid){
        $result = \App\Modules\Book\Services\BookService::newYunQiBook($bid);
        if($result == -1){
            echo 'yunqi book had collected'.PHP_EOL;
        }
        if ($result == -2) {
            echo 'new_yunqi book not exists'.PHP_EOL;
        }
        echo "OK!";
    }
}
