Files
zyt/server/app/common/service/prescriptionai/PrescriptionAiDoctorSnapshot.php
2026-09-21 10:26:14 +08:00

62 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace app\common\service\prescriptionai;
/** Minimal display projection of the immutable prescription; never reads live records. */
final class PrescriptionAiDoctorSnapshot
{
public static function project(array $prescription): array
{
$patient = self::scalars($prescription, ['gender', 'age']);
$patient['name'] = self::text($prescription['patient_name'] ?? null);
$gender = self::text($patient['gender'] ?? null);
$patient['gender_label'] = in_array($gender, ['1', '男'], true) ? '男'
: (in_array($gender, ['0', '2', '女'], true) ? '女' : '未知');
$rx = self::scalars($prescription, [
'prescription_name', 'prescription_type', 'dosage_unit', 'dose_unit', 'dose_basis',
'dose_count', 'usage_instruction', 'usage_days', 'times_per_day', 'usage_time',
'usage_way', 'usage_notes', 'dietary_taboo', 'dosage_amount', 'dosage_bag_count',
'need_decoction', 'bags_per_dose',
]);
$auxUsage = self::scalars(PrescriptionAiPolicy::decode($prescription['aux_usage'] ?? []), [
'prescription_name', 'dosage_amount', 'dosage_bag_count', 'need_decoction',
'bags_per_dose', 'times_per_day', 'usage_days',
]);
if ($auxUsage !== []) {
// Auxiliary formula instructions belong to that formula, never the main usage.
$rx['aux_usage'] = $auxUsage;
}
$rx['herbs'] = [];
foreach (PrescriptionAiPolicy::decode($prescription['herbs'] ?? []) as $herb) {
if (!is_array($herb)) {
continue;
}
// Preserve saved row order and duplicates, without costs, stock, IDs or attachments.
$rx['herbs'][] = self::scalars($herb, [
'name', 'dosage', 'unit', 'dose_basis', 'formula_type', 'processing',
'special_usage', 'instructions', 'usage', 'remark',
]);
}
return [
'patient' => $patient,
// The source has one combined clinical diagnosis. Do not invent a Western/TCM split
// or expose case_record, which may contain other encounters and contact details.
'diagnosis' => ['clinical_diagnosis' => self::text($prescription['clinical_diagnosis'] ?? null)],
'prescription' => $rx,
];
}
private static function scalars(array $row, array $fields): array
{
return array_filter(array_intersect_key($row, array_flip($fields)),
static fn ($value): bool => is_string($value) || is_int($value) || is_float($value));
}
private static function text($value): string
{
return is_string($value) || is_numeric($value) ? trim((string) $value) : '';
}
}