<?php
class MemberAction extends GlobalAction{
    function _initialize(){
		parent::_initialize();
		
	}
	
	/**
	 * 信息列表
	 *
	 */
	public function addmoney(){

		$id = (int)$_GET['id'];
		$username = $_GET['username'];
		
		if($this->isPost()){
			
			$money  = $_POST['money'];
			$Member = D('Member');
    		$Result = $Member->setInc('amount',"id = {$id}",$money);
			
			//记录日志
			$MemberPayLog = D('MemberPayLog');	
			$date['username'] = $username;
			$date['amount']   = $money;
			$date['posttime'] = time();
			$date['ip']       = getip();
			$MemberPayLog->add($date);
    		if($Result){
				
				
				
				
    			$this->success('充值成功');
    		}else{
    			$this->error('充值失败');
    		}
		}
		$this->display();
	}
	//站内信
	function sendzhannei(){
		if($this->isPost()){
			$data['title'] = $_POST['title'];
			$data['content'] = $_POST['content'];
			$data['username'] = $_POST['username'];
			$data['posttime'] = time();
			$data['status'] = 0;
			$data['from_name'] = '系统发送';
			$id=M('member_msg')->add($data);
			if($id){
				$this->success('发送成功!');			
			}
		}
		$this->member = M('member')->where(array('id'=>$_GET['id']))->find();
		$this->display();
		
	}
	
	//删除用户
	function user_delete(){
		M('member')->where(array('id'=>$_GET['id']))->delete();
		$this->success('删除成功!');
	}

    /**
	 * 信息列表
	 *
	 */
    public function index()
	{
		
		$Member = D('Member');
		import('@.ORG.Page'); //导入分页类
		$keyword = trim($_GET['qu']);
		$userstatus = $_GET['userstatus'];
		
		if($keyword){
			$condition = array();
			$condition['username'] = array("like", "%".$keyword."%");
		}
		
		$count = $Member->where($condition)->count();//计算总数
		$p = new page($count,10);
		$list = $Member->where($condition)->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
		$page = $p->show();
		$this->assign('page',$page);
		$this->assign('list',$list);
		$this->display();
    }
	
   
    /**
     * 修改
	 *
     */
    public function edit(){
		
		//表单提交
		if($this->isPost()){

			$_POST['pwd']?$data['pwd'] = $_POST['pwd']:'';
			$_POST['paypwd']?$data['paypwd'] = $_POST['paypwd']:'';
			$_POST['mobile']?$data['mobile'] = $_POST['mobile']:'';
			//$_POST['email']?$data['email'] = $_POST['email']:'';
			$_POST['inviter']?$data['inviter'] = $_POST['inviter']:'';
			$_POST['realname']?$data['realname'] = $_POST['realname']:'';
			$_POST['level']?$data['level'] = $_POST['level']:'';
			$_POST['qq']?$data['qq'] = $_POST['qq']:'';
			$_POST['question']?$data['question'] = $_POST['question']:'';
			$_POST['answer']?$data['answer'] = $_POST['answer']:'';
			$_POST['bankname']?$data['bankname'] = $_POST['bankname']:'';
			$_POST['bankrealname']?$data['bankrealname'] = $_POST['bankrealname']:'';
			$_POST['bankcode']?$data['bankcode'] = $_POST['bankcode']:'';
			$_POST['bankaddress']?$data['bankaddress'] = $_POST['bankaddress']:'';

			$id  = (int)$_POST['id'];
			$Member = D('Member');
			$result = $Member->where("id = {$id}")->save($data);
			if($result){
				$this->assign('jumpUrl',$_POST['returnurl']);
				$this->success('更新成功');					
			}else{
				$this->error('没有任何更新');
			} 
		}
		else{
			//修改信息
			//显示等级
			$memberLevel = D('memberLevel');
			$Level = $memberLevel->findAll();
			$this->assign('level',$Level);
			if(!empty($_GET['id'])){
				$Member = D('Member');
				$data= $Member->getById($_GET['id']);
				if($data){ 
					$this->assign('returnurl',$_SERVER['HTTP_REFERER']);
					$this->assign('data',$data);
					$this->assign('bank',$resultBank);
					$this->display();
				}
				else{
					exit('编辑项不存在');
				}		
			}
			else{
				exit('编辑项不存在');
			}
		}
    }
	
