<?php
namespace Admin\Event;
use Think\Controller;
/**
 * 后台事件控制器
 * @author 鹿文学
 */
class SettlementEvent extends ThinkEvent {
    public function settlement($model = null,$p,$extend=array()) {
        $model || $this->error('模型名标识必须!');
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $arraypage=$page;
        $umap=$extend['umap'];
        $smap=$extend['smap'];
        //解析列表规则
        $fields = $model['fields'];

				$umap['puid'] = 0;
				if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
		$pid = M('promote','tab_')->field('id')->where(array('account'=>$_REQUEST['promote_account']))->find();
        // 关键字搜索
        $udata=M('User','tab_')
            ->field('fgame_id as game_id,fgame_name as game_name,if(parent_id = 0,tab_user.promote_id,parent_id) as pid,count(distinct tab_user.id) as unum,if(isnull(tab_apply.promote_ratio),tab_game.ratio,tab_apply.promote_ratio) as ratio,if(isnull(tab_apply.promote_money),tab_game.money,tab_apply.promote_money) as money')
            ->where($umap)
            ->join('tab_game on tab_user.fgame_id = tab_game.id')
            ->join('left join tab_apply on tab_apply.game_id = tab_game.id and tab_apply.promote_id = '.$pid['id'])
            ->group('tab_user.fgame_id')
            ->order('tab_user.id desc')
            ->select();
            foreach ($udata as $k => $v) {
                $udata[$k]['pid'] = $pid['id'];
                if(get_settlement($_REQUEST['timestart'],$_REQUEST['timeend'],$pid['id'],$v['game_id'])) {
                    unset($udata[$k]);
                }
            }
            $ucount=count($udata);

				$spend = M('Spend','tab_');

        $ssql=$spend
            ->field('tab_spend.pay_order_number,tab_spend.game_id,tab_spend.game_name,t.pid as pid,pay_amount,
            if(isnull(tab_apply.promote_ratio),tab_game.ratio,tab_apply.promote_ratio) as ratio,
            if(isnull(tab_apply.promote_money),tab_game.money,tab_apply.promote_money) as money')
            ->join("INNER JOIN (
                SELECT
                    tp2.*, tp1.id AS pid,
                    tp1.account AS promote_account
                FROM
                    tab_promote tp1
                LEFT JOIN tab_promote tp2 ON tp2.`chain` LIKE CONCAT('%/', tp1.id, '/%')
                WHERE
                    tp1. LEVEL = 1
            ) AS t ON promote_id = t.id")
            ->join('tab_game on tab_spend.game_id= tab_game.id')
            ->join('left join tab_apply on tab_apply.game_id = tab_game.id and tab_apply.promote_id = '.$pid['id'])
            ->where($smap)
            ->group('game_id,pid,pay_order_number')
            ->select(false);

				$sdata= $spend->table('('.$ssql.') as a')
						->field('a.game_id,a.game_name,a.pid,sum(a.pay_amount) as spay_amount,a.ratio,a.money')
						->group('game_id')
                        ->select();
                        
				foreach($sdata as $k => $v) {
					if (empty($sdata[0]['pid'])) {unset($sdata[$k]);}
                    if(get_settlement($_REQUEST['timestart'],$_REQUEST['timeend'],$pid['id'],$v['game_id'])) {
                        unset($sdata[$k]);
                    }
				}
				$scount = count($sdata);


        if(!empty($udata) && !empty($sdata)){
						$data = [];
            foreach ($udata as $key => $value) {
                foreach ($sdata as $k => $v) {
                    if (($value['pid'] == $v['pid']) && ($value['game_id'] == $v['game_id'])) {
                        $data[] = array_merge($value,$v);unset($udata[$key]);unset($sdata[$k]);
                    }
                }
            }
            $data = array_merge($data,$udata,$sdata);

        }elseif (!empty($udata)) {
            $data = $udata;
        }elseif (!empty($sdata)) {
            $data = $sdata;
        }

        $count=$ucount>=$scount?$ucount:$scount;
        //分页

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


        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);
        }
        $data=my_sort($data,$data_order_type,(int)$data_order);
        $size=$row;//每页显示的记录数
        $pnum = ceil(count($data) / $size); //总页数,ceil()函数用于求大于数字的最小整数
        //用array_slice(array,offset,length) 函数在数组中根据条件取出一段值;array(数组),offset(元素的开始位置),length(组的长度)
        $data = array_slice($data, ($arraypage-1)*$size, $size);
        $this->assign('model', $model);
        $this->assign('list_data', $data);
        $this->meta_title = $model['title'].'列表';
        $this->display($model['template_list']);
    }

    public function money_list($model = null,$p,$extend=array()) {
        $model || $this->error('模型名标识必须!');
        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        //解析列表规则
        $fields = $model['fields'];
        // 关键字搜索
        $map    =   empty($extend)?array():$extend;

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

        //读取模型数据列表
        $name = $model['m_name'];
        $new_model = D($name);
        $data = D($name)
            ->where($map)
            ->order($model['order'])
            ->page($page, $row)
            ->select();
        $count = D($name)->where($map)->count();
        $to_map=$map;
        $to_map['status']=1;
        $total =  D($name)
            ->where($to_map)
            ->order($model['order'])
            ->page($page, $row)
            ->sum("sum_money");
        //分页


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

        $this->assign('model', $model);
        $this->assign('list_data', $data);
        $this->assign('total',$total==null?0:$total);
        $this->meta_title = $model['title'].'列表';
        $this->display($model['template_list']);
    }

}