<?php
// +----------------------------------------------------------------------
// | Yershop 开源网店系统
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.yershop.com All rights reserved.
// +----------------------------------------------------------------------
namespace app\seller\model;
use think\Model;
use think\Db;
/**
 * 文档基础模型
 */
class Goods extends Model{
    protected $auto = ["create_time","update_time","start_time","end_time","uid",'attributes','attrs','specs',"shop_id"];
    protected $insert = ["create_time","update_time","uid","shop_id"];  
    protected $update = ["create_time","update_time","start_time","end_time","uid",'attributes','attrs','specs',"shop_id"]; 
     protected function setCreateTimeAttr()
    {
		
		return time();
		
    }
	protected function setUidAttr()
    {
		
		return is_login();
		
    }
	  protected function setUpdateTimeAttr()
    {
		
		return time();
		
    }

   protected function setShopIdAttr()
    {
		
		return get_shop_id();
		
    }
 
	 protected function setAttributesAttr()
    {   
       if(input('post.attributes/a')){ 
	       return implode(",",input('post.attributes/a'));
	   }
      
    }
   protected function setSpecsAttr()
    {   
       if(input('post.specs/a')){ 
	       return implode(",",input('post.specs/a'));
	   }
      
    }
    protected function setAttrsAttr()
    {     //addUserLog(var_export(input('post.attrs/a'),true),6);
       if(input('post.attrs/a')){ 
	       return implode(",",input('post.attrs/a'));
	   }
      
    }
	public function updates(){
	    $Goods = new \app\seller\model\Goods;
		 if(!$_POST['cate_id']){
		 
		     $this->error="自定义商品分类不能为空！";  
				   return false;
		 
		 }
		 if(!$_POST['category_id']){
		 
		     $this->error="系统商品分类分类不能为空！";  
				   return false;
		 
		 }
        /* 添加商品 */
        if(!$_POST['id']){
			//新增商品
		   $res=$Goods->validate(true)->allowField(true)->save($_POST);
		   $error=$Goods->getError();
		   $id=$Goods->id;
		   
		   if($error){
		     $this->error=$error;
			 return false;
		   } 
		   /* 添加sku */
		    if(isset($_POST['nums'])){
				$num=count($_POST['nums']);
				for($i=0; $i<$num; $i++){
					$data["title"]=$_POST['titles'][$i];
					$data["price"]=$_POST['prices'][$i];
					$data["num"]=$_POST['nums'][$i];
					$data["goods_id"]=$id;
					$data["path"]=$_POST['path'][$i];
					db("sku")->insert($data);
					
				}
			}else{
				    $data["title"]="无"; 
					$data["goods_id"]=$id;
					$data["price"]=$_POST['price'];
					$data["num"]=$_POST['num'];
					$data["path"]=$id;
					db("sku")->insert($data);
				
			}
        
        } else { 
				//更新数据
				   $res=$Goods->validate(true)->allowField(true)->save($_POST,['id' => $_POST['id']]);
		           $error=$Goods->getError();
				   if(!$res){
					 $this->error=$error;
					 return false;
				   } 
				$id=$_POST['id']; 
				   /* 更新sku */
		    if(isset($_POST['nums'])){
				$num=count($_POST['nums']);
				for($i=0; $i<$num; $i++){
					$data["title"]=$_POST['titles'][$i];
					$data["price"]=$_POST['prices'][$i];
					$data["num"]=$_POST['nums'][$i];
					$where["goods_id"]=$id;
					$where["path"]=$_POST['path'][$i];;
					if(db("sku")->where($where)->find()){
					   db("sku")->where($where)->update($data);
					}else{
						$data["goods_id"]=$id;
						$data["path"]=$_POST['path'][$i];
						db("sku")->insert($data);
					}
					
				}
			}else{
					$where["goods_id"]=$id;
					$data["price"]=$_POST['price'];
					$data["num"]=$_POST['num'];
					$data["path"]=$id;
					db("sku")->where($where)->update($data);
				
			}	
				
        }
        return $_POST;

    }
}
