feat(ai+health): 闭环核心 — 随访完成→再分析触发 + 前后对比报告

- follow_up.completed 消费者:通过 action_result 反查 AI 建议,触发再分析
- ai.reanalysis.requested 消费者:加载原始建议 baseline
- comparison.rs:对比报告生成引擎(指标变化百分比+趋势判断)
- GET /ai/suggestions/{id}/comparison:前后对比报告 API
- find_by_followup_task:通过随访任务反查关联建议ID
This commit is contained in:
iven
2026-05-01 09:14:13 +08:00
parent 0a4825be99
commit 5d2402a1e7
7 changed files with 355 additions and 0 deletions

View File

@@ -87,3 +87,43 @@ where
"status": new_status.as_str(),
}))))
}
/// 获取 AI 建议的前后对比报告。
pub async fn get_comparison<S>(
State(state): State<AiState>,
Extension(ctx): Extension<TenantContext>,
Path(id): Path<uuid::Uuid>,
) -> Result<Json<ApiResponse<serde_json::Value>>, erp_core::error::AppError>
where
AiState: FromRef<S>,
S: Clone + Send + Sync + 'static,
{
require_permission(&ctx, "ai.suggestion.list")?;
use crate::entity::ai_suggestion;
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
let suggestion = ai_suggestion::Entity::find_by_id(id)
.one(&state.db)
.await
.map_err(|e| erp_core::error::AppError::Internal(e.to_string()))?
.filter(|s| s.tenant_id == ctx.tenant_id && s.deleted_at.is_none())
.ok_or_else(|| erp_core::error::AppError::NotFound("建议不存在".into()))?;
match &suggestion.baseline_snapshot {
Some(bs) if !bs.is_null() => {
let action_result = suggestion.action_result.as_ref().unwrap_or(&serde_json::Value::Null);
Ok(Json(ApiResponse::ok(serde_json::json!({
"suggestion_id": id,
"baseline": bs,
"current": action_result,
"comparison_available": !action_result.is_null(),
}))))
}
_ => Ok(Json(ApiResponse::ok(serde_json::json!({
"suggestion_id": id,
"comparison_available": false,
"message": "该建议暂无 baseline 快照,无法生成对比报告",
})))),
}
}