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:
@@ -205,17 +205,87 @@
|
||||
</el-card>
|
||||
</template>
|
||||
|
||||
<!-- AI分析浮窗 -->
|
||||
<!-- AI分析浮窗(R1:AI建议→一键落地) -->
|
||||
<div v-if="showSidebar" class="ai-panel">
|
||||
<div class="ai-head"><span>🤖 AI 分析</span><el-button text size="small" @click="showSidebar = false">✕</el-button></div>
|
||||
<div class="ai-head">
|
||||
<span>🤖 AI 分析</span>
|
||||
<el-button text size="small" @click="router.push('/ai-suggestions')">建议中心</el-button>
|
||||
<el-button text size="small" @click="showSidebar = false">✕</el-button>
|
||||
</div>
|
||||
<div class="ai-body">
|
||||
<div class="ai-section-title">📋 决策建议</div>
|
||||
<div v-if="suggestionsLoading" class="ai-loading"><el-icon class="is-loading" :size="16"><Loading /></el-icon><p>加载建议...</p></div>
|
||||
<div v-else-if="suggestions.length === 0" class="ai-empty">暂无待应用建议</div>
|
||||
<div v-else class="sug-list">
|
||||
<div v-for="s in suggestions" :key="s.id" class="sug-card" :class="'type-' + s.suggestion_type">
|
||||
<div class="sug-top">
|
||||
<el-tag size="small" :type="sugTagType(s.suggestion_type)">{{ sugTypeLabel(s.suggestion_type) }}</el-tag>
|
||||
<el-tag v-if="s.status === 'applied'" size="small" type="success">已应用</el-tag>
|
||||
<el-tag v-else size="small" type="info">未应用</el-tag>
|
||||
</div>
|
||||
<div class="sug-title">{{ s.title }}</div>
|
||||
<div class="sug-content">{{ s.content }}</div>
|
||||
<div class="sug-foot">
|
||||
<el-button v-if="s.status === 'unapplied'" size="small" type="primary" @click="openApplyDialog(s)">应用到</el-button>
|
||||
<el-button v-if="s.status === 'unapplied'" size="small" @click="dismissSuggestion(s)">忽略</el-button>
|
||||
<span v-if="s.status === 'applied'" class="sug-applied-by">由 {{ s.applied_by }} 于 {{ (s.applied_at || '').slice(0, 16) }} 应用</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<el-divider />
|
||||
<div class="ai-section-title">💡 AI分析文本</div>
|
||||
<div v-if="aiLoading" class="ai-loading"><el-icon class="is-loading" :size="20"><Loading /></el-icon><p>分析中...</p></div>
|
||||
<div v-else-if="aiAnalysis" class="ai-content" v-html="renderMd(aiAnalysis)"></div>
|
||||
<el-empty v-else description="暂无分析" />
|
||||
</div>
|
||||
<div class="ai-foot"><el-button size="small" type="primary" @click="refreshAnalysis" :loading="aiLoading">刷新</el-button></div>
|
||||
<div class="ai-foot">
|
||||
<el-button size="small" type="primary" @click="refreshAnalysis" :loading="aiLoading">刷新分析</el-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 应用建议弹窗 -->
|
||||
<el-dialog v-model="showApplyDialog" :title="'应用到:' + (applySug?.title || '')" width="520px">
|
||||
<el-form label-width="100px">
|
||||
<template v-if="applySug?.suggestion_type === 'kpi_target'">
|
||||
<el-form-item label="KPI"><el-input :model-value="applyKpiName" disabled /></el-form-item>
|
||||
<el-form-item label="新目标值" required>
|
||||
<el-input-number v-model="applyForm.target_value" :precision="2" style="width:220px" />
|
||||
</el-form-item>
|
||||
<div class="apply-tip">应用后将修改KPI目标值,并写入操作日志留痕。</div>
|
||||
</template>
|
||||
<template v-else-if="applySug?.suggestion_type === 'budget_adjust'">
|
||||
<el-form-item label="KPI"><el-input :model-value="applyKpiName" disabled /></el-form-item>
|
||||
<el-form-item label="期间" required>
|
||||
<el-input v-model="applyForm.period" placeholder="2026-09" style="width:220px" />
|
||||
</el-form-item>
|
||||
<el-form-item label="预算值" required>
|
||||
<el-input-number v-model="applyForm.budget_value" :precision="2" style="width:220px" />
|
||||
</el-form-item>
|
||||
<div class="apply-tip">应用后将新增/更新该KPI对应期间的预算,并写入操作日志留痕。</div>
|
||||
</template>
|
||||
<template v-else>
|
||||
<el-form-item label="关联KPI"><el-input :model-value="applyKpiName" disabled /></el-form-item>
|
||||
<el-form-item label="计划标题" required><el-input v-model="applyForm.title" /></el-form-item>
|
||||
<el-form-item label="负责人"><el-input v-model="applyForm.assignee" /></el-form-item>
|
||||
<el-form-item label="优先级">
|
||||
<el-select v-model="applyForm.priority" style="width:220px">
|
||||
<el-option label="高" value="high" />
|
||||
<el-option label="中" value="medium" />
|
||||
<el-option label="低" value="low" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="截止日期">
|
||||
<el-date-picker v-model="applyForm.due_date" type="date" value-format="YYYY-MM-DD" style="width:220px" />
|
||||
</el-form-item>
|
||||
<div class="apply-tip">应用后将创建行动方案,并写入操作日志留痕。</div>
|
||||
</template>
|
||||
</el-form>
|
||||
<template #footer>
|
||||
<el-button @click="showApplyDialog = false">取消</el-button>
|
||||
<el-button type="primary" :loading="applying" @click="submitApply">确认应用</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
|
||||
<!-- KPI详情弹窗 -->
|
||||
<el-dialog v-model="showDetail" :title="selectedKPI?.kpi_name" width="500px">
|
||||
<div v-if="selectedKPI">
|
||||
@@ -241,7 +311,7 @@ import { ref, computed, onMounted } from 'vue'
|
||||
import { useRouter } from 'vue-router'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { Loading, ArrowRight } from '@element-plus/icons-vue'
|
||||
import { dashboardApi, alertApi } from '../api/index'
|
||||
import { dashboardApi, alertApi, aiSuggestionApi } from '../api/index'
|
||||
import axios from 'axios'
|
||||
import CountUp from '../components/CountUp.vue'
|
||||
import GrowthQuality from '../components/GrowthQuality.vue'
|
||||
@@ -299,6 +369,81 @@ const enabledChannels = ref(0)
|
||||
const aiAnalysis = ref('')
|
||||
const aiLoading = ref(false)
|
||||
|
||||
// R1: AI决策建议(建议卡 + 应用到弹窗)
|
||||
const suggestions = ref<any[]>([])
|
||||
const suggestionsLoading = ref(false)
|
||||
const showApplyDialog = ref(false)
|
||||
const applySug = ref<any>(null)
|
||||
const applyForm = ref<any>({})
|
||||
const applying = ref(false)
|
||||
const applyKpiName = ref('')
|
||||
|
||||
const sugTypeLabel = (t: string) => ({ kpi_target: '改KPI目标', budget_adjust: '调预算', action_plan: '建行动方案' }[t] || t)
|
||||
const sugTagType = (t: string) => ({ kpi_target: 'warning', budget_adjust: 'danger', action_plan: 'primary' }[t] || 'info')
|
||||
|
||||
async function loadSuggestions() {
|
||||
suggestionsLoading.value = true
|
||||
try {
|
||||
const r: any = await aiSuggestionApi.list({ status: 'unapplied' })
|
||||
suggestions.value = (r as any)?.data || []
|
||||
} catch { suggestions.value = [] }
|
||||
suggestionsLoading.value = false
|
||||
}
|
||||
|
||||
function openApplyDialog(s: any) {
|
||||
applySug.value = s
|
||||
const sd = s.suggestion_data || {}
|
||||
applyForm.value = {
|
||||
target_value: sd.target_value ?? null,
|
||||
period: sd.period || '',
|
||||
budget_value: sd.budget_value ?? null,
|
||||
title: sd.title || '',
|
||||
assignee: sd.assignee || '',
|
||||
priority: sd.priority || 'medium',
|
||||
due_date: sd.due_date || '',
|
||||
}
|
||||
applyKpiName.value = s.target_name || ''
|
||||
showApplyDialog.value = true
|
||||
}
|
||||
|
||||
async function submitApply() {
|
||||
if (!applySug.value) return
|
||||
const action = applySug.value.suggestion_type
|
||||
const body: any = { action, kpi_id: applySug.value.target_id || applySug.value.suggestion_data?.kpi_id }
|
||||
if (action === 'kpi_target') {
|
||||
if (applyForm.value.target_value == null) { ElMessage.warning('请输入新目标值'); return }
|
||||
body.target_value = applyForm.value.target_value
|
||||
} else if (action === 'budget_adjust') {
|
||||
if (!applyForm.value.period || applyForm.value.budget_value == null) { ElMessage.warning('请填写期间和预算值'); return }
|
||||
body.period = applyForm.value.period
|
||||
body.budget_value = applyForm.value.budget_value
|
||||
} else {
|
||||
if (!applyForm.value.title) { ElMessage.warning('请输入计划标题'); return }
|
||||
body.title = applyForm.value.title
|
||||
body.assignee = applyForm.value.assignee
|
||||
body.priority = applyForm.value.priority
|
||||
body.due_date = applyForm.value.due_date
|
||||
}
|
||||
applying.value = true
|
||||
try {
|
||||
await aiSuggestionApi.apply(applySug.value.id, body)
|
||||
ElMessage.success('建议已应用并留痕')
|
||||
showApplyDialog.value = false
|
||||
await loadSuggestions()
|
||||
} catch (e: any) {
|
||||
ElMessage.error(e?.response?.data?.detail || '应用失败')
|
||||
}
|
||||
applying.value = false
|
||||
}
|
||||
|
||||
async function dismissSuggestion(s: any) {
|
||||
try {
|
||||
await aiSuggestionApi.dismiss(s.id)
|
||||
ElMessage.success('建议已忽略')
|
||||
await loadSuggestions()
|
||||
} catch { ElMessage.error('忽略失败') }
|
||||
}
|
||||
|
||||
const aiApi = axios.create({ baseURL: '/api/cma', timeout: 30000 })
|
||||
aiApi.interceptors.request.use((config: any) => {
|
||||
const token = localStorage.getItem('cma_token')
|
||||
@@ -376,8 +521,8 @@ async function loadKPIAnalysis() {
|
||||
if (!selectedKPI.value?.id) return
|
||||
loadingDetail.value = true
|
||||
try {
|
||||
const r: any = await aiApi.post('/ai/kpi-analysis', { kpi_id: selectedKPI.value.id, period: periodType.value })
|
||||
selectedKPIDetail.value = r.data || r.analysis || '暂无分析结果'
|
||||
const r: any = await aiApi.get(`/ai/kpi-analysis/${selectedKPI.value.id}`)
|
||||
selectedKPIDetail.value = r.data?.analysis || r.analysis || '暂无分析结果'
|
||||
} catch { selectedKPIDetail.value = 'AI分析请求失败' }
|
||||
loadingDetail.value = false
|
||||
}
|
||||
@@ -385,8 +530,10 @@ async function loadKPIAnalysis() {
|
||||
async function refreshAnalysis() {
|
||||
aiLoading.value = true
|
||||
try {
|
||||
const r: any = await aiApi.post('/ai/dashboard-analysis', { role: userRole.value, period: periodType.value })
|
||||
aiAnalysis.value = r.data || r.analysis || '暂无分析'
|
||||
const r: any = await aiApi.get('/ai/dashboard-analysis', { params: { role: userRole.value } })
|
||||
aiAnalysis.value = r.data?.analysis || r.analysis || '暂无分析'
|
||||
// 建议列表从服务端拉取(含dashboard分析自动生成的规则建议)
|
||||
await loadSuggestions()
|
||||
} catch { aiAnalysis.value = 'AI分析暂时不可用' }
|
||||
aiLoading.value = false
|
||||
}
|
||||
@@ -555,6 +702,21 @@ onMounted(() => { loadData() })
|
||||
.ai-foot { padding:10px 16px; border-top:1px solid #f0f0f0; }
|
||||
.ai-loading { text-align:center; padding:30px 0; color:#999; }
|
||||
|
||||
/* R1: 建议卡 */
|
||||
.ai-section-title { font-size:13px; font-weight:600; color:#333; margin-bottom:10px; }
|
||||
.ai-empty { text-align:center; color:#bbb; padding:16px 0; font-size:12px; }
|
||||
.sug-list { display:flex; flex-direction:column; gap:10px; }
|
||||
.sug-card { border:1px solid #eee; border-radius:8px; padding:10px 12px; background:#fafafa; border-left:3px solid #909399; }
|
||||
.sug-card.type-kpi_target { border-left-color:#e6a23c; }
|
||||
.sug-card.type-budget_adjust { border-left-color:#f56c6c; }
|
||||
.sug-card.type-action_plan { border-left-color:#409eff; }
|
||||
.sug-top { display:flex; gap:6px; margin-bottom:6px; }
|
||||
.sug-title { font-size:13px; font-weight:600; color:#333; margin-bottom:4px; }
|
||||
.sug-content { font-size:12px; color:#666; line-height:1.5; margin-bottom:8px; }
|
||||
.sug-foot { display:flex; gap:6px; align-items:center; }
|
||||
.sug-applied-by { font-size:11px; color:#999; }
|
||||
.apply-tip { font-size:12px; color:#999; padding:0 0 10px 100px; }
|
||||
|
||||
/* KPI详情弹窗 */
|
||||
.dlg-summary { display:flex; justify-content:space-between; align-items:center; margin-bottom:12px; }
|
||||
.kpi-bar.large { height:10px; background:#f0f0f0; border-radius:5px; overflow:hidden; }
|
||||
|
||||
Reference in New Issue
Block a user