$params['mall_id'], 'store_id' => $params['store_id'],'id' => $params['id'], 'status' => StatusEnum::ENABLED]); } else if(!empty($params['mall_id']) && !empty($params['goods_id'])&&!empty($params['attr_id']) && !empty ($params['store_id'])) { $model = self::findOne(['mall_id' => $params['mall_id'],'attr_id' => $params['attr_id'], 'store_id' => $params['store_id'],'goods_id' => $params['goods_id'], 'status' => StatusEnum::ENABLED]); } if(empty($model)) { $model = new self(); $model->loadDefaultValues(); } else { $params['stock']=$params['stock']+$model->stock; } if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage()); if (!$model->save()) throw new \Exception($model->getErrorMessage()); return $model; } catch (\Exception $e) { self::$static_error = $e->getMessage(); return false; } } public static function handleStock($goods_attr_id,$num, $store_id){ try{ $model = self::findOne(['attr_id'=>$goods_attr_id,'status'=>StatusEnum::ENABLED, 'store_id' => $store_id, 'status' => StatusEnum::ENABLED, 'is_on_sale' => 1]); if(empty($model)) throw new \Exception('规格不存在'); if(!is_int($num)) throw new \Exception('库存数量必须为整数'); if($num < 0 && $model->stock < abs($num)) throw new \Exception('库存不足'); $model->stock += $num; $res = $model->save(); if($res === false) throw new \Exception($model->getErrorMessage()); return true; }catch(\Exception $e){ self::setStaticError($e->getMessage()); return false; } } public function getAttr(){ return $this->hasOne(GoodsAttr::class,['id'=>'attr_id']); } public function getGoods(){ return $this->hasOne(Goods::class,['id'=>'goods_id']); } }