<?php

namespace App\Console\Commands;

use App\Modules\Statistic\Services\WapVisitStatService;
use App\Modules\User\Services\UserService;
use Illuminate\Console\Command;
use GuzzleHttp\Client;
use App\Modules\Book\Models\Book;
use App\Modules\Book\Models\BookConfig;
use DB;
use Redis;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新图书列表';

    /**
     * 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()
    {
        //return $this->innerSendOrderId();
        /*$a = $this->recordFirstIntoPayPage(1148,123,1645454);
        print_r($a);*/

        $this->getSpecialChannelIdStats();
    }

    /**
     * 更新书籍列表
     * @return int
     */
    public function innerSendOrderId(){
        $template = "select CONCAT('template_',id) as id from `wechat_template_msgs` where del_flag=0 and is_show_list=1 and created_at >='2018-11-02' order by id desc limit 1000;
";
        $custom = "select CONCAT('custom_',id) as id from `custom_send_msgs` where del_flag=0 and is_show_list=1 and created_at >='2018-11-02' order by id desc limit 10000;
";
        $t = DB::select($template);
        if($t){
            foreach ($t as $v){
                $uv = Redis::SMEMBERS('push:distribution_channel_id:alluv:from:' . $v->id);
                $pv = Redis::hget('push:distribution_channel_id:allpv' , $v->id);
                if($uv){
                    Redis::sadd('push:inner_send_order_id:uv:' . $v->id,$uv);
                }
                if($uv){
                    Redis::hset('push:inner_send_order_id:pv' ,$v->id,$pv);
                }
            }
        }

        $c = DB::select($custom);
        if($c){
            foreach ($c as $val){
                $uv = Redis::SMEMBERS('push:distribution_channel_id:alluv:from:' . $val->id);
                $pv = Redis::hget('push:distribution_channel_id:allpv' , $val->id);
                if($uv){
                    Redis::sadd('push:inner_send_order_id:uv:' . $val->id,$uv);
                }
                if($pv){
                    Redis::hset('push:inner_send_order_id:pv' ,$val->id,$pv);
                }
            }
        }
    }

    private function getSpecialChannelIdStats(){
        $result = WapVisitStatService::getSpecialChannelIdStats(168502,'2018-11-24');
        print_r($result);
    }



    private function recordFirstIntoPayPage($bid,$distribution_channel_id,$uid)
    {
        $start_time = env('RECORD_FIRST_VISIT_PAY_PAGE_TIME');
        $template_id = 0;
        if ($distribution_channel_id == 123 && $start_time && $uid) {
            $user = UserService::getById($uid);
            if (strtotime($user->created_at) > $start_time) {
                $old = DB::table('user_first_visit_pay_page')->where('uid', $uid)->select('uid', 'template_type')->first();
                if ($old) {
                    $template_id = $old->template_type;
                }
                if ($bid && $bid == 1148) {
                    $template_id = $uid % 2 == 0 ? 2 : 1;
                    if (!$old) {
                        try {
                            DB::table('user_first_visit_pay_page')->insert([
                                'uid' => $uid,
                                'template_type' => $template_id,
                                'created_at' => date('Y-m-d H:i:s'),
                                'updated_at' => date('Y-m-d H:i:s')
                            ]);
                        } catch (\Exception $e) {
                        }
                    }
                }
            }
        }
        return $template_id;
    }
}
