<?php


namespace App\Http\Controllers\Api;


use App\Facade\TableName;
use App\Game\GameCard;
use App\Http\helper\HttpCurl;
use App\Http\helper\NumConfig;
use App\Jobs\Order;
use App\Notification\TelegramBot;
use App\Services\Extension;
use App\Services\OrderServices;
use App\Util;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class TestController
{
    public function tele(Request $request){
        $telegram=new TelegramBot();
        return $telegram->sendMsg("test123");
    }
    public function index(Request $request)
    {
        return;

        $DateID = $request->input('date',date('Ymd',strtotime('-1 days')));

        $only=GameCard::where('brand','OnlyPlay')->where('state','<>',0)->pluck('gid')->toArray();

        $atmosfera=[71,74,85,86,87,88,89,92,93];

        $platform = ['only','atmosfera'];
        foreach ($platform as $pItem){
            $platformRecord = [
                'ldate' => $DateID,
                'platform' => $pItem,
                'all_bet' => Redis::get('platform_'.$pItem.'_bet')?:0,
                'all_win' => Redis::get('platform_'.$pItem.'_win')?:0,
                'current_bet' => Redis::get('platform_'.$pItem.'_bet_'.$DateID)?:0,
                'current_win' => Redis::get('platform_'.$pItem.'_win_'.$DateID)?:0,
                'current_play' =>Redis::get('platform_'.$pItem.'_play_'.$DateID)?:0,
                // 'sub_detail' => ''
            ];
            $gameType = ($pItem == 'only'?$only:$atmosfera);
            if(is_array($gameType)){
                $subData = [];
                foreach ($gameType as $item){
                    $betkey = 'platform_'.$pItem.'_'.$item. '_bet_' . $DateID;

                    $winkey = 'platform_'.$pItem.'_'.$item. '_win_' . $DateID;

                    $playkey = 'platform_'.$pItem.'_'.$item.'_play_' . $DateID;
                    $subData[$item] = ['current_bet' => Redis::get($betkey)?:0,'current_win' =>Redis::get($winkey)?:0,'current_play' =>Redis::get($playkey)?:0 ];
                }
                $platformRecord['sub_detail'] = json_encode($subData);
            }

            DB::connection('mysql')->table('record_platform_data')->insert($platformRecord);
        }


        return apiReturnSuc([1]);
    }
    public function adjust2(){
        $gateway='https://s2s.adjust.com/event';
        $datas='[{"gps_adid":"7c5f80b2-f558-4074-97fd-55f90e7cdb71","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689822903},{"gps_adid":"8ae07275-cf91-4289-8e61-e1bb4fe1294d","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689823553},{"gps_adid":"4abe169d-f5fc-469e-a7ef-b939f8c48bee","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689823996},{"gps_adid":"56900bac-83f4-4516-9acf-ecf46d6dc139","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689824773},{"gps_adid":"527e198d-e195-4672-9c71-3b65a2514624","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689826682},{"gps_adid":"01b0ba26-1828-42e4-8a59-f51006c9dc43","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689827034},{"gps_adid":"f66ca0c2-48ff-4289-9543-094e4fd45599","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689827070},{"gps_adid":"3bee22c7-0e7a-4042-b45a-ab9b3def6bdc","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689828766},{"gps_adid":"849c3938-943d-4ab2-bc8b-d2a84eb43d73","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689828827},{"gps_adid":"f451ff10-1ad3-41ce-83e9-a3d55be093a2","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689829788},{"gps_adid":"3ea51904-c0d1-40fe-afab-080959c911ab","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689830522},{"gps_adid":"ee164ef5-9f6b-4cf4-b853-db303bf9666d","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689830935},{"gps_adid":"4fd5a944-74ef-4d96-aef9-4921bc32a16b","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689831156},{"gps_adid":"56faedc1-c2ae-4f61-a4c8-8e611ebf6b69","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689832059},{"gps_adid":"c157e8e5-c0d4-4ed8-8e95-65064ee8db23","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689833661},{"gps_adid":"de2e15ba-a935-467a-9168-5ac52c45ec81","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689833731},{"gps_adid":"6955aabf-e96c-4382-9cf3-be82435feb90","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689833887},{"gps_adid":"b6d869c7-386a-4ceb-9aa9-303939af55c3","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689834279},{"gps_adid":"5f265835-9001-4941-acb8-1a37fc422268","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689834665},{"gps_adid":"5ec502d1-1c0b-4854-a936-2178011374a3","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689834758},{"gps_adid":"87ddeffc-8ae2-4b72-900b-4b1a269b349a","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689835293},{"gps_adid":"2d66986f-c3e8-45f0-a08a-1eeea18b9f70","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689836877},{"gps_adid":"8b77da3b-e5d8-44db-8753-0714121b0056","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689838394},{"gps_adid":"849c3938-943d-4ab2-bc8b-d2a84eb43d73","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689840333},{"gps_adid":"d8feceb8-9608-4504-abaa-8f306a70b239","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689841124},{"gps_adid":"ac5d8955-cf8a-4a10-97da-0d59446de457","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689841781},{"gps_adid":"814e3bdf-d8fa-4fcf-a4e9-f20153030c07","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689842404},{"gps_adid":"ac5d8955-cf8a-4a10-97da-0d59446de457","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689842700},{"gps_adid":"7e1a79ef-740c-4015-9a05-80dfe0d113ee","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689844332},{"gps_adid":"849c3938-943d-4ab2-bc8b-d2a84eb43d73","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689846981},{"gps_adid":"80ffe7c7-e491-4bc9-be5d-999fb0f4d34c","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689847370},{"gps_adid":"d2d42ca3-a0f4-44e6-8b9d-c234917a9dbd","event_token":"u5skwu","app_token":"81v0cmjzx9c0","s2s":1,"created_at_unix":1689848297} ]';
        $datas=json_decode($datas,true);
//        print_r($datas);
        echo "<PRE>";
        $i=0;
        $lasti=0;
        $key="adjust_test_no2";
        if(Redis::exists($key)) {
            $lasti=(int)Redis::get($key);
        }
        $step=20;
        $start=0;
        foreach ($datas as $data) {
            $i++;
            if($i>$lasti) {
                $curl = new HttpCurl();
                $data['app_token'] = 'dgc2q9jhsfeo';
                $data['event_token'] = '8m7lz8';
                $data['created_at_unix']=time();
//                print_r($data);
                $res = $curl->curlPost($gateway, $data,'');
                echo "<br/>{$i}_{$res}";
                $start++;
                if($start>=$step)break;

//                sleep(1);
//                $curl = new HttpCurl();
//                $data['event_token'] = '2on9ll';
//                $data['created_at_unix']=time();
//                $res = $curl->curlPost($gateway, $data, '');
//                print_r($res);
            }

        }
        $lasti=$i;
        Redis::set($key,$lasti);
    }

    public function adjust(){
        $gateway='https://s2s.adjust.com/event';
        $datas='[{"gps_adid":"42897349-cef9-4631-b379-096b622ce82f","event_token":"dscml0","app_token":"grajj7r58n40","s2s":1,"created_at_unix":1693841080},{"gps_adid":"42897349-cef9-4631-b379-096b622ce82f","event_token":"5fxu12","app_token":"grajj7r58n40","s2s":1,"created_at_unix":1693841079,"revenue":10,"currency":"BRL"},{"gps_adid":"42897349-cef9-4631-b379-096b622ce82f","event_token":"gd6jin","app_token":"grajj7r58n40","s2s":1,"created_at_unix":1693841078} ,{"gps_adid":"d3bef6f8-5120-4668-adcc-0059a87cc9f2","event_token":"dscml0","app_token":"grajj7r58n40","s2s":1,"created_at_unix":1693873946},{"gps_adid":"d3bef6f8-5120-4668-adcc-0059a87cc9f2","event_token":"gd6jin","app_token":"grajj7r58n40","s2s":1,"created_at_unix":1693873944},{"gps_adid":"d3bef6f8-5120-4668-adcc-0059a87cc9f2","event_token":"5fxu12","app_token":"grajj7r58n40","s2s":1,"created_at_unix":1693873945,"revenue":10,"currency":"BRL"}]';
        $datas=json_decode($datas,true);
        print_r($datas);
        echo "<PRE>";
        $i=0;
        $tokens=[
            'gd6jin'    => 'pi6if1',
            '5fxu12'   => 'ltp413',
            'dscml0' => 'lt7ruq',
            '1n9joo' => 'rvwp03',
        ];


        foreach ($datas as $data) {

            $curl = new HttpCurl();
            $data['app_token'] = 'ok69c325y6tc';
            $data['event_token'] = $tokens[$data['event_token']];
            $data['created_at_unix']=time();
//                print_r($data);
            $res = $curl->curlPost($gateway, $data,'');
            echo "<br/>{$i}_{$res}";

//                sleep(1);
//                $curl = new HttpCurl();
//                $data['event_token'] = '2on9ll';
//                $data['created_at_unix']=time();
//                $res = $curl->curlPost($gateway, $data, '');
//                print_r($res);

        }
    }
    public function repayOrders(){
        $orders=['BR907343225223468585744','BR9073084791786501864977','BR9072862766923101651997','BR9072629966687041935344','BR9072421424348241331333','BR9072367341848521637381','BR9072361173854741395682','BR907231397789871209229','BR9072154682810471428647','BR9071943851446521819498','BR9071918503504261720812','BR9071781047537621151100','BR9071664665360441673957','BR9071441992298301481029','BR907084559613508172139'];

        foreach ($orders as $order){
            $res=$this->repayOrder($order,true);
            echo $order;
            print_r($res);
        }
    }
    public function repayOrder($order_sn,$forceRepay=false){




        try {
            // 查询订单信息
            $order = DB::connection('write')->table('agent.dbo.order')->where('order_sn', $order_sn)->first();

            if (!$order) {
                return '{"success":false,"message":"订单不存在"}';
            }

            //如果不是强制重新支付
            if (!$forceRepay&&(!empty($order->pay_at) || !empty($order->finished_at)) ) {
                return 'SUCCESS';
            }



            $body = [
                'payment_sn' => '',
                'updated_at' => date('Y-m-d H:i:s'),
            ];

            $ordStatus = 1;
            $GiftsID = $order->GiftsID ?: '';
            $userID = $order->user_id ?: '';
            $AdId = $order->AdId ?: '';
            $eventType = $order->eventType ?: '';
            $payAmt = $order->amount/100;

            if(!$AdId){
                $redis = Redis::connection();
                $AdId = $redis->get('user_ad_'.$userID);
            }



            // 说明:除了成功，失败和退款状态,都可以认为是处理中
            switch ($ordStatus) {
                case 1:    // 支付成功


                    $body['pay_status'] = 1;
                    $body['pay_at'] = date('Y-m-d H:i:s');
                    $body['finished_at'] = date('Y-m-d H:i:s');
                    $body['amount'] = $payAmt * NumConfig::NUM_VALUE;

                    // 获取金额
                    $service = new OrderServices();

                    if ($order->amount != $body['amount']) {
                        $body['GiftsID'] = 0;
                        $body['amount'] = $payAmt * NumConfig::NUM_VALUE;
                        $Recharge = $payAmt;
                        $give = 0;
                        $favorable_price = $Recharge + $give;
                        $czReason = 1;
                        $cjReason = 45;
                    } else {
                        [$give, $favorable_price, $Recharge, $czReason, $cjReason] = $service->getPayInfo($GiftsID, $userID, $payAmt);
                    }
                    if($order->pay_status!=1) {
                        $order_up = DB::connection('write')->table('agent.dbo.order')
                            ->where('order_sn', $order_sn)
                            ->update($body);

                    }
                    [$Score] = $service->addRecord($userID, $payAmt, $favorable_price, $order_sn, $GiftsID, $Recharge, $czReason, $give, $cjReason, $AdId, $eventType);
                    // 成功处理回调
                    Order::dispatch([$userID, $payAmt, $Score,$favorable_price, $GiftsID,$order_sn]);

//                    $cpf = @$post['reference'];
//                    if($cpf){
//                        Cpf::insertCpf($userID,$cpf);
//                    }

                    break;
                case 2:    // 支付失败
                    $body['pay_status'] = 2;
                    $order_up = DB::connection('write')->table('agent.dbo.order')
                        ->where('order_sn', $order_sn)
                        ->update($body);
                    break;
            }

//            $order_up = DB::connection('write')->table('agent.dbo.order')
//                ->where('order_sn', $order_sn)
//                ->update($body);



            return 'SUCCESS';
        } catch (\Exception $exception) {
            Util::WriteLog('goopago','Goopago 支付失败' . $exception->getMessage());
            $this->error = $exception->getMessage();
            return '{"success":false,"message":"商户自定义出错信息"}';
        }
    }
}
