Files
hms/crates/erp-health/src/service/action_inbox_service.rs
iven 3bc4597041
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
fix(health): 工作台 UNION ALL 排序 + 团队概览 display_name NULL 处理
- UNION ALL 查询包装子查询解决 PostgreSQL ORDER BY 限制
- get_team_overview 的 display_name 改为 Option<String> 防止 NULL 解码失败
2026-05-02 00:21:27 +08:00

931 lines
30 KiB
Rust

use chrono::{DateTime, Utc};
use erp_core::types::PaginatedResponse;
use sea_orm::{DatabaseBackend, DatabaseConnection, FromQueryResult, Statement};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::error::HealthError;
// ── DTO ──────────────────────────────────────────────────────────────
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ActionType {
AiSuggestion,
Alert,
Followup,
DataAnomaly,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ActionPriority {
Urgent,
High,
Medium,
Low,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ActionStatus {
Pending,
InProgress,
Completed,
Dismissed,
}
#[derive(Debug, Clone, Serialize)]
pub struct ActionItem {
pub id: String,
pub action_type: ActionType,
pub priority: ActionPriority,
pub status: ActionStatus,
pub title: String,
pub summary: String,
pub patient_id: Uuid,
pub patient_name: String,
pub source_ref: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ThreadEvent {
pub step: String,
pub label: String,
pub status: ActionStatus,
#[serde(skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub operator: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub link_to: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ActionDefinition {
pub key: String,
pub label: String,
pub variant: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub api_endpoint: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ThreadResponse {
pub action_item: ActionItem,
pub thread: Vec<ThreadEvent>,
pub available_actions: Vec<ActionDefinition>,
}
#[derive(Debug, Deserialize)]
pub struct ActionInboxQuery {
pub status: Option<String>,
#[serde(rename = "type")]
pub action_type: Option<String>,
pub page: Option<u64>,
pub page_size: Option<u64>,
}
// ── 内部查询结构体 ──────────────────────────────────────────────────
#[derive(Debug, FromQueryResult)]
struct ActionItemRow {
id: Uuid,
action_type: String,
priority_raw: String,
status: String,
params: Option<serde_json::Value>,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
patient_id: Uuid,
patient_name: String,
result_content: Option<String>,
source_id: Option<Uuid>,
}
#[derive(Debug, FromQueryResult)]
struct CountRow {
cnt: i64,
}
#[derive(Debug, FromQueryResult)]
struct ActionDetail {
id: Uuid,
action_type: String,
priority_raw: String,
status: String,
params: Option<serde_json::Value>,
workflow_instance_id: Option<Uuid>,
reanalysis_id: Option<Uuid>,
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
source_id: Option<Uuid>,
patient_id: Uuid,
patient_name: String,
result_content: Option<String>,
analysis_created_at: Option<DateTime<Utc>>,
}
// ── 辅助函数 ────────────────────────────────────────────────────────
fn risk_to_priority(risk: &str) -> ActionPriority {
match risk {
"high" | "urgent" => ActionPriority::Urgent,
"medium" => ActionPriority::High,
"low" => ActionPriority::Medium,
_ => ActionPriority::Low,
}
}
fn suggestion_status_to_action(status: &str) -> ActionStatus {
match status {
"pending" => ActionStatus::Pending,
"approved" => ActionStatus::InProgress,
"executed" => ActionStatus::Completed,
_ => ActionStatus::Dismissed,
}
}
fn alert_status_to_action(status: &str) -> ActionStatus {
match status {
"active" => ActionStatus::Pending,
"acknowledged" => ActionStatus::InProgress,
"resolved" => ActionStatus::Completed,
_ => ActionStatus::Dismissed,
}
}
fn followup_status_to_action(status: &str) -> ActionStatus {
match status {
"pending" => ActionStatus::Pending,
"in_progress" => ActionStatus::InProgress,
"completed" => ActionStatus::Completed,
_ => ActionStatus::Dismissed,
}
}
fn map_action_status(action_type: &str, status: &str) -> ActionStatus {
match action_type {
"alert" => alert_status_to_action(status),
"followup" => followup_status_to_action(status),
_ => suggestion_status_to_action(status),
}
}
fn suggestion_type_to_title(suggestion_type: &str) -> String {
match suggestion_type {
"followup" => "建议安排随访".into(),
"appointment" => "建议预约复查".into(),
"alert" => "建议关注指标异常".into(),
_ => "AI 健康建议".into(),
}
}
fn extract_title(params: &Option<serde_json::Value>, suggestion_type: &str) -> String {
params
.as_ref()
.and_then(|p| p.get("reason"))
.and_then(|v| v.as_str())
.map(|s| s.to_string())
.unwrap_or_else(|| suggestion_type_to_title(suggestion_type))
}
fn summarize_result(result: &Option<String>) -> String {
let Some(text) = result else { return String::new() };
let parsed = serde_json::from_str::<serde_json::Value>(text).ok();
let summary = parsed
.as_ref()
.and_then(|v| v.get("summary"))
.and_then(|v| v.as_str())
.map(|s| s.chars().take(100).collect::<String>());
summary.unwrap_or_else(|| text.chars().take(100).collect())
}
// ── 公开 API ────────────────────────────────────────────────────────
pub async fn list_action_items(
db: &DatabaseConnection,
tenant_id: Uuid,
query: &ActionInboxQuery,
) -> Result<PaginatedResponse<ActionItem>, HealthError> {
let page = query.page.unwrap_or(1).max(1);
let page_size = query.page_size.unwrap_or(20).min(100);
let offset = (page - 1) * page_size;
// 各段的 status 过滤条件
let (sug_status, alert_status, fu_status) = match query.status.as_deref() {
Some("pending") => (
"AND s.status = 'pending'".into(),
"AND al.status = 'active'".into(),
"AND f.status = 'pending'".into(),
),
Some("in_progress") => (
"AND s.status = 'approved'".into(),
"AND al.status = 'acknowledged'".into(),
"AND f.status = 'in_progress'".into(),
),
Some("completed") => (
"AND s.status = 'executed'".into(),
"AND al.status = 'resolved'".into(),
"AND f.status = 'completed'".into(),
),
Some("dismissed") => (
"AND s.status IN ('rejected', 'expired', 'parse_failed')".into(),
"AND al.status IN ('dismissed', 'expired')".into(),
"AND f.status IN ('cancelled', 'skipped')".into(),
),
_ => (String::new(), String::new(), String::new()),
};
// 按类型过滤
let type_filter = query.action_type.as_deref();
let include_sug = type_filter.map_or(true, |t| t == "ai_suggestion");
let include_alert = type_filter.map_or(true, |t| t == "alert");
let include_fu = type_filter.map_or(true, |t| t == "followup");
let mut segments: Vec<String> = Vec::new();
if include_sug {
segments.push(format!(
r#"
SELECT s.id, 'ai_suggestion' AS action_type, s.risk_level AS priority_raw,
s.status, s.params, s.created_at, s.updated_at,
a.patient_id, p.name AS patient_name,
a.result_content, a.id AS source_id
FROM ai_suggestion s
JOIN ai_analysis a ON s.analysis_id = a.id
JOIN patient p ON a.patient_id = p.id
WHERE s.tenant_id = $1 AND s.deleted_at IS NULL {sug_status}"#
));
}
if include_alert {
segments.push(format!(
r#"
SELECT al.id, 'alert' AS action_type, al.severity AS priority_raw,
al.status, NULL::jsonb AS params, al.created_at, al.updated_at,
al.patient_id, p.name AS patient_name,
NULL::text AS result_content, NULL::uuid AS source_id
FROM alerts al
JOIN patient p ON al.patient_id = p.id
WHERE al.tenant_id = $1 AND al.deleted_at IS NULL {alert_status}"#
));
}
if include_fu {
segments.push(format!(
r#"
SELECT f.id, 'followup' AS action_type, 'medium' AS priority_raw,
f.status, NULL::jsonb AS params, f.created_at, f.updated_at,
f.patient_id, p.name AS patient_name,
NULL::text AS result_content, NULL::uuid AS source_id
FROM follow_up_task f
JOIN patient p ON f.patient_id = p.id
WHERE f.tenant_id = $1 AND f.deleted_at IS NULL {fu_status}"#
));
}
if segments.is_empty() {
return Ok(PaginatedResponse {
data: vec![],
total: 0,
page,
page_size,
total_pages: 0,
});
}
let union_sql = segments.join("\n UNION ALL\n");
let data_sql = format!(
r#"SELECT * FROM ({union_sql}) sub
ORDER BY
CASE priority_raw WHEN 'high' THEN 1 WHEN 'urgent' THEN 1 WHEN 'medium' THEN 2 ELSE 3 END,
created_at DESC
LIMIT $2 OFFSET $3"#
);
let count_sql = format!("SELECT COUNT(*) AS cnt FROM ({union_sql}) sub");
let rows: Vec<ActionItemRow> = FromQueryResult::find_by_statement(
Statement::from_sql_and_values(
DatabaseBackend::Postgres,
data_sql,
[
tenant_id.into(),
(page_size as i64).into(),
(offset as i64).into(),
],
),
)
.all(db)
.await
.map_err(|e| HealthError::DbError(e.to_string()))?;
let count_row: Option<CountRow> = FromQueryResult::find_by_statement(
Statement::from_sql_and_values(DatabaseBackend::Postgres, count_sql, [tenant_id.into()]),
)
.one(db)
.await
.map_err(|e| HealthError::DbError(e.to_string()))?;
let total = count_row.map(|r| r.cnt).unwrap_or(0) as u64;
let items: Vec<ActionItem> = rows
.into_iter()
.map(|r| {
let at = match r.action_type.as_str() {
"alert" => ActionType::Alert,
"followup" => ActionType::Followup,
_ => ActionType::AiSuggestion,
};
let title = match r.action_type.as_str() {
"alert" => "健康告警".into(),
"followup" => "随访任务".into(),
_ => extract_title(&r.params, ""),
};
ActionItem {
id: format!("{}:{}", r.action_type, r.id),
action_type: at,
priority: risk_to_priority(&r.priority_raw),
status: map_action_status(&r.action_type, &r.status),
title,
summary: summarize_result(&r.result_content),
patient_id: r.patient_id,
patient_name: r.patient_name,
source_ref: r.id.to_string(),
created_at: r.created_at,
updated_at: r.updated_at,
}
})
.collect();
Ok(PaginatedResponse {
data: items,
total,
page,
page_size,
total_pages: if page_size > 0 { (total + page_size - 1) / page_size } else { 0 },
})
}
pub async fn get_action_thread(
db: &DatabaseConnection,
tenant_id: Uuid,
source_ref: &str,
) -> Result<Option<ThreadResponse>, HealthError> {
// 解析 "action_type:uuid" 格式
let (action_type_str, uuid_str) = source_ref
.find(':')
.map(|pos| (&source_ref[..pos], &source_ref[pos + 1..]))
.ok_or_else(|| HealthError::Validation("无效的 source_ref 格式".into()))?;
let uuid = Uuid::parse_str(uuid_str)
.map_err(|e| HealthError::Validation(format!("无效的 UUID: {e}")))?;
match action_type_str {
"ai_suggestion" => get_ai_suggestion_thread(db, tenant_id, uuid).await,
"alert" => get_alert_thread(db, tenant_id, uuid).await,
"followup" => get_followup_thread(db, tenant_id, uuid).await,
_ => Err(HealthError::Validation(format!("未知类型: {action_type_str}"))),
}
}
async fn get_ai_suggestion_thread(
db: &DatabaseConnection,
tenant_id: Uuid,
uuid: Uuid,
) -> Result<Option<ThreadResponse>, HealthError> {
let detail: Option<ActionDetail> = FromQueryResult::find_by_statement(
Statement::from_sql_and_values(
DatabaseBackend::Postgres,
r#"
SELECT s.id, 'ai_suggestion' AS action_type, s.risk_level AS priority_raw,
s.status, s.params, s.workflow_instance_id, s.reanalysis_id,
s.created_at, s.updated_at,
s.analysis_id AS source_id, a.patient_id, p.name AS patient_name,
a.result_content, a.created_at AS analysis_created_at
FROM ai_suggestion s
JOIN ai_analysis a ON s.analysis_id = a.id
JOIN patient p ON a.patient_id = p.id
WHERE s.id = $1 AND s.tenant_id = $2 AND s.deleted_at IS NULL
"#,
[uuid.into(), tenant_id.into()],
),
)
.one(db)
.await
.map_err(|e| HealthError::DbError(e.to_string()))?;
let detail = match detail {
Some(d) => d,
None => return Ok(None),
};
let action_status = suggestion_status_to_action(&detail.status);
let mut thread = Vec::new();
thread.push(ThreadEvent {
step: "ai_analysis".into(),
label: "AI 分析完成".into(),
status: ActionStatus::Completed,
detail: None,
timestamp: detail.analysis_created_at,
operator: None,
link_to: Some("/health/ai-analysis".into()),
});
let approval_status = match detail.status.as_str() {
"approved" | "executed" => ActionStatus::Completed,
"rejected" => ActionStatus::Dismissed,
_ => ActionStatus::InProgress,
};
let is_terminal = matches!(approval_status, ActionStatus::Completed | ActionStatus::Dismissed);
thread.push(ThreadEvent {
step: "doctor_approval".into(),
label: "医生审批".into(),
status: approval_status,
detail: None,
timestamp: if is_terminal { Some(detail.updated_at) } else { None },
operator: None,
link_to: None,
});
let has_workflow = detail.workflow_instance_id.is_some();
if has_workflow || detail.status == "approved" || detail.status == "executed" {
thread.push(ThreadEvent {
step: "action_dispatched".into(),
label: "执行安排".into(),
status: if has_workflow { ActionStatus::Completed } else { ActionStatus::Pending },
detail: None,
timestamp: None,
operator: None,
link_to: None,
});
}
if detail.reanalysis_id.is_some() || detail.status == "executed" {
thread.push(ThreadEvent {
step: "reanalysis".into(),
label: "前后对比".into(),
status: if detail.reanalysis_id.is_some() { ActionStatus::Completed } else { ActionStatus::Pending },
detail: None,
timestamp: None,
operator: None,
link_to: detail.reanalysis_id.map(|id| format!("/health/ai-analysis/{id}")),
});
}
let available_actions = match detail.status.as_str() {
"pending" => vec![
ActionDefinition {
key: "approve".into(),
label: "批准并执行".into(),
variant: "primary".into(),
api_endpoint: Some(format!("/api/v1/ai/suggestions/{}/approve", detail.id)),
},
ActionDefinition {
key: "reject".into(),
label: "拒绝".into(),
variant: "danger".into(),
api_endpoint: Some(format!("/api/v1/ai/suggestions/{}/approve", detail.id)),
},
],
"approved" => vec![ActionDefinition {
key: "acknowledge".into(),
label: "标记已知悉".into(),
variant: "default".into(),
api_endpoint: None,
}],
"executed" => vec![ActionDefinition {
key: "view_comparison".into(),
label: "查看前后对比".into(),
variant: "primary".into(),
api_endpoint: Some(format!("/api/v1/ai/suggestions/{}/comparison", detail.id)),
}],
_ => vec![],
};
let action_item = ActionItem {
id: format!("ai_suggestion:{}", detail.id),
action_type: ActionType::AiSuggestion,
priority: risk_to_priority(&detail.priority_raw),
status: action_status,
title: extract_title(&detail.params, ""),
summary: summarize_result(&detail.result_content),
patient_id: detail.patient_id,
patient_name: detail.patient_name,
source_ref: detail.id.to_string(),
created_at: detail.created_at,
updated_at: detail.updated_at,
};
Ok(Some(ThreadResponse {
action_item,
thread,
available_actions,
}))
}
async fn get_alert_thread(
db: &DatabaseConnection,
tenant_id: Uuid,
uuid: Uuid,
) -> Result<Option<ThreadResponse>, HealthError> {
let detail: Option<ActionDetail> = FromQueryResult::find_by_statement(
Statement::from_sql_and_values(
DatabaseBackend::Postgres,
r#"
SELECT al.id, 'alert' AS action_type, al.severity AS priority_raw,
al.status, NULL::jsonb AS params,
NULL::uuid AS workflow_instance_id, NULL::uuid AS reanalysis_id,
al.created_at, al.updated_at,
NULL::uuid AS source_id, al.patient_id, p.name AS patient_name,
NULL::text AS result_content, NULL::timestamptz AS analysis_created_at
FROM alerts al
JOIN patient p ON al.patient_id = p.id
WHERE al.id = $1 AND al.tenant_id = $2 AND al.deleted_at IS NULL
"#,
[uuid.into(), tenant_id.into()],
),
)
.one(db)
.await
.map_err(|e| HealthError::DbError(e.to_string()))?;
let detail = match detail {
Some(d) => d,
None => return Ok(None),
};
let action_status = alert_status_to_action(&detail.status);
let mut thread = Vec::new();
thread.push(ThreadEvent {
step: "alert_triggered".into(),
label: "告警触发".into(),
status: ActionStatus::Completed,
detail: None,
timestamp: Some(detail.created_at),
operator: None,
link_to: Some("/health/alert-dashboard".into()),
});
let ack_status = match detail.status.as_str() {
"acknowledged" | "resolved" => ActionStatus::Completed,
"dismissed" | "expired" => ActionStatus::Dismissed,
_ => ActionStatus::Pending,
};
thread.push(ThreadEvent {
step: "doctor_acknowledge".into(),
label: "医生知悉".into(),
status: ack_status,
detail: None,
timestamp: if matches!(ack_status, ActionStatus::Completed) { Some(detail.updated_at) } else { None },
operator: None,
link_to: None,
});
if detail.status == "resolved" {
thread.push(ThreadEvent {
step: "resolved".into(),
label: "处理完成".into(),
status: ActionStatus::Completed,
detail: None,
timestamp: Some(detail.updated_at),
operator: None,
link_to: None,
});
}
let available_actions = match detail.status.as_str() {
"active" => vec![
ActionDefinition {
key: "acknowledge".into(),
label: "标记已知悉".into(),
variant: "primary".into(),
api_endpoint: Some(format!("/api/v1/health/alerts/{}/acknowledge", detail.id)),
},
ActionDefinition {
key: "resolve".into(),
label: "标记已处理".into(),
variant: "default".into(),
api_endpoint: Some(format!("/api/v1/health/alerts/{}/resolve", detail.id)),
},
],
"acknowledged" => vec![ActionDefinition {
key: "resolve".into(),
label: "标记已处理".into(),
variant: "primary".into(),
api_endpoint: Some(format!("/api/v1/health/alerts/{}/resolve", detail.id)),
}],
_ => vec![],
};
let action_item = ActionItem {
id: format!("alert:{}", detail.id),
action_type: ActionType::Alert,
priority: risk_to_priority(&detail.priority_raw),
status: action_status,
title: "健康告警".into(),
summary: summarize_result(&detail.result_content),
patient_id: detail.patient_id,
patient_name: detail.patient_name,
source_ref: detail.id.to_string(),
created_at: detail.created_at,
updated_at: detail.updated_at,
};
Ok(Some(ThreadResponse {
action_item,
thread,
available_actions,
}))
}
async fn get_followup_thread(
db: &DatabaseConnection,
tenant_id: Uuid,
uuid: Uuid,
) -> Result<Option<ThreadResponse>, HealthError> {
let detail: Option<ActionDetail> = FromQueryResult::find_by_statement(
Statement::from_sql_and_values(
DatabaseBackend::Postgres,
r#"
SELECT f.id, 'followup' AS action_type, 'medium' AS priority_raw,
f.status, NULL::jsonb AS params,
NULL::uuid AS workflow_instance_id, NULL::uuid AS reanalysis_id,
f.created_at, f.updated_at,
NULL::uuid AS source_id, f.patient_id, p.name AS patient_name,
NULL::text AS result_content, NULL::timestamptz AS analysis_created_at
FROM follow_up_task f
JOIN patient p ON f.patient_id = p.id
WHERE f.id = $1 AND f.tenant_id = $2 AND f.deleted_at IS NULL
"#,
[uuid.into(), tenant_id.into()],
),
)
.one(db)
.await
.map_err(|e| HealthError::DbError(e.to_string()))?;
let detail = match detail {
Some(d) => d,
None => return Ok(None),
};
let action_status = followup_status_to_action(&detail.status);
let mut thread = Vec::new();
thread.push(ThreadEvent {
step: "followup_created".into(),
label: "随访创建".into(),
status: ActionStatus::Completed,
detail: None,
timestamp: Some(detail.created_at),
operator: None,
link_to: Some("/health/follow-ups".into()),
});
thread.push(ThreadEvent {
step: "execution".into(),
label: "随访执行".into(),
status: action_status,
detail: None,
timestamp: if matches!(action_status, ActionStatus::Completed | ActionStatus::Dismissed) { Some(detail.updated_at) } else { None },
operator: None,
link_to: None,
});
let available_actions = match detail.status.as_str() {
"pending" | "in_progress" => vec![ActionDefinition {
key: "complete".into(),
label: "标记已完成".into(),
variant: "primary".into(),
api_endpoint: Some(format!("/api/v1/health/follow-ups/{}/complete", detail.id)),
}],
_ => vec![],
};
let action_item = ActionItem {
id: format!("followup:{}", detail.id),
action_type: ActionType::Followup,
priority: risk_to_priority(&detail.priority_raw),
status: action_status,
title: "随访任务".into(),
summary: String::new(),
patient_id: detail.patient_id,
patient_name: detail.patient_name,
source_ref: detail.id.to_string(),
created_at: detail.created_at,
updated_at: detail.updated_at,
};
Ok(Some(ThreadResponse {
action_item,
thread,
available_actions,
}))
}
// ── 工作台统计 ──────────────────────────────────────────────────────
#[derive(Debug, Clone, Serialize)]
pub struct WorkbenchStats {
pub total_pending: u64,
pub ai_suggestion_pending: u64,
pub urgent_alerts: u64,
pub followup_due: u64,
pub completion_rate: Option<f64>,
}
#[derive(Debug, Clone, Serialize)]
pub struct TeamMemberOverview {
pub user_id: Uuid,
pub name: String,
pub title: String,
pub pending_count: u64,
pub completed_count: u64,
pub overdue_count: u64,
pub completion_rate: f64,
}
#[derive(Debug, Clone, Serialize)]
pub struct RiskDistribution {
pub high: u64,
pub medium: u64,
pub low: u64,
}
#[derive(Debug, Clone, Serialize)]
pub struct TeamOverview {
pub members: Vec<TeamMemberOverview>,
pub risk_distribution: RiskDistribution,
pub total_pending: u64,
pub total_completed: u64,
}
pub async fn get_workbench_stats(
db: &DatabaseConnection,
tenant_id: Uuid,
) -> Result<WorkbenchStats, HealthError> {
let ai_pending: i64 = FromQueryResult::find_by_statement(
Statement::from_sql_and_values(
DatabaseBackend::Postgres,
"SELECT COUNT(*) AS cnt FROM ai_suggestion WHERE tenant_id = $1 AND status = 'pending' AND deleted_at IS NULL",
[tenant_id.into()],
),
)
.one(db)
.await
.map_err(|e| HealthError::DbError(e.to_string()))?
.map(|r: CountRow| r.cnt)
.unwrap_or(0);
let urgent_alerts: i64 = FromQueryResult::find_by_statement(
Statement::from_sql_and_values(
DatabaseBackend::Postgres,
"SELECT COUNT(*) AS cnt FROM alerts WHERE tenant_id = $1 AND severity = 'urgent' AND status = 'active' AND deleted_at IS NULL",
[tenant_id.into()],
),
)
.one(db)
.await
.map_err(|e| HealthError::DbError(e.to_string()))?
.map(|r: CountRow| r.cnt)
.unwrap_or(0);
let followup_due: i64 = FromQueryResult::find_by_statement(
Statement::from_sql_and_values(
DatabaseBackend::Postgres,
"SELECT COUNT(*) AS cnt FROM follow_up_task WHERE tenant_id = $1 AND status = 'pending' AND deleted_at IS NULL",
[tenant_id.into()],
),
)
.one(db)
.await
.map_err(|e| HealthError::DbError(e.to_string()))?
.map(|r: CountRow| r.cnt)
.unwrap_or(0);
let total_pending = (ai_pending + urgent_alerts + followup_due) as u64;
Ok(WorkbenchStats {
total_pending,
ai_suggestion_pending: ai_pending as u64,
urgent_alerts: urgent_alerts as u64,
followup_due: followup_due as u64,
completion_rate: None,
})
}
pub async fn get_team_overview(
db: &DatabaseConnection,
tenant_id: Uuid,
) -> Result<TeamOverview, HealthError> {
// 成员统计
#[derive(Debug, FromQueryResult)]
struct MemberRow {
user_id: Uuid,
display_name: Option<String>,
title: String,
pending_count: i64,
completed_count: i64,
overdue_count: i64,
}
let members: Vec<MemberRow> = FromQueryResult::find_by_statement(
Statement::from_sql_and_values(
DatabaseBackend::Postgres,
r#"
SELECT dp.user_id, u.display_name, dp.title,
COUNT(CASE WHEN f.status = 'pending' THEN 1 END) AS pending_count,
COUNT(CASE WHEN f.status = 'completed' THEN 1 END) AS completed_count,
COUNT(CASE WHEN f.status = 'overdue' THEN 1 END) AS overdue_count
FROM doctor_profile dp
JOIN users u ON dp.user_id = u.id
LEFT JOIN follow_up_task f ON f.assigned_to = dp.user_id AND f.tenant_id = $1 AND f.deleted_at IS NULL
WHERE dp.tenant_id = $1 AND dp.deleted_at IS NULL
GROUP BY dp.user_id, u.display_name, dp.title
"#,
[tenant_id.into()],
),
)
.all(db)
.await
.map_err(|e| HealthError::DbError(e.to_string()))?;
let total_pending: u64 = members.iter().map(|m| m.pending_count as u64).sum();
let total_completed: u64 = members.iter().map(|m| m.completed_count as u64).sum();
let team_members: Vec<TeamMemberOverview> = members
.into_iter()
.map(|m| {
let total = m.pending_count + m.completed_count + m.overdue_count;
let rate = if total > 0 {
(m.completed_count as f64 / total as f64) * 100.0
} else {
0.0
};
TeamMemberOverview {
user_id: m.user_id,
name: m.display_name.unwrap_or_default(),
title: m.title,
pending_count: m.pending_count as u64,
completed_count: m.completed_count as u64,
overdue_count: m.overdue_count as u64,
completion_rate: rate,
}
})
.collect();
// 风险分布
#[derive(Debug, FromQueryResult)]
struct RiskRow {
severity: String,
cnt: i64,
}
let risk_rows: Vec<RiskRow> = FromQueryResult::find_by_statement(
Statement::from_sql_and_values(
DatabaseBackend::Postgres,
r#"
SELECT severity, COUNT(*) AS cnt
FROM alerts
WHERE tenant_id = $1 AND status = 'active' AND deleted_at IS NULL
GROUP BY severity
"#,
[tenant_id.into()],
),
)
.all(db)
.await
.map_err(|e| HealthError::DbError(e.to_string()))?;
let mut risk_distribution = RiskDistribution {
high: 0,
medium: 0,
low: 0,
};
for row in &risk_rows {
match row.severity.as_str() {
"high" | "urgent" => risk_distribution.high = row.cnt as u64,
"medium" => risk_distribution.medium = row.cnt as u64,
"low" | _ => risk_distribution.low = row.cnt as u64,
}
}
Ok(TeamOverview {
members: team_members,
risk_distribution,
total_pending,
total_completed,
})
}