	//增加余额
	function add_money(){
		$id = $_GET['id']?$_GET['id']:$_POST['uid'];
		$amount = M('member')->where(array('id'=>$id))->getField('amount');
		if($this->isPost()){
			if($_POST['type']==1){
				//支付
				$data['ordernumber'] = ordernumber();				
				$data['username'] = $_POST['username'];
				$data['amount'] = intval($_POST['money']);			 
				$data['memo']  = '后台充值';		
				$data['type']  = '后台充值';	
				$data['status'] = 1;
				$data['posttime'] = time();
				$data['paytime'] = time();
				$data['ip'] = getip();

			$res = M('member_order')->add($data);
			if($res){
				$s_amount = $amount + $data['amount'];
				M('member')->where(array('id'=>$_POST['uid']))->save(array('amount'=>$s_amount));
				
				//查询上家
				$Member   = D('Member');
				$rs_sj = $Member->field('inviter')->where("username = '{$data[username]}'")->find();
				$Topusername  = $rs_sj['inviter'];	//上线
				
				if(!empty($Topusername)){

					// 	//查询奖励比例
					$MemberConfig   = D('MemberConfig');
					
					$rssc = $MemberConfig->field('XiaXianFenCheng')->where('id = 1 ')->find();
					
					$XiaXianFenCheng = $rssc['XiaXianFenCheng'];					//下线分成比例
						
					$XiaXianAmount   = $rssc['XiaXianFenCheng'] * floatval($data['amount']);	//抽成金额
					
					$XiaXianAmount   = intval($XiaXianAmount); 
					$MemberCashback = D('MemberCashback');
					$data['username']  =  $Topusername;
					$data['xxusername']=  $data['username'];
					$data['amount']    =  floatval($data['amount']);
					$data['preamount'] =  $XiaXianAmount;
					$data['type'] 	   =  '下线分成';
					$data['status']    =  1;
					$data['posttime']  =  time();
					$sqlFc = $MemberCashback->add($data);

				// 	//发送msg
					$titleN    = "尊敬的".$Topusername."会员您好！充值成功！";
					$contentN  = "您的下线".$username."充值分成".$XiaXianAmount."元,已赠送到您的账号。";
					$memoN    = "后台充值";
					sendMsg($Topusername,$compnayN='',$titleN,$contentN,$memoN);

					$Member   = D('Member');
					$totalsN   = floatval($XiaXianAmount); 
					
					$addScoreN = $Member->setInc('amount',"username = '{$Topusername}' ",$totalsN);

				//记录充值记录
					$MemberOrder   = D('MemberOrder');
					$dateOrN['ordernumber'] = ordernumber();
					$dateOrN['status']      = 1;
					$dateOrN['paytime']     = time();
					$dateOrN['accNo']       = ordernumber();
					$dateOrN['username']    = $Topusername;
					$dateOrN['amount']      = $totalsN;
					$dateOrN['memo']        = '下线分成 | '.$Topusername.' | '.$totalsN;
					$dateOrN['type']        = '下线分成';
					$dateOrN['posttime']    = time();
					$dateOrN['paytime']     = time();
					$dateOrN['ip']     	    = getip();
					$dateOrN['bank']        = 'system';	
					$MemberOrder->add($dateOrN);
				}
				
				
				
				
				$this->success('操作成功');
			}
				}elseif($_POST['type']==2){
				$s_amount = $amount - intval($_POST['money']);
				M('member')->where(array('id'=>$_POST['uid']))->save(array('amount'=>$s_amount));
				$this->success('操作成功');
			}
		}
		$Member = D('Member');
		$id = $_GET['id'];
		$username = $Member->where(array('id'=>$id))->getField('username');
		$this->assign('username',$username);
		$this->assign('amount',$amount);
		$this->display();
	}
	
	function add_money_do(){
		
	}

     /**
     * 删除
	 *
     */
	 public function del(){
		
		if(!empty($_GET['id'])){
			$id = $_GET['id'];
			$Member = D('Member');
    		$data['state'] = 0;
    		$data = $Member->where("id = {$id}")->save($data);
    		if($data){					
					$this->success('锁定成功');
			}else{
				$this->error('锁定失败');
			}
		}
		else{
			$this->error('删除项不存在');
		}
	 }

