2020-01-13 16:22:16 +08:00
|
|
|
<?php
|
2020-11-29 09:29:14 +08:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-01-13 16:22:16 +08:00
|
|
|
namespace catcher;
|
|
|
|
|
2020-01-21 17:56:28 +08:00
|
|
|
use catcher\base\CatchModel;
|
2020-01-13 16:22:16 +08:00
|
|
|
use think\db\Query;
|
2020-05-26 22:39:06 +08:00
|
|
|
use think\helper\Str;
|
2020-01-21 17:56:28 +08:00
|
|
|
use think\Paginator;
|
2020-01-13 16:22:16 +08:00
|
|
|
|
|
|
|
class CatchQuery extends Query
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月13日
|
2020-12-13 10:16:28 +08:00
|
|
|
* @param mixed $model
|
2020-01-13 16:22:16 +08:00
|
|
|
* @param string $joinField
|
|
|
|
* @param string $currentJoinField
|
2020-01-13 21:24:27 +08:00
|
|
|
* @param array $field
|
2020-01-13 16:22:16 +08:00
|
|
|
* @param string $type
|
|
|
|
* @param array $bind
|
|
|
|
* @return CatchQuery
|
|
|
|
*/
|
2020-12-13 10:16:28 +08:00
|
|
|
public function catchJoin($model, string $joinField, string $currentJoinField, array $field = [], string $type = 'INNER', array $bind = []): CatchQuery
|
2020-01-13 16:22:16 +08:00
|
|
|
{
|
2020-12-13 10:16:28 +08:00
|
|
|
$tableAlias = null;
|
|
|
|
|
|
|
|
if (is_string($model)) {
|
|
|
|
$table = app($model)->getTable();
|
|
|
|
} else {
|
|
|
|
list($model, $tableAlias) = $model;
|
|
|
|
$table = app($model)->getTable();
|
|
|
|
}
|
2020-01-13 16:22:16 +08:00
|
|
|
|
2020-01-13 21:24:27 +08:00
|
|
|
// 合并字段
|
2020-12-13 10:16:28 +08:00
|
|
|
$this->options['field'] = array_merge($this->options['field'] ?? [], array_map(function ($value) use ($table, $tableAlias) {
|
|
|
|
return ($tableAlias ? : $table) . '.' . $value;
|
2020-01-13 21:24:27 +08:00
|
|
|
}, $field));
|
|
|
|
|
2020-12-13 10:16:28 +08:00
|
|
|
return $this->join($tableAlias ? sprintf('%s %s', $table, $tableAlias) : $table
|
|
|
|
|
|
|
|
, sprintf('%s.%s=%s.%s', $tableAlias ? $tableAlias : $table, $joinField, $this->getAlias(), $currentJoinField), $type, $bind);
|
2020-01-13 16:22:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月13日
|
2020-12-13 10:16:28 +08:00
|
|
|
* @param mixed $model
|
2020-01-13 16:22:16 +08:00
|
|
|
* @param string $joinField
|
|
|
|
* @param string $currentJoinField
|
2020-01-13 21:24:27 +08:00
|
|
|
* @param array $field
|
2020-01-13 16:22:16 +08:00
|
|
|
* @param array $bind
|
|
|
|
* @return CatchQuery
|
|
|
|
*/
|
2020-12-13 10:16:28 +08:00
|
|
|
public function catchLeftJoin($model, string $joinField, string $currentJoinField, array $field = [], array $bind = []): CatchQuery
|
2020-01-13 16:22:16 +08:00
|
|
|
{
|
2020-01-13 21:24:27 +08:00
|
|
|
return $this->catchJoin($model, $joinField, $currentJoinField, $field,'LEFT', $bind);
|
2020-01-13 16:22:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-13 21:24:27 +08:00
|
|
|
*
|
2020-01-13 16:22:16 +08:00
|
|
|
* @time 2020年01月13日
|
2020-12-13 10:16:28 +08:00
|
|
|
* @param mixed $model
|
2020-01-13 16:22:16 +08:00
|
|
|
* @param string $joinField
|
|
|
|
* @param string $currentJoinField
|
2020-01-13 21:24:27 +08:00
|
|
|
* @param array $field
|
2020-01-13 16:22:16 +08:00
|
|
|
* @param array $bind
|
|
|
|
* @return CatchQuery
|
|
|
|
*/
|
2020-12-13 10:16:28 +08:00
|
|
|
public function catchRightJoin($model, string $joinField, string $currentJoinField, array $field = [], array $bind = []): CatchQuery
|
2020-01-13 21:24:27 +08:00
|
|
|
{
|
|
|
|
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)) {
|
2020-05-25 14:30:28 +08:00
|
|
|
return $this;
|
2020-01-13 21:24:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_string($field)) {
|
2020-05-25 14:30:28 +08:00
|
|
|
$field = array_map('trim', explode(',', $field));
|
2020-01-13 21:24:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 过滤软删除字段
|
|
|
|
$field[] = $this->model->getDeleteAtField();
|
|
|
|
|
|
|
|
// 字段排除
|
|
|
|
$fields = $this->getTableFields();
|
|
|
|
$field = $fields ? array_diff($fields, $field) : $field;
|
|
|
|
|
|
|
|
if (isset($this->options['field'])) {
|
2020-05-25 14:30:28 +08:00
|
|
|
$field = array_merge((array) $this->options['field'], $field);
|
2020-01-13 21:24:27 +08:00
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:07 +08:00
|
|
|
$this->options['field'] = array_unique($field);
|
|
|
|
|
2020-01-13 21:24:27 +08:00
|
|
|
if ($needAlias) {
|
2020-05-25 14:30:28 +08:00
|
|
|
$alias = $this->getAlias();
|
|
|
|
$this->options['field'] = array_map(function ($field) use ($alias) {
|
2020-01-13 21:24:27 +08:00
|
|
|
return $alias . '.' . $field;
|
2020-01-13 21:40:07 +08:00
|
|
|
}, $this->options['field']);
|
2020-01-13 21:24:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-05-26 09:32:52 +08:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @time 2020年01月13日
|
|
|
|
* @param array $params
|
|
|
|
* @return CatchQuery
|
|
|
|
*/
|
|
|
|
public function catchSearch($params = []): CatchQuery
|
2020-01-13 21:24:27 +08:00
|
|
|
{
|
2020-05-26 09:32:52 +08:00
|
|
|
$params = empty($params) ? \request()->param() : $params;
|
2020-01-13 21:24:27 +08:00
|
|
|
|
2020-04-24 14:31:46 +08:00
|
|
|
if (empty($params)) {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-05-26 22:39:06 +08:00
|
|
|
foreach ($params as $field => $value) {
|
|
|
|
$method = 'search' . Str::studly($field) . 'Attr';
|
2020-11-21 12:33:55 +08:00
|
|
|
// value in [null, '']
|
|
|
|
if ($value !== null && $value !== '' && method_exists($this->model, $method)) {
|
2020-09-02 20:46:30 +08:00
|
|
|
$this->model->$method($this, $value, $params);
|
2020-05-26 22:39:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
2020-01-13 21:24:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2020-01-20 11:46:32 +08:00
|
|
|
public function whereLike(string $field, $condition, string $logic = 'AND', $option = 'both'): Query
|
2020-01-13 16:22:16 +08:00
|
|
|
{
|
2020-01-13 21:24:27 +08:00
|
|
|
switch ($option) {
|
|
|
|
case 'both':
|
|
|
|
$condition = '%' . $condition . '%';
|
|
|
|
break;
|
|
|
|
case 'left':
|
|
|
|
$condition = '%' . $condition;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$condition .= '%';
|
|
|
|
}
|
|
|
|
|
2020-09-16 10:07:34 +08:00
|
|
|
if (strpos($field, '.') === false) {
|
|
|
|
$field = $this->getAlias() . '.' . $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::whereLike($field, $condition, $logic);
|
2020-01-13 16:22:16 +08:00
|
|
|
}
|
2020-01-13 21:40:07 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 额外的字段
|
|
|
|
*
|
|
|
|
* @time 2020年01月13日
|
2020-06-17 16:19:21 +08:00
|
|
|
* @param $fields
|
2020-01-13 21:40:07 +08:00
|
|
|
* @return CatchQuery
|
|
|
|
*/
|
2020-06-17 16:19:21 +08:00
|
|
|
public function addFields($fields): CatchQuery
|
2020-01-13 21:40:07 +08:00
|
|
|
{
|
2020-06-17 16:19:21 +08:00
|
|
|
if (is_string($fields)) {
|
|
|
|
$this->options['field'][] = $fields;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-01-13 21:40:07 +08:00
|
|
|
$this->options['field'] = array_merge($this->options['field'], $fields);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-01-21 17:56:28 +08:00
|
|
|
|
|
|
|
public function paginate($listRows = null, $simple = false): Paginator
|
|
|
|
{
|
|
|
|
if (!$listRows) {
|
|
|
|
$limit = \request()->param('limit');
|
|
|
|
|
|
|
|
$listRows = $limit ? : CatchModel::LIMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::paginate($listRows, $simple); // TODO: Change the autogenerated stub
|
|
|
|
}
|
2020-04-30 17:58:36 +08:00
|
|
|
|
2020-06-17 13:23:43 +08:00
|
|
|
|
2020-04-30 17:58:36 +08:00
|
|
|
/**
|
2020-06-17 13:23:43 +08:00
|
|
|
* 默认排序
|
2020-04-30 17:58:36 +08:00
|
|
|
*
|
2020-06-17 13:23:43 +08:00
|
|
|
* @time 2020年06月17日
|
2020-04-30 17:58:36 +08:00
|
|
|
* @param string $order
|
2020-06-17 13:23:43 +08:00
|
|
|
* @return $this
|
2020-04-30 17:58:36 +08:00
|
|
|
*/
|
2020-06-17 13:23:43 +08:00
|
|
|
public function catchOrder($order = 'desc')
|
2020-04-30 17:58:36 +08:00
|
|
|
{
|
2020-05-25 14:30:28 +08:00
|
|
|
if (in_array('sort', array_keys($this->getFields()))) {
|
2020-06-17 13:23:43 +08:00
|
|
|
$this->order($this->getTable() . '.sort', $order);
|
2020-04-30 17:58:36 +08:00
|
|
|
}
|
|
|
|
|
2020-06-17 13:23:43 +08:00
|
|
|
$this->order($this->getTable() . '.' . $this->getPk(), $order);
|
2020-04-30 17:58:36 +08:00
|
|
|
|
2020-06-17 13:23:43 +08:00
|
|
|
return $this;
|
2020-04-30 17:58:36 +08:00
|
|
|
}
|
2020-06-17 16:19:21 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 新增 Select 子查询
|
|
|
|
*
|
|
|
|
* @time 2020年06月17日
|
|
|
|
* @param callable $callable
|
|
|
|
* @param string $as
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function addSelectSub(callable $callable, string $as)
|
|
|
|
{
|
2020-06-22 07:55:39 +08:00
|
|
|
$this->field(sprintf('%s as %s', $callable()->buildSql(), $as));
|
2020-06-17 16:19:21 +08:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-11-04 14:09:23 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 字段增加
|
|
|
|
*
|
|
|
|
* @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();
|
|
|
|
}
|
2020-01-13 16:22:16 +08:00
|
|
|
}
|