Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
data_report
/
daw
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 3d85b3ba
authored
Jul 17, 2026
by
江名洋
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
feat(analysis): 支持从历史批次生成分析报表
1 parent
71628e68
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
128 additions
and
17 deletions
app/api/endpoints.py
app/templates/analysis.html
app/api/endpoints.py
View file @
3d85b3b
...
@@ -207,6 +207,89 @@ def _find_latest_wenjie_template():
...
@@ -207,6 +207,89 @@ def _find_latest_wenjie_template():
return
fallback
return
fallback
return
None
return
None
def
_load_store_df_from_batch
(
batch_no
:
str
)
->
"pd.DataFrame"
:
"""从DB加载门店兜底配置批次,返回可直接用于build_report的DataFrame"""
from
app.services.upload_service
import
_get_conn
import
pandas
as
pd
conn
=
_get_conn
()
try
:
with
conn
.
cursor
()
as
cur
:
cur
.
execute
(
"SELECT id FROM upload_batch WHERE batch_no=
%
s AND data_type='store_guarantee'"
,
(
batch_no
,))
batch
=
cur
.
fetchone
()
if
not
batch
:
raise
HTTPException
(
status_code
=
404
,
detail
=
f
"批次 {batch_no} 不存在"
)
cur
.
execute
(
"""SELECT store_name, guarantee_target, guarantee_type,
special_case_1, special_case_2,
ew_conversion_rate, ax_conversion_rate, battery_conversion_rate,
ew_compensation, ax_compensation, battery_compensation,
guarantee_end_date
FROM store_guarantee_config WHERE batch_id=
%
s"""
,
(
batch
[
"id"
],))
rows
=
cur
.
fetchall
()
if
not
rows
:
raise
HTTPException
(
status_code
=
400
,
detail
=
"批次中无数据"
)
df
=
pd
.
DataFrame
(
rows
)
df
[
'门店名'
]
=
df
[
'store_name'
]
df
[
'兜底指标'
]
=
df
[
'guarantee_target'
]
df
[
'兜底类型'
]
=
df
[
'guarantee_type'
]
df
[
'特殊情况1'
]
=
df
[
'special_case_1'
]
df
[
'特殊情况2'
]
=
df
[
'special_case_2'
]
df
[
'达标转化率_整车延保'
]
=
df
[
'ew_conversion_rate'
]
df
[
'达标转化率_安心包'
]
=
df
[
'ax_conversion_rate'
]
df
[
'达标转化率_电池'
]
=
df
[
'battery_conversion_rate'
]
df
[
'每单赔付金额_整车'
]
=
df
[
'ew_compensation'
]
df
[
'每单赔付金额_安心包'
]
=
df
[
'ax_compensation'
]
df
[
'每单赔付金额_电池'
]
=
df
[
'battery_compensation'
]
df
[
'兜底截止日期'
]
=
df
[
'guarantee_end_date'
]
return
df
finally
:
conn
.
close
()
def
_load_oem_factory_from_batch
(
batch_no
:
str
)
->
dict
:
"""从DB加载OEM交付批次,返回 {sheet_name: DataFrame} 格式(兼容read_factory_data输出)"""
from
app.services.upload_service
import
_get_conn
import
pandas
as
pd
conn
=
_get_conn
()
try
:
with
conn
.
cursor
()
as
cur
:
cur
.
execute
(
"SELECT id FROM upload_batch WHERE batch_no=
%
s AND data_type='oem_delivery'"
,
(
batch_no
,))
batch
=
cur
.
fetchone
()
if
not
batch
:
raise
HTTPException
(
status_code
=
404
,
detail
=
f
"批次 {batch_no} 不存在"
)
cur
.
execute
(
"""SELECT store_code, store_name, province,
delivery_count, ew_purchase_count, ax_purchase_count,
battery_purchase_count, tire_purchase_count
FROM oem_delivery_data WHERE batch_id=
%
s"""
,
(
batch
[
"id"
],))
rows
=
cur
.
fetchall
()
if
not
rows
:
return
{}
df
=
pd
.
DataFrame
(
rows
)
df
[
'门店名'
]
=
df
[
'store_name'
]
df
[
'厅店代码'
]
=
df
[
'store_code'
]
df
[
'区域(省)'
]
=
df
[
'province'
]
df
[
'交付数'
]
=
pd
.
to_numeric
(
df
[
'delivery_count'
],
errors
=
'coerce'
)
.
fillna
(
0
)
df
[
'整车延保采购数'
]
=
pd
.
to_numeric
(
df
[
'ew_purchase_count'
],
errors
=
'coerce'
)
.
fillna
(
0
)
df
[
'安心包采购数'
]
=
pd
.
to_numeric
(
df
[
'ax_purchase_count'
],
errors
=
'coerce'
)
.
fillna
(
0
)
df
[
'电池延保采购数'
]
=
pd
.
to_numeric
(
df
[
'battery_purchase_count'
],
errors
=
'coerce'
)
.
fillna
(
0
)
df
[
'轮胎险采购数'
]
=
pd
.
to_numeric
(
df
[
'tire_purchase_count'
],
errors
=
'coerce'
)
.
fillna
(
0
)
# 去重
df
=
df
.
drop_duplicates
(
subset
=
[
"门店名"
],
keep
=
"first"
)
return
{
"Sheet1"
:
df
}
finally
:
conn
.
close
()
@router.get
(
"/analysis/template/{template_type}"
)
@router.get
(
"/analysis/template/{template_type}"
)
async
def
download_analysis_template
(
template_type
:
str
):
async
def
download_analysis_template
(
template_type
:
str
):
"""下载分析模板文件"""
"""下载分析模板文件"""
...
@@ -362,33 +445,44 @@ async def analysis_generate(
...
@@ -362,33 +445,44 @@ async def analysis_generate(
ecology_start_date
:
str
=
Form
(
None
),
ecology_start_date
:
str
=
Form
(
None
),
ecology_end_date
:
str
=
Form
(
None
),
ecology_end_date
:
str
=
Form
(
None
),
nature_start_date
:
str
=
Form
(
None
),
nature_start_date
:
str
=
Form
(
None
),
nature_end_date
:
str
=
Form
(
None
)
nature_end_date
:
str
=
Form
(
None
),
store_batch_no
:
str
=
Form
(
None
),
ecology_batch_no
:
str
=
Form
(
None
),
nature_batch_no
:
str
=
Form
(
None
),
):
):
"""根据已上传的数据生成生态系数分析报表(双sheet:生态数据+自然月)"""
"""根据已上传的数据
或历史批次
生成生态系数分析报表(双sheet:生态数据+自然月)"""
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
):
# 加载store_df:优先批次DB数据,其次文件,最后默认模板
# 未上传时使用模板库中最新的默认门店数据
if
store_batch_no
:
store_df
=
_load_store_df_from_batch
(
store_batch_no
)
elif
_analysis_store_file
and
os
.
path
.
exists
(
_analysis_store_file
):
store_df
=
AnalysisService
.
read_store_list
(
_analysis_store_file
)
else
:
default_store
=
_find_latest_wenjie_template
()
default_store
=
_find_latest_wenjie_template
()
if
os
.
path
.
exists
(
default_store
):
if
os
.
path
.
exists
(
default_store
):
_analysis_store_file
=
default_store
_analysis_store_file
=
default_store
store_df
=
AnalysisService
.
read_store_list
(
_analysis_store_file
)
else
:
else
:
raise
HTTPException
(
status_code
=
400
,
detail
=
"请先上传门店列表文件"
)
raise
HTTPException
(
status_code
=
400
,
detail
=
"请先上传门店列表文件"
)
ecology_factory
=
None
ecology_factory
=
{}
nature_factory
=
None
nature_factory
=
{}
if
_analysis_ecology_factory_file
and
os
.
path
.
exists
(
_analysis_ecology_factory_file
):
if
ecology_batch_no
:
ecology_factory
=
_load_oem_factory_from_batch
(
ecology_batch_no
)
elif
_analysis_ecology_factory_file
and
os
.
path
.
exists
(
_analysis_ecology_factory_file
):
ecology_factory
=
AnalysisService
.
read_factory_data
(
_analysis_ecology_factory_file
)
ecology_factory
=
AnalysisService
.
read_factory_data
(
_analysis_ecology_factory_file
)
if
_analysis_nature_factory_file
and
os
.
path
.
exists
(
_analysis_nature_factory_file
):
if
nature_batch_no
:
nature_factory
=
_load_oem_factory_from_batch
(
nature_batch_no
)
elif
_analysis_nature_factory_file
and
os
.
path
.
exists
(
_analysis_nature_factory_file
):
nature_factory
=
AnalysisService
.
read_factory_data
(
_analysis_nature_factory_file
)
nature_factory
=
AnalysisService
.
read_factory_data
(
_analysis_nature_factory_file
)
if
not
ecology_factory
and
not
nature_factory
:
if
not
ecology_factory
and
not
nature_factory
:
raise
HTTPException
(
status_code
=
400
,
detail
=
"请至少上传一个主机厂数据文件(生态或自然月)"
)
raise
HTTPException
(
status_code
=
400
,
detail
=
"请至少上传
或选择
一个主机厂数据文件(生态或自然月)"
)
try
:
try
:
store_df
=
AnalysisService
.
read_store_list
(
_analysis_store_file
)
output_path
=
AnalysisService
.
build_report
(
output_path
=
AnalysisService
.
build_report
(
store_df
,
store_df
,
ecology_factory
=
ecology_factory
or
{},
ecology_factory
=
ecology_factory
or
{},
...
...
app/templates/analysis.html
View file @
3d85b3b
...
@@ -501,12 +501,22 @@
...
@@ -501,12 +501,22 @@
const
storeInput
=
document
.
getElementById
(
'store-file-input'
);
const
storeInput
=
document
.
getElementById
(
'store-file-input'
);
const
ecoInput
=
document
.
getElementById
(
'ecology-factory-input'
);
const
ecoInput
=
document
.
getElementById
(
'ecology-factory-input'
);
const
natInput
=
document
.
getElementById
(
'nature-factory-input'
);
const
natInput
=
document
.
getElementById
(
'nature-factory-input'
);
const
hasStore
=
storeInput
&&
storeInput
.
files
.
length
>
0
;
const
storeBatchSel
=
document
.
getElementById
(
'store-batch-select'
);
const
hasEco
=
ecoInput
&&
ecoInput
.
files
.
length
>
0
;
const
ecoBatchSel
=
document
.
getElementById
(
'ecology-batch-select'
);
const
hasNat
=
natInput
&&
natInput
.
files
.
length
>
0
;
const
natBatchSel
=
document
.
getElementById
(
'nature-batch-select'
);
const
hasStoreFile
=
storeInput
&&
storeInput
.
files
.
length
>
0
;
const
hasEcoFile
=
ecoInput
&&
ecoInput
.
files
.
length
>
0
;
const
hasNatFile
=
natInput
&&
natInput
.
files
.
length
>
0
;
const
hasEcoBatch
=
ecoBatchSel
&&
ecoBatchSel
.
value
;
const
hasNatBatch
=
natBatchSel
&&
natBatchSel
.
value
;
const
hasStoreBatch
=
storeBatchSel
&&
storeBatchSel
.
value
;
const
hasEco
=
hasEcoFile
||
hasEcoBatch
;
const
hasNat
=
hasNatFile
||
hasNatBatch
;
if
(
!
hasEco
&&
!
hasNat
)
{
if
(
!
hasEco
&&
!
hasNat
)
{
alert
(
'请至少上传
一个主机厂数据文件
(步骤2或步骤3)'
);
alert
(
'请至少上传
或选择一个主机厂数据
(步骤2或步骤3)'
);
return
;
return
;
}
}
...
@@ -515,13 +525,16 @@
...
@@ -515,13 +525,16 @@
try
{
try
{
const
uploadForm
=
new
FormData
();
const
uploadForm
=
new
FormData
();
if
(
hasStore
)
uploadForm
.
append
(
'store_file'
,
storeInput
.
files
[
0
]);
if
(
hasStore
File
)
uploadForm
.
append
(
'store_file'
,
storeInput
.
files
[
0
]);
if
(
hasEco
)
uploadForm
.
append
(
'ecology_factory_file'
,
ecoInput
.
files
[
0
]);
if
(
hasEco
File
)
uploadForm
.
append
(
'ecology_factory_file'
,
ecoInput
.
files
[
0
]);
if
(
hasNat
)
uploadForm
.
append
(
'nature_factory_file'
,
natInput
.
files
[
0
]);
if
(
hasNat
File
)
uploadForm
.
append
(
'nature_factory_file'
,
natInput
.
files
[
0
]);
const
anyFile
=
hasStoreFile
||
hasEcoFile
||
hasNatFile
;
if
(
anyFile
)
{
const
uploadRes
=
await
fetch
(
window
.
API_BASE
+
'/analysis/upload'
,
{
method
:
'POST'
,
body
:
uploadForm
});
const
uploadRes
=
await
fetch
(
window
.
API_BASE
+
'/analysis/upload'
,
{
method
:
'POST'
,
body
:
uploadForm
});
const
uploadData
=
await
uploadRes
.
json
();
const
uploadData
=
await
uploadRes
.
json
();
if
(
uploadData
.
status
!==
'success'
)
throw
new
Error
(
uploadData
.
detail
||
'文件上传失败'
);
if
(
uploadData
.
status
!==
'success'
)
throw
new
Error
(
uploadData
.
detail
||
'文件上传失败'
);
}
btnGenerate
.
textContent
=
'\u23f3 正在分析计算...'
;
btnGenerate
.
textContent
=
'\u23f3 正在分析计算...'
;
...
@@ -534,6 +547,10 @@
...
@@ -534,6 +547,10 @@
if
(
eE
)
genForm
.
append
(
'ecology_end_date'
,
eE
);
if
(
eE
)
genForm
.
append
(
'ecology_end_date'
,
eE
);
if
(
nS
)
genForm
.
append
(
'nature_start_date'
,
nS
);
if
(
nS
)
genForm
.
append
(
'nature_start_date'
,
nS
);
if
(
nE
)
genForm
.
append
(
'nature_end_date'
,
nE
);
if
(
nE
)
genForm
.
append
(
'nature_end_date'
,
nE
);
// 传递批次号
if
(
hasStoreBatch
)
genForm
.
append
(
'store_batch_no'
,
storeBatchSel
.
value
);
if
(
hasEcoBatch
)
genForm
.
append
(
'ecology_batch_no'
,
ecoBatchSel
.
value
);
if
(
hasNatBatch
)
genForm
.
append
(
'nature_batch_no'
,
natBatchSel
.
value
);
const
genRes
=
await
fetch
(
window
.
API_BASE
+
'/analysis/generate'
,
{
method
:
'POST'
,
body
:
genForm
});
const
genRes
=
await
fetch
(
window
.
API_BASE
+
'/analysis/generate'
,
{
method
:
'POST'
,
body
:
genForm
});
const
genData
=
await
genRes
.
json
();
const
genData
=
await
genRes
.
json
();
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment