<?php
declare(strict_types=1);
namespace AppRepositories;
use HyperfDatabaseModelModel;
use HyperfDiAnnotationInject;
use PsrContainerContainerInterface;
/**
* 仓库基类
* Class BaseRepository.
*/
class BaseRepository
{
/**
* @var string
*/
public $connection = 'default';
/**
* @var
*/
public $model;
/**
* @Inject
* @var ContainerInterface
*/
protected $container;
/**
* __get
* 可以实现通过仓库类自定义隐式注入需要注入的服务类 暂时不用.
* @param $key
* @return ContainerInterface|void
*/
public function __get($key)
{
switch ($key) {
case 'app':
return $this->container;
break;
default:
return $this->container;
}
}
/**
* 不存在方法时的处理 适用于模型创建.
* @param $method
* @param $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return make($this->model)->setConnection($this->connection)->getModel($this->model)->{$method}(...$parameters);
}
/**
* 自定义链接.
* @param string $connection
* @return BaseRepository
*/
public function setConnection($connection = 'default')
{
$this->connection = $connection;
return $this;
}
/**
* 自定义模型.
* @param $model
* @return BaseRepository
*/
public function setModel($model)
{
$this->model = $model;
return $this;
}
/**
* 获取详情.
* @param array|string[] $columnArr
* @param array $orderBy
* @return null|array|HyperfDatabaseQueryBuilder|Model|object
*/
public function getFirst(array $filter, array $columnArr = ['*'], $orderBy = [])
{
$qb = make($this->model)->setConnection($this->connection)->query();
$qb = queryFilter($filter, $qb)->select($columnArr);
if ($orderBy && ! empty($orderBy)) {
foreach ($orderBy as $col => $direction) {
$qb = $qb->orderBy($col, $direction);
}
}
$data = $qb->first();
return $data ? $data->toArray() : [];
}
/**
* 获取列表.
* @return array
*/
public function getList(array $filter, array $columnArr = ['*'], int $page = 1, int $pageSize = -1, array $orderBy = [])
{
$qb = make($this->model)->setConnection($this->connection)->query();
$qb = queryFilter($filter, $qb)->select($columnArr);
if ($orderBy && ! 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();
}
/**
* 获取带分页的列表.
* @return array
*/
public function lists(array $filter, array $columnArr = ['*'], int $page = 1, int $pageSize = -1, array $orderBy = [])
{
$qb = make($this->model)->setConnection($this->connection)->query();
$qb = queryFilter($filter, $qb)->select($columnArr);
if ($orderBy && ! 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'],
];
}
/**
* 获取列表--原生
* @param array $data
* @return array|HyperfUtilsCollection
*/
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 ($orderBy && ! 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();
}
/**
* 获取单个值
* @return null|mixed
*/
public function getValue(array $filter, string $column = '*', array $orderBy = [])
{
$qb = make($this->model)->setConnection($this->connection)->query();
$qb = queryFilter($filter, $qb);
if ($orderBy && ! empty($orderBy)) {
foreach ($orderBy as $col => $direction) {
$qb = $qb->orderBy($col, $direction);
}
}
return $qb->value($column);
}
/**
* 获取一列.
* @return HyperfUtilsCollection
*/
public function getPluck(array $filter = [], string $columns = '*', int $page = 1, int $pageSize = -1, array $orderBy = [])
{
$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 ($orderBy && ! empty($orderBy)) {
foreach ($orderBy as $col => $direction) {
$qb = $qb->orderBy($col, $direction);
}
}
return $qb->pluck($columns);
}
/**
* 统计数量.
* @return int
*/
public function count(array $filter)
{
$qb = make($this->model)->setConnection($this->connection)->query();
$qb = queryFilter($filter, $qb);
return $qb->count();
}
/**
* 计算.
* @return int
*/
public function sum(array $filter, string $column)
{
$qb = make($this->model)->setConnection($this->connection)->query();
$qb = queryFilter($filter, $qb);
return $qb->sum($column);
}
/**
* 新增数据 不走model 修改器.
* @param bool $getId
* @return bool|int
*/
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);
}
/**
* 走model修改器.
* @return
*/
public function create(array $data)
{
$qb = make($this->model)->setConnection($this->connection);
return $qb->create($data);
}
/**
* 更新数据.
* @return int
*/
public function updateBy(array $filter, array $data)
{
$qb = make($this->model)->setConnection($this->connection)->query();
$qb = queryFilter($filter, $qb);
return $qb->update($data);
}
/**
* 删除数据.
* @return int|mixed
*/
public function deleteBy(array $filter)
{
$qb = make($this->model)->setConnection($this->connection)->query();
$qb = queryFilter($filter, $qb);
return $qb->delete();
}
/**
* 最大值
* @return mixed
*/
public function max(array $filter, string $column)
{
$qb = make($this->model)->setConnection($this->connection)->query();
$qb = queryFilter($filter, $qb);
return $qb->max($column);
}
}
PHP
Copy