<?php
declare (strict_types = 1);

namespace app\admin\model;

use think\facade\Db;use think\Model;

/**
 * @mixin \think\Model
 */
class CombindStock extends Model
{
   protected $createTime='createtime';
   protected $updateTime='updatetime';
   
   protected $hidden=["wsminfo","good"];
   
   protected $append=["specInfo","catInfo"];
   public function good(){
   	    return $this->belongsTo(GoodBasic::class,"spuCode","spuCode")->bind(["cat_id","companyNo","companyName"]);
   }
   
   public function WsmInfo(){
   	return $this->belongsTo(WarehouseInfo::class,'wsm_code','wsm_code')->bind(['wsm_name'=>"name","wsm_type"]);
   }
   
   public function CombindGood(){
   		return $this->hasMany(GoodCombind::class,'spuCode','spuCode')->where("is_del",0);
   }
   
   public function  getSpecInfoAttr($v,$row){
         $spec = Db::name('good_spec')->where(['spuCode'=>$row['spuCode'],'is_del'=>0])->select()->toArray();
          $speclist=[];
         if(!empty($spec)){
            foreach ($spec as $value){
                $temp=[];
                $temp['id']=$value['id'];
                $temp['spuCode']=$value['spuCode'];
                $temp['spec_id']=$value['spec_id'];
                $temp['spec_value_id']=$value['spec_value_id'];
                $temp['is_del']=$value['is_del'];
                $sp = Db::name('specs')->where(['id'=>$value['spec_id']])->find();
                $temp['spec_name']=isset($sp['spec_name']) ? $sp['spec_name']:'';
                $spv = Db::name('spec_value')->where(['id'=>$value['spec_value_id']])->find();
                $temp['spec_value']=isset($spv['spec_value']) ? $spv['spec_value']:'';
                $speclist[]=$temp;
            }
        }
         return  $speclist;
   }
   
   public function  getCatInfoAttr($v,$row){
        return made($row['cat_id']);
   }
}