<?php
class ProjectAction extends GlobalAction{
	
	function _initialize(){		
		
		$ProjectClassid = D('ProjectClassid');
		$Result = $ProjectClassid->findAll();
		$this->assign('Result',$Result);
	}
    

    /**
	 * 已加入项目管理
	 *
	 */
    public function manager(){
 
		$N = M("ProjectBuy");
		$C = M("Project");
		$keyword = $_POST['keyword'];
		if($_POST['type'] == 1){
			$condition['username'] = array('eq',$keyword);	
    	}else if($_POST['type'] == 2){
    		$condition['ordernumber'] = array('like','%'.$keyword.'%');
    	}else{
    		
    	}

		$count = $N->field('*,c.id')->table($N->getTableName().' c') ->join($C->getTableName().' n on n.id=c.projectId') ->where($condition)->count();
		import("@.ORG.Page"); //导入分页类
		$p = new page($count,15);
		$list = $N->field('*,c.id')->table($N->getTableName().' c') ->join($C->getTableName().' n on n.id=c.projectId') ->where($condition)->limit($p->firstRow.','.$p->listRows)->order('c.id desc')->findAll();
		$page = $p->show();
		$this->assign("page",$page);
		$this->assign('list',$list);
		$this->display();
    }

    /**
	 * 栏目列表
	 *
	 */
    public function index(){

		$Project = D("Project");
		import("@.ORG.Page"); //导入分页类
		$count = $Project->count();//计算总数
		$p = new page($count,15);
		$list = $Project->limit($p->firstRow.','.$p->listRows)->order('classid')->findAll();
		$page = $p->show();
		$this->assign("page",$page);
		$this->assign('list',$list);
		$this->display();
    }
	
    //发佣金
    public function getDo(){

    	$user  = trim($_GET['user']);
    	$id    = trim($_GET['id']);
    	$id2   = trim($_GET['id2']);
    	$qxdw  = trim($_GET['qxdw']);
    	$money = trim($_GET['money']);
    	$cishu = trim($_GET['cishu']);
    	$shijian = (int)$_GET['zhouqi'];
    	
    	$Member  = D('Member');
    	$Result  = $Member->where("username = '{$user}'")->find();
    	$huserid = (int)$Result['id'];
    	$hmoney  = floatval($Result['amount']);
    	
    	$ProjectBuy  = D('ProjectBuy');
    	$contion['id'] 		  = $id2;
    	$contion['username']  = $user;
    	$contion['projectId'] = $id;
    	$Rs = $ProjectBuy->where($contion)->find();
    	$probuyId       = $Rs['id'];
    	$useritem_time  = $Rs['useritem_time'];
		$useritem_time2 = $Rs['useritem_time2'];
		$useritem_time4 = date('Y-m-d H:i:s',time());
		$nowcishu	    = $Rs['useritem_count'];
		if($nowcishu >= $shijian){
			echo"<script>alert('申请失败,已经超出能领取的次数！');history.go(-1);</script>";  
			die();
		}

		if($useritem_time4 < $useritem_time2){
			echo"<script>alert('申请失败,还没有到可领取的时间！');history.go(-1);</script>";  
			die();
		}

		
		$Project  = D('Project');
		$resultPro = $Project->where("id = {$id}")->find();

		//金额记录日志
		$ip = getip();
		$projectName =  $resultPro['title'];
		$notice = "项目分红-".$projectName."(+)";
		meoneyLog($user,$money,$notice,'+');

		//加款
		$Member  = D('Member');
    	$Member->setInc('amount',"id = {$huserid}",$money); // 用户的积分加 3

		if($qxdw == '个交易日'){
	    	$zq = weekname(date('w',$useritem_time2));
	    	switch ($zq) {
	    		case '星期一':
					$useritem_time2 = DateAdd("d",1, $useritem_time2);
					break;
				case '星期二':
					$useritem_time2 = DateAdd("d",1, $useritem_time2);
					break;
				case '星期三':
					$useritem_time2 = DateAdd("d",1, $useritem_time2);
					break;
				case '星期四':
					$useritem_time2 = DateAdd("d",1, $useritem_time2);
					break;
				case '星期五':
					$useritem_time2 = DateAdd("d",3, $useritem_time2);
					break;
				case '星期六':
					$useritem_time2 = DateAdd("d",2, $useritem_time2);
					break;
	    		case '星期日':
					$useritem_time2 = DateAdd("d",1, $useritem_time2);
					break;
	    		default:break;
	    	}
		}else{
			$useritem_time2 = DateAdd("d",1, $useritem_time2);
		}
		$ProjectBuy = D('ProjectBuy');
		$data['useritem_time1'] = $useritem_time2;
		$data['useritem_time2'] = $useritem_time2;
		$data['useritem_count'] = $nowcishu + 1;
		$resultslg = $ProjectBuy->where("id = {$probuyId}")->save($data);
		if($resultslg){
			//次数达到返回本金
			if($resultPro['shijian'] == $nowcishu + 1){
				//标记项目结束状态
				$ProjectBuy = D('ProjectBuy');
				$dates['status'] = 0; //结束
				$ProjectBuy->where("id = {$probuyId}")->save($dates);

				//返回金额,benjin
				$ip = getip();
				$projectName =  $resultPro['title'];
				$notice = "项目本金返款-".$projectName."(+)";
				meoneyLog($user,$Rs['amount'],$notice,'+');
				$Member  = D('Member');
				$rsamount = $Rs['amount'];
  				$Member->setInc('amount',"id = {$huserid}",$rsamount); 
			}
			echo"<script>window.location.href=\"/admin888/index.php/Project/manager\";</script>";  
		}else{
			echo"<script>alert('申请失败,请重新操作！');history.go(-1);</script>";  
			die();
		}
    }


