<?php


namespace App\Http\Controllers\Api;


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

class ChannelController
{
    public function index()
    {
        $list = DB::table('QPPlatformDB.dbo.ChannelConfig')->where('ReviewState', 1)->get();

        return apiReturnSuc($list);
    }

    // 渠道包开放的充值渠道
    public function ChannelOpenRecharge(Request $request)
    {
        $Channel = $request->Channel ?: '0';
        $channelNumber = $request->ChannelNumber ?: null;


        if (!$channelNumber) {
            $channelNumber = DB::connection('write')->table('QPPlatformDB.dbo.ChannelPackageName')
                ->where('PackageName', $Channel)->value('Channel');
        }
        $list = DB::connection('write')->table('QPPlatformDB.dbo.ChannelOpenRecharge as go')
            ->join('agent.dbo.admin_configs as cf', 'go.ConfigID', '=', 'cf.id')
            ->where('go.Channel', $channelNumber)
            ->where('go.Status', 1)
            ->where('cf.type', 'pay_method')
            ->orderByDesc('go.Sort')
            ->select('name', 'ConfigID')
            ->get();



        if (!isset($list) || $list->count() == 0) {  // 默认配置
            $list = DB::connection('write')->table('agent.dbo.admin_configs')
                ->where('type', 'pay_method')
                ->where('status', 1)
                ->select('id as ConfigID')
                ->get();
        }
        return apiReturnSuc($list);
    }

    // 渠道包开放的游戏渠道
    public function ChannelGameOpen(Request $request)
    {
        $PackageName=$request->PackageName?:0;
        if($PackageName)$Channel=$PackageName;
        else $Channel = $request->Channel ?: 1;
        $cv=1;
        if(isset($request->cv)){
            $cvstr=explode('.',$request->cv);
            $cv=array_pop($cvstr);
        }
        $list=self::searchChannel($Channel,$cv);
        return apiReturnSuc($list);
    }
    public static function searchChannel($Package, $cv){
        $key="gamelist_{$Package}_{$cv}";

        if(Redis::exists($key)){
            try{
                $list= \GuzzleHttp\json_decode(Redis::get($key), true);
            }catch (\Exception $e){

            }
        }
        if(!isset($list)||!count($list)){
            if ($Package != 1) {
                $list = DB::connection('write')->table('QPPlatformDB.dbo.ChannelPackageName as co')
                    ->leftJoin('QPPlatformDB.dbo.ChannelGameOpen as go', 'co.Channel', 'go.Channel')
                    ->where('PackageName', $Package)
                    ->where('Status', 1)
                    ->where('LimitCV', "<=", $cv)
                    ->whereIn('KindID', config('games.openKGame'))
                    ->orderByDesc('go.Sort')
                    ->select('KindID', 'IconType', 'NewGame')
                    ->get();

            }

            if (!isset($list) || $list->count() == 0) {  // 默认配置
                $list = DB::connection('write')->table('QPPlatformDB.dbo.ChannelGameOpen')
                    ->where('Channel', -1)
                    ->where('Status', 1)
                    ->where('LimitCV', "<=", $cv)
                    ->whereIn('KindID', config('games.openKGame'))
                    ->orderByDesc('Sort')
                    ->select('KindID', 'IconType', 'NewGame')
                    ->get();
            }
            Redis::set($key,json_encode($list));
            Redis::expire($key,60);
        }
        return $list;
    }
    // 渠道包是否展示在新次级游戏界面
    public function ChannelSecondStage(Request $request)
    {
        $Channel = $request->Channel ?: '0';

        if ($Channel != 1) {
            $list = DB::connection('write')->table('QPPlatformDB.dbo.ChannelPackageName as co')
                ->leftJoin('QPPlatformDB.dbo.ChannelGameOpen as go', 'co.Channel', 'go.Channel')
                ->where('PackageName', $Channel)
                ->where('Status', 1)
                ->whereIn('KindID', config('games.openKGame'))
                ->orderByDesc('go.SecondStageSort')
                ->select('KindID')
                ->selectRaw('IsNull(SecondStageStatus,2) SecondStageStatus,IsNull(SecondStageSort,0) SecondStageSort')
                ->get();

        }

        if (!isset($list) || $list->count() == 0) {  // 默认配置
            $list = DB::connection('write')->table('QPPlatformDB.dbo.ChannelGameOpen')
                ->where('Channel', -1)
                ->where('Status', 1)
                ->whereIn('KindID', config('games.openKGame'))
                ->orderByDesc('SecondStageSort')
                ->select('KindID')
                ->selectRaw('IsNull(SecondStageStatus,2) SecondStageStatus,IsNull(SecondStageSort,0) SecondStageSort')
                ->get();
        }

        return apiReturnSuc($list);
    }

    // 分渠道展示游戏分享链接
    public function shareUrl(Request $request)
    {
        $Channel = $request->Channel ?: 100;

        $GameShareUrl = '';
        $UrlSelect = 1;
        if (!empty($Channel)) {
            $res = DB::table(TableName::QPPlatformDB() . 'ChannelPackageName')
                ->where('Channel', $Channel)
                ->first();
            if ($res && $res->UrlStatus && $res->UrlSelect == 1) {
                $GameShareUrl = $res->ShareUrl;
            }
            if ($res && $res->UrlStatus && $res->UrlSelect == 2) {
                $GameShareUrl = $res->SpreadShareUrl;
                $UrlSelect = $res->UrlSelect;
            }
        }

        if (empty($GameShareUrl) || empty($Channel)) {
            $GameShareUrl = DB::table(TableName::QPAccountsDB() . 'SystemStatusInfo')
                ->where('StatusName', 'GameShareUrl')
                ->value('StatusString');
        }


        $data = compact('GameShareUrl', 'UrlSelect');
        return apiReturnSuc($data);
    }
}
