perf(web): AppointmentList 移除 nameCache N+1 请求

后端已内联 patient_name/doctor_name,前端移除逐条查询
patientApi/doctorApi 的 nameCache 逻辑,列表加载降为 O(1) 请求。
This commit is contained in:
iven
2026-04-27 09:41:47 +08:00
parent 4a5dbaeaeb
commit c6856370c6

View File

@@ -22,8 +22,6 @@ import {
} from '@ant-design/icons';
import type { Dayjs } from 'dayjs';
import { appointmentApi, type Appointment, type CreateAppointmentReq } from '../../api/health/appointments';
import { patientApi } from '../../api/health/patients';
import { doctorApi } from '../../api/health/doctors';
import { StatusTag } from './components/StatusTag';
import { PatientSelect } from './components/PatientSelect';
import { DoctorSelect } from './components/DoctorSelect';
@@ -87,7 +85,6 @@ export default function AppointmentList() {
// 患者选择状态(受控组件,不挂在 Form.Item 上)
const [selectedPatientId, setSelectedPatientId] = useState<string | undefined>(undefined);
const [selectedDoctorId, setSelectedDoctorId] = useState<string | undefined>(undefined);
const [nameCache, setNameCache] = useState<Record<string, string>>({});
// 排班校验
const [scheduleHint, setScheduleHint] = useState<string | null>(null);
@@ -103,27 +100,7 @@ export default function AppointmentList() {
status: statusFilter || undefined,
date: dateFilter ? dateFilter.format('YYYY-MM-DD') : undefined,
});
const items = result.data;
// 批量解析患者和医生名称(分别调用对应 API
const missingPatientIds = new Set<string>();
const missingDoctorIds = new Set<string>();
items.forEach((a) => {
if (a.patient_id && !nameCache[a.patient_id]) missingPatientIds.add(a.patient_id);
if (a.doctor_id && !nameCache[a.doctor_id]) missingDoctorIds.add(a.doctor_id);
});
const newCache: Record<string, string> = {};
await Promise.allSettled([
...Array.from(missingPatientIds).map(async (id) => {
try { const p = await patientApi.get(id); newCache[id] = p.name; } catch { /* skip */ }
}),
...Array.from(missingDoctorIds).map(async (id) => {
try { const d = await doctorApi.get(id); newCache[id] = d.name; } catch { /* skip */ }
}),
]);
if (Object.keys(newCache).length > 0) {
setNameCache((prev) => ({ ...prev, ...newCache }));
}
setData(items);
setData(result.data);
setTotal(result.total);
} catch {
message.error('加载预约列表失败');
@@ -274,16 +251,15 @@ export default function AppointmentList() {
key: 'patient_name',
width: 100,
render: (_: unknown, record: Appointment) =>
record.patient_name ?? nameCache[record.patient_id] ?? record.patient_id.slice(0, 8),
record.patient_name ?? record.patient_id.slice(0, 8),
},
{
title: '医护',
dataIndex: 'doctor_name',
key: 'doctor_name',
width: 100,
render: (_: unknown, record: Appointment) => {
return record.doctor_name || nameCache[record.doctor_id ?? ''] || record.doctor_id?.slice(0, 8) || '-';
},
render: (_: unknown, record: Appointment) =>
record.doctor_name ?? record.doctor_id?.slice(0, 8) ?? '-',
},
{
title: '预约类型',