| <?php |
| |
| declare(strict_types=1); |
| |
| |
| |
| |
| |
| |
| namespace App\Repositories; |
| |
| use Hyperf\Di\Annotation\Inject; |
| use Psr\Container\ContainerInterface; |
| |
| |
| |
| |
| |
| class BaseRepository |
| { |
| |
| |
| |
| public $connection = 'default'; |
| |
| |
| |
| |
| public $model; |
| |
| |
| |
| |
| |
| protected $container; |
| |
| |
| |
| |
| |
| |
| |
| public function __get($key) |
| { |
| switch ($key) { |
| case 'app': |
| return $this->container; |
| break; |
| default: |
| return $this->container; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| public function __call($method, $parameters) |
| { |
| return make($this->model)->setConnection($this->connection)->getModel($this->model)->{$method}(...$parameters); |
| } |
| |
| |
| |
| |
| |
| public function setConnection($connection = 'default'): BaseRepository{ |
| $this->connection = $connection; |
| return $this; |
| } |
| |
| |
| |
| |
| |
| |
| public function setModel($model) |
| { |
| $this->model = $model; |
| return $this; |
| } |
| |
| |
| |
| |
| |
| |
| |
| public function getFirst(array $filter, array $columnArr = ['*'], $orderBy = []): array{ |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb)->select($columnArr); |
| if (! empty($orderBy)) { |
| foreach ($orderBy as $col => $direction) { |
| $qb = $qb->orderBy($col, $direction); |
| } |
| } |
| $data = $qb->first(); |
| return $data ? $data->toArray() : []; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getList(array $filter, array $columnArr = ['*'], int $page = 1, int $pageSize = -1, array $orderBy = []): array{ |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb)->select($columnArr); |
| if (! empty($orderBy)) { |
| foreach ($orderBy as $col => $direction) { |
| $qb = $qb->orderBy($col, $direction); |
| } |
| } |
| if ($page > 0 && $pageSize > 0) { |
| $qb = $qb->offset((($page - 1) * $pageSize))->limit($pageSize); |
| } |
| return $qb->get()->toArray(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function lists(array $filter, array $columnArr = ['*'], int $page = 1, int $pageSize = -1, array $orderBy = []): array{ |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb)->select($columnArr); |
| if (! empty($orderBy)) { |
| foreach ($orderBy as $col => $direction) { |
| $qb = $qb->orderBy($col, $direction); |
| } |
| } |
| if ($page > 0 && $pageSize > 0) { |
| $qb = $qb->offset((($page - 1) * $pageSize))->limit($pageSize); |
| } |
| $list = $qb->paginate($pageSize, $columnArr, '', $page) |
| ->toArray(); |
| return [ |
| 'list' => $list['data'], |
| 'total_count' => $list['total'], |
| ]; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function listsBuilder(array $filter, array $columnArr = ['*'], int $page = 1, int $pageSize = -1, array $orderBy = []): array{ |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryBuilder($filter, $qb)->select($columnArr); |
| if (! empty($orderBy)) { |
| foreach ($orderBy as $col => $direction) { |
| $qb = $qb->orderBy($col, $direction); |
| } |
| } |
| if ($page > 0 && $pageSize > 0) { |
| $qb = $qb->offset((($page - 1) * $pageSize))->limit($pageSize); |
| } |
| $list = $qb->paginate($pageSize, $columnArr, '', $page) |
| ->toArray(); |
| return [ |
| 'list' => $list['data'], |
| 'total_count' => $list['total'], |
| ]; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getListRaw(array $filter, string $columns = '*', int $page = 1, int $pageSize = -1, array $orderBy = [], $data = []) |
| { |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb); |
| if ($page > 0 && $pageSize > 0) { |
| $qb->offset((($page - 1) * $pageSize))->limit($pageSize); |
| } |
| |
| if (! empty($orderBy)) { |
| foreach ($orderBy as $col => $direction) { |
| $qb = $qb->orderBy($col, $direction); |
| } |
| } |
| $data = $qb->selectRaw($columns, $data)->get(); |
| return is_array($data) ? $data : $data->toArray(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getValue(array $filter, string $column = '*', array $orderBy = []) |
| { |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb); |
| if (! empty($orderBy)) { |
| foreach ($orderBy as $col => $direction) { |
| $qb = $qb->orderBy($col, $direction); |
| } |
| } |
| return $qb->value($column); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function getPluck(array $filter = [], string $columns = '*', int $page = 1, int $pageSize = -1, array $orderBy = []): \Hyperf\Utils\Collection{ |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb); |
| if ($page > 0 && $pageSize > 0) { |
| $qb->offset((($page - 1) * $pageSize))->limit($pageSize); |
| } |
| if (! empty($orderBy)) { |
| foreach ($orderBy as $col => $direction) { |
| $qb = $qb->orderBy($col, $direction); |
| } |
| } |
| return $qb->pluck($columns); |
| } |
| |
| |
| |
| |
| |
| public function count(array $filter): int{ |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb); |
| return $qb->count(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public function sum(array $filter, string $column) |
| { |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb); |
| return $qb->sum($column); |
| } |
| |
| |
| |
| |
| |
| |
| public function insert(array $data, $getId = false) |
| { |
| if ($getId) { |
| return make($this->model)->setConnection($this->connection)->insertGetId($data); |
| } |
| return make($this->model)->setConnection($this->connection)->insert($data); |
| } |
| |
| |
| |
| |
| |
| public function create(array $data) |
| { |
| $qb = make($this->model)->setConnection($this->connection); |
| return $qb->create($data); |
| } |
| |
| |
| |
| |
| |
| |
| public function updateBy(array $filter, array $data): int{ |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb); |
| return $qb->update($data); |
| } |
| |
| |
| |
| |
| |
| |
| public function deleteBy(array $filter) |
| { |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb); |
| return $qb->delete(); |
| } |
| |
| |
| |
| |
| |
| |
| |
| public function max(array $filter, string $column) |
| { |
| $qb = make($this->model)->setConnection($this->connection)->query(); |
| $qb = queryFilter($filter, $qb); |
| return $qb->max($column); |
| } |
| } |