'id', 'sort' => 'desc', ]; /** * @var array 查询条件 */ protected array $where = [ ['deleted_at', '=', 0] ]; /** * @var array 查询条件In */ protected array $whereIn = []; /** * @var array 下拉列表字段 */ protected array $optionField = ['id', 'name']; /** * @var array 下拉列表字段 */ protected array $queryField = []; /** * @var array 关联加载 */ protected array $with = []; /** * @var string 时间字段 */ protected string $timeField = 'created_at'; /** * @var bool 是否鉴权 */ protected $isAuth = true; public function __construct() { if ($this->isAuth === true) { $this->userInfo = JWTService::getInstance()->getToken()->getUserInfo(); $this->userId = $this->userInfo['id'] ?? 0; $this->roleId = $this->userInfo['role_id'] ?? 0; } $this->utils = UtilsService::getInstance(); } /** * 获取实例 * @return null|static */ public static function getInstance(): null|static { $name = get_called_class(); if (!isset(self::$_instance[$name])) { self::$_instance[$name] = new static(); } return self::$_instance[$name]; } /** * 获取列表数据 * * 本函数负责根据设定的条件,从数据库中查询并返回分页后的数据列表 * 它会根据空值条件动态地修改查询,以实现灵活的查询需求 * * @return array 返回包含分页信息和数据列表的数组 * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ public function getPageList($isArr = true, $isGetWhere = true): array { // 初始化查询条件 if ($isGetWhere === true) $this->getWhere(); // 获取范围查询条件 $searchTime = request()->get('search_time'); if (!empty($searchTime)) { $this->getWhereBetween($searchTime); } // 执行数据库查询,根据条件动态构建查询语句 $result = $this->model::when(!empty($this->with), function ($query) { // 如果关联加载条件存在,则添加关联加载 $query->with($this->with); }) ->when(!empty($this->where), function ($query) { // 如果查询条件存在,则添加查询条件 $query->where($this->where); }) ->when(!empty($this->whereIn), function ($query) { // 如果查询条件存在,则添加查询条件 $query->whereIn($this->whereIn[0], $this->whereIn[1]); }) ->when(!empty($this->whereBetween), function ($query) { // 如果范围查询条件存在,则添加范围查询 $query->whereBetween($this->whereBetween[0], $this->whereBetween[1]); }) ->when(!empty($this->orderBy), function ($query) { // 如果排序条件存在,则添加排序条件 $query->orderBy($this->orderBy['name'], $this->orderBy['sort']); }) ->select($this->selectField) // 执行分页查询,每页返回10条记录 ->paginate(request()->get('pageSize', 20)); if ($isArr === true) { $result = $result->toArray() ?? []; // 返回分页信息和数据列表 return [ 'page' => $result['current_page'], 'size' => $result['per_page'], 'page_count' => $result['last_page'], 'total' => $result['total'], 'items' => $result['data'], ]; } // 返回分页信息和数据列表 return [ 'page' => $result->currentPage(), 'size' => $result->perPage(), 'page_count' => $result->lastPage(), 'total' => $result->total(), 'items' => $result->items(), ]; } /** * 获取下拉列表数据 * @return mixed */ public function getOption(): mixed { $this->getWhere(); return $this->model::when(!empty($this->where), function ($query) { $query->where($this->where); })->when(!empty($this->whereIn), function ($query) { $query->where($this->whereIn[0], $this->whereIn[1]); })->orderBy($this->orderBy['name'], $this->orderBy['sort'])->get($this->optionField); } /** * 详情 * * @param $id * @return mixed * @throws Exception */ public function getDetail($id): mixed { $info = $this->model::with($this->with)->select($this->selectField)->find($id); if (empty($info)) { return $this->utils->notFound('数据不存在'); } return $info; } /** * 新增 * @param $params * @return mixed */ public function insert($params): mixed { if (!array_key_exists('created_at', $params)) { $params['created_at'] = time(); } // return $this->model::insertGetId($this->utils->truncatedOss($params)); return $this->model::insertGetId($params); } /** * 编辑 * @param $id * @param $params * @return mixed * @throws Exception */ public function save($id, $params): mixed { if (empty(trim($id))) { $this->utils->errorThrow('参数错误'); } $this->where[] = ['id', '=', $id]; // 软删除 $info = $this->model::where($this->where)->first(); if (empty($info)) { return $this->utils->notFound('数据不存在'); } if (!array_key_exists('updated_at', $params)) { $params['updated_at'] = time(); } // return $this->model::where('id', $id)->update($this->utils->truncatedOss($params)); return $this->model::where('id', $id)->update($params); } /** * 删除 * * @param $id * @return mixed|true * @throws Exception */ public function del($id): mixed { // 软删除 $info = $this->model::whereIn('id', $id)->get(['id']); if (empty($info)) { return $this->utils->notFound('数据不存在'); } if ($this->model::whereIn('id', $id)->update([ 'deleted_at' => time(), 'updated_at' => time() ])) { return true; } else { $this->utils->errorThrow('删除失败'); } } /** * 获取查询条件 * @return void */ protected function getWhere(): void { $query = request()->query(); if (!empty($this->queryField)) { foreach ($this->queryField as $key => $value) { if (in_array($key, array_keys($query))) { if (!empty($query[$key] ?? '') || $query[$key] == '0') { switch ($value) { case '=': $this->where[] = [$key, $value, $query[$key]]; break; case 'like': $this->where[] = [$key, $value, '%' . $query[$key] . '%']; break; default: break; } } } } } } /** * 获取时间范围查询条件 * @param $searchTime * @return void */ protected function getWhereBetween($searchTime): void { $this->whereBetween = [$this->timeField, [strtotime($searchTime[0] ?? date('Y-m-01 00:00:00')), strtotime(date('Y-m-d 23:59:59', strtotime($searchTime[1])) ?? date('Y-m-d 23:59:59'))]]; } }