<?php
namespace Mobile\Controller;
use Think\Controller;
use User\Api\MemberApi;
use User\Api\SuserApi;

/**
* 交易
*/
class TradeController extends BaseController {
	
	/**
	 * 获取当前出售中商品列表
	 * @param  integer  $p   当前页
	 * @param  integer  $game_id  游戏编号
	 * @author  鹿文学
	 */
	public function index($p=1,$game_id=0) {
		$devices = get_devices_type();
		switch(I('px','')) {
			case 1:$order = 'price asc';$this->assign('px',1);$_GET['px']=2;break;
			case 2:$order = 'price desc';$this->assign('px',2);$_GET['px']='';break;
			default:$order = 'create_time desc';$_GET['px']=1;
		}
		
		if($game_id>0) { $map['game_id'] = $game_id; $this->assign('game_name',get_game_name($game_id));}
		if($_GET['game_name']) { $map['game_name'] = array('like','%'.$_GET['game_name'].'%'); $this->assign('game_name',$_GET['game_name']);}
		
		$map['status'] = 3;
		if($devices) $map['sdk_version'] = $devices;
		
		$lists = D('Merchandise')->lists($p,$map,'id,title,game_name,online_time,price,screenshot',$order);
		
		foreach($lists['lists'] as $k => $v) {
			$data[$k]= array('title'=>$v['title'],'game_name'=>$v['game_name'],'online_time'=>date('Y-m-d H:i:s',$v['online_time']),'price'=>preg_match('/^[1-9]\d*(\.(00)?)?$/',$v['price'])?intval($v['price']):$v['price']);
			$data[$k]['icon'] = '';
			if(!empty($v['screenshot'])) {
			$icon = explode(',',$v['screenshot']);
			$data[$k]['icon'] = get_cover($icon[0],'path');	
			}
			$data[$k]['url'] = U('Trade/detail',array('id'=>$v['id']));
			$data[$k]['id'] = $v['id'];
		}
		$this->assign('lists',$data); 
		$this->assign('page',$lists['current']);
		$this->assign('totalpage',$lists['total']);
		$this->assign('is_app',$_REQUEST['is_app']);
		if($_REQUEST['is_app'] == 1){
            if($_REQUEST['scheme']){
                session('order_scheme',$_REQUEST['scheme']);
            }
		    $token = $_REQUEST['token'];
		    if($token){
                $token = think_decrypt($token);
                $info = json_decode($token,true);
                if($info['account']){
                    $user = M('user','tab_')->field('id,account,nickname')->where(['account'=>$info['account']])->find();
                    if($user){
                        $auth = array(
                                'user_id'   => $user['id'],
                                'account'   => $user['account'],
                                'nickname'  => $user['nickname'],
                        );
                        session('suser_auth', $auth);
                        session('suser_auth_sign', data_auth_sign($auth));
                        $this->assign('login_status',1);
                    }
                }
            }
        }

		$this->display();
	}
	
	/**
	 * 获取当前出售中商品列表
	 * @param  integer  $p   当前页
	 * @param  integer  $game_id  游戏编号
	 * @return json 
	 * @author  鹿文学
	 */
	public function ajaxtrade($p=1,$game_id=0,$game_name='') {
		$devices = get_devices_type();
		switch(I('px','')) {
			case 1:$order = 'price asc';break;
			case 2:$order = 'price desc';break;
			define:$order = 'create_time desc';
		}
		
		$game_id<1 || $map['game_id'] = $game_id;
		empty($game_name) || $map['game_name'] = array('like','%'.$game_name.'%'); 
	
		$map['status'] = 3;
		if($devices) $map['sdk_version'] = $devices;
		
		$lists = D('Merchandise')->lists($p,$map,'id,title,game_name,online_time,price,screenshot',$order);
		
		foreach($lists['lists'] as $k => $v) {
			$data[$k]= array('title'=>$v['title'],'game_name'=>$v['game_name'],'online_time'=>date('Y-m-d H:i:s',$v['online_time']),'price'=>preg_match('/^[1-9]\d*(\.(00)?)?$/',$v['price'])?intval($v['price']):$v['price']);
			$data[$k]['icon'] = '';
			if(!empty($v['screenshot'])) {
			$icon = explode(',',$v['screenshot']);
			$data[$k]['icon'] = get_cover($icon[0],'path');	
			}
			$data[$k]['url'] = U('Trade/detail',array('id'=>$v['id']));
		}
		
		$lists['lists'] = $data; 
		
		$this->ajaxReturn($lists,'json');
		
	}
	