	  /**
     * 解锁
	 *
     */
	 public function open(){
		
		if(!empty($_GET['id'])){
			$id = $_GET['id'];
			$Member = D('Member');
    		$data['state'] = 1;
    		$data = $Member->where("id = {$id}")->save($data);
			M("member")->getLastSql();
    		if($data){					
					$this->success('解锁成功');
			}else{
				$this->error('解锁失败');
			}
		}
		else{
			$this->error('删除项不存在');
		}
	 }
	
	//冻结
	function dongjie(){
		$id = $_GET['id'];
		$Member = D('Member');
		$data['is_dongjie'] = $_GET['is_dongjie'];
		$data = $Member->where("id = {$id}")->save($data);
		//echo  M("member")->getLastSql();
		
		$this->success('修改成功');
	}
   

    //用户留言管理
    public function message(){

    	$Mails = D('Mails');
    	import('@.ORG.Page'); //导入分页类  
    	$count = $Mails->count();//计算总数
		$p = new page($count,15);      
    	$list = $Mails->limit($p->firstRow.','.$p->listRows)->findAll();
    	$this->assign('page',$page);
    	$this->assign('list',$list);
    	$this->display();
    }

    //留言回复
    public function messagereply(){

    	$id = $_GET['id'];
    	$Mails = D('Mails');    
    	$data = $Mails->where("id = {$id}")->find();
    	$this->assign('data',$data);
    	if($_POST){
    		$id = $_POST['id'];
    		$data['replyContent'] = trim($_POST['replymsg']);
    		$data['state'] = 1;
    		$result = $Mails->where("id = {$id}")->save($data);
    		if($result){
				$this->success('回复成功');
			}else{
				$this->error('回复失败');
			}
    	}

    	$this->display();
    }

