<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Modules\OfficialAccount\Services\OfficialAccountService;
use App\Modules\Finance\Services\WithdrawCashService;
use App\Modules\User\Models\UserSign;
use App\Modules\User\Models\TempsUserSign;
use App\Modules\User\Models\SmartPushUserSign;
use App\Modules\OfficialAccount\Models\DistributionSelfDefineConfig;
use App\Modules\Finance\Services\FinancialStatService;
use Redis;

/**
 * 签到user_sign异步批量插入
 * @author zhoulingjie
 *
 */
class AsyncUserSign extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'AsyncUserSign';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '签到user_sign异步批量插入';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->start();
    }

    public function start(){
    	\Log::info('AsyncUserSign_start');
    	// 获取redis里面的所有数据
    	$uids = Redis::smembers('user_sign:uid');
    	$loop_num = 100;
    	$loop_current = 0;
    	if(!empty($uids)){
    		//\Log::info('AsyncUserSign_start_count:'.count($uids));
    		$batch_insert_data = [];
    		foreach($uids as $uid){
    			try{
    				$loop_current ++;
    				
    				$insert_data = [];
    				 
    				$sign_data = Redis::hget('user_sign:uid:info',$uid);
    				$sign_data = object_to_array(json_decode($sign_data));
    				//\Log::info('AsyncUserSign_loop_current:'.$loop_current.' uid:'.$uid.' data:'.json_encode($sign_data));
    				if(!empty($sign_data)){
    					$batch_insert_data[] = $sign_data;
    					Redis::srem('user_sign:uid',$uid);
    					Redis::hdel('user_sign:uid:info',$uid);
    				}
    				if($loop_current == $loop_num){
//    					\Log::info('$batch_insert_data');
//    					\Log::info($batch_insert_data);

    					self::batch_sign_insert($batch_insert_data);
    					$batch_insert_data = [];
    					$loop_current = 0;
    				}
    			}catch(\Exception $e){
	    			\Log::info('AsyncUserSign_ept:'.$e->getMessage());
	    		}
    		}
    		
    		// last
    		\Log::info('$last_batch_insert_data');
    		\Log::info($batch_insert_data);
    		self::batch_sign_insert($batch_insert_data);
    
    	}

    	\Log::info('AsyncUserSign_end:'.count($uids));
    }
    
    public static function batch_sign_insert($batch_insert_data){
    	if(!empty($batch_insert_data)){
            $model = new UserSign();
            $model->setCurrentTable(date('Ym'));
            $model->insert($batch_insert_data);
            $model->setCurrentTable( date('Ym',strtotime('next Month')) );
            $model->insert($batch_insert_data);
    		//UserSign::insert($batch_insert_data);
    		// 中间表，保留2个月数据
//     		TempsUserSign::insert($batch_insert_data);
    		// 智能推送表，保留2天数据
    		SmartPushUserSign::insert($batch_insert_data);
    	}
    }
}
