123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- declare(strict_types=1);
- namespace catcher;
- use catcher\base\CatchModel;
- use think\db\Query;
- use think\helper\Str;
- use think\Paginator;
- class CatchQuery extends Query
- {
- /**
- *
- * @time 2020年01月13日
- * @param string $model
- * @param string $joinField
- * @param string $currentJoinField
- * @param array $field
- * @param string $type
- * @param array $bind
- * @return CatchQuery
- */
- public function catchJoin(string $model, string $joinField, string $currentJoinField, array $field = [], string $type = 'INNER', array $bind = []): CatchQuery
- {
- $table = app($model)->getTable();
- // 合并字段
- $this->options['field'] = array_merge($this->options['field'] ?? [], array_map(function ($value) use ($table) {
- return $table . '.' . $value;
- }, $field));
- return $this->join($table, sprintf('%s.%s=%s.%s', $table, $joinField, $this->getAlias(), $currentJoinField), $type, $bind);
- }
- /**
- *
- * @time 2020年01月13日
- * @param string $model
- * @param string $joinField
- * @param string $currentJoinField
- * @param array $field
- * @param array $bind
- * @return CatchQuery
- */
- public function catchLeftJoin(string $model, string $joinField, string $currentJoinField, array $field = [], array $bind = []): CatchQuery
- {
- return $this->catchJoin($model, $joinField, $currentJoinField, $field,'LEFT', $bind);
- }
- /**
- *
- * @time 2020年01月13日
- * @param string $model
- * @param string $joinField
- * @param string $currentJoinField
- * @param array $field
- * @param array $bind
- * @return CatchQuery
- */
- public function catchRightJoin(string $model, string $joinField, string $currentJoinField, array $field = [], array $bind = []): CatchQuery
- {
- return $this->catchJoin($model, $joinField, $currentJoinField, $field,'RIGHT', $bind);
- }
- /**
- * rewrite
- *
- * @time 2020年01月13日
- * @param array|string $field
- * @param bool $needAlias
- * @return $this|Query
- */
- public function withoutField($field, $needAlias = false)
- {
- if (empty($field)) {
- return $this;
- }
- if (is_string($field)) {
- $field = array_map('trim', explode(',', $field));
- }
- // 过滤软删除字段
- $field[] = $this->model->getDeleteAtField();
- // 字段排除
- $fields = $this->getTableFields();
- $field = $fields ? array_diff($fields, $field) : $field;
- if (isset($this->options['field'])) {
- $field = array_merge((array) $this->options['field'], $field);
- }
- $this->options['field'] = array_unique($field);
- if ($needAlias) {
- $alias = $this->getAlias();
- $this->options['field'] = array_map(function ($field) use ($alias) {
- return $alias . '.' . $field;
- }, $this->options['field']);
- }
- return $this;
- }
- /**
- *
- * @time 2020年01月13日
- * @param array $params
- * @return CatchQuery
- */
- public function catchSearch($params = []): CatchQuery
- {
- $params = empty($params) ? \request()->param() : $params;
-
- if (empty($params)) {
- return $this;
- }
- foreach ($params as $field => $value) {
- $method = 'search' . Str::studly($field) . 'Attr';
- // value in [null, '']
- if ($value !== null && $value !== '' && method_exists($this->model, $method)) {
- $this->model->$method($this, $value, $params);
- }
- }
- return $this;
- }
- /**
- * 追加查询条件
- */
- public function appendQueryCond(Array $conds)
- {
- if (!empty($conds)) {
- // 特殊快捷操作
- $arr = [
- 'whereExp','whereNull','whereNotNull','whereLike','whereNotLike',
- 'whereIn','whereNotIn','whereBetween','whereNotBetween',
- 'whereBetweenTime','whereNotBetweenTime','whereOr',
- 'whereYear','whereMonth','whereDay',
- ];
- foreach($conds as $operate => $exps) {
- if (!in_array($operate, $arr) || !is_array($exps)) {
- $this->$operate($exps);
- continue;
- }
- foreach($exps as $exp) {
- $this->$operate(...$exp);
- }
- }
- }
- return $this;
- }
- /**
- *
- * @time 2020年01月13日
- * @return mixed
- */
- public function getAlias()
- {
- return isset($this->options['alias']) ? $this->options['alias'][$this->getTable()] : $this->getTable();
- }
- /**
- * rewrite
- *
- * @time 2020年01月13日
- * @param string $field
- * @param mixed $condition
- * @param string $option
- * @param string $logic
- * @return Query
- */
- public function whereLike(string $field, $condition, string $logic = 'AND', $option = 'both'): Query
- {
- switch ($option) {
- case 'both':
- $condition = '%' . $condition . '%';
- break;
- case 'left':
- $condition = '%' . $condition;
- break;
- default:
- $condition .= '%';
- }
- if (strpos($field, '.') === false) {
- $field = $this->getAlias() . '.' . $field;
- }
- return parent::whereLike($field, $condition, $logic);
- }
- /**
- * 额外的字段
- *
- * @time 2020年01月13日
- * @param $fields
- * @return CatchQuery
- */
- public function addFields($fields): CatchQuery
- {
- if (is_string($fields)) {
- $this->options['field'][] = $fields;
- return $this;
- }
- $this->options['field'] = array_merge($this->options['field'], $fields);
- return $this;
- }
- public function paginate($listRows = null, $simple = false): Paginator
- {
- if (!$listRows) {
- $limit = \request()->param('limit');
- $listRows = $limit ? : CatchModel::LIMIT;
- }
- if(gettype($listRows)=='string'){
- $listRows=(int)$listRows;
- }
-
- return parent::paginate($listRows, $simple); // TODO: Change the autogenerated stub
- }
- /**
- * 默认排序
- *
- * @time 2020年06月17日
- * @param string $order
- * @return $this
- */
- public function catchOrder($order = 'desc')
- {
- if (in_array('sort', array_keys($this->getFields()))) {
- $this->order($this->getTable() . '.sort', $order);
- }
- $this->order($this->getTable() . '.' . $this->getPk(), $order);
- return $this;
- }
- /**
- * 新增 Select 子查询
- *
- * @time 2020年06月17日
- * @param callable $callable
- * @param string $as
- * @return $this
- */
- public function addSelectSub(callable $callable, string $as)
- {
- $this->field(sprintf('%s as %s', $callable()->buildSql(), $as));
- return $this;
- }
- /**
- * 字段增加
- *
- * @time 2020年11月04日
- * @param $field
- * @param int $amount
- * @throws \think\db\exception\DbException
- * @return int
- */
- public function increment($field, $amount = 1)
- {
- return $this->inc($field, $amount)->update();
- }
- /**
- * 字段减少
- *
- * @time 2020年11月04日
- * @param $field
- * @param int $amount
- * @throws \think\db\exception\DbException
- * @return int
- */
- public function decrement($field, $amount = 1)
- {
- return $this->dec($field, $amount)->update();
- }
- }
|