Commit 5b71ebc5 by 江名洋

feat(analysis): 门店数据改为可选并支持指定文件下载

1 parent 009ed313
...@@ -282,6 +282,11 @@ async def analysis_generate( ...@@ -282,6 +282,11 @@ async def analysis_generate(
global _analysis_store_file, _analysis_ecology_factory_file, _analysis_nature_factory_file global _analysis_store_file, _analysis_ecology_factory_file, _analysis_nature_factory_file
if not _analysis_store_file or not os.path.exists(_analysis_store_file): if not _analysis_store_file or not os.path.exists(_analysis_store_file):
# 未上传时使用模板库中的默认门店数据
default_store = os.path.join("app", "templates", "门店数据-模板.xlsx")
if os.path.exists(default_store):
_analysis_store_file = default_store
else:
raise HTTPException(status_code=400, detail="请先上传门店列表文件") raise HTTPException(status_code=400, detail="请先上传门店列表文件")
ecology_factory = None ecology_factory = None
...@@ -316,7 +321,7 @@ async def analysis_generate( ...@@ -316,7 +321,7 @@ async def analysis_generate(
"status": "success", "status": "success",
"message": "分析报表已生成", "message": "分析报表已生成",
"file_path": output_path, "file_path": output_path,
"download_url": "api/analysis/download", "download_url": f"api/analysis/download?file={os.path.basename(output_path)}",
"ecology_store_count": len(ecology_stores), "ecology_store_count": len(ecology_stores),
"nature_store_count": len(nature_stores), "nature_store_count": len(nature_stores),
"ecology_factory_sheets": list(ecology_factory.keys()) if ecology_factory else [], "ecology_factory_sheets": list(ecology_factory.keys()) if ecology_factory else [],
...@@ -328,9 +333,19 @@ async def analysis_generate( ...@@ -328,9 +333,19 @@ async def analysis_generate(
@router.get("/analysis/download") @router.get("/analysis/download")
async def analysis_download(): async def analysis_download(file: str = Query(None)):
"""下载生成的分析报表""" """下载生成的分析报表,支持指定文件名"""
output_dir = "temp_uploads" output_dir = "temp_uploads"
if file:
file_path = os.path.join(output_dir, file)
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="指定的报表文件不存在")
return FileResponse(
file_path, filename=file,
media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
)
# 无指定时返回最新文件
output_files = sorted( output_files = sorted(
[f for f in os.listdir(output_dir) if f.startswith("问界生态系数分析") and f.endswith(".xlsx")], [f for f in os.listdir(output_dir) if f.startswith("问界生态系数分析") and f.endswith(".xlsx")],
key=lambda f: os.path.getmtime(os.path.join(output_dir, f)), key=lambda f: os.path.getmtime(os.path.join(output_dir, f)),
......
...@@ -626,10 +626,12 @@ class AnalysisService: ...@@ -626,10 +626,12 @@ class AnalysisService:
output_dir = "temp_uploads" output_dir = "temp_uploads"
os.makedirs(output_dir, exist_ok=True) os.makedirs(output_dir, exist_ok=True)
from datetime import datetime
ts = datetime.now().strftime("%Y%m%d_%H%M%S_%f")[:18]
eco_label = ecology_start.replace("-", "") if ecology_start else "" eco_label = ecology_start.replace("-", "") if ecology_start else ""
nat_label = nature_start.replace("-", "") if nature_start else "" nat_label = nature_start.replace("-", "") if nature_start else ""
safe_label = f"生态{eco_label}_自然{nat_label}"[:70] safe_label = f"生态{eco_label}_自然{nat_label}"[:50]
output_path = os.path.join(output_dir, f"问界生态系数分析_{safe_label}.xlsx") output_path = os.path.join(output_dir, f"问界生态系数分析_{safe_label}_{ts}.xlsx")
shutil.copy2(TEMPLATE_FILE, output_path) shutil.copy2(TEMPLATE_FILE, output_path)
wb = load_workbook(output_path) wb = load_workbook(output_path)
......
...@@ -354,10 +354,6 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -354,10 +354,6 @@ document.addEventListener('DOMContentLoaded', () => {
const hasEcology = ecologyFactoryInput && ecologyFactoryInput.files.length > 0; const hasEcology = ecologyFactoryInput && ecologyFactoryInput.files.length > 0;
const hasNature = natureFactoryInput && natureFactoryInput.files.length > 0; const hasNature = natureFactoryInput && natureFactoryInput.files.length > 0;
if (!hasStore) {
alert('请先上传门店列表文件(步骤1)');
return;
}
if (!hasEcology && !hasNature) { if (!hasEcology && !hasNature) {
alert('请至少上传一个主机厂数据文件(步骤2或步骤3)'); alert('请至少上传一个主机厂数据文件(步骤2或步骤3)');
return; return;
...@@ -368,7 +364,7 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -368,7 +364,7 @@ document.addEventListener('DOMContentLoaded', () => {
try { try {
const uploadForm = new FormData(); const uploadForm = new FormData();
uploadForm.append('store_file', storeFileInput.files[0]); if (hasStore) uploadForm.append('store_file', storeFileInput.files[0]);
if (hasEcology) uploadForm.append('ecology_factory_file', ecologyFactoryInput.files[0]); if (hasEcology) uploadForm.append('ecology_factory_file', ecologyFactoryInput.files[0]);
if (hasNature) uploadForm.append('nature_factory_file', natureFactoryInput.files[0]); if (hasNature) uploadForm.append('nature_factory_file', natureFactoryInput.files[0]);
......
...@@ -96,13 +96,13 @@ ...@@ -96,13 +96,13 @@
<!-- 数据分析部分 --> <!-- 数据分析部分 -->
<div id="data-analysis-section" style="display:none;"> <div id="data-analysis-section" style="display:none;">
<div style="display: flex; flex-direction: column; gap: 10px; margin-bottom: 20px;"> <div style="display: flex; flex-direction: column; gap: 10px; margin-bottom: 20px;">
<!-- 步骤1: 上传门店列表 --> <!-- 步骤1: 上传门店列表(可选,未上传时使用默认模板) -->
<div style="background: #f8f9fa; padding: 8px 16px; border-radius: 6px; border: 1px solid #e8e8e8; display: flex; align-items: center; gap: 16px;"> <div style="background: #f8f9fa; padding: 8px 16px; border-radius: 6px; border: 1px solid #e8e8e8; display: flex; align-items: center; gap: 16px;">
<span style="font-size:14px; font-weight:600; color:#333; white-space:nowrap;">步骤1: 门店列表</span> <span style="font-size:14px; font-weight:600; color:#333; white-space:nowrap;">步骤1: 门店列表</span>
<div id="store-drop-zone" style="flex:1; border: 1px dashed #d9d9d9; padding: 6px 12px; border-radius: 4px; cursor: pointer; background: #fff; display: flex; align-items: center; gap: 8px;"> <div id="store-drop-zone" style="flex:1; border: 1px dashed #d9d9d9; padding: 6px 12px; border-radius: 4px; cursor: pointer; background: #fff; display: flex; align-items: center; gap: 8px;">
<input type="file" id="store-file-input" style="display:none;" accept=".xlsx,.xls"> <input type="file" id="store-file-input" style="display:none;" accept=".xlsx,.xls">
<span style="font-size:16px;">🏪</span> <span style="font-size:16px;">🏪</span>
<p id="store-file-name" style="margin:0; font-size:13px; color:#999;">点击选择门店列表文件</p> <p id="store-file-name" style="margin:0; font-size:13px; color:#999;">可选:点击选择门店列表文件(不选则使用系统默认模板)</p>
</div> </div>
<a href="api/analysis/template/store" style="font-size:12px; color:#1890ff; text-decoration:none; white-space:nowrap;">📥 下载模板</a> <a href="api/analysis/template/store" style="font-size:12px; color:#1890ff; text-decoration:none; white-space:nowrap;">📥 下载模板</a>
</div> </div>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!