feat(ai): 每日风险快照批量刷新定时任务

- risk_service 新增 refresh_all_patients 方法
- module on_startup 启动每日刷新后台任务
This commit is contained in:
iven
2026-05-12 22:14:08 +08:00
parent a999ee0036
commit ba0a4f4d2e
2 changed files with 52 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ use crate::entity::copilot_risk_snapshots;
use crate::entity::copilot_rules;
use crate::provider::registry::ProviderRegistry;
use erp_core::error::AppResult;
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, QueryFilter, Set};
use sea_orm::{ActiveModelTrait, ColumnTrait, EntityTrait, FromQueryResult, QueryFilter, Set};
use std::sync::Arc;
use uuid::Uuid;
@@ -165,4 +165,36 @@ impl RiskService {
let _ = db;
Ok(serde_json::json!({}))
}
/// 每日批量刷新所有在管患者的风险快照
/// 通过 raw SQL 查询患者列表(因为 erp-ai 不依赖 erp-health entity
pub async fn refresh_all_patients(db: &sea_orm::DatabaseConnection) -> AppResult<u64> {
#[derive(sea_orm::FromQueryResult)]
struct PatientRow {
id: Uuid,
tenant_id: Uuid,
}
let patients: Vec<PatientRow> =
PatientRow::find_by_statement(sea_orm::Statement::from_sql_and_values(
sea_orm::DatabaseBackend::Postgres,
"SELECT id, tenant_id FROM patients WHERE deleted_at IS NULL",
[],
))
.all(db)
.await?;
let total = patients.len() as u64;
for p in &patients {
if let Err(e) = Self::compute_risk(db, p.tenant_id, p.id).await {
tracing::warn!(
patient_id = %p.id,
tenant_id = %p.tenant_id,
error = %e,
"风险评分刷新失败"
);
}
}
Ok(total)
}
}