<?php
// +----------------------------------------------------------------------
// | WindSNS 国内领先企业级社交开源SNS框架系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://windsns.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 常州微诺信息科技有限公司 http://www.weinuo.net
// +----------------------------------------------------------------------
// | E-mail: open@weinuo.net
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\common\controller\Backend;
use fast\Tree;

// +----------------------------------------------------------------------
// | WindSNS 国内领先企业级社交开源SNS框架系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://windsns.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 常州微诺信息科技有限公司 http://www.weinuo.net
// +----------------------------------------------------------------------
// | E-mail: open@weinuo.net
// +----------------------------------------------------------------------
class Windsnsactivity extends Backend
{

  //活动分类列表页面
    public function windsnsactivity_type()
    {
	    $windsns_windsnsactivityinfos = Db::name('windsnsactivity_types')->order('activitytypeid', 'desc')->paginate(20); //分类列表
			$page = $windsns_windsnsactivityinfos->render();
			$this->assign('windsns_windsnsactivityinfos', $windsns_windsnsactivityinfos);
			$this->assign('page', $page);
	    return $this->view->fetch('windsns_index');
    }

   
   //活动分类添加
    public function windsnsactivitytype_add()
    {
      $time = time();
		  $paytimess = date('Y-m-d H:i:s',$time);
	    $thistimes = strtotime($paytimess);//获取当前时间
	    $wsshoptypenames = $this->request->request('activitytypenames');
	    $windsns_shoptypeadd['activitytypenames'] =  input('post.activitytypenames'); // 店铺分类名称
	    $thisshopname = input('post.activitytypenames'); // 获取应用标题
	    $windsns_shoptypeadd['activitytypeopentype'] =  input('post.activitytypeopentype'); // 开启状态：1正常2关闭隐藏
	    $windsns_shoptypeadd['activitytypeaddtimes'] =  $thistimes;  // 获取添加时间	 
      $sqllittle  = Db::name('windsnsactivity_types')->where(['activitytypenames'=>$thisshopname])->find();//分类名称重复判断！
			if(!$wsshoptypenames){
	    	return $this->view->fetch('windsns_addtypenoa');//标题为空判断，保存失败！
	        }elseif($sqllittle){
		       return $this->view->fetch('windsns_activitytypeaddno');//标题存在重复，保存失败！
			}else{
			 $wedid = Db::name('windsnsactivity_types')->insert($windsns_shoptypeadd);//   保存SQL
			 return $this->view->fetch('windsns_activitytypeaddok');//保存成功！
	
			}
		
    }

     //分类状态切换 ----隐藏
    public function windsnsactivity_typenulldo()
    {
	    $urlactivitytypeid  =  input('get.activitytypeid'); //  获取地址栏栏目ID
	    $data['activitytypeopentype'] =  2; // 状态: 1、开启 2、隐藏
	    $xiugaizhuangtai = Db::name('windsnsactivity_types')->where('activitytypeid', $urlactivitytypeid)->update($data);// 
	    return  $this->fetch('widsns_activitytypedook');//操作OK！，更新隐藏状态2
    }

      //分类状态切换 ----开启
    public function windsnsactivity_typeokdo()
    {
    	
	    $urlactivitytypeid  =  input('get.activitytypeid'); //  获取地址栏栏目ID
	    $data['activitytypeopentype'] =  1; // 状态: 1、开启 2、隐藏
	    $xiugaizhuangtai = Db::name('windsnsactivity_types')->where('activitytypeid', $urlactivitytypeid)->update($data);// 
	    return  $this->fetch('widsns_activitytypedook');//操作OK！，更新隐藏状态2
    }  
   


