<?php

namespace Admin\Controller;

class BehaviorLogController extends ThinkController
{

    private $single_type = [
        0 => '通知到账',
        1 => '补单',
    ];

    /**
     * 游戏预下单记录
     * @return [type] [description]
     */
    public function game()
    {
        $params = I('get.');

        $map        = [];
        if (isset($params['user_account'])) {
            $map['user_account'] = $params['user_account'];
        }
        if (isset($params['extend'])) {
            $map['_string'] = 'extend = "'.$params["extend"]. '" or order_id = "'.$params["extend"] . '"';
        }
        if (isset($params['game_name'])) {
            $game_id = ['in', array_column(getGameByName($params['game_name'], $params['sdk_version']), 'id')];
            $map['game_id'] = $game_id;
        }
        if (isset($params['server_id'])) {
            $map['server_id'] =$params['server_id'];
        }
        if (isset($params['game_player_name'])) {
            $map['game_player_name'] = $params['game_player_name'];
        }
        if (isset($params['game_player_id'])) {
            $map['game_player_id'] = $params['game_player_id'];
        }


        $p   = $params['p'] ? $params['p'] : 1;
        $row = $params['row'] ? $params['row'] : 10;

        $startDate          = empty($params['timestart']) ? date('Y-m-d', strtotime('-6 day', time())) : $params['timestart'];
        $endDate            = empty($params['timeend']) ? date('Y-m-d') : $params['timeend'];
        $startTime          = strtotime($startDate);
        $endTime            = strtotime($endDate) + 86399;
        $map['create_time'] = array('BETWEEN', [$startTime, $endTime]);

        setPowerPromoteIds($map,'promote_id');
        
        if (isset($params['promote_id'])) {
            $promoteId = $params['promote_id'];
            if ($promoteId == 0) {
                $map['promote_id'] = 0;
            } else {
                $promoteMap['chain'] = ['like', "%/$promoteId/%"];
                $promoteIds          = M('Promote', 'tab_')->where($promoteMap)->getField('id', true);
                $promoteIds[] = $promoteId;
                $map['promote_id'] = ['in', $promoteIds];
            }
        }
        // $data = M('pay_info', 'tab_')->field('tab_pay_info.id,tab_pay_info.user_account,extend,tab_pay_info.create_time,tab_pay_info.game_name,tab_pay_info.server_name,tab_pay_info.promote_account,tab_pay_info.order_id,tab_pay_info.game_player_id,tab_pay_info.game_player_name,price,tab_server.server_name')->join('LEFT JOIN tab_server on tab_pay_info.server_id = tab_server.server_num AND tab_pay_info.game_id = tab_server.game_id AND tab_pay_info.server_id <> 0')->where($map)->order('tab_pay_info.id DESC')->page($p, $row)->select();
        $data = M('pay_info', 'tab_')->where($map)->order('id DESC')->page($p, $row)->select();
        foreach($data as $k => $v) {
            ($data[$k]['promote_account']=='官方渠道'||$data[$k]['promote_account']=='自然注册')?($data[$k]['promote_account']=C('OFFICIEL_CHANNEL')):'';
            if(empty($v['extend'])) {
                $data[$k]['extend'] = $v['order_id'];
            }
            if ($v['server_id'] > 0 && !empty($v['extend'])){
                $data[$k]['server_name'] = M('server', 'tab_')->where(['game_id' => $v['game_id'],'server_num' =>$v['server_id']])->getField('server_name');
            } else {
                $data[$k]['server_name'] = '';
                $data[$k]['game_player_name'] = '';
                $data[$k]['game_player_id'] = '';
                
            }
        }
        $this->checkListOrCountAuthRestMap($map,[]);
        $count = M('pay_info', 'tab_')->where($map)->count();
        $page  = set_pagination($count, $row);
        if ($page) {
            $this->assign('_page', $page);
        }
        $this->assign('startDate', $startDate);
        $this->assign('endDate', $endDate);
        $this->assign('data', $data);
        $this->assign("is_admin",is_administrator());
        $show_data_power = (is_administrator()|| session('user_auth')['show_data']);
        $this->assign('show_data_power', $show_data_power);
        $this->display('index');
    }


