<?php

namespace app\index\controller;
use app\common\controller\Frontend;
use think\Config;
use think\Hook;
use think\Db;
use think\Session;
use think\Request;

class Activitiesenroll extends Frontend
{

    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
    protected $noNeedRight = '*';

   public function _initialize()
    {
        parent::_initialize();
    }

    public function windsnsactivitiesenrolldo()
    {
        $getactivitiesypeid  =  input('get.activitiestype');//获取活动类型
    	$getactivitiesid  =  input('get.activitiesid'); //获取活动ID
    	$userid = $this->auth->getUserinfo()['user_id'];;//获取这个会员的ID
    	//获取当前时间
    	$time = time();
		$paytimess = date('Y-m-d H:i:s',$time);
		$addtimes = strtotime($paytimess);
    	//获取当下活动内容信息是否在SQL存在
    	$sqtypeid   = Db::name('windsnsactivity_types')->where(['activitytypeid'=>$getactivitiesypeid])->find();//查询该栏目类型ID是否存在
    	$sqactivitiesid = Db::name('windsnsactivity')->where(['windsnsactivityid'=>$getactivitiesid])->find();//查询该文章ID是否存在
    	$sqinfosid = Db::name('windsnsactivity')->where(['windsnsactivityid'=>$getactivitiesid,'windsnsactivitytypenumber'=>$getactivitiesypeid])->find();//查询该文章对应的该栏目信息是否存在
    	$worldnames =  $sqinfosid['windsnsactivitylittle'];
    	$this->view->assign('title', __($worldnames));
    	//获取用户资金
    	$user_money = Db::name('user')->where('id', $userid)->find(); //会员余额
        $usermoneys =  $user_money['money'];	 //SQL账户总金额
        //获取这个活动的信息，存入报名表里
        $addpay['user_id'] = $userid;//获取该用户的用户ID
        $addpay['windsns_hdid'] = $getactivitiesid;//获取该活动的ID
        $addpay['windsns_hdlittle'] = $sqactivitiesid['windsnsactivitylittle']; // 获取这个活动的名称
        $addpay['windsns_hdfenlei'] = $sqactivitiesid['windsnsactivitytypenumber']; // 获取这个活动的分类
        $addpay['windsns_hdpaytype'] = $sqactivitiesid['windsnsactivitypaytype']; // 获取这个活动是否收费1免费2收费
        $hdtype  = $sqactivitiesid['windsnsactivitypaytype'];
        $hdjiage = $sqactivitiesid['windsnsactivityjiage'];
        
        $addpay['windsns_hdzhuangtai'] = $sqactivitiesid['windsnsactivityopentype']; // 获取这个活动的进行状态
        $hdzhuangtai  = $sqactivitiesid['windsnsactivityopentype'];
        
        $addpay['windsns_hdbaomingzhuangtai'] = 1; // 获取这个活动的报名状态：1、成功、2退回、3失败
        $addpay['windsns_hdaddtimes'] = $addtimes; // 获取这个活动的报名时间
        
        $thiscanyusum = $sqactivitiesid['windsnsactivitycanyusum'];; // 获取这个活动已经报名的人数
        $thissqlcanyusum = $sqactivitiesid['windsnsactivitymaxuser'];; // 获取这个活动设置的最大报名人数

        $sqlcanyuusersum = Db::name('windsnsactivity_paylist')->where(['windsns_hdid'=>$getactivitiesid,'user_id'=>$userid,'windsns_hdbaomingzhuangtai'=>1]) -> count();//查询这个用户报名的次数
 
  
    	//安全判断开始
    	if(!$sqtypeid){
			return $this->view->fetch('index@activities/windsns_activitiesinfoserrorsa');
			}elseif(!$sqactivitiesid){
			return $this->view->fetch('index@activities/windsns_activitiesinfoserrorsb');
			}elseif(!$sqinfosid){
			return $this->view->fetch('index@activities/windsns_activitiesinfoserrorsc');
			}else{
				
				//该活动状态的判断
				if($hdzhuangtai == 1){
					//参与人数是否已经超过设置的报名人数
					if($thiscanyusum == $thissqlcanyusum){
						return $this->view->fetch('index@activities/windsns_activitiesinfoserrorsg');//人数满了
					}else{
							//判断这个用户是否在报名表里面存在
							  if( $sqlcanyuusersum > 0 or $sqlcanyuusersum == 1 ){
							  	
							  	return $this->view->fetch('index@activities/windsns_activitiesinfoserrorsh');//已经报名过了
							  }else{
	
										//该活动是否收费判断
										if($hdtype == 1){
											    $addpay['windsns_hdjiage'] = 0; // 获取这个活动的价格
											    $sqlthishdpaysum = Db::name('windsnsactivity')->where('windsnsactivityid',$getactivitiesid)->setInc('windsnsactivitycanyusum', 1);//参加次数+1
					                            $xiaofei = Db::name('user')->where('id', $userid)->setDec('money', $hdjiage);//SQL余额自减
					                            $sql = Db::name('windsnsactivity_paylist')-> insert($addpay);//   保存订单
                                                return $this->view->fetch('index@activities/windsns_activitiespayok');//购买成功！
										}else{
											
											//用户余额判断
											if($usermoneys > $hdjiage or $usermoneys == $hdjiage){
					                            $addpay['windsns_hdjiage'] = $sqactivitiesid['windsnsactivityjiage']; // 获取这个活动的价格
					                            $sqlthishdpaysum = Db::name('windsnsactivity')->where('windsnsactivityid',$getactivitiesid)->setInc('windsnsactivitycanyusum', 1);//参加次数+1
					                            $xiaofei = Db::name('user')->where('id', $userid)->setDec('money', $hdjiage);//SQL余额自减
					                            $sql = Db::name('windsnsactivity_paylist')-> insert($addpay);//   保存订单
                                                return $this->view->fetch('index@activities/windsns_activitiespayok');//购买成功！
												
											}else{
												return $this->view->fetch('index@activities/windsns_activitiesinfoserrorsf');//余额不足
												
											}
											//用户余额判断END
										}
										//该活动是否收费判断END
							  }
							//判断这个用户是否在报名表里面存在END
					}
					//参与人数是否已经超过设置的报名人数END
				}elseif($hdzhuangtai == 2){
					return $this->view->fetch('index@activities/windsns_activitiesinfoserrorsd');//已经开始
				}elseif($hdzhuangtai == 3){
					return $this->view->fetch('index@activities/windsns_activitiesinfoserrorse');//已经结束
				}else{
					return $this->view->fetch('index@activities/windsns_activitiesinfoserrors');//存在非法操作
				}
                //该活动状态的判断END
			}
          //安全判断END

    }


}
