'ID', 'mall_id' => 'Mall ID', 'user_id' => 'User ID', 'goods_id' => 'Goods ID', 'mirror_price' => 'Mirror Price', 'created_at' => 'Created At', 'is_delete' => 'Is Delete', 'deleted_at' => 'Deleted At', 'updated_at' => 'Updated At', ]; } /** * @param $mall_id * @param $user_id * @param $goods_id * @return bool * @throws \Exception */ public static function createModel($mall_id, $user_id, $goods_id) { $exists = Goods::find()->where(['mall_id' => $mall_id, 'id' => $goods_id, 'is_delete' => 0])->one(); if (!$exists) { throw new \Exception('商品不存在'); } $model = static::findOne([ 'mall_id' => $mall_id, 'user_id' => $user_id, 'goods_id' => $goods_id ]); if (!$model) { $model = new static(); $model->mall_id = $mall_id; $model->user_id = $user_id; $model->goods_id = $goods_id; } else { if ($model->is_delete == 0) { throw new \Exception('已经收藏过啦!'); } } $model->mirror_price = $exists->price; $model->is_delete = 0; return $model->save(); } public static function deleteModel($mall_id, $user_id, $goods_id) { $model = static::findOne([ 'mall_id' => $mall_id, 'user_id' => $user_id, 'goods_id' => $goods_id ]); if ($model) { $model->is_delete = 1; return $model->save(); } else { return true; } } public static function removeBatch($mall_id, $user_id, $goods_id) { $res = static::updateAll( ['is_delete' => 1], ['mall_id' => $mall_id, 'user_id' => $user_id, 'goods_id' => $goods_id] ); return $res; } public function getGoods() { return $this->hasOne(Goods::className(), ['id' => 'goods_id']); } }