	/**
	 * 搜索
	 * @param   integer  $p   页码
	 * @author  鹿文学
	 */
	public function search_list($p=1) {
		$devices = get_devices_type();
		if($_REQUEST['mark']==1) { /* 搜索后 */
			$game_name = $_GET['game_name'];
			
		  $this->assign('mark',1);
		  
			$lists = D('Game')->listsByName($p,$game_name,$devices?$devices:0);
			
			foreach($lists['lists'] as $k => $v) {
				$data[$k]= $v;
				$data[$k]['url'] = U('Trade/index',array('game_id'=>$v['id']));
			}
			$this->assign('referer',session('search_referer'));
		  $this->assign('data',$data);
			$this->assign('page',$lists['current']);
			$this->assign('totalpage',$lists['total']);
		  
		} else {
			$referer = $_SERVER['HTTP_REFERER']?$_SERVER['HTTP_REFERER']:U('Trade/index');
			session('search_referer',$referer);
			$this->assign('referer',$referer);
			$lists = D('Game')->listsByRecommend($devices?$devices:0);
			
			$this->assign('reco',$lists);
			
		}
		
		$this->display();
	}
	
	/**
	 * 搜索
	 * @param   integer  $p   页码
	 * @author  鹿文学
	 */
	public function ajaxsearch_list($p=1) {
		
		$devices = get_devices_type();
		
		$game_name = I('game_name','');
		$is_app = I("is_app","");
		$token = I("token","");
		$lists = D('Game')->listsByName($p,$game_name,$devices?$devices:0);
		
		foreach($lists['lists'] as $k => $v) {
			$data[$k]= $v;
			$data[$k]['url'] = U('Trade/index',array('game_id'=>$v['id'],'is_app'=>$is_app,'token'=>$token));
		}
		
		$lists['lists'] = $data; 
		
		$this->ajaxReturn($lists,'json');
		
	}
	
	/**
	 * 商品详情
	 * @param  integer  $id   商品编号
	 * @return json 
	 * @author  鹿文学
	 */
	public function detail($id=0) {
		
		$this->assign('data',D('Merchandise')->sale_detail($id));
		$this->assign('rule_status',D('User')->get_rule_status());
		$this->assign('user',D('User')->getLoginInfo());
		$this->display();
		
	}
	
	/**
	 * 检测是否登录
	 * @author 鹿文学
	 */
	public function checkLogin() {
		if($this->islogin()) {
			$this->ajaxReturn(array('status'=>1),'json');
		} else {
			
			$html=$this->fetch('Trade/login');
			
			$this->ajaxReturn(array('status'=>0,'html'=>$html),'json');
			
		}
	}
	
	/**
	 * 检测是否自己商品
	 * @author 鹿文学
	 */
	public function checkIsSelf($id=0) {
		if(IS_POST && is_numeric($id) && $id>0 ) {
			if(!$this->islogin()) {$this->ajaxReturn(array('status'=>0,'html'=>$this->fetch('Trade/login')),'json');exit;}
			$user = D('User')->getLoginInfo();
			$merchandise = D('Merchandise')->buy_info($id);
			if($user['user_id'] == $merchandise['seller_id']) {$this->ajaxReturn(array('status'=>0,'info'=>'自己出售的商品,无法购买'),'json');exit;}
			$this->ajaxReturn(array('status'=>1,'info'=>''),'json');exit;
		} else {
			$this->ajaxReturn(array('status'=>0,'info'=>'参数错误'),'json');exit;
		}
	}
	
