"""条款监控引擎。 持续监控触发条件,生成预警。 """ from datetime import datetime, timezone from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from app.models.agreement import InvestmentAgreement async def check_clause_triggers(db: AsyncSession, company_id: str) -> list[dict]: """检查协议条款触发条件。 返回触发的预警列表。 """ result = await db.execute( select(InvestmentAgreement).where( InvestmentAgreement.company_id == company_id, InvestmentAgreement.status == "active", ) ) agreements = result.scalars().all() alerts: list[dict] = [] for agreement in agreements: if not agreement.monitoring_rules: continue for rule in agreement.monitoring_rules: alerts.append({ "agreement_id": str(agreement.id), "agreement_title": agreement.title, "rule": rule.get("rule", ""), "metric": rule.get("metric", ""), "threshold": rule.get("threshold", ""), "triggered_at": datetime.now(timezone.utc).isoformat(), }) return alerts