COMPANY_NAME =C(DEFAULT_COMPANY); // echo "
";
    }
    public function index($p=1)
    {
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $arraypage = $page;

        if (isset($_REQUEST['row'])) {
            $row = $_REQUEST['row'];
        } else {
            $row = 10;
        }
        //排序
        if(!array_key_exists("time_start",$_REQUEST)){
            $this->redirect(ACTION_NAME, array('time_start' => date('Y-m-d',strtotime('-30 day')),"time_end"=>date('Y-m-d')));
        }
        if ($_REQUEST['data_order'] != '') {
            $data_order = reset(explode(',', $_REQUEST['data_order']));
            $data_order_type = end(explode(',', $_REQUEST['data_order']));
            $this->assign('userarpu_order', $data_order);
            $this->assign('userarpu_order_type', $data_order_type);
        }
        //时间
        $istimeselect = false;
        if (isset($_REQUEST['time_start']) && isset($_REQUEST['time_end'])) {
            $istimeselect = ['between', [strtotime($_REQUEST['time_start']), strtotime($_REQUEST['time_end']) + 86399]];
        } elseif (isset($_REQUEST['time_start'])) {
            $istimeselect = ['GT', strtotime($_REQUEST['time_start'])];
        } elseif (isset($_REQUEST['time_end'])) {
            $istimeselect = ['LT', strtotime($_REQUEST['time_end']) + 86399];
        }

        $senddata = [];
        //获取各会长的充值方式
        $cash = self::getJoinPromoteData([],$istimeselect);
        foreach($cash as $k=>$v){
            $senddata[$v['p_id']] = $v;
        }

        //内冲
        $inside = self::getJoinPromoteData([],$istimeselect,"promote_coin");
        foreach($inside as $k=>$v){
            if(array_key_exists($v['p_id'],$senddata)){
                $senddata[$v['p_id']]['inside_cash_count']= $v['pay_amount']-$v['back_amount'];
            }else{
                $v['inside_cash_count'] =  $v['pay_amount']-$v['back_amount'];
                unset($v['pay_amount']);
                unset($v['back_amount']);
                $senddata[$v['p_id']] = $v;
            };
        }
        //官方渠道单算
        //判断公司
        $gfidlag = true;
        $gfgs = true;
        $nwtflag = true;
        if (isset($_REQUEST['company_id'])) {
            if($_REQUEST['company_id']!=0){
                $gfgs = false;
            }
        }
        //判断是否是内团 内团才有官方
        if(isset($_REQUEST['company_belong'])){
            if($_REQUEST['company_belong']!=0){
                $nwtflag = false;
            }
        }
        if(isset($_REQUEST['develop_type'])){
            if($_REQUEST['develop_type']!=0){
                $nwtflag = false;
            }
        }
        if(isset($_REQUEST['admin_id'])){
            if($_REQUEST['admin_id']!=0){
                $nwtflag = false;
            }
        }
        $isMarketAdmin = isMarketAdmin();
        if(I('admin_id', 0) != 0 || $isMarketAdmin){
            $nwtflag = false;
        }
        if (isset($_REQUEST['promote_id'])) {
            if($_REQUEST['promote_id'] == 0){
                $gfidlag = true;
            }else{
                $gfidlag =false;
            }
        }
        $isMarketLeader = isMarketLeader();
        if ($isMarketLeader) {
            $nwtflag = false;
        }
        // dd($gfidlag && $gfgs && $nwtflag);
        if($gfidlag && $gfgs && $nwtflag){
            //计算官方渠道
            unset($_REQUEST['company_id']);
            unset($_REQUEST['company_belong']);
            $_REQUEST['promote_id'] = 0;
            $gfcash = self::getJoinPromoteData([],$istimeselect);
            $senddata[0] = $gfcash[0];

            $pmap['status']=1;
            if($istimeselect){
                $pmap['create_time'] = $istimeselect;
            }
            $admininside = M("provide_user p","tab_")->field("sum(amount) amount")->where($pmap)->find();
            if($admininside['amount'] >0){
                $senddata[0]['inside_cash_count']= $admininside['amount'];
            }

        }

        $data = [];
        $count =array(
            "cash_count"=>0,
            "balance_coin_count"=>0,
            "bind_coin_count"=>0,
            "inside_cash_count"=>0,
        );
        $promotes = [];
        if (count($senddata) > 0) {
            $promoteIds = array_column($senddata, 'p_id');
            if ($promoteIds) {
                $promotes = M('promote', 'tab_')->field(['id', 'account', 'admin_id'])->where(['id' => ['in', $promoteIds]])->select();
            }
        }
        $promotes = index_by_column('id', $promotes);
        $adminUsernames = getAdminUsernameList(array_column($promotes, 'admin_id'));
        foreach($senddata as $k=>$v){
            $promote = $promotes[$v['p_id']] ?? null;
            $v['company_belong'] = $v['company_belong'] ?:0;
            $v['company_belong'] = getCompanyBlong($v['company_belong']);
            if(empty($v['company_name'])) $v['company_name']= $this->COMPANY_NAME;
            if(empty($v['p_id'])) $v['p_id']= "0";
            if(!array_key_exists('cash_count',$v)) $v['cash_count']=0;
            if(!array_key_exists('balance_coin_count',$v)) $v['balance_coin_count']=0;
            if(!array_key_exists('bind_coin_count',$v)) $v['bind_coin_count']=0;
            if(!array_key_exists('inside_cash_count',$v)) $v['inside_cash_count']=0;
            $v['promote_account'] = get_promote_name($v['p_id']);
            ($v['promote_account']=='官方渠道')?($v['promote_account']=C('OFFICIEL_CHANNEL')):'';
            $v['admin_username'] = $adminUsernames[$promote['admin_id']] ?? '无';
            $v['develop_type_text'] = getCompanyDevelopTypeText($v['develop_type']);
            $v['inside_cash_count'] = number_format($v['inside_cash_count'],2,'.','');
            $v['allcount'] = number_format($v['cash_count']-0+$v['balance_coin_count']+$v['bind_coin_count'],2,'.','');
            $count['cash_count'] +=  $v['cash_count'];
            $count['balance_coin_count'] +=  $v['balance_coin_count'];
            $count['bind_coin_count'] +=  $v['bind_coin_count'];
            $count['inside_cash_count'] +=  $v['inside_cash_count'];
            $count['all_count'] +=  $v['allcount'];
            $data[] = $v;
        }
        $data = my_sort($data, $data_order_type, (int)$data_order);
        if(isset($_REQUEST['export'])){
            //操作日志
            $GetData = $_GET;
            unset($GetData['export']);
            addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData),'menu'=>"充值-财务管理-公会统计-导出"]);

            data2csv($data,'公会统计',array(
                "company_name"=>"推广公司",
                "promote_account"=>"会长渠道",
                "admin_username"=>"所属市场专员",
                "company_belong"=>"团体类型",
                "develop_type_text"=>"开发类型",
                "cash_count"=>"游戏现金金额",
                "balance_coin_count"=>"平台币直充支出",
                "bind_coin_count"=>"内充支出",
                "inside_cash_count"=>"平台币内充",
                "allcount"=>"渠道游戏内充值合计"
            ));
        }

        $pagecount = count($data);
        $page = set_pagination($pagecount, $row);
        if ($page) {
            $this->assign('_page', $page);
        }

        $outBelong = 0;
        $insideBelong = 0;
        $otherBelong = 0;
        foreach ($data as $key => $value) {

            if ($value['company_belong']=='内团') {
                $insideBelong+=$value['allcount'];
            } elseif ($value['company_belong']=='外团' || $value['company_belong']=='外团-分发') {
                $outBelong+=$value['allcount'];
            } else {
                $otherBelong+=$value['allcount'];
            }

        }

        $count['cash_count'] =  number_format($count['cash_count'],2,'.','');
        $count['balance_coin_count'] =  number_format($count['balance_coin_count'],2,'.','');
        $count['bind_coin_count'] =  number_format($count['bind_coin_count'],2,'.','');
        $count['inside_cash_count'] =  number_format($count['inside_cash_count'],2,'.','');
        $count['all_count'] =  number_format($count['all_count'],2,'.','');

        $this->checkListOrCountAuthRestMap($map,[]);

        //外团占比
        $this->assign('outBelong',number_format($outBelong/$count['all_count']*100,2,'.',''));
        //内团占比
        $this->assign('insideBelong',number_format($insideBelong/$count['all_count']*100,2,'.',''));
        $this->assign('otherBelong',number_format($otherBelong/$count['all_count']*100,2,'.',''));
        $size = $row;
        $data = array_slice($data, ($arraypage - 1) * $size, $size);
        $this->assign('list_data', $data);
        $this->assign('all_count', $count);
        $this->meta_title = '公会统计';
        $this->display();
    }
    private function getJoinPromoteData($map,$istimeselect,$type="spend")
    {
        // setPowerPromoteIds($map,"s.promote_id");
        //推广员相关检索
        if (isset($_REQUEST['promote_id'])) {
            if ($_REQUEST['promote_id'] == 0) {
                $map['s.promote_id'] = 0;
            } else {
                $promoter_ids = D("Promote")->where("chain like '%/{$_REQUEST['promote_id']}/%' or id={$_REQUEST['promote_id']}")->field('id')->select();
                if ($promoter_ids) {
                    $map['s.promote_id'] = ['in', implode(',', array_column($promoter_ids, 'id'))];
                }
            }
        }
        if (isset($_REQUEST['company_id'])) {
            $map['promote.company_id'] = $_REQUEST['company_id'];

        }
        if (isset($_REQUEST['company_belong'])) {
            $map['promote.company_belong'] = $_REQUEST['company_belong'];

        }
        $isMarketLeader = isMarketLeader();
        if ($isMarketLeader) {
            $map['promote.admin_id'] = ['gt', 0];
        }

        if (!empty(I('develop_type'))) {
            $map['company.develop_type'] = ['in',I('develop_type')];
        }

        $adminId = 0;
        $havingPids = [];
        $isMarketAdmin = isMarketAdmin();
        if ($isMarketAdmin) {
            $userAuth = session('user_auth');
            $adminId = $userAuth['uid'];
        } else {
            if (!empty(I('admin_id'))) {
                $adminId = I('admin_id');
            }
        }
        if ($adminId > 0) {
            $havingPids = M('promote', 'tab_')->where(['admin_id' => $adminId])->getField('id', true);
            $havingPids = count($havingPids) > 0 ? $havingPids : [0];
        }

        if($type == "spend"){
            if (isset($_REQUEST['game_name']) || isset($_REQUEST['game_type'])) {
                $map["s.game_id"] = array("in",implode(',', array_column(getGameByName($_REQUEST['game_name'], $_REQUEST['game_type']), 'id')) );
            }
            if($istimeselect){
                $map['s.payed_time']=$istimeselect;
            }
            if ($adminId > 0) {
                $map['s.market_admin_id'] = $adminId;
            }
            $map['s.pay_status']=1;
            $field= "SUM(CASE WHEN pay_way > 0 THEN pay_amount ELSE 0 END) as cash_count,
                    SUM(CASE WHEN pay_way = 0 THEN pay_amount ELSE 0 END) as balance_coin_count,
                    SUM(CASE WHEN pay_way = -1 THEN pay_amount ELSE 0 END) as bind_coin_count,
                    IFNULL(if(substring_index(substring_index(promote.`chain`,'/',2),'/',-1)='',s.promote_id,substring_index(substring_index(promote.`chain`,'/',2),'/',-1)),0) p_id,
                    IFNULL(company_id,0) company_id,company_name,develop_type,IFNULL(promote.company_belong,0) company_belong";
            $query = M()
                ->table("tab_spend s use index(promote_time)")
                ->field($field)
                ->where($map)
                ->join("tab_promote promote ON s.promote_id = promote.id","left")
                ->join("tab_promote_company company ON promote.company_id = company.id","left")
                ->group('p_id');
            if (count($havingPids) > 0) {
                $query->having('p_id in (' . implode(',', $havingPids) . ')');
            }
            return $query->select();
        }else{
            if($istimeselect){
                $map['s.create_time']=$istimeselect;
            }
            $adminId = 0;
            $havingPids = [];
            $isMarketAdmin = isMarketAdmin();
            if ($isMarketAdmin) {
                $userAuth = session('user_auth');
                $adminId = $userAuth['uid'];
            } else {
                if (!empty(I('admin_id'))) {
                    $adminId = I('admin_id');
                }
            }
            if ($adminId > 0) {
                $havingPids = M('promote', 'tab_')->where(['admin_id' => $adminId])->getField('id', true);
                $havingPids = count($havingPids) > 0 ? $havingPids : [0];
            }
            $map['s.status']=1;
            $map['s.source_id']=0;
            $field= "SUM(CASE WHEN s.type = 1 THEN num ELSE 0 END) as pay_amount,SUM(CASE WHEN s.type = 2 THEN num ELSE 0 END) as back_amount,IFNULL(if(substring_index(substring_index(`chain`,'/',2),'/',-1)='',
                    s.promote_id,substring_index(substring_index(`chain`,'/',2),'/',-1)),0) p_id,
                    IFNULL(company_id,0) company_id,company_name,IFNULL(promote.company_belong,0) company_belong";
            $query = M("promote_coin s","tab_")
                ->field($field)
                ->where($map)
                ->join("tab_promote promote ON s.promote_id = promote.id","left")
                ->join("tab_promote_company company ON promote.company_id = company.id","left")
                ->group('p_id');
            if (count($havingPids) > 0) {
                $query->having('p_id in (' . implode(',', $havingPids) . ')');
            }
            return $query->select();
        }

    }

    //渠道查看
    public function promoteUser($p = 0)
    {
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $arraypage = $page;

        if (isset($_REQUEST['row'])) {
            $row = $_REQUEST['row'];
        } else {
            $row = 10;
        }

        $istimeselect = false;
        if (isset($_REQUEST['time_start']) && isset($_REQUEST['time_end'])) {
            $istimeselect = ['between', [strtotime($_REQUEST['time_start']), strtotime($_REQUEST['time_end']) + 86399]];
        } elseif (isset($_REQUEST['time_start'])) {
            $istimeselect = ['GT', strtotime($_REQUEST['time_start'])];
        } elseif (isset($_REQUEST['time_end'])) {
            $istimeselect = ['LT', strtotime($_REQUEST['time_end']) + 86399];
        }
        $senddata = [];
        //获取各会长的现金充值
        $map=[];
        $title = '';

        if (!empty(I('develop_type'))) {
//            $map['company.develop_type'] = I('develop_type');
            $map['company.develop_type'] = ['in',I('develop_type')];
        }

        self::setPromoteMap($map,"s.promote_id",$title);
        $title .= "渠道统计";
        $this->assign('title', $title);

        $cash = self::promoteUserData($map,$istimeselect);
        foreach($cash as $k=>$v){
            $senddata[$v['promote_id']] = $v;
        }
        $inside = self::promoteUserData($map,$istimeselect,"promote_coin");

        foreach($inside as $k=>$v){
            if(array_key_exists($v['promote_id'],$senddata)){
                $senddata[$v['promote_id']]['inside_cash_count']= $v['pay_amount']-$v['back_amount'];
            }else{
                $v['inside_cash_count'] =  $v['pay_amount']-$v['back_amount'];
                unset($v['pay_amount']);
                $senddata[$v['promote_id']] = $v;
            };
        }
       
        if(array_key_exists(0,$senddata)){
            unset($senddata[0]);
        }
        
        $count = count($senddata);
        if ($_REQUEST['data_order'] != '') {
            $data_order = reset(explode(',', $_REQUEST['data_order']));
            $data_order_type = end(explode(',', $_REQUEST['data_order']));
            $this->assign('userarpu_order', $data_order);
            $this->assign('userarpu_order_type', $data_order_type);
        }

        $page = set_pagination($count, $row);
        if ($page) {
            $this->assign('_page', $page);
        }
        $data = [];
        $allcount =array(
            "cash_count"=>0,
            "balance_coin_count"=>0,
            "bind_coin_count"=>0,
            "inside_cash_count"=>0,
        );

        $marketAdmins = [];
        if (count($senddata) > 0) {
            $promoteIds = array_column($senddata, 'promote_id');
            if (count($promoteIds)) {
                $marketAdmins = getMarketAdminsByPromoteIds($promoteIds);
            }
        }
        
        foreach($senddata as $k=>$v){
            $promote = $promotes[$v['promote_id']] ?? null;
            $v['admin_username'] = isset($marketAdmins[$v['promote_id']]) && $marketAdmins[$v['promote_id']]['admin'] ? $marketAdmins[$v['promote_id']]['admin']['username'] : '无';
            $v['develop_type_text'] = getCompanyDevelopTypeText($v['develop_type']);
            $v['company_belong'] = getCompanyBlong($v['company_belong']);
            if(empty($v['company_name'])) $v['company_name']= $this->COMPANY_NAME;
            if(empty($v['promote_account'])) $v['promote_account']= C('OFFICIEL_CHANNEL');
            if(!array_key_exists('cash_count',$v)) $v['cash_count']=0;
            if(!array_key_exists('balance_coin_count',$v)) $v['balance_coin_count']=0;
            if(!array_key_exists('bind_coin_count',$v)) $v['bind_coin_count']=0;
            if(!array_key_exists('inside_cash_count',$v)) $v['inside_cash_count']=0;
            $v['allcount'] = number_format($v['cash_count']-0+$v['balance_coin_count']+$v['bind_coin_count'],2,'.','');
            $v['inside_cash_count'] = number_format($v['inside_cash_count'],2,'.','');
            $allcount['cash_count'] +=  $v['cash_count'];
            $allcount['balance_coin_count'] +=  $v['balance_coin_count'];
            $allcount['bind_coin_count'] +=  $v['bind_coin_count'];
            $allcount['inside_cash_count'] +=  $v['inside_cash_count'];
            $allcount['all_count'] +=  $v['allcount'];
            $data[] = $v;
        }

        $allcount['cash_count'] =  number_format($allcount['cash_count'],2,'.','');
        $allcount['balance_coin_count'] =  number_format($allcount['balance_coin_count'],2,'.','');
        $allcount['bind_coin_count'] =  number_format($allcount['bind_coin_count'],2,'.','');
        $allcount['inside_cash_count'] =  number_format($allcount['inside_cash_count'],2,'.','');
        $allcount['all_count'] =  number_format($allcount['all_count'],2,'.','');

        $data = my_sort($data, $data_order_type, (int)$data_order);
        if(isset($_REQUEST['export'])){

            $GetData = $_GET;
            unset($GetData['export']);
            addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData),'menu'=>"充值-财务管理-公会统计-渠道查看-导出"]);

            data2csv($data,'渠道统计',array(
                "company_name"=>"推广公司",
                "promote_account"=>"推广员账号",
                "admin_username"=>"所属市场专员",
                "company_belong"=>"团体类型",
                "develop_type_text"=>"开发类型",
                "cash_count"=>"游戏现金金额",
                "balance_coin_count"=>"平台币直充支出",
                "bind_coin_count"=>"内充支出",
                "inside_cash_count"=>"平台币内充",
                "allcount"=>"渠道游戏内充值合计"
            ));
        }

        $this->checkListOrCountAuthRestMap($map,[]);

        $size = $row;
        $data = array_slice($data, ($arraypage - 1) * $size, $size);
        $this->assign('all_count', $allcount);
        $this->assign('list_data', $data);
        $this->meta_title = '渠道统计';
        $this->display();
        // dump($senddata);

    }
    private function promoteUserData($map,$istimeselect,$type="spend")
    {
        if (isset($_REQUEST['company_id'])) {
            $map['promote.company_id'] = $_REQUEST['company_id'];
        }
        if ($type == "spend") {
            $adminId = 0;
            $isMarketAdmin = isMarketAdmin();
            if ($isMarketAdmin) {
                $userAuth = session('user_auth');
                $adminId = $userAuth['uid'];
            } else {
                if (!empty(I('admin_id'))) {
                    $adminId = I('admin_id');
                }
            }

            if (isset($_REQUEST['game_name']) || isset($_REQUEST['game_type'])) {
                $map["s.game_id"] = array("in",implode(',', array_column(getGameByName($_REQUEST['game_name'], $_REQUEST['game_type']), 'id')) );
            }
            if($istimeselect){
                $map['s.pay_time']=$istimeselect;
            }
            $isMarketLeader = isMarketLeader();
            if ($isMarketLeader) {
                $map['s.market_admin_id'] = ['gt', 0];
                $map['promote.admin_id'] = ['gt', 0];
            }
            if ($adminId) {
                $map['s.market_admin_id'] = $adminId;
            }
            $map['s.pay_status']=1;
            // dd($map);
            $field= "SUM(CASE WHEN pay_way > 0 THEN pay_amount ELSE 0 END) as cash_count,
            SUM(CASE WHEN pay_way = 0 THEN pay_amount ELSE 0 END) as balance_coin_count,
            SUM(CASE WHEN pay_way = -1 THEN pay_amount ELSE 0 END) as bind_coin_count,
            IFNULL(company_id,0) company_id,company_name,promote_id,promote.account promote_account,develop_type,IFNULL(promote.company_belong,0) company_belong";
            $query = M()
                ->table("tab_spend s use index(promote_time)")
                ->field($field)
                ->where($map)
                ->join("tab_promote promote ON s.promote_id = promote.id", "left")
                ->join("tab_promote_company company ON promote.company_id = company.id", "left")
                ->group('promote_id');

            $havingPids = [];
            if ($adminId > 0) {
                $topIds = M('promote', 'tab_')->where(['admin_id' => $adminId])->getField('id', true);
                $havingPids = getPromoteIdsByTopIds($topIds);
                $havingPids = count($havingPids) > 0 ? $havingPids : [0];
                $query->having('promote_id in (' . implode(',', $havingPids) . ')');
            }
            return $query->select();
        }else{
            if($istimeselect){
                $map['s.create_time']=$istimeselect;
            }
            $map['s.status']=1;
            $map['s.source_id']=0;

            $adminId = 0;
            $isMarketAdmin = isMarketAdmin();
            if ($isMarketAdmin) {
                $userAuth = session('user_auth');
                $adminId = $userAuth['uid'];
            } else {
                if (!empty(I('admin_id'))) {
                    $adminId = I('admin_id');
                }
            }
            
            $isMarketLeader = isMarketLeader();
            if ($isMarketLeader) {
                $map['promote.admin_id'] = ['gt', 0];
            }
            $field= "SUM(CASE WHEN s.type = 1 THEN num ELSE 0 END) as pay_amount,SUM(CASE WHEN s.type = 2 THEN num ELSE 0 END) as back_amount,
                    IFNULL(company_id,0) company_id,company_name,promote_id,promote.account promote_account,IFNULL(promote.company_belong,0) company_belong";
            $query = M("promote_coin s","tab_")
                ->field($field)
                ->where($map)
                ->join("tab_promote promote ON s.promote_id = promote.id","left")
                ->join("tab_promote_company company ON promote.company_id = company.id", "left")
                ->group('promote_id');

            $havingPids = [];
            if ($adminId > 0) {
                $topIds = M('promote', 'tab_')->where(['admin_id' => $adminId])->getField('id', true);
                $havingPids = getPromoteIdsByTopIds($topIds);
                $havingPids = count($havingPids) > 0 ? $havingPids : [0];
                $query->having('promote_id in (' . implode(',', $havingPids) . ')');
            }
            return $query->select();
        }
    }
    /**
     *游戏统计
     */
    public function gameStatistics($p = 1) {
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $row = $_REQUEST['row'] ?? 10;
        $map = [];
        if (isset($_REQUEST['game_name']) || isset($_REQUEST['game_type'])) {
            $map["s.game_id"] = array("in",implode(',', array_column(getGameByName($_REQUEST['game_name'], $_REQUEST['game_type']), 'id')) );
        }
        if (!empty(I('partner_id'))) {
            $map['g.partner_id'] = I('partner_id');
        }

        if (isset($_REQUEST['time_start']) && isset($_REQUEST['time_end'])) {
            $map['s.pay_time'] = ['between', [strtotime($_REQUEST['time_start']), strtotime($_REQUEST['time_end']) + 86399]];
        } elseif (isset($_REQUEST['time_start'])) {
            $map['s.pay_time'] = ['GT', strtotime($_REQUEST['time_start'])];
        } elseif (isset($_REQUEST['time_end'])) {
            $map['s.pay_time'] = ['LT', strtotime($_REQUEST['time_end']) + 86399];
        }
        if (!empty(I('develop_type'))) {
//            $map['develop_type'] = I('develop_type');
            $map['develop_type'] = ['in',I('develop_type')];
        }
        $isMarketLeader = isMarketLeader();
        if ($isMarketLeader) {
            $map['promote.admin_id'] = ['gt', 0];
            $map['s.market_admin_id'] = ['gt', 0];
        }

        //获取推广员
        $title = '';
        self::setPromoteMap($map,"s.promote_id",$title,false);
        // dd($map);
        $title .= "游戏统计";
        $this->assign('title', $title);
        
        //游戏统计列表
        $data =self::gameStatisticsData($map, $row, $p);
        if(isset($_REQUEST['export'])){
            $data =self::gameStatisticsData($map);
        }else {
            $data =self::gameStatisticsData($map, $row, $p);
        }
        $promotes = [];
        if (count($data) > 0) {
            $promoteIds = array_column($data, 'p_id');
            if (count($promoteIds) > 0) {
                $promotes = M('promote', 'tab_')->field(['id', 'account', 'admin_id'])->where(['id' => ['in', $promoteIds]])->select();
            }
        }
        $promotes = index_by_column('id', $promotes);
        $adminUsernames = getAdminUsernameList(array_column($promotes, 'admin_id'));

        foreach ($data as $key => &$value) {
            $promote = $promotes[$value['p_id']] ?? null;
            $value['cash_count']?:0;
            $value['balance_coin_count']?:0;
            $value['inside_cash_count']?:0;
            $value['all_cash_count'] = number_format($value['cash_count'] + $value['balance_coin_count'] + $value['inside_cash_count'],2,'.','');
            $value['company_belong'] =getCompanyBlong($value['company_belong']);
            ($value['account']=='官方渠道')?($value['account']=C('OFFICIEL_CHANNEL')):'';

            $value['admin_username'] = $adminUsernames[$promote['admin_id']] ?? '无';
            $value['develop_type_text'] = getCompanyDevelopTypeText($value['develop_type']);

            $value['game_name'] =clearGameNameType($value['game_name']);
            $value['sdk_version'] =getGameTypeName($value['sdk_version']);

            if(!$value['game_name']) $value['game_name'] = "无";
            //按类型判断pid
            if($_REQUEST['promote_type'] == 2 && $_REQUEST['promote_id']){
                $value['p_id'] = $_REQUEST['promote_id'];
            }
        }
        if (isset($_REQUEST['export'])) {

            $GetData = $_GET;
            unset($GetData['export']);
            addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData),'menu'=>"充值-财务管理-公会统计-游戏统计-导出"]);

            data2csv($data,$title, array(
                "company_name"=>"推广公司",
                "account"=>"会长账号",
                "admin_username"=>"所属市场专员",
                "company_belong"=>"团体类型",
                "develop_type_text"=>"开发类型",
                "game_name"=>"游戏",
                "sdk_version"=>"设备",
                "cash_count"=>"游戏现金金额",
                "balance_coin_count"=>"平台币直充支出",
                "inside_cash_count"=>"内充支出",
                "all_cash_count"=>"游戏内充值合计"
            ));
        }

        $this->checkListOrCountAuthRestMap($map,[]);

        $allData = self::gameStatisticsData($map);//列表总数
        $count = count($allData);
        $page = set_pagination($count, $row);
        if ($page) {
            $this->assign('_page', $page);//分页
        }

        //统计合计
        $totalData = self::totalGameStatisticsData($map);

        $sumCash = $totalData['cash_count'];
        $sumBalance = $totalData['balance_coin_count'];
        $sumInside = $totalData['inside_cash_count'];
        $sumAll =  number_format($sumCash + $sumBalance + $sumInside,2,'.','');

        $this->assign('sumCash', $sumCash);
        $this->assign('sumBalance', $sumBalance);
        $this->assign('sumInside', $sumInside);
        $this->assign('sumAll', $sumAll);
        $this->assign('data',$data);
        $this->meta_title = '游戏统计';
        $this->display();
    }
    //游戏订单详情
    public function gameStatisticsDetail($p = 1) {
        set_time_limit(0);
        if(isset($_REQUEST['game_id'])){

            //转化为名称和类型,重新请求
            $game = M("game","tab_")->field("relation_game_name,sdk_version")->where(" id = {$_REQUEST['game_id']} ")->find();
            if($game === false){
                $this->error("参数错误");
            }
            $_GET['game_name'] = $game['relation_game_name'];
            $_GET['game_type'] = $game['sdk_version'];
            unset($_GET['game_id']);
            $this->redirect(ACTION_NAME, I('get.'));
        }

        if (isset($_REQUEST['pay_way'])) {
            $map['pay_way'] = $_REQUEST['pay_way'];
        }

        if (!empty($_REQUEST['pay_order_number'])) {
            $map['pay_order_number'] = $_REQUEST['pay_order_number'];
        }

        $adminId = 0;
        $havingPids = [];
        $isMarketAdmin = isMarketAdmin();
        if ($isMarketAdmin) {
            $userAuth = session('user_auth');
            $adminId = $userAuth['uid'];
        } else {
            if (!empty(I('admin_id'))) {
                $adminId = I('admin_id');
            }
        }

        $isMarketLeader = isMarketLeader();
        if ($isMarketLeader) {
            $map['market_admin_id'] = ['gt', 0];
        }

        if ($adminId > 0) {
            $map['market_admin_id'] = $adminId;
        }

        /* if (!empty(I('develop_type'))) {
            $map['company.develop_type'] = I('develop_type');
        }

        if (!empty(I('admin_id'))) {
            $map['market_admin_id'] = I('admin_id');
        } */

        if (isset($_REQUEST['game_name']) || isset($_REQUEST['game_type']) || isset($_REQUEST['partner_id'])) {
            $map["game_id"] = array("in",implode(',', array_column(getGameidByPartnerNameType($_REQUEST['partner_id'],$_REQUEST['game_name'], $_REQUEST['game_type']), 'id')) );
        }

        if (isset($_REQUEST['time_start']) && isset($_REQUEST['time_end'])) {
            $map['pay_time'] = ['between', [strtotime($_REQUEST['time_start']), strtotime($_REQUEST['time_end']) + 86399]];
        } elseif (isset($_REQUEST['time_start'])) {
            $map['pay_time'] = ['GT', strtotime($_REQUEST['time_start'])];
        } elseif (isset($_REQUEST['time_end'])) {
            $map['pay_time'] = ['LT', strtotime($_REQUEST['time_end']) + 86399];
        }
        //获取推广员
        $title = '';
        self::setPromoteMap($map,"promote_id",$title);
        if (isset($_REQUEST['game_name'])){
            $title .= ($_REQUEST['game_name']."-");
        }
        $title .= "游戏订单查看";
        $this->assign('title', $title);
       


        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        if (isset($_REQUEST['row'])) {
            $row = $_REQUEST['row'];
        } else {
            $row = 10;
        }
        $map['pay_status'] = 1;
        if (!empty(I("game_id"))) {
            $map['game_id'] = I("game_id");
        }
        if (!empty(I('user_account'))) {
            $map['user_account'] = array('like','%'.I("user_account").'%');
        }
        if (!empty(I('user_nickname'))) {
            $map['game_player_name'] = array('like',I("user_nickname").'%');
        }
        $field = "pay_order_number,FROM_UNIXTIME(pay_time) as pay_time,user_account,user_nickname,game_name,sdk_version,promote_account,spend_ip,server_name,game_player_name,pay_amount,cost,pay_way,market_admin_id";
        $group = "";
        $order = "pay_time DESC";
        if(isset($_REQUEST['export'])){
            $data = D('spend')->getSpendData($map,$field,$group,$order);
        }else {
            $data = D('spend')->getSpendData($map,$field,$group,$order,$page,$row);
        }

        $marketAdmins = [];
        if (count($data) > 0) {
            $marketAdmins = getAdminUsernameList(array_column($data, 'market_admin_id'));
        }

        foreach($data as $key => $value) {
            $data[$key]['pay_way'] = getPayType($value['pay_way']);
            $data[$key]['game_name'] =clearGameNameType($data[$key]['game_name']);
            $data[$key]['sdk_version'] =getGameTypeName($data[$key]['sdk_version']);
            $data[$key]['market_admin_username'] = $marketAdmins[$value['market_admin_id']] ?? '无';
            ($data[$key]['promote_account']=='官方渠道')?($data[$key]['promote_account']=C('OFFICIEL_CHANNEL')):'';

            if (isset($_REQUEST['export'])) {
                checkEncryptionAuth($data[$key]['user_account'],'gameStatisticsDetail');
            }

        }

        if (isset($_REQUEST['export'])) {

            $GetData = $_GET;
            unset($GetData['export']);
            addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData),'menu'=>"充值-财务管理-公会统计-订单查看-导出"]);

            data2csv($data,$title, array(
                "pay_order_number"=>"支付订单号",
                "pay_time"=>"充值时间",
                "user_account"=>"玩家账号",
                "game_name"=>"游戏名称",
                "sdk_version"=>"设备名称",
                "promote_account"=>"所属推广员",
                "market_admin_username"=>"所属市场专员",
                "spend_ip"=>"充值ip",
                "server_name"=>"游戏区服",
                "game_player_name"=>"角色名称",
                "cost"=>"订单金额",
                "pay_amount"=>"实付金额",
                "pay_way"=>"充值方式",
            ));
        }
        $totalData = self::totalGameStatisticsData($map, false);
        $sumCash = $totalData['cash_count'] ?? 0;
        $sumBalance = $totalData['balance_coin_count'] ?? 0;
        $sumInside = $totalData['inside_cash_count'] ?? 0;
        $sumAll = $sumCash + $sumBalance + $sumInside;

        $this->checkListOrCountAuthRestMap($map,[]);

        $this->assign('sumCash', $sumCash);
        $this->assign('sumBalance', $sumBalance);
        $this->assign('sumInside', $sumInside);
        $this->assign('sumAll', null_to_0($sumAll));

        $count = D("spend")->getSpendData($map,$field,$group,$order);

        $count = count($count);
        $page = set_pagination($count,$row);
        if($page) {$this->assign('_page', $page);}
        $this->meta_title = '游戏订单查看';

        $this->assign('data',$data);
        $from = I('from', '');
        $this->display($from);
    }

    private function gameStatisticsData($map, $row = 0, $page = 1)
    {
        $map['s.pay_status']=1;
        $query = M("spend","tab_")->alias("s")
            ->field("s.game_id,s.game_name,s.sdk_version,p.partner as partner_name,
            SUM(CASE WHEN pay_way > 0 THEN pay_amount ELSE 0 END) as cash_count,
            SUM(CASE WHEN pay_way = 0 THEN pay_amount ELSE 0 END) as balance_coin_count,
            SUM(CASE WHEN pay_way = -1 THEN pay_amount ELSE 0 END) as inside_cash_count,
            IFNULL(if(substring_index(substring_index(promote.`chain`,'/',2),'/',-1)='',s.promote_id,substring_index(substring_index(promote.`chain`,'/',2),'/',-1)),0) p_id,
            IFNULL(company_name,'{$this->COMPANY_NAME}') company_name,develop_type,IFNULL(promote.company_belong,0) company_belong")
            ->join('LEFT JOIN tab_game as g ON s.game_id=g.id')
            ->join("tab_promote promote ON s.promote_id = promote.id","left")
            ->join("tab_promote_company company ON promote.company_id = company.id","left")
            ->join('LEFT JOIN tab_partner as p ON g.partner_id=p.id')
            ->order("p_id asc,cash_count desc")
            ->where($map)
            ->group('p_id,game_id');
        if ($row != 0) {
            $query = $query->page($page,$row);
        }
        $adminId = 0;
        $isMarketAdmin = isMarketAdmin();
        if ($isMarketAdmin) {
            $userAuth = session('user_auth');
            $adminId = $userAuth['uid'];
        } else {
            if (!empty(I('admin_id'))) {
                $adminId = I('admin_id');
            }
        }
        $havingPids = [];
        if ($adminId > 0) {
            $havingPids = M('promote', 'tab_')->where(['admin_id' => $adminId])->getField('id', true);
            $havingPids = count($havingPids) > 0 ? $havingPids : [0];
            $query->having('p_id in (' . implode(',', $havingPids) . ')');
        }
        $data = $query->select(false);
        $data =  M()
        ->table("(".$data.") a")
        ->field("a.*,IFNULL(promote.account,'官方渠道') account")
        ->join("tab_promote promote ON a.p_id = promote.id","left")
        ->select();
        
        return $data;
    }
    private function totalGameStatisticsData($map, $join = true)
    {
        $query = M("spend","tab_")->alias("s")
            ->field("SUM(CASE WHEN pay_way > 0 THEN pay_amount ELSE 0 END) as cash_count,
            SUM(CASE WHEN pay_way = 0 THEN pay_amount ELSE 0 END) as balance_coin_count,
            SUM(CASE WHEN pay_way = -1 THEN pay_amount ELSE 0 END) as inside_cash_count");
        if ($join) {
            $query = $query->join('LEFT JOIN tab_game as g ON s.game_id=g.id')
                ->join('LEFT JOIN tab_partner as p ON g.partner_id=p.id')
                ->join("tab_promote promote ON s.promote_id = promote.id","left")
                ->join("tab_promote_company company ON promote.company_id = company.id","left");
        }
        $data = $query->where(['s.pay_status' => 1])
            ->where($map)
            ->find();
        return $data;
    }
    //游戏弹框详情
    public function gameCoinDetail($p = 1) {

        set_time_limit(0);

        if (isset($_REQUEST['pay_way'])) {
            $map['pay_way'] = $_REQUEST['pay_way'];
            $this->assign('pay_way', $map['pay_way']);
        }
        if (!empty($_REQUEST['pay_order_number'])) {
            $map['pay_order_number'] = $_REQUEST['pay_order_number'];
        }
        if (!empty(I("game_id"))) {
            $map['tab_spend.game_id'] = I("game_id");
        }
        if (isset($_REQUEST['game_name']) || isset($_REQUEST['game_type']) || isset($_REQUEST['partner_id'])) {
            $map["tab_spend.game_id"] = array("in",implode(',', array_column(getGameidByPartnerNameType($_REQUEST['partner_id'],$_REQUEST['game_name'], $_REQUEST['game_type']), 'id')) );
        }
        if (isset($_REQUEST['time_start']) && isset($_REQUEST['time_end'])) {
            $map['pay_time'] = ['between', [strtotime($_REQUEST['time_start']), strtotime($_REQUEST['time_end']) + 86399]];
        } elseif (isset($_REQUEST['time_start'])) {
            $map['pay_time'] = ['GT', strtotime($_REQUEST['time_start'])];
        } elseif (isset($_REQUEST['time_end'])) {
            $map['pay_time'] = ['LT', strtotime($_REQUEST['time_end']) + 86399];
        }
        if ($_REQUEST['promote_id'] != '') {
            if ($_REQUEST['promote_id'] == 0 || $_REQUEST['promote_type'] == 2) {
                $map['tab_spend.promote_id'] = $_REQUEST['promote_id'];
            } else {
                $promoter_ids = D("Promote")->where("chain like '%/{$_REQUEST['promote_id']}/%' or id={$_REQUEST['promote_id']}")->field('id')->select();
                if ($promoter_ids) {
                    $map['tab_spend.promote_id'] = ['in', implode(',', array_column($promoter_ids, 'id'))];
                }
            }
        }


        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        if (isset($_REQUEST['row'])) {
            $row = $_REQUEST['row'];
        } else {
            $row = 10;
        }
        $map['pay_status'] = 1;

        if (!empty(I('user_account'))) {
            $map['tab_spend.user_account'] = array('like','%'.I("user_account").'%');
        }
        if (!empty(I('user_nickname'))) {
            $map['tab_spend.game_player_name'] = array('like',I("user_nickname").'%');
        }
        $field = "pay_order_number,FROM_UNIXTIME(pay_time) as pay_time,tab_spend.user_account,
        tab_spend.user_nickname,tab_spend.game_name,tab_spend.promote_account,spend_ip,tab_spend.server_name,tab_spend.sdk_version,
        game_player_name,pay_amount,cost,pay_way";
        $group = "";
        $order = "pay_time DESC";
        $join = false;
        if ($map['pay_way'] ==-1) {//绑定币
            $field2 = $field . ',bind_balance';
            $join = 'left join tab_user_play as p on p.user_id  = tab_spend.user_id and tab_spend.game_id=p.game_id';
        } elseif ($map['pay_way'] ==0) {//平台币
            $field2 = $field . ',balance';
            $join = 'left join tab_user as u on u.id  = tab_spend.user_id';
        }
        if(isset($_REQUEST['export'])){
            $data = D('spend')->getSpendData($map, $field2, $group, $order,0,0, $join);
        }else {
            $data = D('spend')->getSpendData($map, $field2, $group, $order, $page, $row, $join);
        }

        foreach($data as $key => $value) {
            $data[$key]['pay_way'] = getPayType($value['pay_way']);

            $data[$key]['game_name'] =clearGameNameType($data[$key]['game_name']);
            $data[$key]['sdk_version'] =getGameTypeName($data[$key]['sdk_version']);

            $data[$key]['cost'] = number_format($value['cost'],2,'.','');
            $data[$key]['pay_amount'] = number_format($value['pay_amount'],2,'.','');

            if (isset($_REQUEST['export'])) {
                checkEncryptionAuth($data[$key]['user_account'],'gameCoinDetail');
            }
        }

        if (isset($_REQUEST['export'])) {
            

            $field = array(
                "pay_order_number"=>"支付订单号",
                "pay_time"=>"充值时间",
                "user_account"=>"玩家账号",
                "game_name"=>"游戏名称",
                "sdk_version"=>"设备名称",
                "server_name"=>"游戏区服",
                "game_player_name"=>"角色名称",
                "cost"=>"订单金额",
                "pay_amount"=>"实付金额",
                "pay_way"=>"充值方式"
            );
            if ($map['pay_way'] ==-1) {
                $field["bind_balance"] = "绑定币余额";
                $title = "内充支出明细";
            } elseif ($map['pay_way'] ==0) {
                $field["balance"] = "平台币余额";
                $title = "平台币直充明细";
            }

            $GetData = $_GET;
            unset($GetData['export']);
            addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData),'menu'=>"充值-财务管理-公会统计-".$title."-导出"]);

            data2csv($data,$title,$field);
        }

        $this->checkListOrCountAuthRestMap($map,[]);

        $count = D("spend")->getSpendData($map,$field,$group,$order);
        $count = count($count);
        $page = set_pagination($count,$row);

        $total_cost = D('spend')->sumSpend($map, 'cost');//订单金额合计
        $total_pay_amount = D('spend')->sumSpend($map, 'pay_amount');//实付金额合计

        $this->assign('total_cost', $total_cost);
        $this->assign('total_pay_amount', $total_pay_amount);

        if($page) {$this->assign('_page', $page);}
        $this->meta_title = '游戏订单查看';

        $this->assign('data',$data);
        $this->display();
    }
    //平台币内充弹框
    public function insideCoinDetail($p = 1)
    {
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        if (isset($_REQUEST['row'])) {
            $row = $_REQUEST['row'];
        } else {
            $row = 10;
        }
        //时间
        $istimeselect = false;
        if (isset($_REQUEST['time_start']) && isset($_REQUEST['time_end'])) {
            $istimeselect = ['between', [strtotime($_REQUEST['time_start']), strtotime($_REQUEST['time_end']) + 86399]];
        } elseif (isset($_REQUEST['time_start'])) {
            $istimeselect = ['GT', strtotime($_REQUEST['time_start'])];
        } elseif (isset($_REQUEST['time_end'])) {
            $istimeselect = ['LT', strtotime($_REQUEST['time_end']) + 86399];
        }

        //是否是官方渠道
        $isgf = false;
        if ($_REQUEST['promote_id'] != '') {
            if($_REQUEST['promote_id'] == 0){
                $isgf = true;
            }
            $map['s.promote_id'] = array("in",implode(",",array_column(getPrmoteChlidAccount($_REQUEST['promote_id'],"id"),"id")).",".$_REQUEST['promote_id']);
        }
        //检索
        if (isset($_REQUEST['sn'])) {
            $pmap['order_number'] = array("LIKE","%".$_REQUEST['sn']."%");
            $map['sn'] = array("LIKE","%".$_REQUEST['sn']."%");
        }
        if (isset($_REQUEST['type'])) {
            if($_REQUEST['type'] == 2){
                $pmap['op_id'] =0;//肯定false
            }
            $map['s.type'] =$_REQUEST['type'];
        }

        //
        $senddata = [];
        $count =0;
        $total_cost = 0;
        if($isgf){
            $pmap['status']=1;
            if($istimeselect){
                $pmap['create_time'] = $istimeselect;
            }
            $admincount =  M("provide_user p","tab_")->field("sum(amount) amount,count(*) count")->where($pmap)->find();
            $count = $admincount["count"]?:0;
            $total_cost = $admincount["amount"]?:0;
            if($count >0){
                $admininside = M("provide_user p","tab_")
                    ->field("order_number,user_account,amount,balance,create_time")
                    ->join("left join tab_user as u on u.id  = p.user_id")
                    ->where($pmap);

                if(isset($_REQUEST['export'])){
                    $admininside =  $admininside->select();
                }else {
                    $admininside =  $admininside->page($page, $row)->select();
                }

                foreach ($admininside as $k => $v) {
                    $temp = [
                        "sn"=>$v['order_number'],
                        "op_type"=>"收入",
                        "op_sty"=>"后台发放玩家",
                        "account"=>"管理员",
                        "op_num"=>$v['amount'],
                        "balance_coin"=>$v['balance']?:'0.00',
                        "mark"=>"后台发放平台币",
                        "create_time"=>date("Y-m-d H:i:s",$v['create_time'])
                    ];
                    $senddata[] = $temp;
                }
            }

        }else{
            if($istimeselect){
                $map['s.create_time']=$istimeselect;
            }
            $map['s.status']=1;
            $map['s.source_id']=0;
            $ininsidecount = M("promote_coin s","tab_")
                ->field("SUM(CASE WHEN s.type = 1 THEN num ELSE 0 END) as pay_amount,SUM(CASE WHEN s.type = 2 THEN num ELSE 0 END) as back_amount,count(*) count")
                ->where($map)
                ->find();
            $count = $ininsidecount["count"]?:0;
            $total_cost = ($ininsidecount["pay_amount"]-$ininsidecount["back_amount"]);
            if ($count >0) {
                $field= "num,sn,promote.balance_coin,account,s.type,remark,s.create_time";
                $ininside = M("promote_coin s", "tab_")
                    ->field($field)
                    ->where($map)
                    ->join("tab_promote promote ON s.promote_id = promote.id", "left");
                if(isset($_REQUEST['export'])){
                    $ininside = $ininside->select();
                }else {
                    $ininside = $ininside->page($page, $row)->select();
                }
                foreach ($ininside as $k => $v) {
                    $temp = [
                        "sn"=>$v['sn'],
                        "op_type"=>($v['type'] == 1) ? "收入":"支出",
                        "op_sty"=>($v['type'] == 1) ? "后台发放":"后台回收",
                        "account"=>"管理员",
                        "op_num"=>$v['num']?:'0.00',
                        "balance_coin"=>$v['balance_coin'],
                        "mark"=>$v["remark"]?:"后台发放平台币",
                        "create_time"=>date("Y-m-d H:i:s",$v['create_time'])
                    ];
                    $senddata[] = $temp;
                }
            }

        }
        //
        if (isset($_REQUEST['export'])) {
            $field = array(
                "sn"=>"流水号",
                "op_type"=>"操作类型",
                "op_sty"=>"操作方式",
                "account"=>"对手账号类型",
                "op_num"=>"平台币数量",
                "balance_coin"=>"平台币余额",
                "mark"=>"交易说明",
                "create_time"=>"交易时间"
            );

            $GetData = $_GET;
            unset($GetData['export']);
            addOperationLog(['op_type'=>3,'key'=>getNowDate(),'url'=>U(CONTROLLER_NAME.'/'.ACTION_NAME,$GetData),'menu'=>"充值-财务管理-公会统计-"."平台币内充明细"."-导出"]);

            data2csv($senddata,"平台币内充明细",$field);
        }
        $this->checkListOrCountAuthRestMap($map,[]);

        $this->assign('total_cost', $total_cost);
        $page = set_pagination($count,$row);
        if($page) {$this->assign('_page', $page);}
        $this->assign('data',$senddata);
        $this->display();

    }
    private function setPromoteMap(&$map,$field,&$title,$ptype = true)
    {
        //公司判断
        $pmap = false; //非特意推广员检索
        $gfflg = true; //是否有官方
        if (isset($_REQUEST['company_id'])) {
            $pmap['company_id'] = $_REQUEST['company_id'];
            //获取公司名称
            if($pmap['company_id'] == 0){
                $title .= "{$this->COMPANY_NAME}-";
            }else{
                $gfflg = false;
                $title .= (M('promote_company',"tab_")->field("company_name")->where("id = '{$pmap['company_id']}'")->find()['company_name'] . "-");
            }
            
        }
        if (isset($_REQUEST['company_belong'])) {
            $pmap['company_belong'] = $_REQUEST['company_belong'];
            if($pmap['company_belong'] != 0){
                $gfflg = false;
            }
            $title .= (getCompanyBlong($_REQUEST['company_belong'])."-");
        }

        if (I('develop_type', 0) != 0) {
            $gfflg = false;
        }

        if (!empty(I('admin_id'))) {
            if(I('admin_id') != 0){
                $gfflg = false;
            }
        }

        if (isset($_REQUEST['promote_id'])) {
            if($ptype){
                $pmap = false;//取消非特异推广员检索
            }else{
                $pmap['id'] = $_REQUEST['promote_id'];
            }
             
            $title .= (get_promote_name($_REQUEST['promote_id'])."-");
            if ($_REQUEST['promote_id'] != 0){
                $gfflg = false;
            }
        }
        if($pmap){
            $promoter_ids = D("Promote")->where($pmap)->field('id')->select();
        

            if ($promoter_ids) {
                if($gfflg){
                    $t_in =  "0,".implode(',', array_column($promoter_ids, 'id'));
                }else{
                    $t_in = implode(',', array_column($promoter_ids, 'id'));
                }
                $map[$field] = ['in',$t_in];
            }else{
                $map[$field] = ['in',"-1"];
            }
        }
        // if($ptype){
            //判断是否有推广员
            $level = 1;
            if ($_REQUEST['promote_id'] != '') {
                if ($_REQUEST['promote_id'] != 0){
                    $level = M('promote', 'tab_')->field('level')->where(array('id' => $_REQUEST['promote_id']))->find()['level'];
                }
                if ($_REQUEST['promote_id'] == 0 || $_REQUEST['promote_type'] == 2) {
                    $map[$field] = $_REQUEST['promote_id'];
                } else {
                    if ($ptype) {
                        $promoter_ids = D("Promote")->where("chain like '%/{$_REQUEST['promote_id']}/%' or id={$_REQUEST['promote_id']}")->field('id')->select();
                    }else{
                        unset($pmap['id']);
                        $promoter_ids = D("Promote")->where($pmap)->where("chain like '%/{$_REQUEST['promote_id']}/%' or id={$_REQUEST['promote_id']}")->field('id')->select();
                    }
                    if ($promoter_ids) {
                        $map[$field] = ['in', implode(',', array_column($promoter_ids, 'id'))];
                    }
                }
            }
            $_POST['promote_level'] = $level;
        // }
        // dd($map);
        
    }
    
}