	/**
	 * 购买商品
	 * @author 鹿文学
	 */
	public function buy_good($id=0) {
		if(IS_POST) {
			if(!$this->islogin()) {$this->ajaxReturn(array('status'=>0,'url'=>U('Trade/detail',array('id'=>$id))),'json');exit;}
			
			if(!is_numeric($_POST['way']) || $_POST['way']<1) {$this->ajaxReturn(array('status'=>0,'info'=>'请选择支付方式'),'json');exit;}
			
			$order = D('Order')->get_info_by_good($id);
			if(is_array($order)) {$this->ajaxReturn(array('status'=>0,'info'=>'此商品已出售'),'json');exit;}
			$member = new SuserApi;
			$merchandise = D('Merchandise')->buy_info($id);
			
			$money = $merchandise['price'];
			
			$user = $member->login_info();
			if($user['user_id'] == $merchandise['seller_id']) {$this->ajaxReturn(array('status'=>0,'info'=>'自己出售的商品,无法购买'),'json');exit;}
			// 购买者编号及账户
			$merchandise['user_id'] = $user['user_id'];
			$merchandise['type'] = 'small';
			
			$withdraw_config = M('tool','tab_')->where(['name'=>'withdraw'])->find();
            $withdraw = json_decode($withdraw_config['config'],true);
			if(!is_array($withdraw)) {$this->ajaxReturn(array('status'=>0,'info'=>'设置有误,请联系管理员'),'json');exit;}
			
			$poundage = round($money*$withdraw['sell_rate']/100,2);
			if($poundage*100<$withdraw['poundage']*100) {$poundage = $withdraw['poundage'];}
			$merchandise['real'] = $money-$poundage;
			$merchandise['poundage'] = $poundage;
			
			$pay = A('Pay');			
			$result = $pay->buy($money,json_encode($merchandise),$_POST['way']);
			$this->ajaxReturn($result,'json');
			
		} else {
			
			$this->assign('data',D('Merchandise')->info($id));
			
			$this->display();
		
		}
	}
	
	/**
	 * 付款成功后页面
	 * @author 鹿文学
	 */
	public function success() {
		$orderno=$_GET['trade_no']==""?$_GET['out_trade_no']:$_GET['trade_no'];

		$order = D('Order')->info_by_orderno($orderno);
		
		$this->assign('game_id',$order['game_id']);
		
		$this->display();
		
	}
	
	/**
	 * 成交动态
	 * @param   integer  $p   页码
	 * @author  鹿文学
	 */
	public function deal($p=1) {
			$order = 'over_time desc';
			
			$map['status'] = 1;
			
			$lists = D('Merchandise')->lists($p,$map,'id,title,game_name,online_time,price,screenshot,over_time',$order);
			
			foreach($lists['lists'] as $k => $v) {
				$data[$k]= array('title'=>$v['title'],'game_name'=>$v['game_name'],'over_time'=>date('m-d H:i',$v['over_time']),'price'=>preg_match('/^[1-9]\d*(\.(00)?)?$/',$v['price'])?intval($v['price']):$v['price']);
				$data[$k]['icon'] = '';
				if(!empty($v['screenshot'])) {
				$icon = explode(',',$v['screenshot']);
				$data[$k]['icon'] = get_cover($icon[0],'path');	
				}
				$data[$k]['url'] = U('Trade/deal_detail',array('id'=>$v['id']));
			}
			
			$this->assign('lists',$data); 
			$this->assign('page',$lists['current']);
			$this->assign('totalpage',$lists['total']);
		
		$referer  = $_SERVER['HTTP_REFERER'];
		if(strpos(strtolower($referer),'trade/index')=== false) {$referer = U('Trade/index');}
		$this->assign('referer',$referer);
		
		
		$this->display();
	}
	
	/**
	 * 成交动态
	 * @param   integer  $p   页码
	 * @author  鹿文学
	 */
	public function ajaxdeal($p=1) {

		$order = 'over_time desc';
		
		$map['status'] = 1;
		
		$lists = D('Merchandise')->lists($p,$map,'id,title,game_name,online_time,price,screenshot,over_time',$order);
		
		foreach($lists['lists'] as $k => $v) {
			$data[$k]= array('title'=>$v['title'],'game_name'=>$v['game_name'],'over_time'=>date('m-d H:i',$v['over_time']),'price'=>preg_match('/^[1-9]\d*(\.(00)?)?$/',$v['price'])?intval($v['price']):$v['price']);
			$data[$k]['icon'] = '';
			if(!empty($v['screenshot'])) {
			$icon = explode(',',$v['screenshot']);
			$data[$k]['icon'] = get_cover($icon[0],'path');	
			}
			$data[$k]['url'] = U('Trade/deal_detail',array('id'=>$v['id']));
		}
		
		$lists['lists'] = $data; 
		
		$this->ajaxReturn($lists,'json');
	}
	
	/**
	 * 成交动态商品详情
	 * @param   integer  $id   商品编号
	 * @author  鹿文学
	 */
	public function deal_detail($id=0) {
		
		$this->assign('data',D('Merchandise')->deal_detail($id));
		
		$this->display();
		
	}
	
