<?php

namespace 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_num
from 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);
    }
}