<?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\index\controller;
use app\common\controller\Frontend;
use app\common\controller\Backend;
use think\Db;
use think\Request;


class Shoppay extends Frontend
{
	
  protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
    protected $noNeedRight = '*';

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


	public function goodspaydo()
	    {
		    $time = time();
			$paytimess = date('Y-m-d H:i:s',$time);
		    $tadytimes = strtotime($paytimess);
            $getgoodsid  =  input('get.goodsid'); 
            $getgoodssum =  input('post.goodssum'); 
		    $sqgoodsid   = Db::name('windsnsmall_shoplist')-> where(['windsnsmallshopsid'=>$getgoodsid])->find();
		    $goodsames   =  $sqgoodsid['widsnsmallshopslittle'];
		    $goodstypeid   =  $sqgoodsid['widsnsmallshopstypeid'];
		    $goodsprices =  $sqgoodsid['widsnsmallshopsprice'];
		    $goodsdanwei =  $sqgoodsid['widsnsmallshopsdanwei'];
		    $goodskucun  =  $sqgoodsid['widsnsmallshopskuncunsum'];
		    $goodstype   =  $sqgoodsid['widsnsmallshopstypes'];
		    $goodsdownurl   =  $sqgoodsid['widsnsmallshopsdownurl'];
		    $goodsdownma   =  $sqgoodsid['widsnsmallshopsdownma'];
		    $goodsbaoyou = $sqgoodsid['widsnsmallshopsbaoyou'];
		    $goodsgumaishu  =  $getgoodssum;
		    $goodsgumaitime =  $tadytimes;
		    $goodspriceall = $getgoodssum *  $goodsprices; 
		    $sqtypeid   = Db::name('windsnsmalltype')->where(['windsnsmalltypeid'=>$goodstypeid])->find();
		    $sqgoodstypeinfosid   = Db::name('windsnsmall_shoplist')->where(['windsnsmallshopsid'=>$getgoodsid,'widsnsmallshopstypeid'=>$goodstypeid,])->find();
		    $sqgoodsokid   = Db::name('windsnsmall_shoplist')->where(['windsnsmallshopsid'=>$getgoodsid,'widsnsmallshopsokid'=>1])->find();
		    $userid = $this->auth->getUserinfo()['user_id'];
	        $user_money = Db::name('user')->where('id', $userid)->find(); 
	        $usermoneys =  $user_money['money'];	
		     if(!$sqgoodsid){
		     	return $this->view->fetch('index@shop/windsnserrors_nogoodshop');
		     }elseif(!$sqtypeid){
		     	return $this->view->fetch('index@shop/windsnserrors_nogoodstype');
		     }elseif(!$sqgoodstypeinfosid){
	     	      return $this->view->fetch('index@shop/windsnserrors_nogoodshop');
	          }elseif(!$sqgoodsokid){
		     	return $this->view->fetch('index@shop/windsnserrors_nogoodshopxiajia');
		     }else{
		         if($getgoodssum == 0){
		            	return $this->view->fetch('index@shop/windsnserrors_nogoodspaynull');
		          }elseif($goodskucun == 0){
		            	return $this->view->fetch('index@shop/windsnserrors_nogoodskucunnull');
		            }elseif( $getgoodssum > $goodskucun)
		            return $this->view->fetch('index@shop/windsnserrors_nogoodskucunno');
		            else{
		            	if ($usermoneys >= $goodspriceall) {
							            $sqlshopadd['windsns_userid'] =  $userid;
							            $sqlshopadd['windsns_goodstypes'] =  $goodstype;
							            $sqlshopadd['windsns_goodsnames'] =  $goodsames;
							            $sqlshopadd['windsns_goodsprice'] =  $goodsprices;
							            $sqlshopadd['windsns_goodspaysum'] =  $getgoodssum;
							            $sqlshopadd['windsns_goodspayallprice'] =  $goodspriceall;
							            $sqlshopadd['windsns_goodspaytimes'] =  $tadytimes;
							             $sqlshopadd['windsns_goodsdanwei'] =  $goodsdanwei;
							            $sqlshopadd['windsns_goodsdingdantype'] =  1;
							            $sqlshopadd['windsns_goodspaytype'] =  1;
				              if( $goodstype == 1){
                                    $sqlshopadd['windsns_goodspaywuliu'] = $goodsbaoyou;
				              }else{
				              	    $sqlshopadd['windsns_goodsdownurl'] =  $goodsdownurl;
				              	    $sqlshopadd['windsns_goodstiquma'] =  $goodsdownma;
				              	    $sqlshopadd['windsns_goodspaywuliu'] = 1;
				              }
				            $xiaofei = Db::name('user')->where('id', $userid)->setDec('money', $goodspriceall);
				            $shopdingdansumadd = Db::name('windsnsmall_shoplist')-> where(['windsnsmallshopsid'=>$getgoodsid])->setInc('windsnsmallshopdingdansum', 1);
			                $sql = Db::name('windsnsmall_shoppaylist')->insert($sqlshopadd);
				            return $this->view->fetch('index@shop/windsns_goodspayok');
				        } else {
				        	return $this->view->fetch('index@shop/windsnserrors_nogoodsmoneynull');
				        }
		            	

		            }

		     }


            }



}