123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- namespace app\common\model;
- use think\db\Query;
- use think\Model;
- use think\helper\Str;
- class BaseModel extends Model
- {
- protected $message = '';
- protected $data = [];
- protected $query;
- protected array $select = [];
- protected int $is_status_search = 0;// 默认使用 status = 1 筛选
- protected int $is_delete_search = 0;// 默认使用 is_delete = 0 筛选
- /**
- * 筛选条件
- *
- * @param Query $query
- * @param $value
- * @param array $params
- * @return mixed
- */
- public function searchIdAttribute($query, $value, array $params)
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('id', $value);
- }
- public function searchIdInAttribute($query, $value, array $params)
- {
- if ($value === []){
- return $query->whereIn('id', []);
- }
- if (empty($value)) {
- return $query;
- }
- return $query->whereIN('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 mixed
- */
- public function getList(array $params = [], array $orderBy = [], array $select = [], array $with = [])
- {
- $query = $this->catchSearch($params)
- ->catchPages($params)
- ->sortTool($orderBy)
- ->getQueryObj()
- ->field($select ?: $this->select);
- // 模型关联
- count($with) > 0 && $query->with($with);
- // 规避 禁用 和 删除 数据
- $this->is_status_search === 1 && $query->where('status', 1);
- $this->is_delete_search === 1 && $query->whereNull('deletetime');
- return $this->catchData(json_decode(json_encode($query->select()),true));
- }
- /**
- * 单条数据查询
- * @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()
- ->field($select ?: $this->select);
- // 模型关联
- count($with) > 0 && $query->with($with);
- // 规避 禁用 和 删除 数据
- $this->is_status_search === 1 && $query->where('status', 'normal');
- $this->is_delete_search === 1 && $query->whereNull('deletetime');
- $detail = $query->find();
- if ($detail){
- $detail = json_decode(json_encode($detail),true);
- }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|BaseModel
- */
- protected function catchPages($params)
- {
- $this->query = !empty($this->query) ? $this->query : $this;
- if (empty($params['page']) && empty($params['list_row'])){
- return $this;
- }
- $page = $params['page'] ?? 1;
- $size = $params['list_row'] ?? 15;
- $this->query = $this->query->page($page,$size);
- return $this;
- }
- /**
- * 排序工具
- *
- * @param $orderBy
- * @return $this|BaseModel
- */
- 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->order($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;
- }
- protected function success($message = '', $data = [])
- {
- $this->message = $message;
- $this->data = $data;
- return true;
- }
- protected function error($message = '', $data = [])
- {
- $this->message = $message;
- $this->data = $data;
- return false;
- }
- public function getMessage()
- {
- return $this->message;
- }
- public function getData($name = '')
- {
- return !empty($name) ? $this->data[$name] ?? '' : $this->data;
- }
- }
|