<?php


namespace App\Http\Controllers\Api;


use App\Http\helper\NumConfig;
use App\Models\AccountsInfo;
use App\Services\ExtensionCopy;
use App\Services\Extensions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

// 裂变入口
// 数值统一要除 100
class ExtensionsController
{
    /**
     * /api/extension/getScore?UserID=2111
     * 获取裂变 可领额度
     * @param Request $request
     * @return array
     */
    public function getScore(Request $request)
    {

        $UserID = $request->UserID ?: '';

        if(empty($UserID)||$UserID==0){
            return apiReturnFail();
        }
        $service = new Extensions();
        $AccountsInfoM = new AccountsInfo();
        // 可提现额度
        $data['withdraw_score'] = $AccountsInfoM->Score($UserID);

        $redis = Redis::connection();
        $register_invite_switches = $redis->get('register_invite_switches_' . $UserID);
        // 充值可领额度
        $recharge = $service->recharge($UserID);
        $rechargeData['recharge'] = $recharge;
        $rechargeData['flag'] = ($register_invite_switches  == 1 || $recharge == 0) ? true : false;


        // 注册可领额度
        $Register = $service->register($UserID);

        // 对局可领额度
        $gameCount = $service->register($UserID, 2);
        $gameCount['game'] = $gameCount['Register'];
        unset($gameCount['Register']);

        $data['score'] = [$rechargeData, $Register, $gameCount];
        $data['ReceivedScore'] = [$rechargeData, $Register, $gameCount];


        return apiReturnSuc($data);

    }


    /**
     * 领取裂变额度
     * Type -- 1：充值 2：注册 3：其它
     * @param Request $request
     * @return array
     */
    public function receiveScore(Request $request)
    {
        $UserID = $request->UserID ?: '';
        $Type = $request->Type ?: '';

        $service = new Extensions();

        if ($Type == 1) {

            $result = $service->receiveRechargeScore($UserID);
            if ($result === false) {
                return apiReturnFail($service->getError(), [], 303);
            }

        } elseif ($Type == 2) {

            $result = $service->receiveRegisterScore($UserID);
            if ($result === false) {
                return apiReturnFail($service->getError(), $service->getData(), $service->getCode());
            }

        } elseif ($Type == 3) {
            $result = $service->receiveRegisterScore($UserID,3,2);
            if ($result === false) {
                return apiReturnFail($service->getError(), $service->getData(), $service->getCode());
            }
        } else {

            // ..........

            return apiReturnSuc();
        }

        return apiReturnSuc($result);
    }

    // 获取用户领取记录
    public function list(Request $request)
    {
        $UserID = $request->UserID ?: '';
        $list = DB::connection('write')->table('QPRecordDB.dbo.RecordUserAgent as ra')
            ->join('QPAccountsDB.dbo.AccountsInfo as ai', 'ra.UserID', 'ai.UserID')
            ->where('ra.UserID', $UserID)
            ->select('DirectRebate', 'SendTime', 'ai.GameID')
            ->orderByDesc('SendTime')
            ->get();

        foreach ($list as &$value) {
            $value->SendTime = date('Y-m-d H:i:s', strtotime($value->SendTime));
        }
        unset($value);

        return apiReturnSuc($list);
    }

    // 充值比率
    public function rechargeRate()
    {
        $AgentRebateRatio = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
            ->where('StatusName', 'AgentRebateRatio1')
            ->orWhere('StatusName','AgentRebateRatio2')
            ->select('StatusName','StatusValue')
            ->pluck('StatusValue','StatusName');

        return apiReturnSuc($AgentRebateRatio);
    }


}
