<?php

namespace App\Game\Services;

use App\Facade\TableName;
use App\Game\Config\GameBasicConfig;
use App\Game\GlobalUserInfo;
use App\Http\helper\HttpCurl;
use App\Http\helper\NumConfig;
use App\Models\RecordScoreInfo;
use App\Notification\TelegramBot;
use App\Util;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Yansongda\Pay\Log;

class OuroGameService
{
    const REASON_updateAccount=83;
    const REASON_PwaBonus=82;
    const REASON_RedEnvelope=12;
    const REASON_BindPhone=21;
    const REASON_AgentBonus=2;
    const REASON_AgentWithDraw=81;
    public static function notifyWebHall($UserID,$GlobalUID="",$cmd='pay_finish',$data=["Golds"=>0,"PayNum"=>0]){

        try {
            $url = str_replace("wss:","https:",GameBasicConfig::$HallServer).'/phpapi';
//        aaa=111&password=wojiushimima&cmd=pay_finish&UserID=1
            if(empty($GlobalUID)){
                if(GlobalUserInfo::$me&&GlobalUserInfo::$me->UserID==$UserID){
                    $user=GlobalUserInfo::$me;
                }else{
                    $user=GlobalUserInfo::getGameUserInfo('UserID',$UserID);
                }
                if(!$user)return;
                $GlobalUID=$user->GlobalUID;
            }
            $query = ['aaa' => 111, 'password' => 'wojiushimima', 'cmd' => $cmd, 'UserID' => $UserID, 'GlobalUID' => $GlobalUID,'data' => json_encode($data)];

            $build_query = $url . '?' . http_build_query($query);
            Util::WriteLog("hallnoti",$build_query);
            return (new HttpCurl())->curl_get($build_query);
//            dd($build_query);
        }catch (\Exception $exception){
            $telegram = new TelegramBot();
            $env = env('APP_ENV');
            $telegram->sendMsg($env."24680 hallnotify error:".$exception->getMessage());
        }
        return false;
    }
    public static function notifyMail($user_id,$GlobalUID="")
    {
        self::notifyWebHall($user_id,$GlobalUID,'call_client',["type"=>"refresh_mail"]);

    }
    public static function getUserInGame($UserID,$GlobalUID="")
    {
        $key='ingame_state_'.$UserID;
        $ingame_state=Redis::get($key)??-1;
        return intval($ingame_state);
    }
    public static function AddScore($UserID,$GiftScore,$reason=null,$record=true)
    {
        // 增加用户金币
        $OrgScore = DB::table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->value('Score');

        Log::info('OuroService变化金币' . $GiftScore);
        DB::table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->increment('Score',$GiftScore);

        $NowScore=$OrgScore+$GiftScore;
        if(!$record){
            return [$OrgScore,$NowScore];
        }
        // 服务器通知

//        $url = config('transfer.stock')['url'] . 'notifyPay';
//
//        $data = [
//            'userid' =>  $UserID,
//            'getScore' => $GiftScore,
//            'score' => $NowScore,
//        ];

//        (new HttpCurl())->service($url, $data);
        self::notifyWebHall($UserID,"",'pay_finish',["Golds"=>$NowScore,"PayNum"=>$GiftScore]);


        RecordScoreInfo::addScore($UserID, $GiftScore, $reason, $OrgScore); #赠送彩金

        if($reason){
            DB::connection('sqlsrv')->unprepared("
            SET NOCOUNT ON;
            use QPRecordDB;
            exec QPRecordDB.dbo.GSP_YN_GR_RecordGameScore $UserID,$GiftScore,$reason,0,'',0,0");
        }

        return [$OrgScore,$NowScore];
    }
    public static function AddDrawBase($UserID, $draw_base, $act_no=2)
    {
        // 增加可提额度
        if (DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->first()) {
            DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->increment('DrawBase', $draw_base);
        } else {
            DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->insert(['UserID' => $UserID, 'DrawBase' => $draw_base]);
        }

        // 增加记录
        DB::table(TableName::agent() . 'add_draw_base')
            ->insert([
                'user_id' => $UserID,
                'draw_base' => $draw_base,
                'create_time' => now(),
                'admin_id' => '24680'.$act_no
            ]);


    }
}
