From 0a387c189ac619b9bf32d8c49a2f98db0a003226 Mon Sep 17 00:00:00 2001 From: iven Date: Mon, 27 Apr 2026 09:52:31 +0800 Subject: [PATCH] =?UTF-8?q?perf(health):=20get=5Fhealth=5Fsummary=204?= =?UTF-8?q?=E6=AC=A1=E4=B8=B2=E8=A1=8C=E6=9F=A5=E8=AF=A2=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=20tokio::join!=20=E5=B9=B6=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 总延迟从 sum(4次查询) 降为 max(4次查询),预估延迟降低约75%。 --- .../erp-health/src/service/patient_service.rs | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/crates/erp-health/src/service/patient_service.rs b/crates/erp-health/src/service/patient_service.rs index 8df6c95..fbe836a 100644 --- a/crates/erp-health/src/service/patient_service.rs +++ b/crates/erp-health/src/service/patient_service.rs @@ -428,42 +428,44 @@ pub async fn get_health_summary( find_patient(&state.db, tenant_id, patient_id).await?; use crate::entity::{vital_signs, lab_report, appointment, follow_up_task}; + use sea_orm::QueryOrder; - // 最新体征 - let latest_vitals = vital_signs::Entity::find() - .filter(vital_signs::Column::TenantId.eq(tenant_id)) - .filter(vital_signs::Column::PatientId.eq(patient_id)) - .filter(vital_signs::Column::DeletedAt.is_null()) - .order_by_desc(vital_signs::Column::RecordDate) - .one(&state.db) - .await?; + // 4 个查询并行执行 + let (latest_vitals_res, latest_lab_res, upcoming_res, pending_follow_ups_res) = tokio::join!( + // 最新体征 + vital_signs::Entity::find() + .filter(vital_signs::Column::TenantId.eq(tenant_id)) + .filter(vital_signs::Column::PatientId.eq(patient_id)) + .filter(vital_signs::Column::DeletedAt.is_null()) + .order_by_desc(vital_signs::Column::RecordDate) + .one(&state.db), + // 最新化验 + lab_report::Entity::find() + .filter(lab_report::Column::TenantId.eq(tenant_id)) + .filter(lab_report::Column::PatientId.eq(patient_id)) + .filter(lab_report::Column::DeletedAt.is_null()) + .order_by_desc(lab_report::Column::ReportDate) + .one(&state.db), + // 待处理预约数 + appointment::Entity::find() + .filter(appointment::Column::TenantId.eq(tenant_id)) + .filter(appointment::Column::PatientId.eq(patient_id)) + .filter(appointment::Column::Status.eq("pending")) + .filter(appointment::Column::DeletedAt.is_null()) + .count(&state.db), + // 待办随访数 + follow_up_task::Entity::find() + .filter(follow_up_task::Column::TenantId.eq(tenant_id)) + .filter(follow_up_task::Column::PatientId.eq(patient_id)) + .filter(follow_up_task::Column::Status.eq("pending")) + .filter(follow_up_task::Column::DeletedAt.is_null()) + .count(&state.db), + ); - // 最新化验 - let latest_lab = lab_report::Entity::find() - .filter(lab_report::Column::TenantId.eq(tenant_id)) - .filter(lab_report::Column::PatientId.eq(patient_id)) - .filter(lab_report::Column::DeletedAt.is_null()) - .order_by_desc(lab_report::Column::ReportDate) - .one(&state.db) - .await?; - - // 待处理预约数 - let upcoming = appointment::Entity::find() - .filter(appointment::Column::TenantId.eq(tenant_id)) - .filter(appointment::Column::PatientId.eq(patient_id)) - .filter(appointment::Column::Status.eq("pending")) - .filter(appointment::Column::DeletedAt.is_null()) - .count(&state.db) - .await?; - - // 待办随访数 - let pending_follow_ups = follow_up_task::Entity::find() - .filter(follow_up_task::Column::TenantId.eq(tenant_id)) - .filter(follow_up_task::Column::PatientId.eq(patient_id)) - .filter(follow_up_task::Column::Status.eq("pending")) - .filter(follow_up_task::Column::DeletedAt.is_null()) - .count(&state.db) - .await?; + let latest_vitals = latest_vitals_res?; + let latest_lab = latest_lab_res?; + let upcoming = upcoming_res?; + let pending_follow_ups = pending_follow_ups_res?; Ok(serde_json::json!({ "patient_id": patient_id,