127 lines
5.3 KiB
PHP
127 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Services;
|
|
|
|
use App\ModelFilters\PatientRecordFilter;
|
|
use App\Models\Keyword;
|
|
use App\Models\Patient;
|
|
use App\Models\PatientRecord;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Slowlyo\OwlAdmin\Admin;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class PatientRecordService extends BaseService
|
|
{
|
|
protected string $modelName = PatientRecord::class;
|
|
|
|
protected array $withRelationships = ['doctor', 'patient', 'type', 'creator', 'notifyUser', 'inviter', 'saler', 'illnessType'];
|
|
|
|
protected string $modelFilterName = PatientRecordFilter::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 list()
|
|
{
|
|
$query = $this->listQuery();
|
|
|
|
$list = $query->clone()->paginate(request()->input('perPage', 20));
|
|
$items = $list->items();
|
|
$total = $list->total();
|
|
|
|
$sell_price = round($query->clone()->sum('sell_price'), 2, PHP_ROUND_HALF_DOWN);
|
|
$doctor_money = round($query->clone()->sum('doctor_money'), 2, PHP_ROUND_HALF_DOWN);
|
|
$inviter_money = round($query->clone()->sum('inviter_money'), 2, PHP_ROUND_HALF_DOWN);
|
|
$saler_money = round($query->clone()->sum('saler_money'), 2, PHP_ROUND_HALF_DOWN);
|
|
$total_money = round($doctor_money + $inviter_money + $saler_money, 2, PHP_ROUND_HALF_DOWN);
|
|
|
|
return compact('items', 'total', 'sell_price', 'doctor_money', 'inviter_money', 'saler_money', 'total_money');
|
|
}
|
|
|
|
public function resloveData($data, $model = null)
|
|
{
|
|
if ($images = data_get($data, 'images')) {
|
|
$data['images'] = is_array($images) ? $images : explode(',', $images);
|
|
}
|
|
if (!$model) {
|
|
$data['is_notified'] = data_get($data, 'is_notified', 1);
|
|
$data['creator_id'] = Admin::user()->id;
|
|
$patient = Patient::findOrFail(data_get($data, 'patient_id'));
|
|
$type = $patient->type;
|
|
$data['user_id'] = $patient->user_id;
|
|
$data['type_id'] = $patient->type_id;
|
|
$data['saler_id'] = $patient->saler_id;
|
|
$data['inviter_id'] = $patient->inviter_id;
|
|
if (data_get($data, 'doctor_id')) {
|
|
$data['doctor_ratio'] = data_get($type->options, 'doctor_ratio', 0);
|
|
$data['doctor_money'] = floor($data['sell_price'] * $data['doctor_ratio']) / 100;
|
|
}
|
|
if (data_get($data, 'inviter_id')) {
|
|
$data['inviter_ratio'] = data_get($type->options, 'inviter_ratio', 0);
|
|
$data['inviter_money'] = floor($data['sell_price'] * $data['inviter_ratio']) / 100;
|
|
}
|
|
if (data_get($data, 'saler_id')) {
|
|
$data['saler_ratio'] = data_get($type->options, 'saler_ratio', 0);
|
|
$data['saler_money'] = floor($data['sell_price'] * $data['saler_ratio']) / 100;
|
|
}
|
|
} else {
|
|
if (data_get($data, 'sell_price')) {
|
|
$data['doctor_money'] = floor($data['sell_price'] * $model->doctor_ratio) / 100;
|
|
$data['inviter_money'] = floor($data['sell_price'] * $model->inviter_ratio) / 100;
|
|
$data['saler_money'] = floor($data['sell_price'] * $model->saler_ratio) / 100;
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function validate($data, $model = null)
|
|
{
|
|
$createRules = [
|
|
'patient_id' => 'required',
|
|
'illness_type_id' => 'required',
|
|
'treat_at' => 'required',
|
|
'doctor_id' => 'required',
|
|
'origin_price' => ['required', 'decimal:0,2'],
|
|
'sell_price' => ['required', 'decimal:0,2'],
|
|
'order_status' => 'required',
|
|
'next_treat_at' => [Rule::requiredIf(fn () => data_get($data, 'is_notified') == 0)],
|
|
'notify_at' => [Rule::requiredIf(fn () => data_get($data, 'is_notified') == 0)],
|
|
'notify_user_id' => [Rule::requiredIf(fn () => data_get($data, 'is_notified') == 0)],
|
|
];
|
|
$updateRules = [
|
|
'origin_price' => 'decimal:0,2',
|
|
'sell_price' => 'decimal:0,2',
|
|
];
|
|
$validator = Validator::make($data, $model ? $updateRules : $createRules, [
|
|
'patient_id.required' => '请选择病人',
|
|
'treat_at.required' => '请选择诊疗时间',
|
|
'origin_price.required' => __('patient-record.origin_price') . '必填',
|
|
'origin_price.decimal' => __('patient-record.origin_price') . '保留2位小数',
|
|
'sell_price.required' => __('patient-record.sell_price') . '必填',
|
|
'sell_price.decimal' => __('patient-record.sell_price') . '必填',
|
|
'notify_at.required' => __('patient-record.notify_at') . '必填',
|
|
'notify_user_id.required' => __('patient-record.notify_user_id') . '必填',
|
|
'next_treat_at.required' => __('patient-record.next_treat_at') . '必填',
|
|
'illness_type_id.required' => __('patient-record.illness_type_id') . '必填',
|
|
]);
|
|
if ($validator->fails()) {
|
|
return $validator->errors()->first();
|
|
}
|
|
return true;
|
|
}
|
|
}
|