    public function windsns_activityypedel($activitytypeid) //删除活动分类栏目
    {
        $urlfenleitypeid  =  input('get.activitytypeid'); //  获取地址栏栏目ID
        $typesum = Db::name('windsnsactivity_types')->count();//查询该栏目下存在的资讯数量 
        $typewoldsum = Db::name('windsnsactivity')->where(['windsnsactivitytypenumber' => $urlfenleitypeid])->count();//查询该栏目下存在的资讯数量 
        
        if($typesum > 2 or $typesum == 2){
        	  //如果分类数量大于2
          if($typewoldsum > 0 or $typewoldsum == 1){
			            return $this->view->fetch('windsns_activitytypedelnoa');//如果该栏目ID下存在文章数，删除失败！
			        }else{
			         	$shanchu = Db::name('windsnsactivity_types')->where('activitytypeid',$urlfenleitypeid)->delete();
			 	        if($shanchu){
				            return $this->view->fetch('widsns_activitytypedook');//删除成功！
				        }else{
				          	return $this->view->fetch('windsns_activitytypedelnob');//删除失败！
				        }
			
			        }
        	
        }else{
        	return $this->view->fetch('windsns_activitytypedelnoc');//操作失败！~必须保留一个分类信息
        }
        


   }
   
   
      //活动分类更新执行
    public function windsns_activitytypeup($activitytypeid)
    {
      $appstid =   Request::instance()->request('activitytypeid');//获取当前栏目的ID 
      $activitytypenames['activitytypenames'] =  input('post.activitytypenames'); // 获取资讯栏目名称
      $thisarticlenames = input('post.activitytypenames'); // 获取资讯栏目名称
      $sqlnames  = Db::name('windsnsactivity_types')->where(['activitytypenames'=>$thisarticlenames])->find();//重复判断！
      if(!$activitytypenames['activitytypenames']){
         	return $this->view->fetch('windsns_addtypenoa');//标题为空判断，保存失败！
      }elseif($sqlnames){
			return $this->view->fetch('windsns_activitytypeaddno');//标题存在重复，保存失败！
			}else{
	      	    $xiugaizhuangtai = Db::name('windsnsactivity_types')->where('activitytypeid', $appstid)->update($activitytypenames);// 
	        return  $this->fetch('widsns_activitytypedook');//操作OK！，更新隐藏状态2
	      }

    }



//添加活动、添加活动、添加活动、添加活动、添加活动、添加活动、添加活动、添加活动、添加活动、添加活动、添加活动、添加活动、添加活动、添加活动、

 //活动添加页面
    public function windsnsactivity_add()
    {
     $windsnsactivitytypeinfos = Db::name('windsnsactivity_types') -> where(['activitytypeopentype' => 1]) -> order('activitytypeid', 'desc')->select(); //活动分类打印
     $this->assign('windsnsactivitytypeinfos', $windsnsactivitytypeinfos);
     return $this->view->fetch('windsns_activityaddindex');
    }


