generated from liutk/owl-admin-base
81 lines
1.9 KiB
PHP
81 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Admin;
|
|
|
|
use App\Models\Person;
|
|
use App\Models\Filters\PersonFilter;
|
|
use App\Traits\UploadTrait;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
/**
|
|
* @method Person getModel()
|
|
* @method Person|\Illuminate\Database\Query\Builder query()
|
|
*/
|
|
class PersonService extends BaseService
|
|
{
|
|
use UploadTrait;
|
|
|
|
protected string $modelName = Person::class;
|
|
|
|
protected string $modelFilterName = PersonFilter::class;
|
|
|
|
protected array $withRelationships = ['master'];
|
|
|
|
protected bool $modelSortAble = true;
|
|
|
|
public function query(): Builder
|
|
{
|
|
return $this->modelName::query()->valid()->whereIn('type', [11, 12]);
|
|
}
|
|
|
|
public function store($data): bool
|
|
{
|
|
$columns = $this->getTableColumns();
|
|
$model = $this->getModel();
|
|
|
|
$data['avatar'] = $this->saveImage('avatar', 'persons/avatar')[0] ?? '';
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (!in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
return $model->save();
|
|
}
|
|
|
|
public function update($primaryKey, $data): bool
|
|
{
|
|
$columns = $this->getTableColumns();
|
|
$model = $this->query()->whereKey($primaryKey)->first();
|
|
|
|
if(isset($data['avatar'])){
|
|
$data['avatar'] = $this->saveImage('avatar', 'persons/avatar')[0] ?? '';
|
|
}
|
|
|
|
foreach ($data as $k => $v) {
|
|
if (!in_array($k, $columns)) {
|
|
continue;
|
|
}
|
|
|
|
$model->setAttribute($k, $v);
|
|
}
|
|
|
|
return $model->save();
|
|
}
|
|
|
|
/**
|
|
* 软删除
|
|
*
|
|
* @param string $ids
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function delete(string $ids): mixed
|
|
{
|
|
return $this->query()->whereIn($this->primaryKey(), explode(',', $ids))->update(['valid'=> 0]);
|
|
}
|
|
} |