	/**
	 * 规则显示与否
	 * @param  integer  $type   类型 0:表示买家  1:表示卖家
	 * @return  json 
	 * @author   鹿文学
	 */
	public function rule_status($type=0) {
		if(IS_POST) {
			
			$return = D('User')->set_rule_status($type);
			
			$this->ajaxReturn($return,'json');
			
		} else {
			$this->ajaxReturn(['status'=>0,'info'=>'参数错误'],'json');
		}
	}
	 
	
	/**
	 * 我要卖号
	 * @author  鹿文学
	 */
	public function publish() {
		if(IS_POST) {
            $this->checksafecode($_POST['phone'],$_POST['code'],false);
			$usermodel = D('User');
			$user = $usermodel->getLoginInfo();
			$_POST['user_id'] = $user['user_id'];
			$_POST['user_account'] = $user['account'];
			$small = $usermodel->get_small_info($_POST['small_id']);
			$_POST['day'] = $small['day']+1;
			$_POST['accumulation'] = $small['accumulation'];
			$merchandise = D('Merchandise');
			$pa = $usermodel->field('id')->where(['is_platform'=>1])->find();
			if(is_array($pa)) {
				$res = $merchandise->update($_POST);
				if($res) {								
					$usermodel->where(['id'=>$_POST['small_id']])->setField(['puid'=>$pa['id'],'source_puid'=>$user['user_id']]);
					$this->ajaxReturn(['status'=>1,'info'=>'提交成功,将于1-2个工作日内完成审核','url'=>U('Trade/record')],'json');					
				} else {
					$this->ajaxReturn(['status'=>0,'info'=>$merchandise->getError()],'json');
				}
			} else {
				$this->ajaxReturn(['status'=>0,'info'=>'小号交易还未开通,请联系客服'],'json');
			}
		} else {
			
			if($this->islogin()) {
				
				$this->assign('phone',D('User')->get_phone());
				$this->assign('rule_status',D('User')->get_rule_status(1));
				
			} else {
				$this->assign('url',U('Trade/publish'));
				$this->assign('nologin',$this->fetch('login'));
			}
			
			$this->display();
			
		}
	} 
	
	public function edit_good() {
		if(IS_POST) {
            $this->checksafecode($_POST['phone'],$_POST['code'],false);
			$usermodel = D('User');
			$user = $usermodel->getLoginInfo();
			$_POST['user_id'] = $user['user_id'];
			$_POST['user_account'] = $user['account'];
			$small = $usermodel->get_small_info($_POST['small_id']);
			$_POST['day'] = $small['day'];
			$_POST['accumulation'] = $small['accumulation'];
			$_POST['status'] = 0;
			$merchandise = D('Merchandise');
			$res = $merchandise->update($_POST);
			if($res) {
				
				$pa = $usermodel->field('id')->where(['is_platform'=>1])->find();				
				$usermodel->where(['id'=>$_POST['small_id']])->setField(['puid'=>$pa['id'],'source_puid'=>$user['user_id']]);
				$this->ajaxReturn(['status'=>1,'info'=>'提交成功,将于1-2个工作日内完成审核','url'=>U('Trade/record')],'json');
			} else {
				$this->ajaxReturn(['status'=>0,'info'=>$merchandise->getError()],'json');
			}
		} else {
			
			if($this->islogin()) {
				
				$this->assign('rule_status',D('User')->get_rule_status(1));
				
				$this->assign('data',D('Merchandise')->detail($_REQUEST['id']));
				
			} else {
				$this->assign('url',U('Trade/publish'));
				$this->assign('nologin',$this->fetch('login'));
			}
			
			$this->display();
			
		}
	}

    /**
     * 手机安全码验证
     * @param  bool $flag  true 用于直接异步请求  false 用于方法调用
     * @param  [type]  $vcode [description]
     */
    public function checksafecode($phone,$vcode,$flag=true) {
        $result = R('Common/Sms/verify_sms_code',array($phone,$vcode,false));

        if($result['code']==200) {
            $data['status'] = 1;
            if($flag) {
                echo json_encode($data);exit;
            }
        } else {
            $data['status'] = 0;
            $data['info'] = $result['msg'];
            echo json_encode($data);exit;
        }
    }
	