       public function windsnsactivityadddo()//活动添加执行
    {
         //========================当前时间======================== //
	      $time = time();
			  $paytimess = date('Y-m-d H:i:s',$time);
		    $tadytimes = strtotime($paytimess);//获取当前时间
	      //========================当前时间======================== //
	    	$activitytyadd['windsnsactivitylittle']=  input('post.windsnsactivitylittle'); // 活动标题
	    	$activitytyadd['windsnsactivityshunxu']=  input('post.windsnsactivityshunxu'); // 活动顺序
	    	$activitytyadd['windsnsactivityadmin']=  input('post.windsnsactivityadmin'); // 团长
	    	$activitytyadd['windsnsactivityadmintel']=  input('post.windsnsactivityadmintel'); // 团长电话
	    	$file = request()->file('windsnsactivitypic'); // 获取活动封面图
	    	$activitytyadd['windsnsactivitytypenumber']=  input('post.windsnsactivitytypenumber'); //活动分类
	    	$activitytyadd['windsnsactivityaddress']=  input('post.windsnsactivityaddress'); // 活动地址
	    	$activitytyadd['windsnsactivitymaxuser']=  input('post.windsnsactivitymaxuser'); // 活动最大报名人数
	    	$activitytyadd['windsnsactivityendtime']=  input('post.windsnsactivityendtime'); // 活动结束时间
	    	$activitytyadd['windsnsactivityhot']=  input('post.windsnsactivityhot'); // 是否推荐
	    	$activitytyadd['windsnsactivityopentype']=  input('post.windsnsactivityopentype'); // 发布状态
	    	$activitytyadd['windsnsactivitypaytype']=  input('post.windsnsactivitypaytype'); // 是否收费：1免费 2收费'
	    	$activitytyadd['windsnsactivityjiage']=  input('post.windsnsactivityjiage'); // 售价
	    	$activitytyadd['windsnsactivityinfos']=  input('post.windsnsactivityinfos'); // 内容
	    	$activitytyadd['windsnsactivityaddtimes']=  $tadytimes; // 发布时间

			  if($file){
			            $info = $file->validate(['size'=>2000000000,'ext'=>'jpg,png'])->move(ROOT_PATH . 'public/static' . DS . 'uploads/activityimg');
			            if($info){
			            	$imgpath = $info->getSaveName();//文件路径
			              $imgnames = $info->getFilename();//文件名
			        	    $activitytyadd['windsnsactivitypic']=  $imgpath; // 获取视频图片路径
						        $activitytyadd['windsnsactivitypicurl']=  $imgnames; // 附件：图片名称
						    	$wedid = Db::name('windsnsactivity')->insert($activitytyadd);//   保存SQL
						    	return $this->view->fetch('windsns_activityadddook');
			    	        }else{
			            // 上传失败获取错误信息
			           return $this->view->fetch('windsns_activityadddono');//操作失败！判断文件类型，大小是否OK
			        }
            }else{
            	$wedid = Db::name('windsnsactivity')->insert($activitytyadd);//   保存SQL
            	return $this->view->fetch('windsns_activityadddook');
            }

    }

   
         //活动编辑页面
   
