<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use \App\Models;

/**
 * 代理管理模型类
 */
class Agent extends Model
{


    /**
     * 获取游戏url及参数
     *
     * @access public
     * @param mixed $getGameUrlWhere 查询条件
     * @return String
     */
    public function getGameUrl($getGameUrlWhere)
    {
        // 获取游戏地址数据.
        $gameUrlModel = new Models\GameUrl;
        $getGameUrlSelect = [
            'gameUrl_url',
            'gameUrl_data',
        ];
        $getGameUrl = $gameUrlModel
            ->select($getGameUrlSelect)
            ->where($getGameUrlWhere)
            ->first();

        return $getGameUrl;

    }//end getGameUrl()


    /**
     * 获取用户信息
     *
     * @access public
     * @return array
     */
    public function getMember()
    {
        // 获取游戏地址数据.
        $memberModel = new Models\Member;
        $getMemberSelect = [
            'name',
            'members_agentPassword',
            'uuid',
        ];
        $getMemberWhere['id'] = S('token_user_id');
        $getMember = $memberModel
            ->select($getMemberSelect)
            ->where($getMemberWhere)
            ->first();
        $result['name'] = $getMember->name;
        // 接口密码.
        if (empty($getMember->members_agentPassword) === true) {
            $newUuid = getUUID();
            $newUuid = substr($getMember->uuid, 3,12) . md5(uniqid(mt_rand(), true));
            $newAgentPassword = createPasswd(trim($newUuid), $newUuid);
            $result['password'] = $newAgentPassword;
            $getMember = $memberModel
                ->where($getMemberWhere)
                ->update(['members_agentPassword' => $newAgentPassword]);
        } else {
            $result['password'] = $getMember->members_agentPassword;
        }

        return $result;

    }//end getMember()


    /**
     * 获取用户余额
     *
     * @access public
     * @return array
     */
    public function getMembersDetailed()
    {
        // 获取用户资金.
        $membersDetailedModel = new Models\MembersDetailed;
        $getMembersDetailedSelect = [
            'membersDetailed_cash',
        ];
        $getMembersDetailedWhere['members_id'] = S('token_user_id');
        $getMembersDetailed = $membersDetailedModel
            ->select($getMembersDetailedSelect)
            ->where($getMembersDetailedWhere)
            ->first();

        return $getMembersDetailed;

    }//end getMembersDetailed()


    /**
     * 修改用户余额
     *
     * @access public
     * @param mixed $update 修改字段
     * @return array
     */
    public function updateMembersDetailed($update)
    {
        // 获取用户资金.
        $membersDetailedModel = new Models\MembersDetailed;
        $updateMbrDtlWhr['members_id'] = S('token_user_id');
        $updateMbrDtl = $membersDetailedModel
            ->where($updateMbrDtlWhr)
            ->update($update);

        return $updateMbrDtl;

    }//end updateMembersDetailed()


    /**
     * 添加转账记录
     *
     * @access public
     * @param mixed $uuid 唯一标识
     * @param mixed $money 金额
     * @param mixed $type 类型    1增加2减少
     * @param mixed $cash 剩余金额
     * @param mixed $describe 描述
     * @param mixed $status 状态  1成功2失败
     * @return string
     */
    public function addMoneyDetails($uuid, $money, $type, $cash, $describe, $status=1)
    {
        $moneyDetailsModel = new Models\MoneyDetails;
        $data = [
            'moneyDetails_identity' => $uuid,
            'members_id' => S('token_user_id'),
            'moneyDetails_money' => $money,
            'moneyDetails_time' => date('Y-m-d H:i:s'),
            'moneyDetails_type' => $type,
            'moneyDetails_cash' => $cash,
            'moneyDetails_describe' => $describe,
            'moneyDetails_status' => $status,
        ];
        $add = $moneyDetailsModel->insert($data);

        return $add;

    }//end addMoneyDetails()


    /**
     * 修改游戏余额
     *
     * @access public
     * @param mixed $money 金额
     * @param mixed $where 查询条件
     * @return string
     */
    public function updateGameMoney($money, $where)
    {
        $gameMoneyModel = new Models\GameMoney;
        $result = $gameMoneyModel
            ->where($where)
            ->update(['gameMoney_money' => $money]);

        return $result;

    }//end updateGameMoney()


    /**
     * 查询游戏余额
     *
     * @access public
     * @param mixed $money 金额
     * @param mixed $where 查询条件
     * @return string
     */
    public function seeGameMoney($money, $where)
    {
        $gameMoneyModel = new Models\GameMoney;
        $result = $gameMoneyModel
            ->where($where)
            ->first();

        return $result;

    }//end seeGameMoney()


    /**
     * 新增游戏余额信息
     *
     * @access public
     * @param mixed $data 数据
     * @return string
     */
    public function addGameMoney($data)
    {
        $gameMoneyModel = new Models\GameMoney;
        $result = $gameMoneyModel->insert($data);

        return $result;

    }//end addGameMoney()


}
