| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 | <?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);                }            }        //}    }}
 |