更新
This commit is contained in:
@@ -248,20 +248,47 @@ class PrescriptionLogic
|
||||
/**
|
||||
* 添加处方
|
||||
*/
|
||||
public static function add(array $params, int $adminId): ?int
|
||||
{
|
||||
// 如果没有诊单ID,允许直接创建处方模板
|
||||
if (!empty($params['diagnosis_id'])) {
|
||||
$diagnosis = Diagnosis::find($params['diagnosis_id']);
|
||||
if (!$diagnosis) {
|
||||
self::setError('诊单不存在');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$dateYmd = self::normalizePrescriptionDate($params['prescription_date'] ?? date('Y-m-d'));
|
||||
$diagnosisIdRule = (int) ($params['diagnosis_id'] ?? 0);
|
||||
if ($diagnosisIdRule > 0 && !self::assertUniquePrescriptionPerDiagnosisDay($diagnosisIdRule, $adminId, $dateYmd, null)) {
|
||||
public static function add(array $params, int $adminId, array $adminInfo): ?int
|
||||
{
|
||||
self::setError('');
|
||||
$diagnosis = null;
|
||||
$authoritativeCaseRecord = null;
|
||||
$authoritativeAppointmentId = 0;
|
||||
$diagnosisIdRule = (int) ($params['diagnosis_id'] ?? 0);
|
||||
if ($diagnosisIdRule > 0) {
|
||||
if (!DiagnosisLogic::canManageDiagnosis($diagnosisIdRule, $adminId, $adminInfo)) {
|
||||
self::setError('诊单不存在或无权访问');
|
||||
return null;
|
||||
}
|
||||
$diagnosis = Diagnosis::where('id', $diagnosisIdRule)
|
||||
->whereNull('delete_time')
|
||||
->find();
|
||||
if (!$diagnosis) {
|
||||
self::setError('诊单不存在或无权访问');
|
||||
return null;
|
||||
}
|
||||
|
||||
$requestedAppointmentId = (int) ($params['appointment_id'] ?? 0);
|
||||
if ($requestedAppointmentId > 0) {
|
||||
$appointment = Appointment::where('id', $requestedAppointmentId)
|
||||
// 历史数据中 appointment.patient_id 指向诊单主键。
|
||||
->where('patient_id', $diagnosisIdRule)
|
||||
->find();
|
||||
if (!$appointment) {
|
||||
self::setError('预约与诊单不一致');
|
||||
return null;
|
||||
}
|
||||
$authoritativeAppointmentId = $requestedAppointmentId;
|
||||
}
|
||||
$authoritativeCaseRecord = DiagnosisLogic::detail(['id' => $diagnosisIdRule], $adminInfo);
|
||||
if (!is_array($authoritativeCaseRecord) || $authoritativeCaseRecord === []) {
|
||||
self::setError('诊单病历暂时无法读取');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
$dateYmd = self::normalizePrescriptionDate($params['prescription_date'] ?? date('Y-m-d'));
|
||||
if ($diagnosisIdRule > 0 && !self::assertUniquePrescriptionPerDiagnosisDay($diagnosisIdRule, $adminId, $dateYmd, null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -288,7 +315,12 @@ class PrescriptionLogic
|
||||
$assistantIdForRx = (int) ($diagAssistant ?? 0);
|
||||
}
|
||||
|
||||
$data = [
|
||||
$doctorName = trim((string) (Admin::where('id', $adminId)->value('name') ?? ''));
|
||||
if ($doctorName === '') {
|
||||
self::setError('当前账号未配置医师姓名,无法开方');
|
||||
return null;
|
||||
}
|
||||
$data = [
|
||||
'sn' => $sn,
|
||||
'prescription_name' => $params['prescription_name'] ?? '',
|
||||
'prescription_type' => $params['prescription_type'] ?? '浓缩水丸',
|
||||
@@ -298,12 +330,24 @@ class PrescriptionLogic
|
||||
'need_decoction' => (int)($params['need_decoction'] ?? 0),
|
||||
'bags_per_dose' => isset($params['bags_per_dose']) ? (int)$params['bags_per_dose'] : 1,
|
||||
'diagnosis_id' => (int)($params['diagnosis_id'] ?? 0),
|
||||
'appointment_id' => (int)($params['appointment_id'] ?? 0),
|
||||
'patient_id' => (int)($params['patient_id'] ?? 0),
|
||||
'patient_name' => $params['patient_name'] ?? '',
|
||||
'gender' => (int)($params['gender'] ?? 1),
|
||||
'age' => (int)($params['age'] ?? 0),
|
||||
'phone' => $params['phone'] ?? '',
|
||||
'appointment_id' => $diagnosis !== null
|
||||
? $authoritativeAppointmentId
|
||||
: (int) ($params['appointment_id'] ?? 0),
|
||||
'patient_id' => $diagnosis !== null
|
||||
? (int) ($diagnosis->patient_id ?? 0)
|
||||
: (int) ($params['patient_id'] ?? 0),
|
||||
'patient_name' => $diagnosis !== null
|
||||
? (string) ($diagnosis->patient_name ?? '')
|
||||
: (string) ($params['patient_name'] ?? ''),
|
||||
'gender' => $diagnosis !== null
|
||||
? (int) ($diagnosis->gender ?? 1)
|
||||
: (int) ($params['gender'] ?? 1),
|
||||
'age' => $diagnosis !== null
|
||||
? (int) ($diagnosis->age ?? 0)
|
||||
: (int) ($params['age'] ?? 0),
|
||||
'phone' => $diagnosis !== null
|
||||
? (string) ($diagnosis->phone ?? '')
|
||||
: (string) ($params['phone'] ?? ''),
|
||||
'visit_no' => $params['visit_no'] ?? $sn,
|
||||
'prescription_date' => $dateYmd,
|
||||
'pulse' => $params['pulse'] ?? '',
|
||||
@@ -311,7 +355,9 @@ class PrescriptionLogic
|
||||
'tongue' => $params['tongue'] ?? '',
|
||||
'tongue_image' => $params['tongue_image'] ?? '',
|
||||
'clinical_diagnosis' => $params['clinical_diagnosis'] ?? '',
|
||||
'case_record' => $params['case_record'] ?? null,
|
||||
'case_record' => $diagnosis !== null
|
||||
? $authoritativeCaseRecord
|
||||
: ($params['case_record'] ?? null),
|
||||
'herbs' => $herbs,
|
||||
'dose_count' => (int)($params['dose_count'] ?? 1),
|
||||
'dose_unit' => $params['dose_unit'] ?? '剂',
|
||||
@@ -324,7 +370,7 @@ class PrescriptionLogic
|
||||
'dietary_taboo' => is_array($params['dietary_taboo'] ?? null) ? implode(',', $params['dietary_taboo']) : ($params['dietary_taboo'] ?? ''),
|
||||
'usage_notes' => $params['usage_notes'] ?? '',
|
||||
'amount' => (float)($params['amount'] ?? 0),
|
||||
'doctor_name' => $params['doctor_name'] ?? '',
|
||||
'doctor_name' => $doctorName,
|
||||
'doctor_signature' => $params['doctor_signature'] ?? '',
|
||||
'template_id' => (int)($params['template_id'] ?? 0),
|
||||
'is_shared' => (int)($params['is_shared'] ?? 0),
|
||||
@@ -405,11 +451,17 @@ class PrescriptionLogic
|
||||
}
|
||||
}
|
||||
|
||||
// 检查权限:只有创建者或共享的处方才能编辑
|
||||
if ($prescription->creator_id != $adminId && $prescription->is_shared != 1) {
|
||||
self::setError('无权限编辑此处方');
|
||||
return false;
|
||||
}
|
||||
// 共享只扩大只读范围,不能扩大写权限。
|
||||
if ((int) $prescription->creator_id !== $adminId) {
|
||||
self::setError('无权限编辑此处方');
|
||||
return false;
|
||||
}
|
||||
|
||||
$requestedDiagnosisId = (int) ($params['diagnosis_id'] ?? $prescription->diagnosis_id);
|
||||
if ($requestedDiagnosisId !== (int) $prescription->diagnosis_id) {
|
||||
self::setError('处方不允许改绑其他诊单');
|
||||
return false;
|
||||
}
|
||||
|
||||
$herbs = $params['herbs'] ?? [];
|
||||
if (empty($herbs) || !is_array($herbs)) {
|
||||
@@ -419,7 +471,7 @@ class PrescriptionLogic
|
||||
|
||||
$herbs = self::normalizeHerbIdentities($herbs);
|
||||
|
||||
$newDiagnosisId = (int) ($params['diagnosis_id'] ?? $prescription->diagnosis_id);
|
||||
$newDiagnosisId = (int) $prescription->diagnosis_id;
|
||||
$newDateYmd = self::normalizePrescriptionDate($params['prescription_date'] ?? $prescription->prescription_date);
|
||||
if ($newDiagnosisId > 0 && !self::assertUniquePrescriptionPerDiagnosisDay($newDiagnosisId, (int) $prescription->creator_id, $newDateYmd, (int) $params['id'])) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user