	    public function windsnsactivity_edit($windsnsactivityid)
	    {
		    $urlwindsnsactivityid  =  input('get.windsnsactivityid'); // 获取地址栏ID
	    	$windsnsactivityinfos = Db::name('windsnsactivity') -> where('windsnsactivityid',$urlwindsnsactivityid) ->
	    	alias('a')->join('ws_windsnsactivity_types w','a.windsnsactivitytypenumber = w.activitytypeid') 
	    	-> select();//查询这个资讯所有信息
	      $this->assign('windsnsactivityinfos', $windsnsactivityinfos);//资讯信息赋值到页面
		    $windsnsactivitytypeinfos = Db::name('windsnsactivity_types') -> where(['activitytypeopentype' => 1]) -> order('activitytypeid', 'desc')->select(); //活动分类打印
	      $this->assign('windsnsactivitytypeinfos', $windsnsactivitytypeinfos);
		    return $this->view->fetch('windsns_activityedit');
	    }
	    
   
    //活动编辑执行
    public function windsnsactiviteitdo($windsnsactivityid)
    {
    	$urlid  =  input('get.windsnsactivityid'); //  获取地址栏栏目ID

     
     
            //========================当前时间======================== //
			        $time = time();
					    $paytimess = date('Y-m-d H:i:s',$time);
				      $tadytimes = strtotime($paytimess);//获取当前时间
			        //========================当前时间======================== //
			    	
			        $activitytyadd['windsnsactivitylittle']=  input('post.windsnsactivitylittle'); // 活动标题
				    	$activitytyadd['windsnsactivityshunxu']=  input('post.windsnsactivityshunxu'); // 活动顺序
				    	$activitytyadd['windsnsactivityadmin']=  input('post.windsnsactivityadmin'); // 团长
				    	$activitytyadd['windsnsactivityadmintel']=  input('post.windsnsactivityadmintel'); // 团长电话
				    	$file = request()->file('windsnsactivitypic'); // 获取活动封面图
				    	$activitytyadd['windsnsactivitytypenumber']=  input('post.windsnsactivitytypenumber'); //活动分类
				    	$gethdfenleis =  input('post.windsnsactivitytypenumber');
				    	
				    	$activitytyadd['windsnsactivityaddress']=  input('post.windsnsactivityaddress'); // 活动地址
				    	$activitytyadd['windsnsactivitymaxuser']=  input('post.windsnsactivitymaxuser'); // 活动最大报名人数
				    	$activitytyadd['windsnsactivityendtime']=  input('post.windsnsactivityendtime'); // 活动结束时间
				    	$activitytyadd['windsnsactivityhot']=  input('post.windsnsactivityhot'); // 是否推荐
				    	$activitytyadd['windsnsactivityopentype']=  input('post.windsnsactivityopentype'); // 发布状态
				    	$gethdzhuangtais = input('post.windsnsactivityopentype'); 
				    	
				    	$activitytyadd['windsnsactivitypaytype']=  input('post.windsnsactivitypaytype'); // 是否收费：1免费 2收费'
				    	$activitytyadd['windsnsactivityjiage']=  input('post.windsnsactivityjiage'); // 售价
				    	$activitytyadd['windsnsactivityinfos']=  input('post.windsnsactivityinfos'); // 内容
				    	$activitytyadd['windsnsactivityaddtimes']=  $tadytimes; // 发布时间
 
 
  if($file){
			            $info = $file->validate(['size'=>2000000000,'ext'=>'jpg,png'])->move(ROOT_PATH . 'public/static' . DS . 'uploads/activityimg');

			            if($info){
			            	
			            	$imgpath = $info->getSaveName();//文件路径
			              $imgnames = $info->getFilename();//文件名
			            	$activitytyadd['windsnsactivitypic']=  $imgpath; // 获取视频图片路径
						        $activitytyadd['windsnsactivitypicurl']=  $imgnames; // 附件：图片名称
						        
						        
              //同步更新用户报名表下，所有该活动的状态
            	$upbaominghdzhuangtai['windsns_hdzhuangtai'] = $gethdzhuangtais;
            	$upbaominghdzhuangtai['windsns_hdfenlei'] =  $gethdfenleis;
            	$savebaomingsql = Db::name('windsnsactivity_paylist')-> where('windsns_hdid', $urlid) ->update($upbaominghdzhuangtai);//   保存报名的SQL
            	//同步更新用户报名表下，所有该活动的状态END
						        
						        
							    	$wedid = Db::name('windsnsactivity')-> where('windsnsactivityid', $urlid) -> update($activitytyadd);//   保存SQL
							    	return $this->view->fetch('windsns_activityadddook');
			    	        }else{
			            // 上传失败获取错误信息
			           return $this->view->fetch('windsns_activityadddono');//操作失败！判断文件类型，大小是否OK
			        }
    	
            }else{
            	
            	//同步更新用户报名表下，所有该活动的状态
            	$upbaominghdzhuangtai['windsns_hdzhuangtai'] = $gethdzhuangtais;
            	$upbaominghdzhuangtai['windsns_hdfenlei'] =  $gethdfenleis;
            	$savebaomingsql = Db::name('windsnsactivity_paylist')-> where('windsns_hdid', $urlid) ->update($upbaominghdzhuangtai);//   保存报名的SQL
            	//同步更新用户报名表下，所有该活动的状态END
            	
            	
            	$wedid = Db::name('windsnsactivity')-> where('windsnsactivityid', $urlid) ->update($activitytyadd);//   保存SQL
            	return $this->view->fetch('windsns_activityadddook');
            	
            }
     

    }   
   
   //活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、活动列表、

   
   //活动列表页面
       public function windsnsactivity_list()
    {
        $windsnsactivitylist = Db::name('windsnsactivity')->
        alias('a')->join('ws_windsnsactivity_types w','a.windsnsactivitytypenumber = w.activitytypeid') ->
        order('windsnsactivityshunxu', 'desc')->paginate(20); //列表
        $page = $windsnsactivitylist->render();
    	  $this->assign('windsnsactivitylist', $windsnsactivitylist);
        $this->assign('page', $page);
        return $this->view->fetch('windsns_activitylist');
    }

