<?php

namespace Admin\Controller;

class RechargeSumOtherController extends ThinkController
{

    public function summation($p = 1)
    {

        $startDateTime = empty($_REQUEST['timeStart']) ? date('Y-m-d', strtotime('-6 day', time())) : $_REQUEST['timeStart'];
        $endDateTime = empty($_REQUEST['timeEnd']) ? date('Y-m-d') : $_REQUEST['timeEnd'];
        $_GET['timeStart'] = $startDateTime;
        $_GET['timeEnd'] = $endDateTime;

        if(!array_key_exists("timeStart",$_REQUEST)){
            $this->redirect(ACTION_NAME, array('timeStart' => date('Y-m-d',strtotime('-7 day')),"timeEnd"=>date('Y-m-d')));
        }

        $total = ['current' => 0, 'sum' => 0];
        $map['date'] = array('BETWEEN', array($startDateTime, $endDateTime));

        $_REQUEST['game_name'] ? ($map['game_name'] = $_REQUEST['game_name']) :'';

        $data = M("recharge_text","tab_")->where($map)->order("date desc")->select();

        if (isset($_REQUEST['row'])) {
            $row = $_REQUEST['row'];
        } else {
            $row = 10;
        }
        foreach ($data as $k => $v) {
            $total['sum'] += $v['day_pay'];
        }
        $count = count($data);
        $export_data = $data;
        $page = set_pagination($count, $row);
        if ($page) {
            $this->assign('_page', $page);
        }
        //用array_slice(array,offset,length) 函数在数组中根据条件取出一段值;array(数组),offset(元素的开始位置),length(组的长度)
        $data = array_slice($data, ($p - 1) * $row, $row);

        foreach ($data as $k => $v) {
            $total['current'] += $v['day_pay'];
        }


        //判断是否执行导出
        if (I('export') == 1) {
            //操作日志
            $GetData = $_GET;
            unset($GetData['xlsname']);
            unset($GetData['export']);
            addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U('RechargeSum/summation',$GetData),'menu'=>'充值-游戏订单-充值汇总-导出']);

            $this->exportSum($export_data);
        }

        $this->assign('showPromote', session('user_auth')['show_promote']);
        $this->assign('meta_title', "充值汇总");
        $this->assign('list_data', $data);
        $this->assign('total', $total);
        $this->assign('startDate', $startDateTime);
        $this->assign('endDate', $endDateTime);
        $this->assign("is_admin",is_administrator());
        $this->m_title = '充值汇总';
        $this->assign('commonset', M('Kuaijieicon')->where(['url' => 'RechargeSum/summation', 'status' => 1])->find());


        $this->display();
    }

    public function exportSum($data)
    {
        $export = A('Export');
        $exptitle = $_REQUEST['xlsname'];
        $expCellName = array(
            array('date', '日期'),
            array('from_channel', '渠道来源'),
            array('game_name', '游戏名称'),
            array('new_person', '新增人数'),
            array('active_person', '活跃人数'),
            array('new_pay_person', "新增玩家付费"),
            array('day_pay', '本日累计'),
        );
        if (session('user_auth')['show_promote'] == 0) {
            foreach ($expCellName as $key => $row) {
                if ($row[0] == 'promote_id') {
                    unset($expCellName[$key]);
                    break;
                }
            }
            $expCellName = array_values($expCellName);
        }
        $expTableData = $data;
        $export->exportExcel($exptitle, $expCellName, $expTableData);
    }
}