<?php
// +----------------------------------------------------------------------
// | yershop网店管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
// | 版权申明：yershop网店管理系统不是一个自由软件，是贝云网络官方推出的商业源码，严禁在未经许可的情况下
// | 拷贝、复制、传播、使用yershop网店管理系统的任意代码，如有违反，请立即删除，否则您将面临承担相应
// | 法律责任的风险。如果需要取得官方授权，请联系官方http://www.yershop.com
// +----------------------------------------------------------------------
namespace app\index\controller;
use think\Controller;
use think\Db;
/*购物车*/
class Cart extends Home{
	
	public function index() {
		 	
        if(!session_uid() ) {
			$this->redirect("User/login");
		}
			
	   $uid=session_uid();
		
		$map[]=["uid",'=',$uid];
		//$map[]=["cart_shop_id",'=',0];
		$list=Db::name('cart')->where($map)->select();
		$lists="";
		if($list){//dump($list);
				$code=$this->getNonceStr();
				$list2= Db::name('cart')->where($map)->Distinct(true)->field('shop_id')->select();
					
				
				foreach($list2 as $k=> $val){
					$shop_id=$val['shop_id'];
					if($shop_id>0){
						
						 $mapp[]=["shop_id",'=',$shop_id];
					}
					else{
						
						$mapp[]=["shop_id",'=',0];
						
					}
					$mapp[]=["uid",'=',$uid];
			       
		            $mapp[]=["status",'=',1];
					
						
				    $data["uid"]=$uid;
			        $data["shop_id"]=$val['shop_id'];
		            $data["status"]=1; 
				    $data["create_time"]=time();
					$data["code"]=$code;
					$cart_shop_id=Db::name("cart_shop")->insertGetId($data);
					
					
					$where[]=["uid",'=',$uid];
					$where[]=["shop_id",'=',$shop_id];
					
					$update["cart_shop_id"]=$cart_shop_id;
					$update["key"]=$code;
					$perfix=config("database.prefix");
					$sql="update ".$perfix."cart set cart_shop_id=".$cart_shop_id." where shop_id=".$shop_id.' and uid='.$uid;
	                Db::query($sql);
	            }
				
				//购物车输出数据
				$data2[]=["uid",'=',$uid];
					
					
				$data2[]=["code",'=',$code];
				$lists=Db::name('cart_shop')->where($data2)->select();
				foreach($lists as $n=> $val){
					unset($map);
					$map["cart_shop_id"]=$val["id"];//$map["key"]=$val["code"];
				    $lists[$n]['n']= Db::name('cart')->where($map)->select();
						 foreach ($lists[$n]['n'] as &$v) {
						          $v['total'] = $v['price']*$v['num'];//计算价格
					    }
				} 
				
	        }
		/*查询购物车*/
		$this->assign('list',$lists);
	
		//购买该商品同时还购买了
		$meta_title = '我的购物车';
		$this->assign('meta_title', $meta_title);
        $lists=lists("goods",'',"15","id desc");
		$this->assign('lists',$lists);
	    return $this->fetch();

	}
	  public  function getNonceStr($length = 10) {
		$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		$str = "";
		for ($i = 0; $i < $length; $i++) {
		  $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
		}
		return $str;
    }
	
	/*加入购物车*/
	public  function additem() {
		$Cart= new \app\index\model\Cart;
		$data=$Cart->additem() ;
	    return $data;
	}
    /*列表页加入购物车*/
    public  function addList() {
			$Cart= new \app\index\model\Cart;
		$data=$Cart->addList() ;
	    return $data;
	}
	/*购物车增加商品数量*/
    public  function incNum() {
		$Cart= new \app\index\model\Cart;
		$data=$Cart->incNum() ;
	    return $data;
	}
	public  function decNum() {
		$Cart= new \app\index\model\Cart;
		$data=$Cart->decNum() ;
	    return $data;
	}
	 //已登录删除单个个商品
	public  function del() {
		$Cart= new \app\index\model\Cart;
		$data=$Cart->del() ;
	    return $data;
	}
    //删除单一商品
    public  function delItem() {
		$Cart= new \app\index\model\Cart;
		$data=$Cart->delItem() ;
	    return $data;
	}
    //删除多个商品
	public  function delCart() {
		$Cart= new \app\index\model\Cart;
		$data=$Cart->delCart() ;
	    return $data;
	}
    //查询
	public  function checkCart() {
		$Cart= new \app\index\model\Cart;
		$data=$Cart->checkCart() ;
	    return $data;
	}	
    //清空购物车
	public  function clear() {
			$Cart= new \app\index\model\Cart;
		$data=$Cart->clear() ;
	    return $data;
	}
}