     //一键功能
    public function getDo2(){

    	$N = M("ProjectBuy");
		$C = M("Project");
		$rs10000 = $N->field('*,c.id as buyid')->table($N->getTableName().' c') ->join($C->getTableName().' n on n.id=c.projectId') ->where('c.status = 1')->findAll();
		$i = 0 ;
		$j = 0 ;
		foreach ($rs10000 as $key => $value) {
			
			$nowMoney = 0;
			$money 	  = 0;

			$username = $value['username'];		//投注用户
			$id   = $value['projectId'];		//项目ID
			$id2  = $value['buyid'];			//投注表ID。
			if($value['hkfs'] == 0){
                $money = floatval($value['jyrsy'] * $value['amount']/100);
            }else{
                $money = floatval($value['jyrsy'] * $value['amount']/100 * $value['shijian']);
            }

			 
			$shijian = (int)$value['shijian'];
			$qxdw   = $value['qxdw'];
			$user_id= $username;
			$i++;

			$Member  = D('Member');
			$rs100 	 = $Member->where("username= '{$user_id}'")->find();
			$huserid = $rs100['id'];
			$hmoney  = $rs100['amount'];

			//获取投列表
			$ProjectBuy  = D('ProjectBuy');
			$rs  = $ProjectBuy->where("username= '{$user_id}' and id = '{$id2}' and projectId= '{$id}'")->find();

			$proId  = $rs['id'];	//更新
			$useritem_time  = $rs['useritem_time'];
			$useritem_time2 = $rs['useritem_time2'];
			$useritem_time4 = date('Y-m-d H:i:s',time());
			$nowcishu       = (int)$rs['useritem_count'];
			if($nowcishu >= $shijian){
				$j++;
			}else if($useritem_time4<$useritem_time2){
				$j++;
			}else{
			$data['useritem_time1'] = $useritem_time2;
			if($qxdw == '个交易日'){
	    	$zq = weekname(date('w',$useritem_time2));
	    	switch ($zq) {
	    		case '星期一':
					$data['useritem_time2'] = DateAdd("d",1, $useritem_time2);
					break;
				case '星期二':
					$data['useritem_time2'] = DateAdd("d",1, $useritem_time2);
					break;
				case '星期三':
					$data['useritem_time2'] = DateAdd("d",1, $useritem_time2);
					break;
				case '星期四':
					$data['useritem_time2'] = DateAdd("d",1, $useritem_time2);
					break;
				case '星期五':
					$data['useritem_time2'] = DateAdd("d",3, $useritem_time2);
					break;
				case '星期六':
					$data['useritem_time2'] = DateAdd("d",2, $useritem_time2);
					break;
	    		case '星期日':
					$data['useritem_time2'] = DateAdd("d",1, $useritem_time2);
					break;
	    		default:break;
		    	}
			}else{
				$data['useritem_time2'] = DateAdd("d",1, $useritem_time2);
			}	
			$data['useritem_count'] = $nowcishu + 1;
			$UpdateTi = $ProjectBuy->where("id = {$proId}")->save($data);

			//更新金额日志
			$Project  = D('Project');
			$resultPro = $Project->where("id = {$id}")->find();

			//金额记录日志
			$ip = getip();
			$projectName =  $resultPro['title'];
			$notice = "项目分红-".$projectName."(+)";
			meoneyLog($user_id,$money,$notice,'+');

	    	//次数达到返回本金
			if((int)$rs['sendDay_count'] == $nowcishu + 1){
				
				//标记项目结束状态
				$ProjectBuy = D('ProjectBuy');
				$dates['status'] = 0; //结束
				$ProjectBuy->where("id = {$proId}")->save($dates);
				//返回金额,benjin
				$ip = getip();
				$projectName =  $resultPro['title'];
				$notice = "项目本金返款-".$projectName."(+)";
				meoneyLog($user_id,$rs['amount'],$notice,'+');
				$nowMoney = round($rs['amount']?$rs['amount']:0,2);

			}
				$nowMoneys = round($nowMoney + $money,2);
				//加款
				$Member  = D('Member');
		    	$resultslg = $Member->setInc('amount',"id = {$huserid}",$nowMoneys); // 用户的积分
		    	$sqlCondti = $Member->getlastsql();
				//记录金钱SQL
				insertMoneySql($user_id,$money,$nowMoney,$nowMoneys,$sqlCondti);
			}
		}
			$peo = $i-$j;
			$msg = "反佣成功。返佣".$i."人，成功".$peo."人，".$j."人时间未到！";
			echo"<script>alert('{$msg}');window.location.href=\"/admin888/index.php/Project/manager\";</script>";  
	}

