<?php
namespace app\admin\model;
use think\Model;
use think\Db;
/**
 * 文档基础模型
 */
class Attributes extends Model{

	//获取规格属性
	public function getlist($id,$type){
	   $info=db("category")->find($id);
	   $map["types_id"]=$info["types_id"];
	   $map["pid"]=0;  
	   $map["type"]=$type; 
	   $data=db("attributes")->where($map)->select();
	   foreach($data as  $n=>$val){
                  $where["pid"]=$val['id'];
				  $data[$n]["list"]=db("attributes")->where($where)->select();
				  
             					
	   }
		return $data;
   }
public function updates(){
	    $Attributes = new \app\admin\model\Attributes;
        /* 添加或新增属性 */
        if(!$_POST['id']){
			//新增属性
		   $res=$Attributes->validate(true)->allowField(true)->save($_POST);
		   $error=$Attributes->getError();
		   $id=$Attributes->getLastInsID();
		   if($error){
		     $this->error=$error;
			 return false;
		   }
             if($res){  
				  if($_POST["value"]){
						$arr=explode("、",$_POST["value"]); 
						 $save=array();
                          foreach($arr as $key=>$val){
		                     $data["title"]=$val;
							 $data["pid"]=$id;
							 if(!db('Attributes')->where($data)->find()){
								 $data["types_id"]=$_POST["types_id"];
								 db('Attributes')->insert($data); 
							 }else{
								 $save["types_id"]=$_POST["types_id"];
								 db('Attributes')->where($data)->update($save);  
							 }
		                  }
					 } 
			 }else{
			   return false;
			 }
        } else { 
				//更新数据
				   $res=$Attributes->validate(true)->allowField(true)->save($_POST,['id' => $_POST['id']]);
		           $error=$Attributes->getError();
				   if(!$res){
					 $this->error=$error;
					 return false;
				   }else{
					 if($_POST["value"]){
						$arr=explode("、",$_POST["value"]); 
						 $save=array();
                          foreach($arr as $key=>$val){
		                     $data["title"]=$val;
							 $data["pid"]=$_POST["id"];
							 if(!db('Attributes')->where($data)->find()){
								$data["types_id"]=$_POST["types_id"];
								db('Attributes')->insert($data); 
							 }else{
								 $save["types_id"]=$_POST["types_id"];
								 db('Attributes')->where($data)->update($save); 
								 
							 }
		                  }
					 }   
					   
				   }	
				
        }
        return $_POST;

    }

}
