<?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 Videosvip extends Frontend
{

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

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

    
    public function index()
    {
    	
	$getvideosid  =  input('get.videosid'); // 获取链接上面的文章ID
	$videoslittle = Db::name('admin_videos')-> where(['videosid'=>$getvideosid]) -> find();
	$videoslittle_names = $videoslittle['videoslittle'];
	$this->view->assign('title', __($videoslittle_names));//赋值视频标题-名称

     
    $videosinfos = Db::name('admin_videos')->//视频内容展示
    alias('a')->join('ws_admin_videostype w','a.videos_fenleiid = w.video_typeid') ->
    where(['videos_opentype'=>1,'videosid'=>$getvideosid]) -> select(); 
	$this->assign('videosinfos', $videosinfos);
      
     
    $videoshot = Db::name('admin_videos')->//视频内容展示---推荐视频
    alias('a')->join('ws_admin_videostype w','a.videos_fenleiid = w.video_typeid') ->
    where(['videos_opentype'=>1,'videos_hot'=>2]) ->limit(30)-> select(); //视频内容
	$this->assign('videoshot', $videoshot);
     
    $sqlvideo = Db::name('admin_videos')-> where(['videos_opentype'=>1,'videosid'=>$getvideosid]) -> find();
    
    
//  查询这个视频购买记录表内容，是否有这个会员的购买记录
    $userid = $this->auth->getUserinfo()['user_id'];;//获取这个会员的ID
    $sqluserpay = Db::name('admin_videospaylist')-> where(['user_id'=>$userid,'videospay_videosid'=>$getvideosid,'videospay_type'=>1,'videospay_del'=>1]) -> find();
    

     if(!$sqlvideo){
     	
     	return $this->view->fetch('videos_sqlnulla');//该视频不存在或者已经下架
     	
     }elseif(!$sqluserpay){
     	
     	   $sqlvideoinfos = Db::name('admin_videos')-> where(['videosid'=>$getvideosid]) -> find();
     	   $sqlvideo_names = $sqlvideoinfos['videoslittle'];
     	   $sqlvideo_price = $sqlvideoinfos['viseos_prices'];
     	   $sqlvideo_videosid = $sqlvideoinfos['videosid'];
     	   $this->assign('sqlvideo_names', $sqlvideo_names); //该视频名称赋值到页面
     	   $this->assign('sqlvideo_price', $sqlvideo_price); //该视频售价赋值到页面
     	   $this->assign('sqlvideo_videosid', $sqlvideo_videosid); //该视频ID赋值到页面
     	
     		//抱歉！~用户未购买该视频，需要进行购买！
     		return $this->view->fetch('videos_nopay');
     }else{
     	$sqlliulansum = Db::name('admin_videos')-> where(['videosid'=>$getvideosid])->setInc('videos_liulansum', 1);//浏览次数+1
     	return $this->view->fetch('videos_infos');
     }
     

	 
        
    }




    public function paydo()
    {


        $time = time();
		$paytimess = date('Y-m-d H:i:s',$time);
		$addtimes = strtotime($paytimess);//获取当前时间


        $userid = $this->auth->getUserinfo()['user_id'];;//获取这个会员的ID
        $user_money = Db::name('user')->where('id', $userid)->find(); //会员余额
        $usermoneys =  $user_money['money'];	 //SQL账户总金额


        $getvideosid  =  input('get.videosid'); // 获取浏览器视频ID
        $this->assign('getvideosid', $getvideosid); //该视频名称赋值到页面
        $sqlvideoinfos = Db::name('admin_videos')-> where(['videosid'=>$getvideosid]) -> find();
        $sqlvideo_price = $sqlvideoinfos['viseos_prices']; // 获取这个视频的售价



        $addpay['user_id'] = $userid;//获取购买人的用户ID
	    $addpay['videospay_videosid']= $getvideosid;//获取购买的视频ID
        $addpay['videospay_prices']= $sqlvideo_price;//获取购买的视频价格
        $addpay['videospay_type']= 1;//获取购买状态：1购买成功2购买失败',
        $addpay['videospay_del']= 1;//删除状态：1正常显示2用户端删除',
        $addpay['videospay_times']= $addtimes;//当前支付时间




		if($usermoneys > $sqlvideo_price or $usermoneys == $sqlvideo_price){
			
			$xiaofei = Db::name('user')->where('id', $userid)->setDec('money', $sqlvideo_price);//SQL余额自减
			$sql = Db::name('admin_videospaylist')->insert($addpay);//   保存订单
			return $this->view->fetch('videos_payok');//购买成功！
		}else{
			return $this->view->fetch('videospayno');//购买失败，余额不足！
		
		}
			









			}












}