    /**
     * 添加信息
	 *
     */
    public function add()
	{
		//表单提交
		if($this->isPost()){
			$Project = D('Project');
			if($Project->create()){
  				//上传图片
				if(!empty($_FILES['pic']['name'])){			
					$this->_upload();
					$Project->pic = $_POST['savename'];
				}
				// if($_POST['hkfs'] == 0){
				// 	$Project->cishu = $_POST['shijian'];
				// }else{
				// 	$Project->cishu = 1;
				// }
				$Project->posttime = time();
				if(false !== $Project->add()){
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}
			}else{
				$this->error($Project->getError());
			}
		}else{
			//添加信息
			$this->display();
		}
    }
	
    /**
     * 修改
	 *
     */
    public function edit(){
		//表单提交
		if($this->isPost()){
			$Project = D('Project');
			if($Project->create()){
				//上传图片
				if(!empty($_FILES['pic']['name'])){			
					$this->_upload();
					$Project->pic = $_POST['savename'];
				}
				if(false !== $Project->save()){
					$this->assign('jumpUrl',$_POST['returnurl']);
					$this->success('更新成功');					
				}
				else{
					$this->error('没有任何更新');
				}
			}
			else{
				$this->error($Project->getError());
			}
		}
		else{
			//修改栏目
			if(!empty($_GET['id'])){
				$Project = D('Project');
				$data= $Project->getById($_GET['id']);
				if($data){
					$this->assign('returnurl',$_SERVER['HTTP_REFERER']);
					$this->assign('data',$data);
					$this->display();
				}
				else{
					exit('编辑项不存在');
				}		
			}
			else{
				exit('编辑项不存在');
			}
		}
    }

     /**
     * 删除
	 *
     */
	 public function buydelete(){
		
		if(!empty($_GET['id'])){
			$ProjectBuy = D('ProjectBuy');
			if($ProjectBuy->delete($_GET['id'])) {
				$this->success('删除成功!');
			}
			else{
				$this->error('删除出错!');
			}
		}
		else{
			$this->error('删除项不存在');
		}
	}

    /**
     * 删除
	 *
     */
	 public function delete(){
		
		if(!empty($_GET['id'])){
			$Project = D('Project');
			if($Project->delete($_GET['id'])) {
				$this->success('删除成功!');
			}
			else{
				$this->error('删除出错!');
			}
		}
		else{
			$this->error('删除项不存在');
		}
	}

	 /**
     * 文件上传
     *
     */
    protected function _upload(){
        import('@.ORG.UploadFile');
        $upload = new UploadFile();        
        $upload->maxSize = 3292200 ; //设置上传文件大小        
        $upload->allowExts = explode(',','jpg,gif,png,jpeg,pdf,rar,doc,xls'); //设置上传文件类型      
        $upload->savePath = '../public/Project/'; //设置附件上传目录	    
        $upload->thumb = true; //设置需要生成缩略图，仅对图像文件有效
       	$upload->thumbPrefix = 'thumb_';  //设置需要生成缩略图的文件后缀,生产2张缩略图
       	$upload->thumbMaxWidth = '127'; //设置缩略图最大宽度       
		$upload->thumbMaxHeight = '190'; //设置缩略图最大高度	   
	    $upload->saveRule = uniqid; //设置上传文件规则	   
	    $upload->thumbRemoveOrigin = false; //删除原图
        if(!$upload->upload()){            
            $this->error($upload->getErrorMsg()); //捕获上传异常
        }
		else{            
            $uploadList = $upload->getUploadFileInfo(); //取得成功上传的文件信息
			$_POST['savename']  = $uploadList[0]['savename'];
        }
	}
}
?>