90 lines
2.4 KiB
PHP
90 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\ModelFilters\PatientFilter;
|
|
use Illuminate\Validation\Rule;
|
|
use App\Models\{Patient, PatientRecord};
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class PatientService extends BaseService
|
|
{
|
|
protected string $modelName = Patient::class;
|
|
|
|
protected array $withRelationships = ['doctor', 'inviter', 'saler', 'type', 'user'];
|
|
|
|
protected string $modelFilterName = PatientFilter::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();
|
|
}
|
|
|
|
/**
|
|
* 处理表单数据
|
|
*
|
|
* @param array $data
|
|
* @return array
|
|
*/
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
if ($images = data_get($data, 'images')) {
|
|
$data['images'] = is_array($images) ? $images : explode(',', $images);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 表单验证
|
|
*
|
|
* @param array $data
|
|
* @param mixed $model 空: 添加, 非空: 修改
|
|
* @return mixed true: 验证通过, string: 错误提示
|
|
*/
|
|
public function validate($data, $model = null)
|
|
{
|
|
$userRule = Rule::unique('patients', 'user_id');
|
|
$createRule = [
|
|
'type_id' => 'required',
|
|
'name' => 'required',
|
|
'user_id' => $userRule->where('type_id', data_get($data, 'type_id'))
|
|
];
|
|
$updateRule = [
|
|
'user_id' => $userRule->where('type_id', data_get($data, 'type_id', $model?->type_id))
|
|
];
|
|
$validator = Validator::make($data, $model ? $updateRule : $createRule, [
|
|
'type_id.required' => __('patient.type_id') . '必填',
|
|
'name.required' => __('patient.name') . '必填',
|
|
'user_id.unique' => __('patient.user_id') . '已经存在',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除的前置方法
|
|
*
|
|
* @param array $ids 主键id
|
|
* @return mixed true: 继续后续操作, string: 中断操作, 返回错误提示
|
|
*/
|
|
public function preDelete(array $ids)
|
|
{
|
|
// 删除就诊记录
|
|
PatientRecord::whereIn('patient_id', $ids)->delete();
|
|
return true;
|
|
}
|
|
}
|