perf(web): AppointmentList 移除 nameCache N+1 请求
后端已内联 patient_name/doctor_name,前端移除逐条查询 patientApi/doctorApi 的 nameCache 逻辑,列表加载降为 O(1) 请求。
This commit is contained in:
@@ -22,8 +22,6 @@ import {
|
|||||||
} from '@ant-design/icons';
|
} from '@ant-design/icons';
|
||||||
import type { Dayjs } from 'dayjs';
|
import type { Dayjs } from 'dayjs';
|
||||||
import { appointmentApi, type Appointment, type CreateAppointmentReq } from '../../api/health/appointments';
|
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 { StatusTag } from './components/StatusTag';
|
||||||
import { PatientSelect } from './components/PatientSelect';
|
import { PatientSelect } from './components/PatientSelect';
|
||||||
import { DoctorSelect } from './components/DoctorSelect';
|
import { DoctorSelect } from './components/DoctorSelect';
|
||||||
@@ -87,7 +85,6 @@ export default function AppointmentList() {
|
|||||||
// 患者选择状态(受控组件,不挂在 Form.Item 上)
|
// 患者选择状态(受控组件,不挂在 Form.Item 上)
|
||||||
const [selectedPatientId, setSelectedPatientId] = useState<string | undefined>(undefined);
|
const [selectedPatientId, setSelectedPatientId] = useState<string | undefined>(undefined);
|
||||||
const [selectedDoctorId, setSelectedDoctorId] = 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);
|
const [scheduleHint, setScheduleHint] = useState<string | null>(null);
|
||||||
@@ -103,27 +100,7 @@ export default function AppointmentList() {
|
|||||||
status: statusFilter || undefined,
|
status: statusFilter || undefined,
|
||||||
date: dateFilter ? dateFilter.format('YYYY-MM-DD') : undefined,
|
date: dateFilter ? dateFilter.format('YYYY-MM-DD') : undefined,
|
||||||
});
|
});
|
||||||
const items = result.data;
|
setData(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);
|
|
||||||
setTotal(result.total);
|
setTotal(result.total);
|
||||||
} catch {
|
} catch {
|
||||||
message.error('加载预约列表失败');
|
message.error('加载预约列表失败');
|
||||||
@@ -274,16 +251,15 @@ export default function AppointmentList() {
|
|||||||
key: 'patient_name',
|
key: 'patient_name',
|
||||||
width: 100,
|
width: 100,
|
||||||
render: (_: unknown, record: Appointment) =>
|
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: '医护',
|
title: '医护',
|
||||||
dataIndex: 'doctor_name',
|
dataIndex: 'doctor_name',
|
||||||
key: 'doctor_name',
|
key: 'doctor_name',
|
||||||
width: 100,
|
width: 100,
|
||||||
render: (_: unknown, record: Appointment) => {
|
render: (_: unknown, record: Appointment) =>
|
||||||
return record.doctor_name || nameCache[record.doctor_id ?? ''] || record.doctor_id?.slice(0, 8) || '-';
|
record.doctor_name ?? record.doctor_id?.slice(0, 8) ?? '-',
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: '预约类型',
|
title: '预约类型',
|
||||||
|
|||||||
Reference in New Issue
Block a user