<?php


namespace App\Console\Commands;

use App\Http\AppFlyerEvent\AppflyerEvent;
use App\Http\helper\Helper;
use App\Http\helper\HttpCurl;
use App\Services\StoredProcedure;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '订单队列';

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
//        $redis = new \Redis();
//
//        $redis->connect('127.0.0.1', '6379');

//        $redis->auth('uKliY6dhGYhWAwCW');
//        $redis->connect('127.0.0.1', '6379');

        while (true) {

            $res = Redis::rPop('payService');

            if ($res) {

                $jsonToData = \GuzzleHttp\json_decode($res, true);
                $userID = $jsonToData['userID'];
                $payMoney = $jsonToData['payAmt'];
                $favorable_price = $jsonToData['favorable_price'];
                $Score = $jsonToData['Score'];
                $GiftsID = $jsonToData['GiftsID'];

                // 开始执行时间
                $startTime = Helper::millisecond();

                // 执行存储过程 -- 防刷机制
                StoredProcedure::SetUserTabType($userID);
                //DB::connection('write')->select("SET NOCOUNT ON use QPAccountsDB exec GSP_GP_SetUserTabType12 $userID");

                Log::info('GSP_GP_SetUserTabType12 执行时间：' . ((Helper::millisecond() - $startTime) / 1000));

                # 单控标签 -- 执行存储过程
                StoredProcedure::user_label($userID, 1, $payMoney);

                Log::info('CheckAccountsLabel 执行时间：' . ((Helper::millisecond() - $startTime) / 1000));


                // 服务器通知
                $url = config('transfer.stock')['url'] . 'notifyPay';

                $data = [
                    'userid' => $userID,
                    'getScore' => $favorable_price,
                    'score' => $Score,
                    'giftsid' => empty($GiftsID) ? 0 : $GiftsID
                ];

                (new HttpCurl())->service($url, $data);

                Log::info('中转服 执行时间：' . ((Helper::millisecond() - $startTime) / 1000));

                // AF 事件
                $bool = (new AppflyerEvent())->event($userID, '', 'af_purchase_new', $payMoney);

                Log::info('AF 执行时间：' . ((Helper::millisecond() - $startTime) / 1000));


                echo '李雪峰';


            }
            sleep(3);
        }
    }
}