	/**
	 * 选择游戏
	 * @param   integer  $p   页码
	 * @author 鹿文学
	 */
	public function select_game($p=1) {
		
		$devices = get_devices_type();
		
		empty($devices) || $map['g.sdk_version'] = $devices;
		
		$lists = D('Game')->lists_by_spend($p,$map,'g.id,g.sdk_version,g.game_name,g.icon');
		
		foreach($lists['lists'] as $k => $v) {
			$data[$k]= $v;
			$data[$k]['icon'] = '';
			if(!empty($v['icon'])) {
			$icon = get_cover($v['icon'],'path');
			$data[$k]['icon'] = $icon;
			}
			$data[$k]['url'] = U('Trade/select_small',array('game_id'=>$v['id']));
		}
		
		$this->assign('gamelists',$data); 
		$this->assign('gamepage',$lists['current']);
		$this->assign('gametotalpage',$lists['total']);
		
		$html = $this->fetch('select_game');
		
		$this->ajaxReturn(['html'=>$html],'json');
		
	}
	
	/**
	 * 选择游戏
	 * @param   integer  $p   页码
	 * @author 鹿文学
	 */
	public function ajax_select_game($p=1) {
		
		$lists = D('Spend')->lists_by_spend($p,$map,'g.id,g.sdk_version,g.game_name,g.icon');
		
		foreach($lists['lists'] as $k => $v) {
			$data[$k]= $v;
			$data[$k]['icon'] = '';
			if(!empty($v['icon'])) {
			$icon = get_cover($v['icon'],'path');
			$data[$k]['icon'] = is_file(MROOT.substr($icon,1))?$icon:'';	
			}
			$data[$k]['url'] = U('Trade/select_small',array('game_id'=>$v['id']));
		}
		
		$lists['lists'] = $data; 
		
		$this->ajaxReturn($lists,'json');
	}
	
	/**
	 * 选择小号
	 * @param   integer  $game_id   游戏编号
	 * @author 鹿文学
	 */
	public function select_small($game_id=0) {
		$game = D('Game')->get_info($game_id);
		if($game) {
            $icon = get_cover($game['icon'],'path');
            $game['icon'] = $icon;
			$this->assign('game',$game); 
			$this->assign('smalldata',D('Spend')->get_small_list_by_game($game_id)); 
		}
		$html = $this->fetch('select_small');
		
		$this->ajaxReturn(['html'=>$html],'json');
		
	}
	
	/**
	 * 选择区服
	 * @param   integer  $game_id			游戏编号
	 * @author 鹿文学
	 */
	public function select_server($game_id=0) {
		$game = D('Game')->get_info($game_id);
		if(is_array($game)) {
            $game['icon_url'] = get_cover($game['icon']);
			$this->assign('game',$game); 
			$this->assign('serverdata',D('Server')->get_server_list_by_game($game_id)); 
		}
		$html = $this->fetch('select_server');
		
		$this->ajaxReturn(['html'=>$html],'json');
		
	}
	
	/**
	 * 选择标题
	 * @author 鹿文学
	 */
	public function sell_title() {
		
		$html = $this->fetch('sell_title');
		
		$this->ajaxReturn(['html'=>$html],'json');
		
	}
	
	/**
	 * 选择描述
	 * @author 鹿文学
	 */
	public function sell_content() {
		
		$html = $this->fetch('sell_content');
		
		$this->ajaxReturn(['html'=>$html],'json');
		
	}
	
	/**
	 * 选择二级密码
	 * @author 鹿文学
	 */
	public function sell_password() {
		
		$html = $this->fetch('sell_password');
		
		$this->ajaxReturn(['html'=>$html],'json');
		
	}
	
