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

@@ -312,6 +312,25 @@ impl ErpModule for AiModule {
let copilot_handles = crate::event::copilot_consumer::spawn(&ctx.db, &ctx.event_bus);
std::mem::forget(copilot_handles);
// 每日凌晨 2:00 批量刷新所有在管患者风险快照
let refresh_db = ctx.db.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(std::time::Duration::from_secs(86400));
loop {
interval.tick().await;
match crate::service::risk_service::RiskService::refresh_all_patients(&refresh_db)
.await
{
Ok(count) => {
tracing::info!(patient_count = count, "每日风险快照刷新完成");
}
Err(e) => {
tracing::warn!(error = %e, "每日风险快照刷新失败");
}
}
}
});
tracing::info!(
module = "ai",
"AI 模块事件处理器已注册(监听 ai.* 事件 + Copilot 事件)"