diff --git a/crates/erp-ai/src/service/analysis_queue.rs b/crates/erp-ai/src/service/analysis_queue.rs index 219696d..d5d0017 100644 --- a/crates/erp-ai/src/service/analysis_queue.rs +++ b/crates/erp-ai/src/service/analysis_queue.rs @@ -88,15 +88,21 @@ impl AnalysisQueue { Ok(id) } - pub async fn claim_next(&self) -> AiResult> { - let sql = r#" - SELECT * FROM ai_analysis_queue - WHERE status = 'pending' - AND deleted_at IS NULL - AND scheduled_at <= NOW() - ORDER BY priority DESC, scheduled_at ASC - LIMIT 1 - "#; + pub async fn claim_next(&self, tenant_id: Option) -> AiResult> { + let sql = match tenant_id { + Some(tid) => format!( + "SELECT * FROM ai_analysis_queue WHERE tenant_id = '{}' AND status = 'pending' AND deleted_at IS NULL AND scheduled_at <= NOW() ORDER BY priority DESC, scheduled_at ASC LIMIT 1", + tid + ), + None => r#" + SELECT * FROM ai_analysis_queue + WHERE status = 'pending' + AND deleted_at IS NULL + AND scheduled_at <= NOW() + ORDER BY priority DESC, scheduled_at ASC + LIMIT 1 + "#.to_string(), + }; let row: Option = QueueRow::find_by_statement( Statement::from_string(sea_orm::DatabaseBackend::Postgres, sql.to_string()),