Commit d2b02056 by 江名洋

feat(strategy): 支持单条策略分析及列表查询重构

- 新增单条策略分析接口,支持对指定策略执行分析并保存结果
- 重构策略列表查询,改为以主表 LEFT JOIN 结果表方式,同时展示未分析策略
- 新增 monitor_refresh_time 字段及旧表兼容处理
- 兜底类型判断改为从 ods_guarantee_config 配置表查询
- 详情接口新增周维度明细与前后对比数据实时计算
- 前端移除全局分析任务栏,改为每行独立刷新分析按钮
1 parent 0a2bdd1a
......@@ -601,6 +601,21 @@ async def strategy_trigger_analyze():
raise HTTPException(status_code=500, detail=f"触发分析任务失败: {str(e)}")
@router.post("/strategy/analyze/{strategy_id}")
async def strategy_analyze_single(strategy_id: int):
"""对单条策略执行分析并保存结果"""
try:
result = StrategyService.analyze_and_save_single(strategy_id)
if result is None:
raise HTTPException(status_code=404, detail=f"策略 #{strategy_id} 未找到或分析失败")
return {"status": "success", "message": f"策略 #{strategy_id} 分析完成", "data": result}
except HTTPException:
raise
except Exception as e:
logger.error(f"Single strategy analyze failed: {e}", exc_info=True)
raise HTTPException(status_code=500, detail=f"单条策略分析失败: {str(e)}")
@router.get("/strategy/analyze/status")
async def strategy_analyze_status():
"""获取策略分析任务进度状态"""
......
......@@ -71,9 +71,6 @@
.effect-tag.down { background: #fff7e6; color: #fa8c16; }
.effect-tag.down-strong { background: #fff2f0; color: #f5222d; }
.btn-detail { padding: 5px 12px; border: 1px solid #d9d9d9; background: #fff; border-radius: 4px; cursor: pointer; font-size: 12px; color: #666; }
.btn-detail:hover { color: #1890ff; border-color: #1890ff; }
.pagination { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin-top: 16px; }
.pagination button { padding: 5px 12px; border: 1px solid #d9d9d9; background: #fff; border-radius: 4px; cursor: pointer; font-size: 12px; }
.pagination button:hover:not(:disabled) { color: #1890ff; border-color: #1890ff; }
......@@ -84,14 +81,12 @@
.loading-mask { text-align: center; padding: 40px; color: #999; font-size: 13px; }
.empty-tip { text-align: center; padding: 40px; color: #999; }
.task-bar { background: #fff; border-radius: 8px; padding: 12px 16px; margin-bottom: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.08); display: flex; align-items: center; gap: 12px; }
.task-bar .btn-analyze { padding: 6px 16px; background: #1890ff; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 13px; white-space: nowrap; }
.task-bar .btn-analyze:disabled { background: #ccc; cursor: not-allowed; }
.task-info { font-size: 13px; color: #666; }
.task-info .running-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #52c41a; animation: pulse 1.2s infinite; margin-right: 4px; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }
.progress-bar { flex: 1; min-width: 120px; height: 6px; background: #f0f0f0; border-radius: 3px; overflow: hidden; }
.progress-bar .fill { height: 100%; background: #1890ff; border-radius: 3px; transition: width 0.3s; }
.btn-analyze { padding: 5px 12px; background: #1890ff; color: #fff; border: none; border-radius: 4px; cursor: pointer; font-size: 12px; white-space: nowrap; }
.btn-analyze:disabled { background: #ccc; cursor: not-allowed; }
.btn-analyze.analyzing { background: #fa8c16; }
.btn-detail { padding: 5px 12px; border: 1px solid #d9d9d9; background: #fff; border-radius: 4px; cursor: pointer; font-size: 12px; color: #666; }
.btn-detail:hover { color: #1890ff; border-color: #1890ff; }
.row-actions { display: flex; gap: 6px; justify-content: center; }
/* 详情弹窗 */
.modal { display: none; position: fixed; z-index: 1000; left: 0; top: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.45); overflow: auto; }
......@@ -163,15 +158,6 @@
</div>
</div>
<!-- 分析任务栏 -->
<div class="task-bar" id="task-bar">
<button class="btn-analyze" id="btn-analyze" onclick="triggerAnalyze()">刷新分析</button>
<div class="task-info" id="task-info">就绪,点击按钮开始分析</div>
<div class="progress-bar" id="progress-bar" style="display:none">
<div class="fill" id="progress-fill" style="width:0%"></div>
</div>
</div>
<!-- 筛选栏 -->
<div class="filter-bar">
<input type="text" id="filter-keyword" placeholder="搜索门店名称">
......@@ -287,7 +273,7 @@
'<td class="right"><div class="change-cell"><span class="change-ratio ' + getChangeClass(ax.pp_change) + '">' + (ax.pp_change != null ? (ax.pp_change > 0 ? '↑' : (ax.pp_change < 0 ? '↓' : '→')) + ' ' + Math.abs(ax.pp_change).toFixed(1) + 'pp' : '-') + '</span><span class="change-detail">' + (ax.old_rate != null ? ax.old_rate.toFixed(1) + '%→' + ax.new_rate.toFixed(1) + '%' : '-') + '</span></div></td>',
'<td class="right"><div class="change-cell"><span class="change-ratio ' + getChangeClass(bt.pp_change) + '">' + (bt.pp_change != null ? (bt.pp_change > 0 ? '↑' : (bt.pp_change < 0 ? '↓' : '→')) + ' ' + Math.abs(bt.pp_change).toFixed(1) + 'pp' : '-') + '</span><span class="change-detail">' + (bt.old_rate != null ? bt.old_rate.toFixed(1) + '%→' + bt.new_rate.toFixed(1) + '%' : '-') + '</span></div></td>',
'<td class="center"><span class="effect-tag ' + row.effect_class + '">' + escapeHtml(row.effect || '-') + '</span></td>',
'<td class="center"><button class="btn-detail" onclick="showDetail(' + row.id + ')">详情</button></td>',
'<td class="center"><div class="row-actions"><button class="btn-analyze" onclick="analyzeSingle(' + row.id + ', this)">刷新分析</button><a class="btn-detail" href="./strategy/detail?id=' + row.id + '">详情</a></div></td>',
'</tr>');
});
......@@ -476,105 +462,30 @@
}
};
let pollTimer = null;
async function triggerAnalyze() {
const btn = document.getElementById('btn-analyze');
const info = document.getElementById('task-info');
async function analyzeSingle(id, btn) {
btn.disabled = true;
btn.textContent = '触发中...';
info.innerHTML = '正在启动分析任务...';
btn.textContent = '分析中...';
btn.classList.add('analyzing');
try {
const res = await fetch(window.API_BASE + '/strategy/analyze', { method: 'POST' });
const res = await fetch(window.API_BASE + '/strategy/analyze/' + id, { method: 'POST' });
const json = await res.json();
if (json.status === 'warning') {
info.innerHTML = '<span class="running-dot"></span>' + json.message;
btn.textContent = '分析中...';
btn.disabled = true;
startPolling();
} else if (json.status === 'success') {
info.innerHTML = json.message;
btn.textContent = '分析中...';
btn.disabled = true;
startPolling();
if (json.status === 'success') {
loadData();
} else {
info.innerHTML = '触发失败: ' + (json.detail || '未知错误');
btn.textContent = '刷新分析';
btn.disabled = false;
alert('分析失败: ' + (json.detail || '未知错误'));
}
} catch (err) {
info.innerHTML = '触发失败: ' + escapeHtml(err.message);
btn.textContent = '刷新分析';
alert('分析失败: ' + err.message);
} finally {
btn.disabled = false;
btn.textContent = '刷新分析';
btn.classList.remove('analyzing');
}
}
function startPolling() {
if (pollTimer) clearInterval(pollTimer);
pollTimer = setInterval(pollStatus, 2000);
}
function stopPolling() {
if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
}
async function pollStatus() {
try {
const res = await fetch(window.API_BASE + '/strategy/analyze/status');
const json = await res.json();
if (json.status !== 'success') return;
const s = json.data;
const btn = document.getElementById('btn-analyze');
const info = document.getElementById('task-info');
const bar = document.getElementById('progress-bar');
const fill = document.getElementById('progress-fill');
if (s.running) {
bar.style.display = 'flex';
const pct = s.total > 0 ? Math.round(s.completed / s.total * 100) : 0;
fill.style.width = pct + '%';
btn.textContent = '分析中...';
btn.disabled = true;
info.innerHTML = '<span class="running-dot"></span>分析中: ' + (s.completed + s.failed) + '/' + s.total + ' (成功 ' + s.completed + ', 失败 ' + s.failed + ')';
if (s.current) info.innerHTML += ' | 当前策略 #' + s.current;
} else {
bar.style.display = 'none';
stopPolling();
btn.textContent = '刷新分析';
btn.disabled = false;
if (s.total > 0) {
info.innerHTML = '完成: ' + s.completed + ' 条成功, ' + s.failed + ' 条失败 | 共 ' + s.total + ' 条 | ' + (s.finished_at ? s.finished_at.slice(0, 16) : '');
loadData();
} else {
info.innerHTML = '就绪,结果表中有缓存数据';
}
}
} catch (err) {
console.error('轮询状态失败:', err);
}
}
async function checkInitialStatus() {
try {
const res = await fetch(window.API_BASE + '/strategy/analyze/status');
const json = await res.json();
if (json.status !== 'success') return;
const s = json.data;
if (s.running) {
const btn = document.getElementById('btn-analyze');
btn.textContent = '分析中...';
btn.disabled = true;
const info = document.getElementById('task-info');
info.innerHTML = '<span class="running-dot"></span>分析中: ' + (s.completed + s.failed) + '/' + s.total;
startPolling();
}
} catch (err) {}
}
document.addEventListener('DOMContentLoaded', () => {
loadOptions();
loadData();
checkInitialStatus();
document.getElementById('btn-search').addEventListener('click', collectFilters);
document.getElementById('btn-reset').addEventListener('click', resetFilters);
......
......@@ -121,6 +121,7 @@ PAGE_ROUTES = {
"/analysis": "analysis.html",
"/harmony": "harmony.html",
"/strategy": "strategy.html",
"/strategy/detail": "strategy_detail.html",
}
......
......@@ -495,21 +495,27 @@ for col_idx, val in enumerate(sample_data_003, 1):
tws.row_dimensions[3].height = 18
# ── 条件格式 (与003一致) ──
# X3:X1000 门店等级变色(背景填充)
# ── 条件格式 (参照 输出模板-鸿蒙-模板008) ──
# X3:X1000 门店等级变色(字体色+背景填充)
tws.conditional_formatting.add('X3:X1000',
FormulaRule(formula=['$X3="红色门店"'], fill=PatternFill('solid', fgColor='FFC7CE')))
FormulaRule(formula=['$X3="红色门店"'],
fill=PatternFill('solid', fgColor='FFFF0000', bgColor='FFFF7C80')))
tws.conditional_formatting.add('X3:X1000',
FormulaRule(formula=['$X3="绿色门店"'], fill=PatternFill('solid', fgColor='C6EFCE')))
FormulaRule(formula=['$X3="绿色门店"'],
fill=PatternFill('solid', fgColor='FFC6EFCE', bgColor='FF92D050')))
tws.conditional_formatting.add('X3:X1000',
FormulaRule(formula=['$X3="黄色门店"'], fill=PatternFill('solid', fgColor='FFEB9C')))
FormulaRule(formula=['$X3="黄色门店"'],
fill=PatternFill('solid', fgColor='FFFFEB9C', bgColor='FFFFFF99')))
tws.conditional_formatting.add('X3:X1000',
FormulaRule(formula=['$X3="服务商"'], fill=PatternFill('solid', fgColor='E2EFDA')))
FormulaRule(formula=['$X3="服务商"'],
fill=PatternFill('solid', fgColor='FFE2EFDA', bgColor='FF99CCFF')))
tws.conditional_formatting.add('X3:X1000',
FormulaRule(formula=['$X3="暂无交车"'], fill=PatternFill('solid', fgColor='D9D9D9')))
FormulaRule(formula=['$X3="暂无交车"'],
fill=PatternFill('solid', fgColor='FFD9D9D9', bgColor='FFB2B2B2')))
# W3:W1000 触发兜底标红(背景填充)
tws.conditional_formatting.add('W3:W1000',
FormulaRule(formula=['$W3="是"'], fill=PatternFill('solid', fgColor='FF6B6B')))
FormulaRule(formula=['$W3="是"'],
fill=PatternFill('solid', fgColor='FFFF6B6B', bgColor='00000000')))
tws.freeze_panes = 'A3'
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!