<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Api\AgentController;
use Illuminate\Support\Facades\DB;
use \App\Http\Models;
use \Exception;

/**
 * 排行榜控制器
 */
class RankingController extends Controller
{

    /**
     * 盈利排行榜
     *
     * @access public
     * @param  mixed $req 参数.
     * @return string
     */
    public function winRanking(Request $req){
        $code = -2;
        $msg = '操作失败';
        try {
            $sportsAgent = new AgentController;
            // 体育盈利.
            $userDataWin = [];
            $sportsData  = $sportsAgent->userProfit();
            foreach ($sportsData as $v) {
                $userDataKey = array_search($v->name, array_column($userDataWin, 'name'));
                if (strlen($userDataKey)) {
                    $userDataWin[$userDataKey]['money'] += abs($v->money);
                } else {
                    $userDataWin[] = (array) $v;
                }
            }

            // 排序.
            $moneyArr = array_column($userDataWin, 'money');
            array_multisort($moneyArr, SORT_DESC, $userDataWin);
            // 计算当前用户排名.
            $myName  = DB::table('members')->select('name')->where('id', S('token_user_id'))->first();
            $userKey = array_search($myName->name, array_column($userDataWin, 'name'));

            // 取前10个.
            $winRanking = array_slice($userDataWin, 0, 10);
            $result     = [
                'winRanking' => $winRanking,
                'myRank'     => ($userKey + 1),
            ];

            return toJson(1, '成功', $result);
        } catch (Exception $e) {
            return toJson($code, $msg, []);
        }//end try

    }//end winRanking()


    /**
     * 盈利排行榜
     *
     * @access public
     * @param  mixed $req 参数.
     * @return string
     */
    public function cashRanking(Request $req){
        $code = -2;
        $msg  = '操作失败';
        try {
            $agentCashModel = new Models\AgentCash;
            $userInfoModel  = new Models\UserInfo;
            // 获取所有人游戏余额.
            $userDataAll = [];
            $gameMoney   = $agentCashModel->allGameMoney();
            foreach ($gameMoney as $v) {
                $userDataKey = array_search($v->members_id, array_column($userDataAll, 'id'));
                if (strlen($userDataKey)) {
                    $userDataAll[$userDataKey]['money'] += abs($v->gameMoney_money);
                } else {
                    $thisData['money'] = abs($v->gameMoney_money);
                    $thisData['id']    = $v->members_id;
                    $thisData['name']  = $v->name;
                    $userDataAll[]     = $thisData;
                }
            }

            // 获取用户余额.
            $userCashSelect = [
                'membersDetailed_cash',
                'members_id',
                'name',
            ];
            $userCash       = $userInfoModel->getAllUserInfo($userCashSelect);
            foreach ($userCash as $v) {
                $userDataKey = array_search($v->members_id, array_column($userDataAll, 'id'));
                if (strlen($userDataKey)) {
                    $userDataAll[$userDataKey]['money'] += abs($v->membersDetailed_cash);
                } else {
                    $thisData['money'] = abs($v->membersDetailed_cash);
                    $thisData['id']    = $v->members_id;
                    $thisData['name']    = $v->name;
                    $userDataAll[]     = $thisData;
                }
            }

            // 排序.
            $moneyArr = array_column($userDataAll, 'money');
            array_multisort($moneyArr, SORT_DESC, $userDataAll);
            // 计算当前用户排名.
            $userKey = array_search(S('token_user_id'), array_column($userDataAll, 'id'));
            // 取前10个.
            $winRanking = array_slice($userDataAll, 0, 10);
            $result     = [
                'winRanking' => $winRanking,
                'myRank'     => ($userKey + 1),
            ];

            return toJson(1, '成功', $result);
        } catch (Exception $e) {
            return toJson($code, $msg, []);
        }//end try

    }//end rankingList()


}