    public function messagedelete(){

    	if($_GET){
    		$id = $_GET['id'];
    		$Mails = D('Mails');    
    		$result = $Mails->where("id = {$id}")->delete();
    		if($result){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
    	}
    }

    //提现
    public function deal(){

    	if($_GET){
    		$id = $_GET['id'];
    		$Mails = D('Mails');    
    		$result = $Mails->where("id = {$id}")->delete();
    		if($result){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
    	}
    	$keyword = trim($_POST['qu']);
		if($keyword){
			$condition = array();
			$condition['username'] = array("like", "%".$keyword."%");
		}

    	$MemberDeal = D('MemberDeal');   
    	import('@.ORG.Page'); //导入分页类
    	$condition['status'] = array('eq',$_POST['status']?$_POST['status']:0);	//未处理
    	$count = $MemberDeal->where($condition)->count();//计算总数
		$p = new page($count,15);         
    	$list = $MemberDeal->where($condition)->order('id desc')->findAll();
    	$page = $p->show();
		$this->assign('page',$page);
		$this->assign('list',$list);
		$this->assign('status',$_POST['status']);
    	$this->display();
    }


     //提现记录
    public function deallog(){

    	$keyword = trim($_POST['qu']);
    	$status  = trim($_POST['status']);
		if($keyword){
			$condition = array();
			$condition['username'] = array("like", "%".$keyword."%");
		}

		if($status){
			$condition = array();
			$condition['status'] = array('eq',$status);
			if($status == 3){
				$condition['status'] = array('eq',03);
			}
		}


    	$MemberDeal = D('MemberDeal');   
    	import('@.ORG.Page'); //导入分页类 
    	$count = $MemberDeal->where($condition)->count();//计算总数
		$p = new page($count,15);         
    	$list = $MemberDeal->where($condition)->order('id desc')->findAll();
    	$page = $p->show();
		$this->assign('page',$page);
		$this->assign('list',$list);
		$this->assign('status',$_POST['status']);
    	$this->display();
    }

    //删除提现
    public function dealdel(){

		if($_GET){
    		$id = $_GET['id'];
    		$MemberDeal = D('MemberDeal');
    		$resultc = $MemberDeal->where("id = {$id}")->delete(); 
    		if($resultc){
    			$this->success('删除成功');
    		}else{
				$this->error('删除失败');
    		}
		}
    }


    //确认提现
    public function dealconf(){

		if($_GET){
    		$id = $_GET['id'];
    		$MemberDeal = D('MemberDeal');
			$data['status'] = 1; 	
    		$resultc = $MemberDeal->where("id = {$id}")->save($data); 
    		if($resultc){
    			$this->success('提现成功');
    		}else{
				$this->error('提现失败');
    		}
		}
    }

     //取消提现
    public function dealchannel(){

    	if($_GET){
    		$id = $_GET['id'];
    		$MemberDeal = D('MemberDeal');
			$Result = $MemberDeal->where("id = {$id}")->find();
			if(!$Result){
				$this->error('不存在');
			}

			$data['status'] = 2; 	//取消
    		$resultc = $MemberDeal->where("id = {$id}")->save($data); 

    		//返回金额
			$money  = $Result['amount'];
			$username   = $Result['username'];
			$Member = D('Member');
    		$Result = $Member->setInc('amount',"username = '{$username}'",$money);
    		if($resultc && $Result){
				$this->success('取消提现成功');
			}else{
				$this->error('取消提现失败');
			}
    	}
		//$this->display();
    }


    //充值
    public function pay(){

    	$keyword = trim($_POST['keyword']);
    	$MemberOrder = D('MemberOrder');   
    	import('@.ORG.Page'); //导入分页类
    	if($_POST['type'] == 1){
			$condition['username'] = array('eq',$keyword);	
    	}else if($_POST['type'] == 2){
    		$condition['ordernumber'] = array('like','%'.$ordernumber.'%');
    	}else{
    		
    	}
    	$count = $MemberOrder->where($condition)->count();//计算总数
		$p = new page($count,15);         
    	$list = $MemberOrder->where($condition)->limit($p->firstRow.','.$p->listRows)->order('id desc')->findAll();
    	$page = $p->show();
		$this->assign('page',$page);
		$this->assign('list',$list);
		$this->assign('status',$_POST['status']);
    	$this->display();
    }

    //公司入款账充值
   public function charge(){

   		$MemberCompanypay = D('MemberCompanypay');
   		$keyword = trim($_POST['qu']);
		if($keyword){
			$condition = array();
			$condition['username'] = array("like", "%".$keyword."%");
		}

   		import('@.ORG.Page'); //导入分页类   
		$count = $MemberCompanypay->where($condition)->count();//计算总数
		$p = new page($count,15);         
    	$list = $MemberCompanypay->where($condition)->order('id desc')->findAll();
    	$page = $p->show();
		$this->assign('page',$page);
		$this->assign('list',$list);
		$this->assign('status',$_POST['status']);
   		$this->display();
   }


   //确认充值 
   public function payconf(){

    	if($_GET){

    		$id = $_GET['id'];
    		$MemberCompanypay = D('MemberCompanypay');
			$Result = $MemberCompanypay->where("id = {$id}")->find();
			if(!$Result){
				$this->error('不存在');
			}

			//增加金额增加
			$user   = $Result['username'];
			$money  = $Result['amount'];
			$notice = '充值返款(+)';
			meoneyLog($user,$money,$notice,'+');

			//发送msg
			$title    = "尊敬的".$user."会员您好！充值成功！";
			$content  = "您的银行充值".$money."已成功,已送到您的账号。";
			$memo     = "网银公司充值";
			sendMsg($user,$compnayN='',$title,$content,$memo);


			//增加积分
			$Member   = D('Member');
			$totals   = floatval($Result['amount']); 
			$username = $Result['username'];
			$addScore = $Member->setInc('amount',"username = '{$username}' ",$totals);

			$data['status'] = 1;
			$upMemPay = $MemberCompanypay->where("id = {$id}")->save($data);

			//记录充值记录
			$MemberOrder   = D('MemberOrder');
			$dateOr['ordernumber'] = ordernumber();
			$dateOr['status']      = 1;
			$dateOr['paytime']     = time();
			$dateOr['accNo']       = ordernumber();
			$dateOr['username']    = $username;
			$dateOr['amount']      = $totals;
			$dateOr['memo']        = '公司入款 | '.$username.' | '.$totals;
			$dateOr['type']        = '在线充值';
			$dateOr['posttime']    = time();
			$dateOr['paytime']     = time();
			$dateOr['ip']     	   = getip();
			$dateOr['bank']        = 'system';	
			$upMemOrder = $MemberOrder->add($dateOr);


			//查询上家
			$rs_sj = $Member->field('inviter')->where("username = '{$username}'")->find();
			$Topusername  = $rs_sj['inviter'];	//上线
			if(!empty($Topusername)){

				// 	//查询奖励比例
				$MemberConfig   = D('MemberConfig');
				$rssc = $MemberConfig->field('XiaXianFenCheng')->where('id = 1 ')->find();
				$XiaXianFenCheng = $rssc['XiaXianFenCheng'];					//下线分成比例
				$XiaXianAmount   = $rssc['XiaXianFenCheng'] * floatval($money);	//抽成金额
				$XiaXianAmount   = intval($XiaXianAmount); 
				$MemberCashback = D('MemberCashback');
				$data['username']  =  $Topusername;
			 	$data['xxusername']=  $username;
				$data['amount']    =  floatval($money);
			 	$data['preamount'] =  $XiaXianAmount;
			 	$data['type'] 	   =  '下线分成';
			 	$data['status']    =  1;
				$data['posttime']  =  time();
			 	$sqlFc = $MemberCashback->add($data);

			// 	//发送msg
			 	$titleN    = "尊敬的".$Topusername."会员您好！充值成功！";
				$contentN  = "您的下线".$username."充值分成".$XiaXianAmount."元,已赠送到您的账号。";
			 	$memoN    = "网银公司充值";
				sendMsg($Topusername,$compnayN='',$titleN,$contentN,$memoN);

			 	$Member   = D('Member');
			 	$totalsN   = floatval($XiaXianAmount); 
			 	$addScoreN = $Member->setInc('amount',"username = '{$Topusername}' ",$totalsN);

			//记录充值记录
			 	$MemberOrder   = D('MemberOrder');
			 	$dateOrN['ordernumber'] = ordernumber();
			 	$dateOrN['status']      = 1;
			 	$dateOrN['paytime']     = time();
			 	$dateOrN['accNo']       = ordernumber();
				$dateOrN['username']    = $Topusername;
			 	$dateOrN['amount']      = $totalsN;
			 	$dateOrN['memo']        = '下线分成 | '.$Topusername.' | '.$totalsN;
			 	$dateOrN['type']        = '下线分成';
			 	$dateOrN['posttime']    = time();
			 	$dateOrN['paytime']     = time();
			 	$dateOrN['ip']     	    = getip();
			 	$dateOrN['bank']        = 'system';	
				$MemberOrder->add($dateOrN);
			}

			if($addScore && $upMemPay && $upMemOrder){
				$this->success('充值成功');
			}else{
				$this->error('充值失败');	
			}
    	}

    }



    // 删除充值记录
    public function orderdel(){

    	if($_GET){

    		$id = $_GET['id'];
    		$MemberOrder = D('MemberOrder');
			$Result = $MemberOrder->where("id = {$id}")->delete();
			if($Result){
				$this->success('删除记录成功');
			}else{
				$this->error('删除记录失败');	
			}
		}
    }


    // 删除记录
    public function paydel(){

    	if($_GET){

    		$id = $_GET['id'];
    		$MemberCompanypay = D('MemberCompanypay');
			$Result = $MemberCompanypay->where("id = {$id}")->delete();
			if($Result){
				$this->success('删除记录成功');
			}else{
				$this->error('删除记录失败');	
			}
		}
    }

   
    //VIP管理
    public function rank(){

    	
    	$MemberLevel = D('MemberLevel');
    	$list = $MemberLevel->order('id desc')->findAll();
    	$this->assign('list',$list);
    	$this->display();
    }

    //编辑VIP管理
    public function rankedit(){

    	if($_POST){
			$MemberLevel = D('MemberLevel');
			if($MemberLevel->create()){
				if(false !== $MemberLevel->save()){					
					$this->success('更新成功');
				}
				else{
					$this->error('更新失败');
				}
			}
			else{
				$this->error('更新失败');
			}
    	}else{
			$id =  $_GET['id'];
	    	$MemberLevel = D('MemberLevel');
	    	$data = $MemberLevel->where("id = {$id}")->findAll();
	    	$this->assign('data',$data[0]);
    	}
    	$this->display();
    }


    //删除vip
    public function rankdel(){

    	if($_GET){
    		$id =  $_GET['id'];
    		$MemberLevel = D('MemberLevel');
    		$data = $MemberLevel->where("id = {$id}")->delete();
    		if($data){					
					$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
    	}
    }

    //添加vip
    public function rankadd(){

    	if($_POST){
    		$MemberLevel = D('MemberLevel');
			if($MemberLevel->create()){
				if(false !== $MemberLevel->add()){					
					$this->success('添加成功');
				}
				else{
					$this->error('添加失败');
				}
			}
			else{
				$this->error('添加失败');
			}
    	}
    	$this->display();
    }

}
?>