<?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 UserPayUpdateTask extends Command
{
    /**
     * 执行命令   php artisan force_user_active
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'userPayUpdateTask';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新user_pay表字段';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $min_id = DB::table('global_configs')->where('config','pay_user_orders_max_id')->first();
        $min_id = $min_id->config_value;
        $max_id = DB::table('orders')->max('id');
        while ($min_id <= $max_id) {
            \Log::info('insert_sql:'."insert into user_pay (uid,distribution_channel_id,first_recharge_time,first_recharge_amount) 
                select uid,distribution_channel_id,min(created_at),price from orders
                 where status ='PAID'  and orders.id >=".$min_id." and orders.id<=".($min_id+9999)." and not EXISTS
                 (select 1 from user_pay where uid = orders.uid) GROUP BY uid");

            \Log::info("sec_recharge_update:update user_pay set user_pay.sec_recharge_time = (SELECT orders.created_at FROM orders
where user_pay.uid = orders.uid and orders.created_at > user_pay.first_recharge_time and status = 'PAID' LIMIT 1),user_pay.sec_recharge_amount=(SELECT orders.price FROM orders
where user_pay.uid = orders.uid and orders.created_at > user_pay.first_recharge_time and status = 'PAID' LIMIT 1) WHERE user_pay.sec_recharge_time is null");
            $min_id+9999;
        }
    }


    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]);
                }
            }
          
        }

    }

    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();
        }

    }
}


