diff --git a/app/store/controller/Recovery.php b/app/store/controller/Recovery.php index 998de595..75c13240 100644 --- a/app/store/controller/Recovery.php +++ b/app/store/controller/Recovery.php @@ -175,10 +175,9 @@ class Recovery extends Controller * @return Json * @author: wanghousheng */ - public function deleteRecovery(array $recoveryId): Json + public function deleteRecovery(array $serverId): Json { - $model = new ServerRecovery; - if ($model->remove($recoveryId)) { + if(ServerRecovery::deleteRecoveryId($serverId)){ return $this->renderSuccess('删除成功'); } return $this->renderError('删除失败'); diff --git a/app/store/model/server/ServerRecovery.php b/app/store/model/server/ServerRecovery.php index 29e16d3a..48221a1d 100644 --- a/app/store/model/server/ServerRecovery.php +++ b/app/store/model/server/ServerRecovery.php @@ -59,4 +59,39 @@ class ServerRecovery extends BaseServerRecovery { return static::detail($recoveryId, ['image', 'category']); } + + public static function deleteRecoveryId($data, bool $force = false) + { + // 传入空值(包括空字符串和空数组)的时候不会做任何的数据删除操作,但传入0则是有效的 + if (empty($data) && 0 !== $data) { + return false; + } + $model = (new static()); + + $query = $model->db(false); + + // 仅当强制删除时包含软删除数据 + if ($force) { + $query->removeOption('soft_delete'); + } + + if (is_array($data) && key($data) !== 0) { + $query->where($data); + $data = null; + } elseif ($data instanceof \Closure) { + call_user_func_array($data, [&$query]); + $data = null; + } elseif (is_null($data)) { + return false; + } + + $resultSet = $query->select($data); + + foreach ($resultSet as $result) { + /** @var Model $result */ + $result->force($force)->delete(); + } + + return true; + } } \ No newline at end of file