<?php
namespace App\Model;
/**
 * 消费模型
 * @author  鹿文学
 */
class SpendModel extends BaseModel {

	public function get_spend_list($user_id, $p=1) {
		
			$page = intval($p);
      $page = $page ? $page : 1; //默认显示第一页数据
			
			$row = 10;
			
			$spend = M('Spend','tab_');
			$deposit = M('Deposit','tab_');
			$bind = M('BindRecharge','tab_');

		
				$map['user_id'] = $user_id;		
				$bindids = $bind->field('GROUP_CONCAT(id) as ids')->where($map)->group('user_id')->select();
				$map['pay_way'] = array('gt',0);
				$depositids = $deposit->field('GROUP_CONCAT(id) as ids')->where($map)->group('user_id')->select();
				$spendids = $spend->field('GROUP_CONCAT(id) as ids')->where($map)->group('user_id')->select();
			
				$ids='';
				if(!empty($bindids[0]['ids'])){ $bids = $bindids[0]['ids']; $ids .= ','.$bids;$bmap['b.id'] = array('in',$bids);}
				if(!empty($depositids[0]['ids'])){$dids = $depositids[0]['ids']; $ids .= ','.$dids;$dmap['d.id'] = array('in',$dids);}
				if(!empty($spendids[0]['ids'])){$sids = $spendids[0]['ids']; $ids .= ','.$sids;$smap['s.id'] = array('in',$sids);}
				if(!empty($ids)) {
					$count = count(explode(',',substr($ids,1)));
				
					if(!empty($smap)) {
						empty($bmap) || $bind_data = $bind->alias('b')->field('left(b.pay_order_number,2) as props_name,b.id,b.amount as pay_amount,FROM_UNIXTIME(b.create_time,"%Y-%m-%d %H:%i:%s") as pay_time,b.game_id,b.game_name,b.zhekou')->where($bmap)->select(false);
						empty($dmap) || $deposit_data = $deposit->alias('d')->field('left(b.pay_order_number,2) as props_name,d.id,d.pay_amount,FROM_UNIXTIME(d.create_time,"%Y-%m-%d %H:%i:%s") as pay_time,if(d.user_id,0,0) as game_id,if(d.user_account,"","") as game_name,IF (d.user_id, -1, -1) as zhekou')->where($dmap)->select(false);
						$sql = $spend->alias('s')->field('s.id,ifnull(s.
						,"游戏消费"),s.pay_amount,FROM_UNIXTIME(s.pay_time,"%Y-%m-%d %H:%i:%s") as pay_time,s.game_id,s.game_name,IF (s.user_id, -1, -1) as zhekou')
							->union($bind_data)	
							->union($deposit_data)			
							->where($smap)->select(false);
						$lists = $spend->table('('.$sql.') as a')->page($page,$row)->select();
					} elseif(!empty($dmap)) {
						empty($bmap) || $bind_data = $bind->alias('b')->field('left(b.pay_order_number,2) as props_name,b.id,b.amount as pay_amount,FROM_UNIXTIME(b.create_time,"%Y-%m-%d %H:%i:%s") as pay_time,b.game_id,b.game_name,b.zhekou')->where($bmap)->select(false);
						$sql = $deposit->alias('d')->field('left(b.pay_order_number,2) as props_name,d.id,d.pay_amount,FROM_UNIXTIME(d.create_time,"%Y-%m-%d %H:%i:%s") as pay_time,if(d.user_id,0,0) as game_id,if(d.user_account,"","") as game_name,IF (d.user_id, -1, -1) as zhekou')
							->union($bind_data)		
							->where($dmap)->select();
						$lists = $spend->table('('.$sql.') as a')->page($page,$row)->select();
					}	elseif(!empty($bmap)) {
						$sql = $bind->alias('b')->field('left(b.pay_order_number,2) as props_name,b.id,b.amount as pay_amount,FROM_UNIXTIME(b.create_time,"%Y-%m-%d %H:%i:%s") as pay_time,b.game_id,b.game_name,b.zhekou')->where($bmap)->select();
						$lists = $spend->table('('.$sql.') as a')->page($page,$row)->select();
					}
					
				}
			
			if(is_array($lists)) {
				$data['lists'] = $lists;$data['status']=1;
				if($count > $row){
				$data['total'] = ceil($count/$row);
				} else {
					$data['total']=1;
				}
			} else {
				$data['lists']='';$data['total'] = 1;$data['status'] = 0;
			}
			$data['current'] = $page;
			
			return $data;
			
		
		
	}
	
}