find()) { $sn = self::generateSn(); } $data = [ 'sn' => $sn, 'diagnosis_id' => (int)$params['diagnosis_id'], 'appointment_id' => (int)($params['appointment_id'] ?? 0), 'patient_id' => (int)$diagnosis->patient_id, 'patient_name' => $params['patient_name'] ?? $diagnosis->patient_name, 'gender' => (int)($params['gender'] ?? $diagnosis->gender), 'age' => (int)($params['age'] ?? $diagnosis->age ?? 0), 'phone' => $params['phone'] ?? $diagnosis->phone ?? '', 'visit_no' => $params['visit_no'] ?? $sn, 'prescription_date' => $params['prescription_date'] ?? date('Y-m-d'), 'pulse' => $params['pulse'] ?? $diagnosis->pulse ?? '', 'tongue' => $params['tongue'] ?? $diagnosis->tongue_coating ?? '', 'clinical_diagnosis' => $params['clinical_diagnosis'] ?? '', 'case_record' => $params['case_record'] ?? null, 'herbs' => $herbs, 'dose_count' => (int)($params['dose_count'] ?? 1), 'dose_unit' => $params['dose_unit'] ?? '剂', 'usage_instruction' => $params['usage_instruction'] ?? '水煎服一日二次', 'amount' => (float)($params['amount'] ?? 0), 'doctor_name' => $params['doctor_name'] ?? '', 'doctor_signature' => $params['doctor_signature'] ?? '', 'template_id' => (int)($params['template_id'] ?? 0), 'creator_id' => $adminId, ]; $prescription = new Prescription(); $prescription->save($data); return (int)$prescription->id; } /** * 处方详情 */ public static function detail(int $id): ?array { $row = Prescription::find($id); if (!$row) { return null; } $arr = $row->toArray(); $arr['gender_desc'] = $arr['gender'] == 1 ? '男' : '女'; return $arr; } /** * 根据诊单ID获取处方列表 */ public static function listByDiagnosis(int $diagnosisId): array { return Prescription::where('diagnosis_id', $diagnosisId) ->whereNull('delete_time') ->order('id', 'desc') ->select() ->toArray(); } /** * 根据预约ID获取处方 */ public static function getByAppointment(int $appointmentId): ?array { $row = Prescription::where('appointment_id', $appointmentId) ->whereNull('delete_time') ->order('id', 'desc') ->find(); return $row ? $row->toArray() : null; } /** * 作废处方 */ public static function void(int $id, int $adminId, string $adminName): bool { $row = Prescription::find($id); if (!$row) { self::setError('处方不存在'); return false; } if ((int)($row->void_status ?? 0) === 1) { self::setError('该处方已作废'); return false; } $row->void_status = 1; $row->void_time = time(); $row->void_by = $adminId; $row->void_by_name = $adminName; return $row->save(); } }