feat: 路线图R1决策建议一键落地+R2机会推送+R5预算闭环
R1(P0): AI建议一键应用到KPI/预算/行动方案
- 新表 ai_suggestions + AISuggestion 模型(init_db自动建)
- /api/cma/ai/suggestions CRUD + /{id}/apply(复用kpis/budget/action_plans) + dismiss
- 应用写 OperationLog(action=ai_suggestion_apply, detail含suggestion_id/before/after)
- 规则驱动建议生成 generate_rule_suggestions(低执行率/高执行率/预算超支/pending预警)
- 幂等: 同entity+type+target_id+title+unapplied不重复建; applied后拒绝重复应用
- 前端: Dashboard AI面板建议卡(应用到/忽略) + 建议中心页 /ai-suggestions
R2(P1): 数据找人扩大-机会类推送
- scripts/opportunity_detector.py: KPI向好(执行率>110%)/预算余量(<70%且actual>0)/预测上行
- scripts/daily_push.py: 异常+机会 每日9:15推企微(8800/send, --dry-run调试)
- crontab: 15 9 * * * (alert_generator 9:00之后)
R5(P0): 预算闭环加固
- auto-decompose批量幂等: 只取年度行(period=YYYY-00)+同KPI多版本取一行
- scripts/closed_loop_check.py: 预算执行率异常→检查现金流/行动同步→缺失提示+报告
- scripts/verify_decompose_idempotent.py: 幂等验证脚本
测试: test_ai_suggestions(10例)+test_roadmap_r2r5(14例); 修test_budget幂等契约适配年度行
全量: 673 passed
This commit is contained in:
+201
-10
@@ -2,17 +2,185 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func, text as sa_text
|
||||
from sqlalchemy import func, text as sa_text, or_
|
||||
from app.database import get_db
|
||||
from app.deps import get_entity_id
|
||||
from app.auth_middleware import require_auth, require_role
|
||||
from app.models import KPIDefinition, KPIValue, KPIAlert, StrategicMap, User, ActionPlan
|
||||
from app.models import KPIDefinition, KPIValue, KPIAlert, StrategicMap, User, ActionPlan, BudgetPlan, AISuggestion
|
||||
from app.utils.cache import get as cache_get, set as cache_set
|
||||
import json, hashlib, httpx, os
|
||||
from datetime import datetime
|
||||
from datetime import datetime, date
|
||||
router = APIRouter(prefix="/api/cma/ai", tags=["AI分析"],
|
||||
dependencies=[Depends(require_role("ceo", "finance", "business", "it"))],
|
||||
)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# R1 决策建议生成(规则驱动,稳定可复现,落库 ai_suggestions)
|
||||
# ============================================================
|
||||
def _sug_dict(s: AISuggestion) -> dict:
|
||||
return {
|
||||
"id": s.id,
|
||||
"entity_id": s.entity_id,
|
||||
"source": s.source,
|
||||
"suggestion_type": s.suggestion_type,
|
||||
"target_type": s.target_type,
|
||||
"target_id": s.target_id,
|
||||
"title": s.title,
|
||||
"content": s.content,
|
||||
"suggestion_data": s.suggestion_data or {},
|
||||
"status": s.status,
|
||||
"applied_by": s.applied_by,
|
||||
"applied_at": s.applied_at.isoformat() if s.applied_at else None,
|
||||
"apply_detail": s.apply_detail or [],
|
||||
"created_at": s.created_at.isoformat() if s.created_at else None,
|
||||
}
|
||||
|
||||
|
||||
def _existing_unapplied(db: Session, entity_id: int, suggestion_type: str,
|
||||
target_id: int, title: str) -> bool:
|
||||
"""幂等:同entity+类型+目标+标题的未应用建议存在则跳过"""
|
||||
return db.query(AISuggestion).filter(
|
||||
AISuggestion.entity_id == entity_id,
|
||||
AISuggestion.suggestion_type == suggestion_type,
|
||||
AISuggestion.target_id == target_id,
|
||||
AISuggestion.title == title,
|
||||
AISuggestion.status == "unapplied",
|
||||
).first() is not None
|
||||
|
||||
|
||||
def generate_rule_suggestions(db: Session, entity_id: int,
|
||||
source: str = "dashboard", user_id: int = None,
|
||||
kpi_id: int = None) -> list:
|
||||
"""从数据规则生成决策建议并落库(R1,路线图2026-08-30)
|
||||
|
||||
规则:
|
||||
1. KPI执行率<70% → 建议建行动方案(异常类)
|
||||
2. KPI执行率>110% → 建议上调KPI目标(机会类)
|
||||
3. 预算执行率>110% → 建议调预算(预算类)
|
||||
4. 有pending预警 → 建议建行动方案处理预警
|
||||
幂等:同 entity+type+target_id+title+status=unapplied 不重复建。
|
||||
"""
|
||||
now = datetime.now()
|
||||
period = now.strftime("%Y-%m")
|
||||
created = []
|
||||
|
||||
def _add(suggestion_type: str, target_type: str, tid: int,
|
||||
title: str, content: str, suggestion_data: dict):
|
||||
nonlocal created
|
||||
if _existing_unapplied(db, entity_id, suggestion_type, tid, title):
|
||||
return
|
||||
sug = AISuggestion(
|
||||
entity_id=entity_id,
|
||||
user_id=user_id,
|
||||
source=source,
|
||||
suggestion_type=suggestion_type,
|
||||
target_type=target_type,
|
||||
target_id=tid,
|
||||
title=title,
|
||||
content=content,
|
||||
suggestion_data=suggestion_data,
|
||||
status="unapplied",
|
||||
)
|
||||
db.add(sug)
|
||||
created.append(sug)
|
||||
|
||||
# 查询KPI(可按kpi_id过滤)
|
||||
q = db.query(KPIDefinition).filter(KPIDefinition.entity_id == entity_id,
|
||||
KPIDefinition.status == "active")
|
||||
if kpi_id:
|
||||
q = q.filter(KPIDefinition.id == kpi_id)
|
||||
kpis = q.all()
|
||||
|
||||
for k in kpis:
|
||||
latest = db.query(KPIValue).filter(
|
||||
KPIValue.kpi_id == k.id,
|
||||
or_(
|
||||
KPIValue.entity_id == entity_id,
|
||||
KPIValue.entity_id.is_(None),
|
||||
),
|
||||
).order_by(KPIValue.period.desc()).first()
|
||||
if not latest or latest.actual_value is None:
|
||||
continue
|
||||
actual = latest.actual_value
|
||||
target = k.target_value
|
||||
ratio = (actual / target) if target else None
|
||||
|
||||
# 1. 异常:执行率<70% → 建行动方案
|
||||
if ratio is not None and ratio < 0.7:
|
||||
title = f"提升 {k.kpi_name}:达成率仅{ratio*100:.0f}%"
|
||||
content = (f"KPI[{k.kpi_name}] 最新期间{latest.period}实际值{actual:g},"
|
||||
f"目标{target:g},达成率{ratio*100:.1f}%,低于70%预警线。"
|
||||
f"建议制定专项改善行动方案。")
|
||||
_add("action_plan", "kpi", k.id, title, content, {
|
||||
"kpi_id": k.id, "priority": "high",
|
||||
"title": f"改善: {k.kpi_name}达成率提升",
|
||||
})
|
||||
# 2. 机会:执行率>110% → 上调KPI目标
|
||||
elif ratio is not None and ratio > 1.1:
|
||||
new_target = round(actual * 1.05, 2)
|
||||
title = f"上调 {k.kpi_name} 目标:达成率{ratio*100:.0f}%超预期"
|
||||
content = (f"KPI[{k.kpi_name}] 达成率{ratio*100:.1f}%超过110%,"
|
||||
f"建议将目标从{target:g}上调至{new_target:g},保持牵引力。")
|
||||
_add("kpi_target", "kpi", k.id, title, content, {
|
||||
"kpi_id": k.id, "target_value": new_target,
|
||||
})
|
||||
|
||||
# 3. 预算执行率>110% → 调预算
|
||||
budget_rows = db.query(BudgetPlan).filter(
|
||||
BudgetPlan.entity_id == entity_id,
|
||||
BudgetPlan.status == "active",
|
||||
BudgetPlan.period == period,
|
||||
).all()
|
||||
for b in budget_rows:
|
||||
actual = db.query(func.max(KPIValue.actual_value)).filter(
|
||||
KPIValue.kpi_id == b.kpi_id,
|
||||
KPIValue.period == b.period,
|
||||
).scalar()
|
||||
if actual is None or b.budget_value is None or b.budget_value <= 0:
|
||||
continue
|
||||
exec_ratio = actual / b.budget_value
|
||||
if exec_ratio > 1.1:
|
||||
kpi_name = "KPI"
|
||||
k = db.query(KPIDefinition).filter(KPIDefinition.id == b.kpi_id).first()
|
||||
if k:
|
||||
kpi_name = k.kpi_name
|
||||
title = f"调整 {kpi_name} 预算:执行率{exec_ratio*100:.0f}%超预算"
|
||||
content = (f"预算[{kpi_name}] {period}预算值{b.budget_value:g},"
|
||||
f"实际{actual:g},执行率{exec_ratio*100:.1f}%超过110%。"
|
||||
f"建议同步调整预算/现金流/行动方案。")
|
||||
_add("budget_adjust", "budget", b.kpi_id, title, content, {
|
||||
"kpi_id": b.kpi_id, "period": period, "budget_value": round(actual, 2),
|
||||
})
|
||||
|
||||
# 4. pending预警 → 建行动方案
|
||||
alerts = db.query(KPIAlert).filter(KPIAlert.status == "pending").all()
|
||||
for a in alerts:
|
||||
k = db.query(KPIDefinition).filter(KPIDefinition.id == a.kpi_id).first()
|
||||
kpi_name = k.kpi_name if k else f"KPI#{a.kpi_id}"
|
||||
title = f"处理预警:{kpi_name} {a.alert_message[:30]}"
|
||||
content = f"存在待处理预警({a.alert_level}级):{a.alert_message}。建议建立行动方案跟进。"
|
||||
_add("action_plan", "alert", a.id, title, content, {
|
||||
"kpi_id": a.kpi_id, "priority": "high" if a.alert_level == "red" else "medium",
|
||||
"alert_id": a.id,
|
||||
"title": f"处理预警: {kpi_name}",
|
||||
})
|
||||
|
||||
if created:
|
||||
db.commit()
|
||||
for s in created:
|
||||
db.refresh(s)
|
||||
return created
|
||||
|
||||
|
||||
def _unapplied_suggestions(db: Session, entity_id: int, limit: int = 20) -> list:
|
||||
items = db.query(AISuggestion).filter(
|
||||
AISuggestion.entity_id == entity_id,
|
||||
AISuggestion.status == "unapplied",
|
||||
).order_by(AISuggestion.created_at.desc()).limit(limit).all()
|
||||
return [_sug_dict(s) for s in items]
|
||||
|
||||
|
||||
async def _call_deepseek(prompt: str) -> str:
|
||||
"""调用DeepSeek API"""
|
||||
api_key = os.getenv("DEEPSEEK_API_KEY", "sk-8e24e6eb87f2475e96ea0980002dc2e8")
|
||||
@@ -34,15 +202,23 @@ async def _call_deepseek(prompt: str) -> str:
|
||||
return data.get("choices", [{}])[0].get("message", {}).get("content", "")
|
||||
|
||||
@router.get("/dashboard-analysis")
|
||||
async def dashboard_analysis(role: str = Query("ceo"), db: Session = Depends(get_db)):
|
||||
async def dashboard_analysis(role: str = Query("ceo"), db: Session = Depends(get_db),
|
||||
entity_id: int = Depends(get_entity_id)):
|
||||
"""AI分析驾驶舱数据"""
|
||||
# 尝试缓存
|
||||
cache_key = f"dashboard_analysis:{role}"
|
||||
cache_key = f"dashboard_analysis:{role}:{entity_id}"
|
||||
cached = cache_get("ai", cache_key)
|
||||
if cached:
|
||||
# 缓存命中(LLM文本10分钟内不重复调用),但轻量规则建议仍执行(幂等)
|
||||
try:
|
||||
generate_rule_suggestions(db, entity_id, source="dashboard")
|
||||
except Exception:
|
||||
pass
|
||||
cached["suggestions"] = _unapplied_suggestions(db, entity_id)
|
||||
return cached
|
||||
# 获取当前KPI数据
|
||||
kpis = db.query(KPIDefinition).filter(KPIDefinition.status == "active").all()
|
||||
kpis = db.query(KPIDefinition).filter(KPIDefinition.entity_id == entity_id,
|
||||
KPIDefinition.status == "active").all()
|
||||
kpi_summary = []
|
||||
for k in kpis:
|
||||
latest = db.query(KPIValue).filter(KPIValue.kpi_id == k.id).order_by(KPIValue.period.desc()).first()
|
||||
@@ -81,17 +257,25 @@ async def dashboard_analysis(role: str = Query("ceo"), db: Session = Depends(get
|
||||
except Exception as e:
|
||||
analysis = f"AI分析暂时不可用: {str(e)}"
|
||||
|
||||
result = {"analysis": analysis, "kpi_count": len(kpi_summary), "alert_count": alerts}
|
||||
# R1: 规则驱动生成可落地决策建议(幂等落库)
|
||||
try:
|
||||
generate_rule_suggestions(db, entity_id, source="dashboard")
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
result = {"analysis": analysis, "kpi_count": len(kpi_summary), "alert_count": alerts,
|
||||
"suggestions": _unapplied_suggestions(db, entity_id)}
|
||||
# 缓存10分钟
|
||||
cache_set("ai", cache_key, result, ttl_seconds=600)
|
||||
return result
|
||||
|
||||
|
||||
@router.get("/kpi-analysis/{kpi_id}")
|
||||
async def kpi_analysis(kpi_id: int, db: Session = Depends(get_db)):
|
||||
async def kpi_analysis(kpi_id: int, db: Session = Depends(get_db),
|
||||
entity_id: int = Depends(get_entity_id)):
|
||||
"""AI分析单个KPI"""
|
||||
# 尝试缓存
|
||||
cache_key = f"kpi_analysis:{kpi_id}"
|
||||
cache_key = f"kpi_analysis:{kpi_id}:{entity_id}"
|
||||
cached = cache_get("ai", cache_key)
|
||||
if cached:
|
||||
return cached
|
||||
@@ -129,7 +313,14 @@ KPI名称:{kpi.kpi_name}
|
||||
except Exception as e:
|
||||
analysis = f"分析暂时不可用: {str(e)}"
|
||||
|
||||
result = {"kpi_name": kpi.kpi_name, "analysis": analysis}
|
||||
# R1: 生成该KPI的可落地建议
|
||||
try:
|
||||
generate_rule_suggestions(db, entity_id, source="kpi", kpi_id=kpi_id)
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
result = {"kpi_name": kpi.kpi_name, "analysis": analysis,
|
||||
"suggestions": _unapplied_suggestions(db, entity_id)}
|
||||
cache_set("ai", cache_key, result, ttl_seconds=600)
|
||||
return result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user