<?php

namespace Home\Controller;



/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class PlayersController extends BaseController {

    //玩家行为日志
    public function playAction() {
        $page = intval(I('p', 1));
        $row = intval(I('row', 10));
        $relationGameId = intval(I('relation_game_id', 0));
        $sdkVersion = intval(I('sdk_version', 0));
        $serverId = trim(I('server_id', ''));
        $gamePlayerName = trim(I('game_player_name', ''));
        $userAccount = trim(I('user_account', ''));
        $userId =  trim(I('user_id', ''));
        $extend = trim(I('extend', ''));          //cp订单号
        $begTime = strtotime(I('begtime'));
        $endTime = strtotime(I('endtime'));
        $levelPromote = $this->getLevelPromote();
        $queryPromote = $this->getQueryPromote($levelPromote);
      //  $loginPromote = $this->getLoginPromote();
        $map = [];

        $map['chain'] = ['like', $queryPromote['chain'] . $queryPromote['id'] . '/%'];
        $ids = M('promote', 'tab_')->where($map)->getField('id', true);
        $ids[] = $queryPromote['id'];
        $map = [];
        $map['tab_pay_info.promote_id'] = ['in', $ids];

        if ($relationGameId != 0 || $sdkVersion != 0) {
            if ($relationGameId != 0) {
                $gameMap['relation_game_id'] = $relationGameId;
            }
            if ($sdkVersion != 0) {
                $gameMap['sdk_version'] = $sdkVersion;
            }
            $gameId = M('game', 'tab_')->where($gameMap)->getField('id', true);
            if(empty($gameId)) {
                $gameId = [100000];
            }
            $map['game_id'] = ['in', $gameId];
        }
        if (!empty($serverId)) {
            $map['tab_pay_info.server_id'] = $serverId;
        }
        if (!empty($gamePlayerName)) {
            $map['tab_pay_info.game_player_name'] = $gamePlayerName;
        }
        if (!empty($userAccount)) {
            $map['tab_pay_info.user_account'] = $userAccount;
        }
        if (!empty($extend)) {
            $map['tab_pay_info.extend'] = $extend;
        }
        if (!empty($userId)) {
            $map['tab_pay_info.game_player_id'] = $userId;
        }
        if (!empty($begTime) && !empty($endTime)) {
            $map['tab_pay_info.create_time'] = ['between', [$begTime, $endTime + 24*3600 - 1]];
        }else if (empty($begTime) && !empty($endTime)) {
            $map['tab_pay_info.create_time'] = ['elt',$endTime + 24*3600];
        }else if (!empty($begTime) && empty($endTime)) {
            $map['tab_pay_info.create_time'] = ['EGT',$begTime + 24*3600];
        }
        $page = intval(I('get.p', 1));
        $page = $page ? $page : 1; //默认显示第一页数据arraypage
        $rs = M('pay_info','tab_')->where($map)->order('tab_pay_info.create_time desc')
            ->page($page, $row)->select();
        foreach($rs as $key => $v) {
                if($v['server_id'] != 0 && !empty($v['extend'])) {
                    $serverId1 = $v['server_id'];
                    $gameId = $v['game_id'];
                    $serverInfo = M('server','tab_')->field('server_name')->where(['server_num'=>$serverId1,'game_id'=>$gameId])->find();
                    $rs[$key]['server_name'] = $serverInfo['server_name'];
                }else {
                    $rs[$key]['server_name'] = "";
                    $rs[$key]['game_player_id'] = "";
                    $rs[$key]['game_player_name'] = "";
                }
                if(empty($v['extend'])) {
                    $v['extend'] = $v['order_id'];
                }
                $rs[$key]['extend'] = encryption($v['extend']);
                $rs[$key]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                $rs[$key]['user_account'] = encryption($rs[$key]['user_account']);
                $promoteInfo = M('promote','tab_')->field("account")->where(['id' => intval($v['promote_id'])])->find();
                
                $rs[$key]['promote_id']= $promoteInfo['account']; 
                
            }
        $count = M('pay_info', 'tab_')
            ->field('tab_pay_info.id')
            ->where($map)
            ->select();
        $count = count($count);

        //分页
        $parameter['p'] = $page;
        $parameter['row'] = $row;
        $parameter['begtime'] = I('begtime');
        $parameter['endtime'] = I('endtime');
        $parameter['relation_game_id'] = $relationGameId;
        $parameter['sdk_version'] = $sdkVersion;
        $parameter['server_id'] = $serverId;
        $parameter['game_player_name'] = $gamePlayerName;
        $parameter['user_account'] = $userAccount;
        $parameter['extend'] = $extend;
        $parameter['user_id'] = $user_id;
        $parameter['level_promote_2'] = $levelPromote[0];
        $parameter['level_promote_3'] = $levelPromote[1];
        $parameter['level_promote_4'] = $levelPromote[2];
        $serverData = $this->getServer(I('relation_game_id'), I('sdk_version'));
        $page = set_pagination($count, $row, $parameter);
        if ($page) {
            $this->assign('_page', $page);
        }
        $this->assign('listData',$rs);
        $this->assign('serverData', $serverData['data']);
        $this->assign('initBegTime', I('begtime'));
        $this->assign('initEndTime', I('endtime'));
       $this->display();
    }

    private function getLevelPromote()
    {
        $levelPromote = [];
        $levelPromote[] = I('level_promote_2', 0);
        $levelPromote[] = I('level_promote_3', 0);
        $levelPromote[] = I('level_promote_4', 0);
        return $levelPromote;
    }

    private function getQueryPromote($levelPromote)
    {
        $queryPromote = null;
        $promote = $this->getLoginPromote();
        $queryPromoteId = 0;
        foreach ($levelPromote as $item) {
            if ($item != 0) {
                $queryPromoteId = $item;
            }
        }
        if ($queryPromoteId == 0) {
            $queryPromote = $this->getLoginPromote();
        } else {
            $queryPromote = M('promote', 'tab_')->where(['id' => $queryPromoteId])->find();
        }
        return $queryPromote;
    }

    public function getServer($relationGameId, $sdkVersion)
    {
        $result['status'] = 0;
        $result['data'] = [];

        if (empty($relationGameId)) {
            return $result;
        }

        $map['relation_game_id'] = intval($relationGameId);
        empty($sdkVersion) || $map['sdk_version'] = intval($sdkVersion);

        $gameIds = M('Game', 'tab_')
            ->where($map)
            ->getField('id', true);

        if (empty($gameIds)) {
            return $result;
        }

        $gameIds = implode(',', $gameIds);
        $where['game_id'] = ['in', $gameIds];
        $where['show_status'] = 1;

        $serverData = M('Server', 'tab_')
            ->field('server_id,server_name')
            ->where($where)
            ->order('server_version,start_time desc')
            ->select();
        $result['status'] = 1;
        $result['data'] = $serverData;

        return $result;
    }
    public function getServerAjax()
    {
        $serverData = $this->getServer(I('relation_game_id'), I('sdk_version'));

        $this->ajaxReturn($serverData);
    }
}