generated from liutk/owl-admin-base
174 lines
3.9 KiB
PHP
174 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Slowlyo\OwlAdmin\Services\AdminService;
|
|
|
|
/**
|
|
* @method Region getModel()
|
|
* @method Region|\Illuminate\Database\Query\Builder query()
|
|
*/
|
|
class BaseService extends AdminService
|
|
{
|
|
protected string $modelFilterName = '';
|
|
|
|
protected bool $modelSortAble = false;
|
|
|
|
protected Model $currentModel;
|
|
|
|
public function getCurrentModel()
|
|
{
|
|
return $this->currentModel;
|
|
}
|
|
|
|
public function sortColumn()
|
|
{
|
|
return 'id';
|
|
}
|
|
|
|
public function getTree()
|
|
{
|
|
$list = $this->query()->orderByDesc('sort')->get();
|
|
$minNum = $list->min('parent_id');
|
|
|
|
return array2tree($list->toArray(), $minNum);
|
|
}
|
|
|
|
public function list()
|
|
{
|
|
$query = $this->listQuery();
|
|
if (request()->input('_all')) {
|
|
$list = (clone $query)->get();
|
|
$items = $list->all();
|
|
$total = $list->count();
|
|
} else {
|
|
$list = (clone $query)->paginate(request()->input('perPage', 20));
|
|
$items = $list->items();
|
|
$total = $list->total();
|
|
}
|
|
|
|
return compact('items', 'total');
|
|
}
|
|
|
|
public function getModelFilter()
|
|
{
|
|
return $this->modelFilterName;
|
|
}
|
|
|
|
public function listQuery()
|
|
{
|
|
$query = $this->query();
|
|
|
|
$this->addRelations($query);
|
|
|
|
if ($filter = $this->getModelFilter()) {
|
|
$query->filter(request()->input(), $filter);
|
|
}
|
|
|
|
if ($this->modelSortAble) {
|
|
$query->sort();
|
|
}
|
|
|
|
$this->sortable($query);
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function store($data): bool
|
|
{
|
|
$data = $this->resloveData($data);
|
|
|
|
$validate = $this->validate($data);
|
|
if ($validate !== true) {
|
|
$this->setError($validate);
|
|
|
|
return false;
|
|
}
|
|
|
|
$model = $this->modelName::create($data);
|
|
$this->currentModel = $model;
|
|
|
|
return true;
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$model = $this->query()->whereKey($primaryKey)->firstOrFail();
|
|
$data = $this->resloveData($data, $model);
|
|
$validate = $this->validate($data, $model);
|
|
if ($validate !== true) {
|
|
$this->setError($validate);
|
|
|
|
return false;
|
|
}
|
|
$model->update($data);
|
|
$this->currentModel = $model;
|
|
|
|
return true;
|
|
}
|
|
|
|
public function delete(string $ids): mixed
|
|
{
|
|
$this->preDelete(explode(',', $ids));
|
|
|
|
return parent::delete($ids);
|
|
}
|
|
|
|
/**
|
|
* 处理表单数据
|
|
*
|
|
* @param array $data
|
|
* @param Model $model 空 : 添加, 非空: 修改
|
|
* @return array
|
|
*/
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 表单验证
|
|
*
|
|
* @param array $data
|
|
* @param Model $model 空: 添加, 非空: 修改
|
|
* @return mixed true: 验证通过, string: 错误提示
|
|
*/
|
|
public function validate($data, $model = null)
|
|
{
|
|
// $createRules = [
|
|
// 'key' => ['required', Rule::unique('keywords', 'key')],
|
|
// 'name' => ['required'],
|
|
// ];
|
|
// $updateRules = [
|
|
// 'key' => [Rule::unique('keywords', 'key')->ignore($model->id)]
|
|
// ];
|
|
// $validator = Validator::make($data, $model ? $updateRules : $createRules, [
|
|
// 'key.unique' => ':input 已经存在'
|
|
// ]);
|
|
// if ($validator->fails()) {
|
|
// return $validator->errors()->first();
|
|
// }
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除的前置方法
|
|
*
|
|
* @param array $ids 主键id
|
|
*/
|
|
public function preDelete(array $ids): void
|
|
{
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function addRelations($query, string $scene = 'list')
|
|
{
|
|
if (property_exists($this, 'withRelationships')) {
|
|
$query->with($this->withRelationships);
|
|
}
|
|
}
|
|
}
|