<?php

namespace App\Game\Services;

use App\Facade\TableName;
use App\Util;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\DB;

class PlatformService
{


    public static function platformBet($platform, $game, $bet, $uid)
    {
        self::platformAllBet($platform, $bet);
        self::platformDayBet($platform, $bet);
        if ($game)
            self::platformSubDayBet($platform, $game, $bet);
        if ($bet > 0) {
            self::platformDayPlay($platform, $uid);
            if ($game)
                self::platformSubDayPlay($platform, $game, $uid);
        }
        return true;
    }

    public static function platformWin($UserID, $platform, $game, $win, $bet, $score = 0, $addScore = true)
    {

        self::userPlatformWin($platform, $UserID, $win - $bet);

        $time = time();
        $strUniqueCode = "$platform|$game";
        $gameid = ['atmosfera' => 81, 'betby' => 80, 'PP' => 83,'pp' => 83, 'pg' => 84, 'only' => 85, 'aviatrix' => 86,'luckystreak'=>87][$platform] . (is_numeric($game) ? $game : 0);

        $lBeforeScore = $score - $win;
        $lChangeScore = $win - $bet;
        try {
            $dbh = DB::connection()->getPdo();

            $stmt = $dbh->prepare("exec QPTreasureDB.dbo.GSP_GR_RecordScoreInfo $UserID,$lBeforeScore,$lChangeScore,$gameid,0,'$strUniqueCode'");
            $stmt->execute();

        } catch (\Exception $exception) {
            Util::WriteLog('pw', $exception->getMessage() . "\nexec QPTreasureDB.dbo.GSP_GR_RecordScoreInfo $UserID,$lBeforeScore,$lChangeScore,$gameid,0,'$strUniqueCode'");
        }

//        if($win<=0){
//            return false;
//        }
        if ($addScore && $win > 0) {
            DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->increment('Score', $win);
        }

        if ($bet) {
//            $addDraw = $win-$bet;
            $addDraw = floor($bet / 5);
            //用户输赢数据

            // 增加可提额度
            if ($totalData = DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->exists()) {
//                if($totalData->DrawBase+$addDraw>$score){
//                    $addDraw = max($score - $totalData->DrawBase,0);
//                }
//                DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->increment('DrawBase', $addDraw);
                DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->increment('DrawBase', $addDraw);
                DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->increment('TotalBet', $bet);
            } else {
//                DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->insert(['UserID' => $UserID, 'DrawBase' => $addDraw]);
                DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->insert(['UserID' => $UserID, 'DrawBase' => $addDraw, 'TotalBet' => $bet]);
            }
//            $draw_base = intval(($win-$bet) * 0.08);
            $draw_base = intval(($win - $bet) * 0.1);
            if ($draw_base > 0)
                DB::table(TableName::QPRecordDB() . 'RecordUserTotalStatistics')->where('UserID', $UserID)->increment('Revenue', $draw_base);
        }
        if ($win <= 0) {
            return false;
        }
        self::platformAllWin($platform, $win);
        self::platformDayWin($platform, $win);
        self::platformSubDayWin($platform, $game, $win);
        return true;
    }


    /**
     * 总下注
     * @param $platform
     * @param $bet
     * @return void
     */

    ###########平台总数据##################
    public static function platformAllBet($platform, $bet)
    {
        $key = 'platform_' . $platform . '_bet';
        return self::incrementOrSet($key, $bet);
    }

    public static function platformAllWin($platform, $win)
    {
        $key = 'platform_' . $platform . '_win';
        return self::incrementOrSet($key, $win);
    }


    ###########平台每天数据################
    public static function platformDayBet($platform, $bet)
    {
        $key = 'platform_' . $platform . '_bet_' . date('Ymd');
        return self::incrementOrSet($key, $bet, 86400 * 2);
    }

    public static function platformDayWin($platform, $win)
    {
        $key = 'platform_' . $platform . '_win_' . date('Ymd');
        return self::incrementOrSet($key, $win, 86400 * 2);
    }

    public static function platformDayPlay($platform, $uid)
    {
        $playKey = 'platform_' . $platform . '_play_' . $uid . '_' . date('Ymd');
        if (!Redis::exists($playKey)) {
            Redis::set($playKey, 1);
            Redis::expire($playKey, 86400 * 2);
            $key = 'platform_' . $platform . '_play_' . date('Ymd');
            self::incrementOrSet($key, 1, 86400 * 2);
        }
        return true;
    }


    ###########平台子游戏每天数据################
    public static function platformSubDayBet($platform, $game, $bet)
    {
        $key = 'platform_' . $platform . '_' . $game . '_bet_' . date('Ymd');
        return self::incrementOrSet($key, $bet, 86400 * 2);
    }

    public static function platformSubDayWin($platform, $game, $win)
    {
        $key = 'platform_' . $platform . '_' . $game . '_win_' . date('Ymd');
        return self::incrementOrSet($key, $win, 86400 * 2);
    }

    public static function platformSubDayPlay($platform, $game, $uid)
    {
        $playKey = 'platform_' . $platform . '_' . $game . '_play_' . $uid . '_' . date('Ymd');
        if (!Redis::exists($playKey)) {
            Redis::set($playKey, 1);
            Redis::expire($playKey, 86400 * 2);
            $key = 'platform_' . $platform . '_' . $game . '_play_' . date('Ymd');
            self::incrementOrSet($key, 1, 86400 * 2);
        }
        return true;
    }


    public static function userPlatformWin($platform, $uid, $win)
    {
        $dkey = 'platform_' . $platform . '_' . $uid . '_win_' . date('Ymd');
        self::incrementOrSet($dkey, $win, 86400 * 2);

        $key = 'platform_' . $platform . '_' . $uid . '_win';
        self::incrementOrSet($key, $win);
        return true;
    }


    public static function incrementOrSet($key, $value, $expired = 0)
    {
        // 使用 Redis::exists 检查键是否存在
        if (Redis::exists($key)) {
            // 键存在，使用 Redis::incrby 增加相应的数值（如果 $value 是负数，则减少）
            Redis::incrby($key, $value);
        } else {
            // 键不存在，使用 Redis::set 设置为当前值
            Redis::set($key, $value);
            if ($expired) {
                Redis::expire($key, $expired);
            }
        }

        // 返回当前键的值
        return Redis::get($key);
    }

}

