<?php

namespace App\Console\Commands;

use App\Http\helper\Helper;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class DbQueue extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'db_queue';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '批量处理redis队列插入数据';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        /*
         * redis队列数据格式要求：
         * db: 数据库前缀;table: 表名;type: 操作类型;data: 数据表内字段和数据,字段需要与表字段保持一致
         * 例: {"db": "QPRecordDB.dbo.", "table": "RecordNewThunderBonueLogs", "type": "insert", "data": {"UserID": 123, "KindID": 6001, "SortID": 1,"BuyBase": 600, "BuyScore": 60000, "ChangeScore": 0, "Control": 10, "Type": 2}}
         */
        $redis = Redis::connection('ServerGameRedis');

        while(true){
            $queueData = $redis->command('brpop', ['dbQueue', 30]);
            if ($queueData && count($queueData) > 1) {
                $jsonArr = json_decode($queueData[1], true);
                $data = [];
                if ($jsonArr) {
                    foreach ($jsonArr['data'] as $k => $v) {
                        $data[$k] = $v;
                    }
                    Log::info('Redis队列任务：' . json_encode($data));
                    if($jsonArr['table'] == 'UserWinRank'&&false){
                        DB::connection('sqlsrv')->table($jsonArr['db'] . $jsonArr['table'])->where('UserID',$data['UserID'])->increment('Score',$data['Score']);
                        if(@$data['CellScore']){
                            //更新子游戏榜单
                            $obj = [
                                'CellScore' => $data['CellScore'],
                                'Datatime' => date('Y-m-d H:i:s'),
                                'Icon' => "1,11,3",
                                "Multiple" => intval($data['Score']/$data['CellScore']),
                                "NickName" => $data['NickName'],
                                "TotalScore" => $data['Score'],
                                "UserID" => $data['UserID']
                            ];
                            $redis->zadd('subgameranking_'.$data['KindID'].'_'.$data['SortID'],intval($data['Score']/$data['CellScore']),json_encode($obj));
                        }

                    }else if($jsonArr['table'] == 'RecordNewThunderBonueLogs'){
                        DB::connection('sqlsrv')->table($jsonArr['db'] . $jsonArr['table'])->insert($data);
                    }else if($jsonArr['table'] == 'RecordJackpot'){
                        $data['created'] = date('Y-m-d H:i:s');
                        DB::connection('sqlsrv')->table($jsonArr['db'] . $jsonArr['table'])->insert($data);
                    }

                }
            }else{
                sleep(5);
            }
        }
    }
}
