<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/20
 * Time: 下午5:26
 */

namespace App\Console\Commands;

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


class TemporaryTask extends Command
{
    /**
     * 执行命令   php artisan force_user_active
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'temporaryTask {start_id}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '临时任务';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $access_token = unserialize(Redis::get('[wechat_op.common.access_token.wxceb2aacdce248393][1]'));
        var_dump($access_token);
        return;
        $this->test1();
        return;
        print_r('start_temporary:-----------------' . date('Y-m-d H:i:s'));
        $total_users = $above_five_thousands_total = $above_five_thousands_male = $above_five_thousands_felman = 0;
        $above_three_thousands_total = $above_three_thousands_male = $above_three_thousands_felman = 0;

        $min_uid = $this->argument('start_id');
        for ($i = 1; $i <= 10; $i++) {
            $inside_min_uid = $min_uid + ($i - 1) * 10000;
            $users = DB::table('user_pay')
                ->select('uid')
                ->where([['id', '>=', $inside_min_uid], ['id', '<=', $inside_min_uid + 9999]])
                ->orderBy('id')
                ->get();
            foreach ($users as $user) {
                //单本书15天内有无订阅
                try {
                    $book_order = DB::table('book_orders')
                        ->where('uid', $user->uid)
                        ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-15 day')))
                        ->sum('fee');
                    $table_seq = ($user->uid) % 512;
                    $chapter_order = DB::connection('chapter_order_mysql')
                        ->table('chapter_orders' . $table_seq)
                        ->where('uid', $user->uid)
                        ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-15 day')))
                        ->sum('fee');
                    //\Log::info(($user->uid).$chapter_order.date('Y-m-d 00:00:00',strtotime('-15 day')));
                    //}
                    $subscribe_fee = $book_order + $chapter_order;
                    if ($subscribe_fee > 0) {
                        //\Log::info(($user->uid).':'.$subscribe_fee);
                        $total_users++;
                        $gender = 0;
                        if ($subscribe_fee >= 3000) {
                            $user_channel = DB::table('force_subscribe_users')
                                ->leftjoin('books', 'books.id', '=', 'force_subscribe_users.bid')
                                ->leftjoin('book_categories', 'book_categories.id', '=', 'books.category_id')
                                ->where('force_subscribe_users.uid', $user->uid)
                                ->first();
                            if ($user_channel) {
                                $gender = $user_channel->pid;
                            } else {
                                \Log::error('user_gender_not_found:uid:' . $user->uid);
                            }
                            $above_three_thousands_total++;
                            if ($gender == 1) {
                                $above_three_thousands_male++;
                            }
                            if ($gender == 2) {
                                $above_three_thousands_felman++;
                            }
                        }

                        if ($subscribe_fee >= 5000) {
                            $above_five_thousands_total++;
                            if ($gender == 1) {
                                $above_five_thousands_male++;
                            }
                            if ($gender == 2) {
                                $above_five_thousands_felman++;
                            }
                        }
                    }
                } catch (\Exception $e) {
                    \Log::error('temp task error:' . $e->getMessage());
                }

                print_r('$total_users:' . 'min_uid' . ($min_uid) . 'uid:' . ($user->id) . ':' . $total_users . '||$above_five_thousands_total:' . $above_five_thousands_total . '||$above_five_thousands_felman:' . $above_five_thousands_felman . '||$above_five_thousands_male:' . $above_five_thousands_male . "\r\n");
                print_r('||$above_three_thousands_total:' . $above_three_thousands_total . '||$above_three_thousands_felman:' . $above_three_thousands_felman . '||$above_three_thousands_male:' . $above_three_thousands_male . "\r\n");
                \Log::info('actual_total_users:' . 'min_uid' . ($min_uid) . 'uid:' . ($user->id) . ':' . $total_users . '||$above_five_thousands_total:' . $above_five_thousands_total . '||$above_five_thousands_felman:' . $above_five_thousands_felman . '||$above_five_thousands_male:' . $above_five_thousands_male . "\r\n" . '||$above_three_thousands_total:' . $above_three_thousands_total . '||$above_three_thousands_felman:' . $above_three_thousands_felman . '||$above_three_thousands_male:' . $above_three_thousands_male);
                //\Log::info('||$above_three_thousands_total:'.$above_three_thousands_total.'||$above_three_thousands_felman:'.$above_three_thousands_felman.'||$above_three_thousands_male:'.$above_three_thousands_male."\r\n");
            }
        }

        /*
        DB::table('user_pay')
            ->orderBy('uid','asc')
            ->chunk(1000,function($users) use(&$total_users,&$above_five_thousands_total,&$above_five_thousands_felman,&$above_five_thousands_male,&$above_three_thousands_total,&$above_three_thousands_male,&$above_three_thousands_felman){
                foreach ($users as $user) {
                    //单本书15天内有无订阅
                    try{
                        $book_order = DB::table('book_orders')
                            ->where('uid',$user->uid)
                            ->where('created_at','>=',date('Y-m-d 00:00:00',strtotime('-15 day')))
                            ->sum('fee');
                        //\Log::info(($user->uid).':'.$book_order.date('Y-m-d 00:00:00',strtotime('-15 day')));
                        //$chapter_order=0;
                        //if(!$book_order) {
                        $table_seq = ($user->uid)%512;
                        $chapter_order = DB::connection('chapter_order_mysql')
                            ->table('chapter_orders'.$table_seq)
                            ->where('uid',$user->uid)
                            ->where('created_at','>=',date('Y-m-d 00:00:00',strtotime('-15 day')))
                            ->sum('fee');
                        //\Log::info(($user->uid).$chapter_order.date('Y-m-d 00:00:00',strtotime('-15 day')));
                        //}
                        $subscribe_fee = $book_order + $chapter_order;
                        if($subscribe_fee > 0){
                            //\Log::info(($user->uid).':'.$subscribe_fee);
                            $total_users++;
                            $gender =0;

                            if($subscribe_fee>=3000){
                                $user_channel = DB::table('force_subscribe_users')
                                    ->leftjoin('books','books.id','=','force_subscribe_users.bid')
                                    ->leftjoin('book_categories','book_categories.id','=','books.category_id')
                                    ->where('force_subscribe_users.uid',$user->uid)
                                    ->first();
                                if($user_channel){
                                    $gender=$user_channel->pid;
                                }else{
                                    \Log::error('user_gender_not_found:uid:'.$user->uid);
                                }
                                $above_three_thousands_total++;
                                if($gender==1){
                                    $above_three_thousands_male++;
                                }
                                if($gender==2){
                                    $above_three_thousands_felman++;
                                }
                            }

                            if($subscribe_fee>=5000) {
                                $above_five_thousands_total++;
                                if($gender==1){
                                    $above_five_thousands_male++;
                                }
                                if($gender==2){
                                    $above_five_thousands_felman++;
                                }
                            }
                        }
                    }catch (\Exception $e){
                        \Log::error('temp task error:'.$e->getMessage());
                    }

                    print_r('$total_users:'.$total_users.'||$above_five_thousands_total:'.$above_five_thousands_total.'||$above_five_thousands_felman:'.$above_five_thousands_felman.'||$above_five_thousands_male:'.$above_five_thousands_male."\r\n");
                    print_r('||$above_three_thousands_total:'.$above_three_thousands_total.'||$above_three_thousands_felman:'.$above_three_thousands_felman.'||$above_three_thousands_male:'.$above_three_thousands_male."\r\n");
                    \Log::info('$total_users:uid:'.($user->uid).$total_users.'||$above_five_thousands_total:'.$above_five_thousands_total.'||$above_five_thousands_felman:'.$above_five_thousands_felman.'||$above_five_thousands_male:'.$above_five_thousands_male."\r\n");
                    \Log::info('||$above_three_thousands_total:'.$above_three_thousands_total.'||$above_three_thousands_felman:'.$above_three_thousands_felman.'||$above_three_thousands_male:'.$above_three_thousands_male."\r\n");
                }
            });*/
        \Log::info('final_stats:min_uid:' . ($min_uid) . '$total_users:' . $total_users . '||$above_five_thousands_total:' . $above_five_thousands_total . '||$above_five_thousands_felman:' . $above_five_thousands_felman . '||$above_five_thousands_male:' . $above_five_thousands_male . '$total_users:' . $total_users . '||$above_five_thousands_total:' . $above_five_thousands_total . '||$above_five_thousands_felman:' . $above_five_thousands_felman . '||$above_five_thousands_male:' . $above_five_thousands_male);
        print_r('final_stats:min_uid:' . ($min_uid) . '$total_users:' . $total_users . '||$above_five_thousands_total:' . $above_five_thousands_total . '||$above_five_thousands_felman:' . $above_five_thousands_felman . '||$above_five_thousands_male:' . $above_five_thousands_male . "\r\n");
        print_r('final_stats:min_uid:' . ($min_uid) . '||$above_three_thousands_total:' . $above_three_thousands_total . '||$above_three_thousands_felman:' . $above_three_thousands_felman . '||$above_three_thousands_male:' . $above_three_thousands_male . "\r\n");
        print_r('final_stats:min_uid:' . ($min_uid) . 'end_temporary:-----------------' . date('Y-m-d H:i:s'));
        \Log::info('final_stats:min_uid:' . ($min_uid) . 'end_temporary:-----------------' . date('Y-m-d H:i:s'));
    }


    private function test1()
    {
        $array = ['recent_read','subscribe','text_sign','text_search','readerover','readrecord','daily_sign'];//

//        $array = ['daily_sign'];

        foreach ($array as $v){
            $users = Redis::SMEMBERS("temp_from_where:".$v);
            foreach ($users as $user){
                $sub = DB::connection('chapter_order_mysql')
                    ->table('chapter_orders'.($user%512))
                    ->where('uid',$user)
                    ->whereBetween('created_at',['2018-11-23','2018-12-07 23:59:59'])
                    ->first();
                if($sub){
                    $fee = DB::connection('chapter_order_mysql')
                        ->table($v.'_user')
                        ->select('uid','fee')
                        ->where('uid',$user)
                        ->first();
                    DB::connection('chapter_order_mysql')
                        ->table($v.'_user_sub')
                        ->insert(['uid'=>$user,'fee'=>isset($fee->fee)?$fee->fee:-1]);
                }
            }
             /*DB::connection('chapter_order_mysql')
                 ->table($v.'_user_sub')
                 ->select('uid','fee')
                 ->orderBy('id')
                 ->chunk(10000,function($users) use($v){
                     foreach ($users as $user){
                         $sub = DB::table('chapter_orders'.(($user->uid)%512))
                             ->where('uid',$user->uid)
                             ->whereBetween('created_at',['2018-11-23','2018-12-07 23:59:59'])
                             ->first();
                         if($sub){
                             DB::table($v.'_user_sub')->insert(['uid'=>$user->uid,'fee'=>$user->fee]);
                         }
                     }
            });*/

            //$total_users = $above_five_thousands_total = $above_five_thousands_male = $above_five_thousands_felman = 0;
            //$above_three_thousands_total = $above_three_thousands_male = $above_three_thousands_felman = 0;
            //$users = Redis::SMEMBERS("temp_from_where:".$v);
            /*foreach ($users as $user) {

                DB::connection('chapter_order_mysql')->table($v.'_user')->insert(['uid'=>$user]);
                /*$book_order = DB::table('book_orders')
                    ->where('uid', $user)
                    ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-15 day')))
                    ->first();
                $table_seq = ($user) % 512;
                $chapter_order = DB::connection('chapter_order_mysql')
                    ->table('chapter_orders' . $table_seq)
                    ->where('uid', $user)
                    ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime('-15 day')))
                    ->first();

                if ($book_order || $chapter_order) {
                    //\Log::info(($user->uid).':'.$subscribe_fee);
                    $total_users++;
                    $chapter_order_sum = DB::connection('chapter_order_mysql')
                        ->table('chapter_orders' . $table_seq)
                        ->where('uid', $user)
                        ->sum('fee');
                    $book_order_sum = DB::table('book_orders')
                        ->where('uid', $user)
                        ->sum('fee');
                    $subscribe_fee = $book_order_sum + $chapter_order_sum;
                    $gender = 0;
                    if ($subscribe_fee >= 3000) {
                        $user_channel = DB::table('force_subscribe_users')
                            ->leftjoin('books', 'books.id', '=', 'force_subscribe_users.bid')
                            ->leftjoin('book_categories', 'book_categories.id', '=', 'books.category_id')
                            ->where('force_subscribe_users.uid', $user)
                            ->first();
                        if ($user_channel) {
                            $gender = $user_channel->pid;
                        } else {
                            \Log::error('user_gender_not_found:uid:' . $user);
                        }
                        $above_three_thousands_total++;
                        if ($gender == 1) {
                            $above_three_thousands_male++;
                        }
                        if ($gender == 2) {
                            $above_five_thousands_felman++;
                        }
                    }

                    if ($subscribe_fee >= 5000) {
                        $user_channel = DB::table('force_subscribe_users')
                            ->leftjoin('books', 'books.id', '=', 'force_subscribe_users.bid')
                            ->leftjoin('book_categories', 'book_categories.id', '=', 'books.category_id')
                            ->where('force_subscribe_users.uid', $user)
                            ->first();
                        if ($user_channel) {
                            $gender = $user_channel->pid;
                        } else {
                            \Log::error('user_gender_not_found:uid:' . $user);
                        }
                        $above_three_thousands_total++;
                        if ($gender == 1) {
                            $above_three_thousands_male++;
                        }
                        if ($gender == 2) {
                            $above_five_thousands_felman++;
                        }
                    }


                //}
            }*/
            //return true;
            /*myLog('temp_from_where')->info($v.'---------------------------------------------------');
            myLog('temp_from_where')->info(compact(
                'v',
                'total_users',
                'above_five_thousands_total',
                'above_five_thousands_male',
                'above_five_thousands_felman',
                'above_three_thousands_total',
                'above_three_thousands_male',
                'above_three_thousands_felman'
            ));*/
        }

    }

    private function test2() {
        $users = DB::table('daily_sign_user_sub')->where('fee',-1)->get();
        foreach ($users as $user){
            $fee = DB::table('chapter_orders'.($user%512))
                ->where('uid',$user->uid)
                ->where('created_at','<=','2018-12-07 23:59:59')
                ->sum('fee');
            $user->fee = $fee;
            $user->save();
        }

    }
}


