65 lines
1.6 KiB
PHP
65 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\ModelFilters\UserFilter;
|
|
use App\Models\Patient;
|
|
use App\Models\PatientRecord;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class UserService extends BaseService
|
|
{
|
|
protected string $modelName = User::class;
|
|
|
|
protected array $withRelationships = [];
|
|
|
|
protected string $modelFilterName = UserFilter::class;
|
|
|
|
public function listQuery()
|
|
{
|
|
$filter = $this->getModelFilter();
|
|
|
|
$query = $this->query();
|
|
if ($this->withRelationships) {
|
|
$query->with($this->withRelationships);
|
|
}
|
|
|
|
if ($filter) {
|
|
$query->filter(request()->input(), $filter);
|
|
}
|
|
|
|
return $query->sort();
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$phoneRule = Rule::unique('users', 'phone');
|
|
if ($model) {
|
|
$phoneRule->ignore($model->id);
|
|
}
|
|
$validator = Validator::make($data, [
|
|
'phone' => [Rule::requiredIf(!$model), 'phone', $phoneRule],
|
|
], [
|
|
'phone.required' => __('user.phone') . '必填',
|
|
'phone.phone' => __('user.phone') . ' 不是合法手机号',
|
|
'phone.unique' => __('user.phone') . '已经注册',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function preDelete(array $ids)
|
|
{
|
|
// 删除病人记录
|
|
Patient::whereIn('user_id', $ids)->delete();
|
|
// 删除病历记录
|
|
PatientRecord::whereIn('user_id', $ids)->delete();
|
|
return true;
|
|
}
|
|
}
|