	/**
	 * 交易记录
	 * @param  integer  $p   当前页
	 * @author 鹿文学
	 */
	public function record($p=1,$type='') {
		if($this->islogin()) {
		$map=array();
		$_GET['type']=$type;
		$merchandise = D('Merchandise');
		$user = D('User')->getLoginInfo();
		switch($type) {
			case 1:{/* 已购买 */
				$map['m.status']=array('in',[1,4]);$map['m.buyer_status'] = 0;$map['buyer_id']=$user['user_id'];$order='pay_time desc';
				$lists = $merchandise->lists_and_order($p,$map,'m.id,title,m.remark,if(m.status=4,1,m.status) as status,m.user_id,game_name,pay_time as time,price,screenshot,o.is_refund',$order);
			};break;
			case 2:{/* 出售中 */
				$map['m.status']=3;$map['m.seller_status'] = 0;$map['m.user_id']=$user['user_id'];$order='online_time desc';
				$lists = $merchandise->lists($p,$map,'m.id,title,m.remark,game_name,m.status,m.user_id,online_time as time,price,screenshot',$order);
			};break;
			case 3:{/* 已出售 */
				$map['m.status']=array('in',[1,4]);$map['m.seller_status'] = 0;$map['m.user_id']=$user['user_id'];$order='pay_time desc';
				$lists = $merchandise->lists_and_order($p,$map,'m.id,title,m.remark,if(m.status=4,1,m.status) as status,m.user_id,game_name,pay_time as time,price,screenshot',$order);
			};break;
			default:{
				$_GET['type']='';
				$map['m.status'] = array('gt',-2);
				$map['_complex']=array(['m.user_id'=>$user['user_id'],'m.seller_status'=>0],['buyer_id'=>$user['user_id'],'m.buyer_status'=>0,'pay_status'=>1,'m.status'=>array('in',[1,4])],'_logic'=>'or');
				$order='create_time desc';
				$lists = $merchandise->lists_and_order($p,$map,'m.id,title,m.remark,if(m.status=4,1,m.status) as status,m.user_id,game_name,create_time as time,price,screenshot,o.is_refund',$order);
			}
		}
		foreach($lists['lists'] as $k => $v) {
			$data[$k]= array('id'=>$v['id'],'title'=>$v['title'],'remark'=>$v['remark'],'game_name'=>$v['game_name'],'otime'=>$v['time'],'time'=>date('m-d H:i',$v['time']),'price'=>preg_match('/^[1-9]\d*(\.(00)?)?$/',$v['price'])?intval($v['price']):$v['price']);
			$data[$k]['icon'] = '';
			if(!empty($v['is_refund'])) {$data[$k]['is_refund']=$v['is_refund'];}
			if(!empty($v['screenshot'])) {
			$icon = explode(',',$v['screenshot']);
			$data[$k]['icon'] = get_cover($icon[0],'path');	
			}
			if($user['user_id'] != $v['user_id'] && $v['status']==1){$data[$k]['status']=6;} else{$data[$k]['status']=$v['status'];}
			$data[$k]['url'] = U('Trade/record_info',array('id'=>$v['id']));
		}
		
		$this->assign('lists',$data); 
		$this->assign('page',$lists['current']);
		$this->assign('totalpage',$lists['total']);
		
		} else {
			$this->assign('url',U('Trade/record'));
			$this->assign('nologin',$this->fetch('login'));
		}
		
		$this->display();
	}
	
	/**
	 * 交易记录
	 * @param  integer  $p   当前页
	 * @return json 
	 * @author  鹿文学
	 */
	public function ajaxrecord($p=1,$type='') {
		$map=array();
		$_GET['type']=$type;
		$merchandise = D('Merchandise');
		$user = D('User')->getLoginInfo();
		switch($type) {
			case 1:{/* 已购买 */
				$map['m.status']=array('in',[1,4]);$map['m.buyer_status'] = 0;$map['buyer_id']=$user['user_id'];$order='pay_time desc';
				$lists = $merchandise->lists_and_order($p,$map,'m.id,title,m.remark,if(m.status=4,1,m.status) as status,m.user_id,game_name,pay_time as time,price,screenshot',$order);
			};break;
			case 2:{/* 出售中 */
				$map['m.status']=3;$map['m.seller_status'] = 0;$map['m.user_id']=$user['user_id'];$order='online_time desc';
				$lists = $merchandise->lists($p,$map,'m.id,title,m.remark,game_name,m.status,m.user_id,online_time as time,price,screenshot',$order);
			};break;
			case 3:{/* 已出售 */
				$map['m.status']=array('in',[1,4]);$map['m.seller_status'] = 0;$map['m.user_id']=$user['user_id'];$order='pay_time desc';
				$lists = $merchandise->lists_and_order($p,$map,'m.id,title,m.remark,if(m.status=4,1,m.status) as status,m.user_id,game_name,pay_time as time,price,screenshot',$order);
			};break;
			default:{
				$_GET['type']='';
				$map['_complex']=array(['m.user_id'=>$user['user_id'],'m.seller_status'=>0],['buyer_id'=>$user['user_id'],'m.buyer_status'=>0],'_logic'=>'or');
				$order='create_time desc';
				$lists = $merchandise->lists_and_order($p,$map,'m.id,title,m.remark,if(m.status=4,1,m.status) as status,m.user_id,game_name,create_time as time,price,screenshot',$order);
			}
		}
		foreach($lists['lists'] as $k => $v) {
			$data[$k]= array('id'=>$v['id'],'title'=>$v['title'],'remark'=>$v['remark'],'game_name'=>$v['game_name'],'otime'=>$v['time'],'time'=>date('m-d H:i',$v['time']),'price'=>preg_match('/^[1-9]\d*(\.(00)?)?$/',$v['price'])?intval($v['price']):$v['price']);
			$data[$k]['icon'] = '';
			if(!empty($v['is_refund'])) {$data[$k]['is_refund']=$v['is_refund'];}
			if(!empty($v['screenshot'])) {
			$icon = explode(',',$v['screenshot']);
			$data[$k]['icon'] = get_cover($icon[0],'path');	
			}
			if($user['user_id'] != $v['user_id'] && $v['status']==1){$data[$k]['status']=6;} else{$data[$k]['status']=$v['status'];}
			$data[$k]['url'] = U('Trade/record_info',array('id'=>$v['id']));
		}
		
		$lists['lists'] = $data; 
		
		$this->ajaxReturn($lists,'json');
		
	}
	
