diff --git a/server/app/adminapi/lists/dept/JobsLists.php b/server/app/adminapi/lists/dept/JobsLists.php index 9c73a2976cceed9f452263d2e2ae6e51814d2ca1..5a842cf03affdee40971a1e5f8a50b16c52acb76 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 0000000000000000000000000000000000000000..6e699e41afbf1b51437ac800f94563d305b57533 --- /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 0000000000000000000000000000000000000000..512629cd9882b23b5a1efe131d111fb8ac829588 --- /dev/null +++ b/server/app/common/lists/ListsSoftDeleteInterface.php @@ -0,0 +1,15 @@ +