<?php
/**
 * Created by PhpStorm.
 * User: z-yang
 * Date: 2020/4/10
 * Time: 11:52
 */

namespace App\Console\Commands\SendOrder;

use DB;
use Illuminate\Console\Command;
use Log;
use Redis;

class SendOrderSendOrderPvUv extends Command
{
    /**
     * 执行命令   php artisan send_order:generate_day_stat
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'SendOrder:SendOrderSendOrderPvUv {--type=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '派单即时访问量';

    /**
     * Execute the console command.
     *
     * @return mixed
     */

    public function handle(){
        $type = $this->option('type');
        if($type == 'history'){
            $this->updateHistotyStats();
        }else{
            $this->updateTodayStats();
        }
    }

    private function updateTodayStats(){
        $day = date('Y-m-d');
        $i = 0;
        $send_order_list = Redis::SMEMBERS('send_order' . $day);
        //while (true){
            /*$list = Redis::sscan('send_order' . $day,$i,['match'=>'*','count'=>100]);
            if(!$list){
                break;
            }
            list($i,$send_order_list) = $list;*/
            foreach ($send_order_list as $send_order_id){
                $uv = (int)(Redis::hget('send_order_uv_'.$send_order_id,$day));
                $pv = (int)(Redis::hget('send_order_pv_'.$send_order_id,$day));
                $pvInfo = DB::table('send_order_uv_pv')->where('send_order_id',$send_order_id)->select('history_pv','history_uv','total_pv','total_uv','uv_reach_200_time','uv_reach_500_time')->first();
                if($pvInfo){
                    $total_uv = $pvInfo->history_uv+$uv;
                    $total_pv = $pvInfo->history_pv+$pv;
                    $update_data = [
                        'today_pv'=>$pv,
                        'today_uv'=>$uv,
                        'total_pv'=>$total_pv,
                        'total_uv'=>$total_uv,
                        'updated_at'=>date('Y-m-d H:i:s')
                    ];

                    if(!$pvInfo->uv_reach_200_time  && ($total_uv-200) > 0){
                        $update_data['uv_reach_200_time'] = date('Y-m-d H:i:s');
                    }
                    if(!$pvInfo->uv_reach_200_time && $pvInfo->total_uv < 200 && $total_uv>200){
                        $update_data['uv_reach_200_time'] = date('Y-m-d H:i:s');
                    }
                    if(!$pvInfo->uv_reach_500_time && ($total_uv-500) > 0){
                        $update_data['uv_reach_500_time'] = date('Y-m-d H:i:s');
                    }
                    if(!$pvInfo->uv_reach_500_time && $pvInfo->total_uv < 500 && $total_uv>500){
                        $update_data['uv_reach_500_time'] = date('Y-m-d H:i:s');
                    }
                    DB::table('send_order_uv_pv')->where('send_order_id',$send_order_id)->update($update_data);
                }else{
                    $insert_data = [
                        'today_pv'=>$pv,
                        'today_uv'=>$uv,
                        'total_pv'=>$pv,
                        'total_uv'=>$uv,
                        'history_pv'=>0,
                        'history_uv'=>0,
                        'send_order_id'=>$send_order_id,
                        'created_at'=>date('Y-m-d H:i:s'),
                        'updated_at'=>date('Y-m-d H:i:s')
                    ];
                    if(($uv-200) > 0){
                        $insert_data['uv_reach_200_time'] = date('Y-m-d H:i:s');
                    }
                    if(($uv-500) > 0){
                        $insert_data['uv_reach_500_time'] = date('Y-m-d H:i:s');
                    }
                    DB::table('send_order_uv_pv')->insert($insert_data);
                }
            }
        //}
    }
    private function updateHistotyStats(){
        $day = date('Y-m-d',time()-86400);
        $i = 0;
        $send_order_list = Redis::SMEMBERS('send_order' . $day);
        //while (true){
            /*$list = Redis::sscan('send_order' . $day,$i,['match'=>'*','count'=>100]);
            if(!$list){
                break;
            }
            list($i,$send_order_list) = $list;*/
            foreach ($send_order_list as $send_order_id){
                $uv = (int)(Redis::hget('send_order_uv_'.$send_order_id,$day));
                $pv = (int)(Redis::hget('send_order_pv_'.$send_order_id,$day));
                $pvInfo = DB::table('send_order_uv_pv')->where('send_order_id',$send_order_id)->select('history_pv','history_uv','uv_reach_200_time','uv_reach_500_time')->first();
                if($pvInfo){
                    $total_uv = $pvInfo->history_uv+$uv;
                    $total_pv = $pvInfo->history_pv+$pv;
                    $update_data = [
                        'history_pv'=>$total_pv,
                        'history_uv'=>$total_uv,
                        'updated_at'=>date('Y-m-d H:i:s')
                    ];
                    DB::table('send_order_uv_pv')->where('send_order_id',$send_order_id)->update($update_data);
                }else{
                    $insert_data = [
                        'today_pv'=>0,
                        'today_uv'=>0,
                        'total_pv'=>$pv,
                        'total_uv'=>$uv,
                        'history_pv'=>$pv,
                        'history_uv'=>$uv,
                        'send_order_id'=>$send_order_id,
                        'created_at'=>date('Y-m-d H:i:s'),
                        'updated_at'=>date('Y-m-d H:i:s')
                    ];
                    if(abs($uv-200) <10){
                        $insert_data['uv_reach_200_time'] = date('Y-m-d H:i:s');
                    }
                    if(abs($uv-500) <10){
                        $insert_data['uv_reach_500_time'] = date('Y-m-d H:i:s');
                    }
                    DB::table('send_order_uv_pv')->insert($insert_data);
                }
            }
        //}
    }
}