<?php


namespace App\Http\Controllers\Admin;


use App\Facade\TableName;
use App\Http\helper\NumConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class WinningsController
{
    /**
     * 彩金池管理
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function operate()
    {
        $Games = DB::table(TableName::QPPlatformDB() . 'RoomConfig')
            //->whereIn('GameID', GameRoomInfo::OpenKindItem)
            ->whereIn('GameID', config('games.openKGame'))
            ->select('GameID', 'SortID', 'RoomName')
            ->orderBy('GameID')
            ->orderBy('SortID')
            ->get();

        $redis = Redis::connection('ServerGameRedis');
//        $redis = Redis::connection();
        foreach ($Games as &$game) {

            $game->Value = $redis->get("jackpot_{$game->GameID}_$game->SortID") / NumConfig::NUM_VALUE;
        }
        $data = compact('Games');
        return view('admin.winnings.operate', $data);
    }

    /**
     * 彩金池修改值
     * @param Request $request
     * @param $GameID
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function update(Request $request,$GameID)
    {

        $SortID = $request->SortID;

        if ($request->isMethod('post')) {
            $Value = (int)$request->Value;
            $redis = Redis::connection('ServerGameRedis');
//            $redis = Redis::connection();
            if ($Value < 0) {

                $Value = abs($Value) * NumConfig::NUM_VALUE;
                $redis->decrBy("jackpot_{$GameID}_{$SortID}", $Value);

            } else {
                $redis->incrBy("jackpot_{$GameID}_{$SortID}", $Value * NumConfig::NUM_VALUE);
            }

            return apiReturnSuc();
        } else {
            return view('admin.winnings.update', compact('GameID', 'SortID'));
        }


    }
}
