<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use \App\Http\Models as HttpModel;
use Illuminate\Support\Facades\DB;

/**
 * 赛事代理管理模型类
 */
class SportsAgent extends Model
{


    /**
     * 获取体育token
     *
     * @access public
     * @param mixed $gmenuId 游戏ID
     * @return string
     */
    public function getSportsToken($gmenuId)
    {
        $agentModel = new HttpModel\Agent;
        // 获取url及参数.
        $getGameUrlWhere = [
            'gmenu_id' => $gmenuId,
            'gameUrl_type' => 'getToken',
        ];
        $getGameUrl = $agentModel->getGameUrl($getGameUrlWhere);

        // 获取Token传参.
        $data = json_decode($getGameUrl->gameUrl_data);
        // 获取Token url.
        $url = $getGameUrl->gameUrl_url;
        $dataString = '';
        foreach ($data as $key => $value) {
            $dataString .= '&' . $key . '=' . urlencode($value);
        }
        $commonModel = new HttpModel\Common;
        // 获取token.
        $getToken = $commonModel->make_request($url, $dataString);
        if ($getToken && json_decode($getToken)->status === 1) {
            // 存儲token
            S('sportsToken', json_decode($getToken)->data->token);
            return 1;
        } else {
            return 2;
        }

    }//end getSportsToken()


    /**
     * 登陆体育
     *
     * @access public
     * @param mixed $gmenuId 游戏ID
     * @param mixed $getMemberInfo 用户信息
     * @return string
     */
    public function loginSports($gmenuId, $getMemberInfo)
    {
        $agentModel = new HttpModel\Agent;
        // 获取登陆 url及参数.
        $getGameUrlWhere = [
            'gmenu_id' => $gmenuId,
            'gameUrl_type' => 'login',
        ];
        $getGameUrl = $agentModel->getGameUrl($getGameUrlWhere);
        // 获取登陆传参.
        $data = json_decode($getGameUrl->gameUrl_data);
        // 获取登陆url.
        $url = $getGameUrl->gameUrl_url;
        // 登陆.
        $paramsData = [
            'agent' => $data->agent,
            'username' => $getMemberInfo['name'],
            'method' => $data->method,
            'password' => $getMemberInfo['password'],
        ];
        $commonModel = new HttpModel\Common;
        $loginUrl = $commonModel->sportsAgentParams($paramsData, $url);
        $loginData = $commonModel->make_request($loginUrl, '');
        if ($loginData && json_decode($loginData)->status === 1) {
            return 1;
        } else {
            return 2;
        }

    }//end loginSports()


    /**
     * 体育转账
     *
     * @access public
     * @param mixed $gmenuId 游戏ID
     * @param mixed $money 金额
     * @param mixed $cash 余额
     * @param mixed $getMemberInfo 用户信息
     * @param mixed $type IN转入OUT转出
     * @return string
     */
    public function transferSports($gmenuId, $money, $cash, $getMemberInfo, $type)
    {
        $agentModel = new HttpModel\Agent;
        // 扣去用户余额.
        DB::beginTransaction();
        $newUuid = getUUID();
        $agentModel->updateMembersDetailed(['membersDetailed_cash' => $cash]);
        // 添加转账记录.
        $agentModel->addMoneyDetails($newUuid, $money, 2, $cash, '转账至体育赛事');
        // 获取转账 url及参数.
        $getGameUrlWhere = [
            'gmenu_id' => $gmenuId,
            'gameUrl_type' => 'transfer',
        ];
        $getGameUrl = $agentModel->getGameUrl($getGameUrlWhere);
        // 获取转账传参.
        $data = json_decode($getGameUrl->gameUrl_data);
        // 获取转账url.
        $url = $getGameUrl->gameUrl_url;
        // 转账.
        $transferParams = [
            'agent' => $data->agent,
            'username' => $getMemberInfo['name'],
            'method' => $data->method,
            'password' => $getMemberInfo['password'],
            'billno' => $newUuid,
            'credit' => $money,
            'type' => $type,
        ];
        $commonModel = new HttpModel\Common;
        $transferUrl = $commonModel->sportsAgentParams($transferParams, $url);
        $transferData = $commonModel->make_request($transferUrl, '');
        $self = $this;
        if ($transferData && json_decode($transferData)->status === 1) {
            DB::commit();
            $self->seeSportsCash($gmenuId, $getMemberInfo);
            return 1;
        } else {
            DB::rollBack();
            return 2;
        }
    }//end transferSports()


    /**
     * 查看体育余额
     *
     * @access public
     * @param mixed $gmenuId 游戏ID
     * @param mixed $getMemberInfo 用户信息
     * @return string
     */
    public function seeSportsCash($gmenuId, $getMemberInfo)
    {
        $agentModel = new HttpModel\Agent;
        // 获取余额 url及参数.
        $getGameUrlWhere = [
            'gmenu_id' => $gmenuId,
            'gameUrl_type' => 'seeCash',
        ];
        $getGameUrl = $agentModel->getGameUrl($getGameUrlWhere);
        // 获取余额传参.
        $data = json_decode($getGameUrl->gameUrl_data);
        // 获取余额url.
        $url = $getGameUrl->gameUrl_url;
        // 查询余额.
        $seeCashParams = [
            'agent' => $data->agent,
            'username' => $getMemberInfo['name'],
            'method' => $data->method,
            'password' => $getMemberInfo['password'],
        ];
        $commonModel = new HttpModel\Common;
        $seeCashUrl = $commonModel->sportsAgentParams($seeCashParams, $url);
        $seeCash = $commonModel->make_request($seeCashUrl, '');
        if ($seeCash && json_decode($seeCash)->status === 1) {
            // 将查询到的余额写入.
            $gameMoneySelect = [
                'gameMoney_id',
            ];
            $gameMoneyWhere = [
                'members_id' => S('token_user_id'),
                'gmenu_id' => $gmenuId,
            ];
            // 查询是否存在该记录.
            $backData = $agentModel->seeGameMoney($gameMoneySelect, $gameMoneyWhere);
            if (empty($backData) === true) {
                $gameMoneyData = [
                    'gameMoney_money' => json_decode($seeCash)->data,
                    'members_id' => S('token_user_id'),
                    'gmenu_id' => $gmenuId,
                ];
                $agentModel->addGameMoney($gameMoneyData);
            } else {
                $agentModel->updateGameMoney(json_decode($seeCash)->data, $gameMoneyWhere);
            }
        }
    }//end transferSports()


}