    /**
     * 补单记录
     * @return [type] [description]
     */
    public function singleSupplement() {
        $p = I('get.p', 1);
        $row = I('get.row', 10);
        $params = I('get.');

        if(isset($params['timestart']) && isset($params['timeend'])) {
            $startTime          = strtotime($params['timestart']);
            $endTime            = strtotime($params['timeend']) + 86399;
            $map['tab_repair_pay.create_time'] = array('BETWEEN', [$startTime, $endTime]);
        }

        if(isset($params['type'])) {
            $map['tab_repair_pay.type'] = $params['type'];
        }



        if(isset($params['pay_order_number'])) {
            $map['tab_repair_pay.pay_order_number'] = $params['pay_order_number'];
        }

        if(isset($params['admin_name'])) {
            $map['tab_repair_pay.admin_name'] = $params['admin_name'];
        }


        if(!empty(I('get.extend'))) {                                       
            $map['tab_spend.extend'] = I('get.extend');
            $data = M('repair_pay', 'tab_')->join('tab_spend on tab_spend.pay_order_number = tab_repair_pay.pay_order_number')->where($map)->page($p, $row)->order('tab_repair_pay.id DESC')->select();
        } else {
            $data = M('repair_pay', 'tab_')->where($map)->page($p, $row)->order('id DESC')->select();
        }

        $show_data = [];
        foreach($data as $k => $v) {
            if($v['tab'] == 'spend') {
               $order_detail =  M('spend', 'tab_')->field('extend,pay_time,payed_time,user_account,promote_account,game_name,pay_amount,pay_way')->where(['pay_order_number' => $v['pay_order_number']])->find();
               $re_data['pay_time'] = $order_detail['pay_time'];
               $re_data['payed_time'] = date("Y-m-d H:i:s", $order_detail['payed_time']);
            } elseif($v['tab'] == 'deposit') {
                $order_detail =  M('deposit', 'tab_')->field('create_time,user_account,payed_time,promote_account,pay_amount,pay_way')->where(['pay_order_number' => $v['pay_order_number']])->find();
               $re_data['pay_time'] = $order_detail['create_time'];
               $re_data['payed_time'] = date("Y-m-d H:i:s", $order_detail['payed_time']);
            }
            ($order_detail['promote_account']=='官方渠道'||$order_detail['promote_account']=='自然注册')?($re_data['promote_account']="江息网络"):($re_data['promote_account'] = $order_detail['promote_account']);
               $re_data['extend'] = isset($order_detail['extend']) ? $order_detail['extend'] : '-';
               $re_data['user_account'] = $order_detail['user_account'];
//               $re_data['promote_account'] = $order_detail['promote_account'];
               $re_data['game_name'] = !empty($order_detail['game_name']) ? $order_detail['game_name'] : '-';
               $re_data['pay_amount'] = $order_detail['pay_amount'];
               $re_data['pay_way'] = get_pay_way($order_detail['pay_way']);
               $re_data['pay_order_number'] = $v['pay_order_number'];
               $re_data['type'] = $this->single_type[$v['type']];
               $re_data['create_time'] = $v['create_time'];
               $re_data['admin_name'] = $v['admin_name'];
               $show_data[] = $re_data;
        }

        if(!empty(I('get.extend'))) {                                       
            $map['tab_spend.extend'] = I('get.extend');
            $count = M('repair_pay', 'tab_')->join('tab_spend on tab_spend.pay_order_number = tab_repair_pay.pay_order_number')->where($map)->count();
        } else {
            $count = M('repair_pay', 'tab_')->where($map)->count();
        }
        $this->checkListOrCountAuthRestMap($map,[]);
        $page  = set_pagination($count, $row);
        if ($page) {
            $this->assign('_page', $page);
        }
        $this->assign('show_data', $show_data);
        $show_data_power = (is_administrator()|| session('user_auth')['show_data']);
        $this->assign('show_data_power', $show_data_power);
        $this->display('singleSupplement');

    }
}