	/**
	 * 商品详情
	 * @param  integer  $id   商品编号
	 * @return json 
	 * @author  鹿文学
	 */
	public function record_info($id=0) {
		
		if($this->islogin()) {
			
			$record=D('Merchandise')->record_info($id);
			
			$this->assign('data',$record);
			
			$this->assign('relevant',D('Merchandise')->relevant($record['game_id'],$id));
			
		
		}
		$this->display();
		
	}
	
	
	/**
	 * 删除记录
	 * @author 鹿文学
	 */
	public function del_record($id='',$b='') {
		
		$merchandise = D('Merchandise');
		
		if($merchandise->del_record($id,$b)) {
			$this->ajaxReturn(['status'=>1,'info'=>'删除成功'],'json');
		} else {
			$this->ajaxReturn(['status'=>0,'info'=>$merchandise->getError()],'json');
		}
	}
	
	/**
	 * 下架记录
	 * @author 鹿文学
	 */
	public function offline_record($id='') {
		
		$merchandise = D('Merchandise');
		
		if($merchandise->offline_record($id)) {
			$this->ajaxReturn(['status'=>1,'info'=>'下架成功'],'json');
		} else {
			$this->ajaxReturn(['status'=>0,'info'=>$merchandise->getError()],'json');
		}
	}
	
	/**
	 * 更改价格
	 * @author 鹿文学
	 */
	public function change_price($id='',$price='') {
		
		$merchandise = D('Merchandise');
		
		if($merchandise->change_price($id,$price)) {
			$this->ajaxReturn(['status'=>1,'info'=>'改价成功'],'json');
		} else {
			$this->ajaxReturn(['status'=>0,'info'=>$merchandise->getError()],'json');
		}
	}
	
	/**
	 * 使用说明
	 * @author 鹿文学
	 */
	public function useing_record($id='') {
		
		$html = $this->fetch('useing_record');
		
		$this->ajaxReturn(['html'=>$html],'json');
		
	}
	
	/**
	 * 交易须知
	 * @author 鹿文学
	 */
	public function trade_notice() {
		
		$this->display();
		
	}
	
	/**
	 * 提现信息页
	 * @author 鹿文学
	 */
	public function withdraw() {
		if($this->islogin()) {
			$this->assign('gold_coin',D('User')->get_gold_coin());
		}
		$this->display();
	}
	
