From ad9b6ac4a1e90f259a9ac51f4c3c86e0c36b5447 Mon Sep 17 00:00:00 2001 From: Tenko_ Date: Thu, 14 Jul 2022 18:32:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=9B=9E=E6=94=B6=E7=AB=99?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/app/adminapi/lists/dept/JobsLists.php | 30 +++++++++++-- server/app/common/enum/SoftDeleteEnum.php | 10 +++++ server/app/common/lists/BaseDataLists.php | 42 +++++++++++++++++++ .../common/lists/ListsSoftDeleteInterface.php | 15 +++++++ 4 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 server/app/common/enum/SoftDeleteEnum.php create mode 100644 server/app/common/lists/ListsSoftDeleteInterface.php diff --git a/server/app/adminapi/lists/dept/JobsLists.php b/server/app/adminapi/lists/dept/JobsLists.php index 9c73a2976..5a842cf03 100644 --- a/server/app/adminapi/lists/dept/JobsLists.php +++ b/server/app/adminapi/lists/dept/JobsLists.php @@ -15,15 +15,18 @@ namespace app\adminapi\lists\dept; use app\adminapi\lists\BaseAdminDataLists; +use app\common\enum\SoftDeleteEnum; use app\common\lists\ListsSearchInterface; +use app\common\lists\ListsSoftDeleteInterface; use app\common\model\dept\Jobs; +use think\db\BaseQuery; /** * 岗位列表 * Class JobsLists * @package app\adminapi\lists\dept */ -class JobsLists extends BaseAdminDataLists implements ListsSearchInterface +class JobsLists extends BaseAdminDataLists implements ListsSearchInterface, ListsSoftDeleteInterface { /** @@ -49,7 +52,8 @@ class JobsLists extends BaseAdminDataLists implements ListsSearchInterface */ public function lists(): array { - $lists = Jobs::where($this->searchWhere) + $lists = $this->query + ->where($this->searchWhere) ->append(['status_desc']) ->limit($this->limitOffset, $this->limitLength) ->order(['sort' => 'desc', 'id' => 'desc']) @@ -68,7 +72,27 @@ class JobsLists extends BaseAdminDataLists implements ListsSearchInterface */ public function count(): int { - return Jobs::where($this->searchWhere)->count(); + return $this->query->where($this->searchWhere)->count(); } + /** + * @notes 设置支持软删除的查询对象 + * @author Non1TH + * @return BaseQuery + */ + public function setSoftDeleteQuery(): BaseQuery + { + switch ($this->withTrashed) { + case SoftDeleteEnum::WITH_TRASHED: + $model = Jobs::withTrashed(); + break; + case SoftDeleteEnum::ONLY_TRASHED: + $model = Jobs::onlyTrashed(); + break; + default: + $model = (new Jobs)->db(); + } + + return $model; + } } \ No newline at end of file diff --git a/server/app/common/enum/SoftDeleteEnum.php b/server/app/common/enum/SoftDeleteEnum.php new file mode 100644 index 000000000..6e699e41a --- /dev/null +++ b/server/app/common/enum/SoftDeleteEnum.php @@ -0,0 +1,10 @@ +request = request(); $this->params = $this->request->param(); + //软删除处理 + $this->initSoftDeleteSupport(); + //分页初始化 $this->initPage(); @@ -205,6 +212,41 @@ abstract class BaseDataLists implements ListsInterface } } + /** + * @notes 初始化支持软删除的类 + * @return bool + * @author Non1TH + */ + private function initSoftDeleteSupport() + { + if ($this instanceof ListsSoftDeleteInterface) { + $trashed = $this->request->get('trashed/d'); + if ($trashed == SoftDeleteEnum::WITH_TRASHED || $trashed == SoftDeleteEnum::ONLY_TRASHED) { + $this->withTrashed = $trashed; + } + + $this->query = $this->setSoftDeleteQuery(); + + return true; + } else { + // 没有实现ListsSoftDeleteInterface接口的场景 + $this->withTrashed = SoftDeleteEnum::WITHOUT_TRASHED; + + preg_match('/^app\\\\adminapi\\\\lists\\\\(.*)Lists$/', static::class, $matches); + $modelClass = '\\app\\common\\model\\' . $matches[1] . 'Model'; + if (class_exists($modelClass)) { + // 可以被自动查找的场景 + $model = new $modelClass(); + $this->query = $model->db(); + } else { + // 无法查找到模型时设置为null + $this->query = null; + } + + return false; + } + } + /** * @notes 不需要分页,可以调用此方法,无需查询第二次 * @return int diff --git a/server/app/common/lists/ListsSoftDeleteInterface.php b/server/app/common/lists/ListsSoftDeleteInterface.php new file mode 100644 index 000000000..512629cd9 --- /dev/null +++ b/server/app/common/lists/ListsSoftDeleteInterface.php @@ -0,0 +1,15 @@ +