perf(health): 随访列表内联负责人名称 — 消除 N+1 查询
Some checks failed
CI / rust-check (push) Has been cancelled
CI / rust-test (push) Has been cancelled
CI / frontend-build (push) Has been cancelled
CI / security-audit (push) Has been cancelled

follow_up list_tasks 批量查询 users 表获取 assigned_to_name,
前端移除 doctorLabels 逐条请求缓存,直接使用后端内联字段。
This commit is contained in:
iven
2026-04-27 13:22:46 +08:00
parent 2519ad8fee
commit a5646ddbb3
4 changed files with 36 additions and 30 deletions

View File

@@ -50,6 +50,8 @@ pub struct FollowUpTaskResp {
pub patient_id: Uuid,
pub assigned_to: Option<Uuid>,
pub patient_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub assigned_to_name: Option<String>,
pub follow_up_type: String,
pub planned_date: NaiveDate,
pub status: String,

View File

@@ -68,9 +68,32 @@ pub async fn list_tasks(
HashMap::new()
};
// 批量查询 assigned_to_name从 users 表)
let assigned_ids: HashSet<Uuid> = models.iter().filter_map(|m| m.assigned_to).collect();
let assigned_names: HashMap<Uuid, String> = if !assigned_ids.is_empty() {
let ids_csv = assigned_ids.iter().map(|id| format!("'{}'", id)).collect::<Vec<_>>().join(",");
let sql = format!(
"SELECT id, COALESCE(display_name, username) AS name FROM users WHERE id IN ({}) AND tenant_id = '{}'",
ids_csv, tenant_id
);
let rows = state.db.query_all(sea_orm::Statement::from_string(
sea_orm::DatabaseBackend::Postgres, sql,
)).await?;
rows.into_iter()
.filter_map(|row| {
let id: Uuid = row.try_get_by_index(0).ok()?;
let name: String = row.try_get_by_index(1).ok()?;
Some((id, name))
})
.collect()
} else {
HashMap::new()
};
let data = models.into_iter().map(|m| FollowUpTaskResp {
id: m.id, patient_id: m.patient_id, assigned_to: m.assigned_to,
patient_name: patient_names.get(&m.patient_id).cloned(),
assigned_to_name: m.assigned_to.and_then(|uid| assigned_names.get(&uid).cloned()),
follow_up_type: m.follow_up_type, planned_date: m.planned_date,
status: m.status, content_template: m.content_template,
related_appointment_id: m.related_appointment_id,
@@ -95,7 +118,7 @@ pub async fn get_task(
Ok(FollowUpTaskResp {
id: m.id, patient_id: m.patient_id, assigned_to: m.assigned_to,
patient_name: None,
patient_name: None, assigned_to_name: None,
follow_up_type: m.follow_up_type, planned_date: m.planned_date,
status: m.status, content_template: m.content_template,
related_appointment_id: m.related_appointment_id,
@@ -156,7 +179,7 @@ pub async fn create_task(
Ok(FollowUpTaskResp {
id: m.id, patient_id: m.patient_id, assigned_to: m.assigned_to,
patient_name: None,
patient_name: None, assigned_to_name: None,
follow_up_type: m.follow_up_type, planned_date: m.planned_date,
status: m.status, content_template: m.content_template,
related_appointment_id: m.related_appointment_id,
@@ -227,7 +250,7 @@ pub async fn update_task(
Ok(FollowUpTaskResp {
id: m.id, patient_id: m.patient_id, assigned_to: m.assigned_to,
patient_name: None,
patient_name: None, assigned_to_name: None,
follow_up_type: m.follow_up_type, planned_date: m.planned_date,
status: m.status, content_template: m.content_template,
related_appointment_id: m.related_appointment_id,