       public function windsnsactivitytypelist()//活动分类类型下的列表
    {
        $urlwindsnsactivitytypeid  =  input('get.windsnsactivitytypelistid'); //  获取地址视频分类ID
        $windsnsactivitylist = Db::name('windsnsactivity')->
        alias('a')->join('ws_windsnsactivity_types w','a.windsnsactivitytypenumber = w.activitytypeid') ->
        where(['windsnsactivitytypenumber'=>$urlwindsnsactivitytypeid])-> 
        order('windsnsactivityshunxu', 'desc')-> paginate(20,false,['query' => request()->param()]); //列表
        $page = $windsnsactivitylist->render();
    	  $this->assign('windsnsactivitylist', $windsnsactivitylist);
        $this->assign('page', $page);
        return $this->view->fetch('windsns_activitylist');
    }

             //活动状态切换 ----不推荐
    public function windsnsactivity_tuijianokdo()
    {
	    $urlhdtypeid  =  input('get.windsnsactivityid'); //  获取地址视频ID
	    $data['windsnsactivityhot'] =  2; // 推荐状态：1不推荐2推荐',
	    $xiugaizhuangtai = Db::name('windsnsactivity')->where('windsnsactivityid', $urlhdtypeid)->update($data);// 
	    return  $this->fetch('windsns_activitytuijiandook');//操作OK！，更新隐藏状态2
    }
    
    
      //活动状态切换 ----推荐
    public function windsnsactivity_tuijiannodo()
    {
	    $urlhdtypeid  =  input('get.windsnsactivityid'); //  获取地址视频ID
	    $data['windsnsactivityhot'] =  1; // 推荐状态：1不推荐2推荐',
	    $xiugaizhuangtai = Db::name('windsnsactivity')->where('windsnsactivityid', $urlhdtypeid)->update($data);// 
	    return  $this->fetch('windsns_activitytuijiandook');//操作OK！，更新隐藏状态2
    }

   
	  public function windsnsactivity_del($windsnsactivityid) //删除活动
	    {
	         $urlwindsnsactivityid  =  input('get.windsnsactivityid'); //  获取地址栏视频ID
	         $shanchu = Db::name('windsnsactivity')->where('windsnsactivityid',$urlwindsnsactivityid)->delete();
	         if($shanchu){
	            return $this->view->fetch('windsns_activitydeldook');//删除成功！
	         }else{
	          	return $this->view->fetch('windsns_activitydelno');//删除失败！
	         }
	    }
   
   
 
   
   
     public function upload()
    {
        $files = request()->file();
        $imags = [];
        $errors = [];
        foreach($files as $file){
            // 移动图片到/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'imguploads/windsnsactivity');
          
          
          
            if($info){
               // 成功上传 图片地址加入到数组
                $path = '/imguploads/windsnsactivity/'.$info->getSaveName();
                array_push($imags,$path);
            }else{
                array_push($errors,$file->getError());
            }
        }

        //输出wangEditor规定的返回数据
        if(!$errors){
            $msg['errno'] = 0;
            $msg['data'] = $imags;
            return json($msg);
        }else{
            $msg['errno'] = 1;
            $msg['data'] = $imags;
            $msg['msg'] = "上传出错";
            return json($msg);
        }
    }    
   
   
// ============== 每个活动下面的用户列表===============

    public function windsnsactivity_enroll()
    {
        $urlid  =  input('get.windsnsactivityid'); //  获取地址活动id
        $windsnsactivitylist = Db::name('windsnsactivity_paylist')->
        alias('a')->join('ws_windsnsactivity_types w','a.windsns_hdfenlei = w.activitytypeid') ->
        alias('a')->join('ws_user f','a.user_id = f.id') ->
        where(['windsns_hdid' => $urlid]) ->
        order('windsnsactivitypayid', 'desc')-> paginate(20,false,['query' => request()->param()]); //列表
        $page = $windsnsactivitylist->render();
    	  $this->assign('windsnsactivitylist', $windsnsactivitylist);
        $this->assign('page', $page);
   
        $windsnspaysum  = Db::name('windsnsactivity_paylist')->where(['windsns_hdid' => $urlid])->count();
   
         if($windsnspaysum > 1 or $windsnspaysum == 1){
        	return $this->view->fetch('windsnsactivity_enrolllist');//报名列表
        }else{
           return $this->view->fetch('windsns_activityenronull');

        }
       
   
    } 
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
}
