| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858 | <?phpnamespace App\Console\Commands;use GuzzleHttp\Client;use Illuminate\Console\Command;use Redis;use DB;use App\Modules\User\Services\ReadRecordService;class WapVisitStat extends Command{    /**     * The name and signature of the console command.     *     * @var string     */    protected $signature = 'wvs {type} {--extra=}';    /**     * The console command description.     *     * @var string     */    protected $description = 'redis total';    /**     * Create a new command instance.     *     * @return void     */    public function __construct()    {        parent::__construct();    }    /**     * Execute the console command.     *     * @return mixed     */    public function handle()    {        $options = $this->option('extra');        $param = $this->argument('type');        if(empty($param)){            echo 'wvs {type:1,2,3,4,5...} {--extra}'.PHP_EOL;            return false;        }        if(!is_numeric($param)){            return false;        }        if($options && !is_numeric($options)){            return false;        }        switch ($param){            case 1:                $this->sendorderstats();                break;            case 2:                $this->sendOrderStatsEveryDay();                break;            case 3:                $this->allstats();                break;            case 4:                $this->allStatsEveryDay();                break;            case 5:                $this->activitystats();                break;            case 6:                $this->activityStatsEveryDay($options);                break;            case 7:                $this->beforeForceSubAndBeforeVipStats();                break;            case 8:                $this->beforeForceSubAndBeforeVipStatsEveryDay();                break;            case 9:                $this->start();                break;            case 10:                $this->subscribeKeyDel();                break;            case 11:                $this->substituteStats();                break;            case 12:                $this->StylePushStats();                break;            case 13:                $this->crm();                break;                }        return false;    }    public function start()    {        $this->allStatsEveryDay();        $this->sendOrderStatsEveryDay();        $this->nowActivitys();        $this->beforeForceSubAndBeforeVipStatsEveryDay();        $this->browserSendOrderStatsEveryDay();        $this->substituteStats();        $this->StylePushStats();        $this->crm();        $this->InnerOuterWeixinStats();    }    private function sendorderstats()    {        $day = date('Y-m-d',time()-86400);        // Redis::hincrby('send_order_uv_'.$param,$key,1);        //Redis::hincrby('send_order_pv_'.$param,$key,1);//每天        DB::table('send_orders')->select('id')->orderBy('id')->where('created_at', '<', date('Y-m-d H:i:s'))->chunk(1000, function ($res) use ($day){            $now = date('Y-m-d H:i:s');            foreach ($res as $v) {                $temp = Redis::hgetall('send_order_pv_' . $v->id);                if ($temp) {                    $data = [];                    foreach ($temp as $d => $u) {                        if ($d == 'total') {                            continue;                        }                        if($d == date('Y-m-d')){                            continue;                        }                        //$temp_uv = (int)Redis::hget('send_order_uv_' . $v->id, $d);                        //$data[] = ['day' => $d, 'key' => $v->id, 'from_type' => 'send_orders', 'pv' => $u, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now];                        Redis::hdel('send_order_pv_' . $v->id,$d);                        Redis::hdel('send_order_uv_' . $v->id,$d);                    }                    //DB::table('wap_visit_stats')->insert($data);                    if(!Redis::hget('send_order_pv_' . $v->id,date('Y-m-d'))){                        Redis::del('send_order_pv_' . $v->id);                    }                    if(!Redis::hget('send_order_uv_' . $v->id,date('Y-m-d'))){                        Redis::del('send_order_uv_' . $v->id);                    }                }            }        });    }    private function sendOrderStatsEveryDay()    {        $day = date('Y-m-d', time() - 86400);        $now = date('Y-m-d H:i:s');        $key_like = 'send_order_pv_';        //$keys = Redis::keys($key_like . '*');        $data = [];        $i = 0;        $all_send_order_id = Redis::SMEMBERS('send_order'.$day);        Redis::EXPIRE('send_order'.$day,86400);        foreach ($all_send_order_id as $send_order_id) {            $key = $key_like.$send_order_id;            $temp = Redis::hget($key, $day);            $temp_uv = (int)Redis::hget('send_order_uv_' . $send_order_id, $day);            $data[] = ['day' => $day, 'key' => $send_order_id, 'from_type' => 'send_orders', 'pv' => $temp, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now];            if ($i && $i % 100 == 0) {                DB::table('wap_visit_stats')->insert($data);                $data = [];            }            $i++;            Redis::hdel($key,$day);            if(!Redis::hget($key,date('Y-m-d'))){                Redis::del($key);            }            Redis::hdel('send_order_uv_' . $send_order_id,$day);            if(!Redis::hget('send_order_uv_' . $send_order_id,date('Y-m-d'))){                Redis::del('send_order_uv_' . $send_order_id);            }        }        if($data){            DB::table('wap_visit_stats')->insert($data);        }        //Redis::del('send_order'.$day);    }    private function browserSendOrderStatsEveryDay()    {        $day = date('Y-m-d', time() - 86400);        $now = date('Y-m-d H:i:s');        $key_like = 'browser_send_order_pv_';        //$keys = Redis::keys($key_like . '*');        $data = [];        $i = 0;        $all_send_order_id = Redis::SMEMBERS('browser_send_order'.$day);        foreach ($all_send_order_id as $send_order_id) {            $key = $key_like.$send_order_id;            $temp = Redis::hget($key, $day);            $temp_uv = (int)Redis::hget('browser_send_order_uv_' . $send_order_id, $day);            $data[] = ['day' => $day, 'key' => $send_order_id, 'from_type' => 'browser_send_orders', 'pv' => $temp, 'uv' => $temp_uv, 'type' => 3, 'created_at' => $now, 'updated_at' => $now];            if ($i && $i % 100 == 0) {                DB::table('wap_visit_stats')->insert($data);                $data = [];            }            $i++;            Redis::hdel($key,$day);            if(!Redis::hget($key,date('Y-m-d'))){                Redis::del($key);            }            Redis::hdel('browser_send_order_uv_' . $send_order_id,$day);            if(!Redis::hget('browser_send_order_uv_' . $send_order_id,date('Y-m-d'))){                Redis::del('browser_send_order_uv_' . $send_order_id);            }        }        if($data){            DB::table('wap_visit_stats')->insert($data);        }        Redis::del('browser_send_order'.$day);    }    private function allstats()    {        //Redis::sadd('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $uid_cookie);        //Redis::hincrby('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1);        $day = date('Y-m-d');        $now = date('Y-m-d H:i:s');        $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();        //$all_channel_id = [14];        foreach ($all_channel_id as $v) {            if($v->id == 2){                continue;            }            $like_pv_key = 'customer:push:click:distribution_channel_id:' . $v->id . 'from:';            $froms = $this->getFrom($v->id);            foreach ($froms as $from) {                $true_key = $like_pv_key.$from;                //echo '$true_key: '.$true_key.PHP_EOL;                $temp = Redis::hgetall($true_key);                $data = [];                $i = 0;                foreach ($temp as $d => $u) {                    if($d == $day){                        continue;                    }                    $uv_key = 'push:distribution_channel_id:' . $v->id . 'from:' . $from . ':date:' . $d;                    $temp_uv = (int)Redis::scard($uv_key);                    $data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $from, 'pv' => $u, 'uv' => $temp_uv, 'type' => 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];                    Redis::del($uv_key);                    Redis::hdel($true_key,$d);                    if($i && $i%100 == 0){                        DB::table('wap_visit_stats')->insert($data);                        $data = [];                    }                    $i++;                }                $temp = Redis::hget($true_key,date('Y-m-d'));                if(!$temp){                    Redis::del($true_key);                }                DB::table('wap_visit_stats')->insert($data);            }        }    }    private function allStatsEveryDay()    {        //Redis::sadd('push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . date('Y-m-d'), $uid_cookie);        //Redis::hincrby('customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from, date('Y-m-d'), 1);        $day = date('Y-m-d', time() - 86400);        $now = date('Y-m-d H:i:s');        //$all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();        $alls = Redis::SMEMBERS('wap_from_stats'.$day);        $i = 0;        $data = [];        $wechatKeyData = [];        foreach ($alls as $v){            $arr = explode('-',$v);            $distribution_channel_id = $arr[0];            $from = $arr[1];            if(count($arr) == 3){                $from = $arr[1].$arr[2];            }            $pv_key = 'customer:push:click:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from;            $pv = (int)Redis::hget($pv_key, $day);            $uv_key = 'push:distribution_channel_id:' . $distribution_channel_id . 'from:' . $from . ':date:' . $day;            //echo '$pv_key: '.$pv_key.',$uv_key: $uv_key'.$uv_key.PHP_EOL;            $temp_uv = (int)Redis::scard($uv_key);            //echo $pv.'--'.$temp_uv.PHP_EOL;            if(starts_with($from, 'keyword_')){                    $wechatKeyData[] = ['from'=>$from,'distribution_channel_id'=>$distribution_channel_id,'pv'=>$pv,'uv'=>$temp_uv];            }            $data[] = ['day' => $day, 'key' => $distribution_channel_id, 'from_type' => $from, 'pv' => $pv, 'uv' => $temp_uv, 'type' => 1, 'created_at' => $now, 'updated_at' => $now];            if($i && $i%100 == 0){                DB::table('wap_visit_stats')->insert($data);                $data = [];            }            $i++;            Redis::del($uv_key);            Redis::hdel($pv_key, $day);            //$temp = Redis::hget($pv_key,date('Y-m-d'));            $temp = Redis::hget($pv_key,date('Y-m-d'));            if(!$temp){                Redis::del($pv_key);            }        }        Redis::del('wap_from_stats'.$day);        if($data){            DB::table('wap_visit_stats')->insert($data);        }        if($wechatKeyData){            foreach ($wechatKeyData as  $value) {                list($tmep,$id) = explode('_',$value['from']);                if(!$id) continue;                $we_info = DB::table('wechat_keyword_stats')->where('wechat_keyword_id',$id)->where('date',$day)->first();                    if($we_info){                        DB::table('wechat_keyword_stats')->where('wechat_keyword_id',$id)->where('date',$day)->update(                            ['pv'=>$value['pv'],'uv'=>$value['uv'],'visist_updated_at'=>date('Y-m-d H:i:s')]                        );                    }else{                        try{                            DB::table('wechat_keyword_stats')->insert([                                'wechat_keyword_id'=>$id,                                'date'=>$day,                                'pv'=>$value['pv'],                                'uv'=>$value['uv'],                                'charge_amount'=>0,                                'success_order_num'=>0,                                'created_at'=>date('Y-m-d H:i:s'),                                'updated_at'=>date('Y-m-d H:i:s'),                                'visist_updated_at'=>date('Y-m-d H:i:s'),                                'charge_updated_at'=>date('Y-m-d H:i:s'),                            ]);                        }catch (\Exception $e){}                    }            }        }    }    private function activitystats()    {        $day = date('Y-m-d');        $now = date('Y-m-d H:i:s');        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';        $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();        foreach ($all_channel_id as $v) {            for ($i = 1; $i <= 140; $i++) {                $pv_key = sprintf($pv_key_format, $i, $v->id);                $pv_temp = Redis::hgetall($pv_key);                if ($pv_temp) {                    $data = [];                    foreach ($pv_temp as $d => $u) {                        if($d == $day){                            continue;                        }                        $uv_key = sprintf($uv_key_format, $i, $v->id, $d);                        //$temp_uv = (int)Redis::scard($uv_key);                        //$data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $i, 'pv' => $u, 'uv' => $temp_uv, 'type' => 2, 'created_at' => $now, 'updated_at' => $now];                        Redis::del($uv_key);                    }                    //DB::table('wap_visit_stats')->insert($data);                }                Redis::del($pv_key);            }        }    }    private function activityStatsEveryDay($activity_id)    {        $day = date('Y-m-d',time()-86400);        $now = date('Y-m-d H:i:s');        $uv_key_format = 'activity:%s:distribution_channel_id:%s:date:%s:uv';        $pv_key_format = 'activity:%s:distribution_channel_id:%s:pv';        $all_channel_id = DB::table('distribution_channels')->select('id')->orderBy('id')->where('created_at', '<', $now)->get();        foreach ($all_channel_id as $v) {            $i = $activity_id;            $pv_key = sprintf($pv_key_format, $i, $v->id);            $pv_temp = Redis::hgetall($pv_key);            if ($pv_temp) {                $data = [];                foreach ($pv_temp as $d => $u) {                    if($d != $day){                        continue;                    }                    $uv_key = sprintf($uv_key_format, $i, $v->id, $d);                    $temp_uv = (int)Redis::scard($uv_key);                    $data[] = ['day' => $d, 'key' => $v->id, 'from_type' => $i, 'pv' => $u, 'uv' => $temp_uv, 'type' => 2, 'created_at' => $now, 'updated_at' => $now];                    Redis::del($uv_key);                    Redis::hdel($pv_key,$d);                    try{                        DB::table('activity_statistic_all')                            ->where('activity_id',$activity_id)                            ->where('day',$d)                            ->where('distribution_channel_id',$v->id)->update(['pv'=>$u,'uv'=>$temp_uv]);                    }catch (\Exception $e){}                }                DB::table('wap_visit_stats')->insert($data);                if( !Redis::hget($pv_key,date('Y-m-d'))){                    Redis::del($pv_key);                }            }        }    }    private function nowActivitys(){        $day = date('Y-m-d',time()-86400);        $activty_ids = DB::table('activity')->where('start_time','<=',$day.' 23:00:00')->where('end_time','>=',$day)->select('id')->get();        foreach ($activty_ids as $v){            //echo $v->id.PHP_EOL;            $this->activityStatsEveryDay($v->id);        }    }    private function beforeForceSubAndBeforeVipStats()    {        //$uv_key = 'book:'.$book->bid.':beforevip:uv:date:'.$date.':send_order_id:'.$send_order_id;        //$pv_key = 'book:'.$book->bid.':beforevip:pv:send_order_id:'.$send_order_id;        $send_order_ids = DB::table('send_orders')->where('send_time','>=','2017-12-10')->select('id')->get();        $now = date('Y-m-d H:i:s');        for ($i = 1; $i < 100; $i++) {            $like_key = 'book:' . $i . ':beforevip:pv:send_order_id:';            //$all_key = Redis::keys($like_key . '*');            if ($send_order_ids) {                foreach ($send_order_ids as $ids) {                    $data = [];                    $send_order_id = $ids->id;                    $kkk = $like_key.$send_order_id;                    $temp_pv = Redis::hgetall($kkk);                    foreach ($temp_pv as $d => $u) {                        if($d == 'total'){                            continue;                        }                        if($d == date('Y-m-d')){                            continue;                        }                        $uv_key = 'book:' . $i . ':beforevip:uv:date:' . $d . ':send_order_id:' . $send_order_id;                        //$temp_uv = (int)Redis::scard($uv_key);                        //$data[] = ['day' => $d, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $u, 'uv' => $temp_uv, 'type' => 4, 'created_at' => $now, 'updated_at' => $now];                        Redis::del($uv_key);                        Redis::hdel($kkk,$d);                    }                    if(!Redis::hget($kkk,date('Y-m-d'))){                        Redis::del($kkk);                    }                    //DB::table('wap_visit_stats')->insert($data);                }            }        }        //$uv_key = 'book:' . $book->bid . ':beforeforcesub:uv:date:' . $date . ':send_order_id:' . $send_order_id;        //$pv_key = 'book:' . $book->bid . ':beforeforcesub::pvsend_order_id:' . $send_order_id;        for ($i = 1; $i < 100; $i++){            $like_key = 'book:' . $i . ':beforeforcesub::pvsend_order_id:';            //$send_order_ids = DB::table('send_orders')->where('send_time','>=','2017-12-10')->select('id')->get();            //$all_key = Redis::keys($like_key . '*');            if($send_order_ids){                foreach ($send_order_ids as $vvvv){                    $send_order_id = $vvvv->id;                    $ks = $like_key.$send_order_id;                    $data = [];                    $t_pv = Redis::hgetall($ks);                    foreach ($t_pv as $day => $v){                        if($day == 'total'){                            continue;                        }                        if($day == date('Y-m-d')){                            continue;                        }                        $uv_key = 'book:' . $i . ':beforeforcesub:uv:date:' . $day . ':send_order_id:' . $send_order_id;                        //$t_uv = (int)Redis::scard($uv_key);                        //$data[] = ['day' => $day, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $v, 'uv' => $t_uv, 'type' => 5, 'created_at' => $now, 'updated_at' => $now];                        Redis::del($uv_key);                        Redis::hdel($ks,$day);                    }                    if(!Redis::hget($ks,date('Y-m-d'))){                        Redis::del($ks);                    }                    //DB::table('wap_visit_stats')->insert($data);                }            }        }    }    private function beforeForceSubAndBeforeVipStatsEveryDay()    {        //$uv_key = 'book:'.$book->bid.':beforevip:uv:date:'.$date.':send_order_id:'.$send_order_id;        //$pv_key = 'book:'.$book->bid.':beforevip:pv:send_order_id:'.$send_order_id;        $now = date('Y-m-d H:i:s');        $day = date('Y-m-d',time()-86400);        //Redis::SISMEMBER('beforevip'.$date,$book->bid.'-'.$send_order_id);        $alls = Redis::SMEMBERS('beforevip'.$day);        $j = 0;        $data = [];        foreach ($alls as $v){            $temp = explode('-',$v);            $i = $temp[0];            $send_order_id = $temp[1];            $like_key = 'book:' . $i . ':beforevip:pv:send_order_id:';            $kkk = $like_key.$send_order_id;            $temp_pv = Redis::hget($kkk,$day);            $uv_key = 'book:' . $i . ':beforevip:uv:date:' . $day . ':send_order_id:' . $send_order_id;            $temp_uv = (int)Redis::scard($uv_key);            $data[] = ['day' => $day, 'key' => $i, 'from_type' => $send_order_id, 'pv' => $temp_pv, 'uv' => $temp_uv, 'type' => 4, 'created_at' => $now, 'updated_at' => $now];            Redis::del($uv_key);            Redis::hdel($kkk,$day);            if($j && $j%100 == 0){                DB::table('wap_visit_stats')->insert($data);                $data = [];            }            if(!Redis::hget($kkk,date('Y-m-d'))){                Redis::del($kkk);            }            $j++;        }        if($data){            DB::table('wap_visit_stats')->insert($data);        }        Redis::del('beforevip'.$day);        $data = [];        // Redis::sadd('beforeforcesub'.$date,$book->bid.'-'.$send_order_id);        $alls = Redis::SMEMBERS('beforeforcesub'.$day);        if($alls){            $i = 0;            foreach ($alls as $vs){                $temp = explode('-',$vs);                $bid = $temp[0];                $send_order_id = $temp[1];                $like_key = 'book:' . $bid . ':beforeforcesub::pvsend_order_id:';                $kkk = $like_key.$send_order_id;                $temp_pv = Redis::hget($kkk,$day);                $uv_key = 'book:' . $bid . ':beforeforcesub:uv:date:' . $day . ':send_order_id:' . $send_order_id;                $temp_uv = (int)Redis::scard($uv_key);                $data[] = ['day' => $day, 'key' => $bid, 'from_type' => $send_order_id, 'pv' => $temp_pv, 'uv' => $temp_uv, 'type' => 5, 'created_at' => $now, 'updated_at' => $now];                Redis::del($uv_key);                Redis::hdel($kkk,$day);                if($i && $i%100 == 0){                    DB::table('wap_visit_stats')->insert($data);                    $data = [];                }                if(!Redis::hget($kkk,date('Y-m-d'))){                    Redis::del($kkk);                }                $i++;            }            if($data){                DB::table('wap_visit_stats')->insert($data);            }        }        Redis::del('beforeforcesub'.$day);    }    private function getFrom(int $channel_id):array {        $from = ['point_push','not_pay','hot_push','unpaid_send_activity','pay_daily_push',            'auto_custom_trusteeship','subscribe_push','sign_push','reader','signcallback','reader-year','recovery_push'];        $custom_ids = DB::table('custom_send_msgs')->select('id','trusteeship')->where('distribution_channel_id',$channel_id)->get();        if($custom_ids){            foreach ($custom_ids as $item){                if($item->trusteeship){                    for ($i=1;$i<=4;$i++){                        $from[] = 'custom_'.$item->id.'_'.$i;                    }                    $from[] = 'custom_'.$item->id;                }else{                    $from[] = 'custom_'.$item->id;                }            }        }        $template = DB::table('wechat_template_msgs')->select('id')->where('distribution_channel_id',$channel_id)->get();        if($template)  {            foreach ($template as $val){                $from[] = 'template_'.$val->id;            }        }        $keyword = DB::table('wechat_keyword_msgs')->select('id')->where('distribution_channel_id',$channel_id)->get();        if($keyword){            foreach ($keyword as $value){                $from[] = 'keyword_'.$value->id;            }        }        return $from;    }    private function subscribeKeyDel(){        $account = $this->officialAccount();        $key_format = 'www.zhuishuyun.comforce_subscribe:scene:%s:%s';        //print_r($account);return;        /*for ($i=1;$i<10000;$i++){            foreach ($account as $appid){                Redis::del(sprintf($key_format,$i,$appid));            }        }*/        for ($i = 10;$i<100;$i++){            $skip = $i*10000;            $res = DB::table('users')->select('id','distribution_channel_id')->orderBy('id')->where('id','>=',$skip)->limit(10000)->get();            foreach ($res as $v){                if(isset($account[$v->distribution_channel_id]) &&  $account[$v->distribution_channel_id]){                    foreach ($account[$v->distribution_channel_id] as $appid){                        Redis::del(sprintf($key_format,$i,$appid));                    }                }            }            $res = null;        }    }    private function officialAccount():array{        $res = DB::table('official_accounts')->select('distribution_channel_id','appid')->get();        $data = [];        foreach ($res as $v){            $data[$v->distribution_channel_id][] = $v->appid;            //$data[] = $v->appid;        }        return $data;    }    private function substituteStats(){        $day = date('Y-m-d',time()-86400);        //代充值按钮        $substituteButtonUvPv = Redis::SMEMBERS('substitutebuttonpvuv'.$day);        if($substituteButtonUvPv){            $i = 0;            $data = [];            foreach ($substituteButtonUvPv as $channel_id){                $temp_pv = Redis::hget('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,$day);                $temp_uv_key = sprintf('substitutebuttonpvuv:distribution_channel_id:%s:date:%s',$channel_id,$day);                $temp_uv = Redis::scard($temp_uv_key);                $data[] = [                    'day' => $day,                    'key' => $channel_id,                    'from_type' => 'substitutebuttonpvuv',                    'pv' => $temp_pv,                    'uv' => $temp_uv,                    'type' => 6,                    'created_at' => date('Y-m-d H:i:s'),                    'updated_at' => date('Y-m-d H:i:s')                ];                if($i && $i%100 == 0){                    DB::table('wap_visit_stats')->insert($data);                    $data = [];                }                $i++;                Redis::del($temp_uv_key);                Redis::hdel('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,$day);                $temp = Redis::hget('substitutebuttonpvuv:distribution_channel_id:' . $channel_id,date('Y-m-d'));                if(!$temp){                    Redis::del('substitutebuttonpvuv:distribution_channel_id:' . $channel_id);                }            }            if($data){                DB::table('wap_visit_stats')->insert($data);            }        }        Redis::del('substitutebuttonpvuv'.$day);        //代充值页面        $substituteButtonUvPv = Redis::SMEMBERS('substitutepagepvuv'.$day);        if($substituteButtonUvPv){            $i = 0;            $data = [];            foreach ($substituteButtonUvPv as $channel_id){                $temp_pv = Redis::hget('substitutepagepvuv:distribution_channel_id:' . $channel_id,$day);                $temp_uv_key = sprintf('substitutepagepvuv:distribution_channel_id:%s:date:%s',$channel_id,$day);                $temp_uv = Redis::scard($temp_uv_key);                $data[] = [                    'day' => $day,                    'key' => $channel_id,                    'from_type' => 'substitutepagepvuv',                    'pv' => $temp_pv,                    'uv' => $temp_uv,                    'type' => 7,                    'created_at' => date('Y-m-d H:i:s'),                    'updated_at' => date('Y-m-d H:i:s')                ];                if($i && $i%100 == 0){                    DB::table('wap_visit_stats')->insert($data);                    $data = [];                }                $i++;                Redis::del($temp_uv_key);                Redis::hdel('substitutepagepvuv:distribution_channel_id:' . $channel_id,$day);                $temp = Redis::hget('substitutepagepvuv:distribution_channel_id:' . $channel_id,date('Y-m-d'));                if(!$temp){                    Redis::del('substitutepagepvuv:distribution_channel_id:' . $channel_id);                }            }            if($data){                DB::table('wap_visit_stats')->insert($data);            }        }        Redis::del('substitutepagepvuv'.$day);    }    private function StylePushStats(){        $date = date('Y-m-d',time()-86400);        $record = Redis::SMEMBERS(sprintf('push:stylepush:date:%s',$date));        if(!$record)return ;        $i = 0;        $data = [];        foreach ($record as $item){            $item_array = explode('_',$item);            $distribution_channel_id = $item_array[0];            $gxhp = $item_array[1];            $uv_key = sprintf('push:stylepush:uv:%s:gxhp:%s:date:%s',$distribution_channel_id,$gxhp,$date);            $pv_key = sprintf('push:stylepush:pv:%s:gxhp:%s:date:%s',$distribution_channel_id,$gxhp,$date);            $uv = Redis::scard($uv_key);            $pv = Redis::get($pv_key);            $data[] = [                'day' => $date,                'key' => $distribution_channel_id,                'from_type' => $gxhp,                'pv' => $pv,                'uv' => $uv,                'type' => 8,                'created_at' => date('Y-m-d H:i:s'),                'updated_at' => date('Y-m-d H:i:s')            ];            Redis::del($uv_key);            Redis::del($pv_key);            if($i && $i%100 == 0){                DB::table('wap_visit_stats')->insert($data);                $data = [];            }            $i++;        }        if($data){            DB::table('wap_visit_stats')->insert($data);        }        Redis::del(sprintf('push:stylepush:date:%s',$date));    }    private function crm(){        $date = date('Y-m-d',time()-86400);        $record = Redis::SMEMBERS('crm_'.$date);        $data = [];        $sql = "select order_params.data_hour_key,sum(price) as charge_amount, count(*) as charge_numfrom orders join order_params on orders.id = order_params.order_id where orders.created_at BETWEEN '%s' and '%s' and order_params.gxhp='crm'  and orders.status = 'PAID'GROUP by order_params.data_hour_key";        if($record){            foreach ($record as $value){                $pv = (int)Redis::hget(sprintf('crm:pv:%s',$value),$date);                $uv = (int)Redis::hget(sprintf('crm:uv:%s',$value),$date);                $crm_param_info = explode('_',$value);                $crm_id = isset($crm_param_info[1])?  $crm_param_info[1]:0;                 $data[] =                    ['crm_id'=>(int)$crm_id,'day'=>$date,'pv'=>$pv,'crm_param'=>$value,                        'uv'=>$uv,'charge_num'=>0,'charge_amount'=>0,                        'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];                Redis::hdel(sprintf('crm:pv:%s',$value),$date);                Redis::hdel(sprintf('crm:uv:%s',$value),$date);            }            if($data){                DB::table('crm_stats')->insert($data);            }        }        $data = [];        $order_info = DB::select(sprintf($sql,$date,date('Y-m-d 00:00:00')));        if($order_info){            foreach ($order_info as $o){                $crm_param_info = explode('_',$o->data_hour_key);                $crm_id = isset($crm_param_info[1])?  $crm_param_info[1]:0;                $update_status = DB::table('crm_stats')->where('day',$date)->where('crm_param',$o->data_hour_key)->update([                    'charge_num'=>$o->charge_num,                    'charge_amount'=>$o->charge_amount                ]);                if(!$update_status){                    $data[] =                        ['crm_id'=>(int)$crm_id,'day'=>$date,'pv'=>0,'crm_param'=>$o->data_hour_key,                            'uv'=>0,'charge_num'=>$o->charge_num,'charge_amount'=>$o->charge_amount,                            'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];                }            }            if($data){                DB::table('crm_stats')->insert($data);            }        }        Redis::del('crm_'.$date);    }    public function InnerOuterWeixinStats(){        $day = date('Y-m-d',time()-86400);        $keys = Redis::SMEMBERS('InnerOuterWeixinStats:'.$day);        if (!$keys) {            return ;        }        $data = [];        $i = 0;        foreach ($keys as $k){            list($from,$distribution_channel_id) = explode(':',$k);            if(!$from || !$distribution_channel_id) continue;            $pv_key = sprintf('InnerOuterWeixinStats:pv:%s:%s',$from,$distribution_channel_id);            $uv_key = sprintf('InnerOuterWeixinStats:uv:%s:%s:%s',$from,$distribution_channel_id,$day);            $pv = Redis::hget($pv_key,$day);            $uv = Redis::scard($uv_key);            $type = '';            $param = '';            if(is_numeric($from)){                $type = 'send_order';                $param = $from;            }            if(str_contains($from,['template'])){                $type = 'template';                list($temp,$param) = explode('_',$from);            }            if(str_contains($from,['custom'])){                $type = 'custom';                list($temp,$param) = explode('_',$from);            }            Redis::del($uv_key);            Redis::hdel($pv_key,$day);            if(!$type || !$param || !is_numeric($param)) continue;            $data[] = [                'type'=>$type,                'param'=>$param,                'pv'=>$pv,                'uv'=>$uv,                'day'=>$day,                'charge'=>0,                'updated_at'=>date('Y-m-d H:i:s'),                'created_at'=>date('Y-m-d H:i:s')            ];            if($i++ % 100 == 0){                DB::table('inner_outer_weixin_stats')->insert($data);                $data = [];            }        }        if($data){DB::table('inner_outer_weixin_stats')->insert($data);}        Redis::del('InnerOuterWeixinStats:'.$day);    }}
 |