	/**
	 * 提现操作页
	 * @author 鹿文学
	 */
	public function withdraw_gold() {
		if(IS_POST) {
			$user = D('User')->getLoginInfo();
			if(!is_array($user)) {$this->ajaxReturn(['status'=>0,'info'=>'请登录'],'json');}
			
			$gold_all = D('User')->field('gold_coin')->where(['id'=>$user['user_id']])->find();
			$all = $gold_all['gold_coin'];
			$coin = abs(is_numeric($_POST['coin'])?$_POST['coin']:0);			
			$rate = C('withdraw.widthdraw_rate');
			$low_coin = C('withdraw.coin');
			$low_poundage = C('withdraw.poundage');
			
			if(($all*100<($low_poundage+$low_coin)*100) || ($all*100<$coin*100)) {$this->ajaxReturn(['status'=>0,'info'=>'可提取金币不足'],'json');}
			if($coin*100<($low_poundage*100+$low_coin*100)) {$this->ajaxReturn(['status'=>0,'info'=>'提现金币不少于'. ($low_poundage+$low_coin)],'json');}
			$poundage = round($coin*$rate/100,2);$money=0;
			/* if($poundage*100<100) {$poundage=1;} */
			if($poundage*100<$low_poundage*100) {$this->ajaxReturn(['status'=>0,'info'=>'可提取金币不足'],'json');}
			
			$money=$coin-$poundage;
			$data['user_id'] = $user['user_id'];
			$data['user_account'] = $user['account'];
			$data['coin'] = $coin;
			$data['poundage'] = $poundage;
			$data['money'] = $money;
			$data['alipay'] = $_POST['alipay'];
			$data['alipay_real_name'] = $_POST['alipay_real_name'];
			$wgc = D('WithdrawGoldCoin');
			$ye = $all-$coin;
			if($ye<0){$this->ajaxReturn(['status'=>0,'info'=>'提现金币不足'],'json');}
			$data['gold_coin_balance'] = $ye;
			$res = $wgc->update($data);
			if($res) {
				D('User')->where(['id'=>$user['user_id']])->setField('gold_coin',$ye);
				$this->ajaxReturn(['status'=>1,'info'=>'提交成功,1-2个工作日内到账','url'=>U('Trade/withdraw')],'json');
			} else {
				$this->ajaxReturn(['status'=>0,'info'=>$wgc->getError()],'json');
			}
		} else {
		    $issdk = $_REQUEST['issdk'];
			if($this->islogin()) {
			    $this->assign('issdk',$issdk);
				$this->assign('user',D('User')->get_info_about_gold());
			} else {
				$this->assign('url',U('Trade/withdraw_gold',array('issdk'=>$issdk)));
				$this->assign('nologin',$this->fetch('login'));
			}
			
			
			$this->display();
		}
	}
	
	/**
	 * 支付宝信息
	 * @author 鹿文学
	 */
	public function alipay() {
		
		$html = $this->fetch('alipay');
		
		$this->ajaxReturn(['html'=>$html],'json');
		
	}
	
	/**
	 * 添加支付宝信息
	 * @author 鹿文学
	 */
	public function save_alipay() {
		if(IS_POST) {
			$member = new MemberApi();
			$user = D('User')->getLoginInfo();
			if(is_array($user)) {
				$res = $member->save_alipay($user['user_id'],$_POST);
				$res['alipay'] = str_replace(substr($_POST['alipay'],3,-3),'*****',$_POST['alipay']);
				$res['alipay_real_name'] = mb_substr($_POST['alipay_real_name'],0,1,'utf-8').'**';
				$this->ajaxReturn($res,'json');				
			} else {
				$this->ajaxReturn(['status'=>0,'msg'=>'请登录'],'json');
			}
		} else {
			$this->ajaxReturn(['status'=>0,'msg'=>'参数错误'],'json');
		}
	}

	/**
	 * 判断商品是否被下单
	 * lcj
	 */
	
	public function trade_is_exist(){
		if(IS_POST){
			$member = new MemberApi();
			$user = D('User')->getLoginInfo();
			if(is_array($user)) {
				$map['merchandise_id'] = $_POST['good_id'];
				$map['buyer_id'] = $user['user_id'];
				$map['pay_status'] = 0;
				$order_data = M('order','tab_')->where($map)->find();
				if($order_data){
					$time = 1800-(time()-$order_data['order_time']);
					if($time<0){
                        M('order','tab_')->where($map)->delete();
                        $url = U('Trade/detail',array('id'=>$_POST['good_id']));
					}else{
	                    $url = U("Trade/buy_good",array("id"=>$_POST["good_id"],'time'=>$time));
					}
				}else{
	                $url = U('Trade/detail',array('id'=>$_POST['good_id']));
				}
				$this->ajaxReturn(array('status'=>1,'url'=>$url));
			}else{
				$this->ajaxReturn(array('status'=>0,'msg'=>'请先登录'));
			}
		}
	}
	
}