123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- <?php
- declare(strict_types=1);
- /**
- * This file is part of Hyperf.
- *
- * @link https://www.hyperf.io
- * @document https://hyperf.wiki
- * @contact group@hyperf.io
- * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
- */
- namespace App\Model;
- use Hyperf\DbConnection\Model\Model as BaseModel;
- use Hyperf\Stringable\Str;
- abstract class Model extends BaseModel
- {
- protected $query;
- protected array $select = [];
- protected int $is_status_search = 1;// 默认使用 status = 1 筛选
- protected int $is_delete_search = 1;// 默认使用 is_delete = 0 筛选
- protected string $message = '';
- protected array $data = [];
- /**
- * 筛选条件
- *
- * @param $query
- * @param $value
- * @param array $params
- * @return mixed
- */
- public function searchIdAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('id', $value);
- }
- /**
- * 设置 select
- * @param array $select
- * @return BaseModel
- */
- public function setSelect(array $select = ['*'])
- {
- $this->select = $select;
- return $this;
- }
- /**
- * 设置 默认使用 status = 1 筛选
- * @param int $is_status_search
- * @return $this
- */
- public function setIsStatusSearchValue(int $is_status_search = 1)
- {
- $this->is_status_search = $is_status_search;
- return $this;
- }
- /**
- * 设置 默认使用 is_delete = 0 筛选
- * @param int $is_delete_search
- * @return $this
- */
- public function setIsDeleteSearchValue(int $is_delete_search = 1)
- {
- $this->is_delete_search = $is_delete_search;
- return $this;
- }
- /**
- * 列表查询
- *
- * @param array $params
- * @param array $orderBy
- * @param array $select
- * @param array $with
- * @return array
- */
- public function getList(array $params = [], array $orderBy = [], array $select = [], array $with = [])
- {
- $query = $this->catchSearch($params)
- ->catchPages($params)
- ->sortTool($orderBy)
- ->getQueryObj()
- ->select(!empty($select) ? $select : $this->select);
- // 模型关联
- count($with) > 0 && $query->with($with);
- // 规避 禁用 和 删除 数据
- $this->is_status_search === 1 && $query->where('status', 1);
- $this->is_delete_search === 1 && $query->where('is_delete', 0);
- return $this->catchData($query->get()->toArray());
- }
- /**
- * 获取总数
- * @param array $params
- * @return mixed
- */
- public function getTotal(array $params = [])
- {
- $query = $this->catchSearch($params)->getQueryObj();
- // 规避 禁用 和 删除 数据
- $this->is_status_search === 1 && $query->where('status', 1);
- $this->is_delete_search === 1 && $query->where('is_delete', 0);
- return $query->count();
- }
- /**
- * 单条数据查询
- * @param array $params
- * @param array $orderBy
- * @param array $select
- * @param array $with
- * @return array
- */
- public function getDetail(array $params = [], array $orderBy = [], array $select = [], array $with = [])
- {
- $query = $this->catchSearch($params)
- ->catchPages($params)
- ->sortTool($orderBy)
- ->getQueryObj()
- ->select(!empty($select) ? $select : $this->select);
- // 模型关联
- count($with) > 0 && $query->with($with);
- // 规避 禁用 和 删除 数据
- $this->is_status_search === 1 && $query->where('status', 1);
- $this->is_delete_search === 1 && $query->where('is_delete', 0);
- $detail = $query->first();
- if ($detail){
- $detail = $detail->toArray();
- }else{
- $detail = [];
- }
- return $this->catchData($detail);
- }
- /**
- * 查询器
- *
- * @param array $params
- * @return $this
- */
- protected function catchSearch(array $params = [])
- {
- $this->query = !empty($this->query) ? $this->query : $this;
- if (empty($params)) {
- return $this;
- }
- foreach ($params as $field => $value) {
- $method = 'search' . Str::studly($field) . 'Attribute';
- if ($value !== null && $value !== '' && method_exists($this, $method)) {
- $this->query = $this->$method($this->query,$value,$params);
- }
- }
- return $this;
- }
- /**
- * 分页器
- *
- * @param $params
- * @return $this
- */
- protected function catchPages($params)
- {
- $this->query = !empty($this->query) ? $this->query : $this;
- if (empty($params['page']) && empty($params['list_rows'])){
- return $this;
- }
- $page = intval($params['page'] ?? 1);
- $size = intval($params['list_rows'] ?? 15);
- $this->query = $this->query->offset(($page - 1) * $size)->limit($size);
- return $this;
- }
- /**
- * 排序工具
- *
- * @param $orderBy
- * @return $this
- */
- protected function sortTool($orderBy)
- {
- $this->query = !empty($this->query) ? $this->query : $this;
- if (empty($orderBy)){
- return $this;
- }
- foreach ($orderBy as $sort=>$order){
- if (empty($sort) || empty($order)){
- continue;
- }
- $this->query = $this->query->orderBy($sort,$order);
- }
- return $this;
- }
- /**
- * 数据集处理器
- * @param array $data
- * @return array
- */
- protected function catchData(array $data): array
- {
- if (isset($data[0])){
- foreach ($data as $key=>$val){
- foreach ($val as $k=>$v){
- $method = 'data' . Str::studly($k) . 'Attribute';
- if (method_exists($this, $method)) {
- $data[$key][$k] = $this->$method($v,$data);
- }
- }
- }
- }else{
- foreach ($data as $key=>$val){
- $method = 'data' . Str::studly($key) . 'Attribute';
- if (method_exists($this, $method)) {
- $data[$key] = $this->$method($val,$data);
- }
- }
- }
- return $data;
- }
- /**
- * @return mixed
- */
- protected function getQueryObj()
- {
- return $this->query;
- }
- /**
- * 返回成功结果
- * @param string $message
- * @param array $data
- * @return bool
- */
- protected function success(string $message = 'success',array $data = []): bool
- {
- $this->message = $message;
- $this->data = $data;
- return true;
- }
- /**
- * 返回失败结果
- * @param string $message
- * @param array $data
- * @return bool
- */
- protected function error(string $message = 'error',array $data = []): bool
- {
- $this->message = $message;
- $this->data = $data;
- return false;
- }
- /**
- * 获取成功数据
- * @return array
- */
- public function getData(): array
- {
- return $this->data;
- }
- /**
- * 获取消息
- * @return string
- */
- public function getMessage(): string
- {
- return $this->message;
- }
- }
|