Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
data_report
/
data-report
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 2792dd8a
authored
Oct 16, 2026
by
周立明
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
北汽部分字段修复
1 parent
642410a9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
145 additions
and
14 deletions
data-process/baic_special_processor.py
data-process/baic_special_processor.py
View file @
2792dd8
...
@@ -716,6 +716,73 @@ def fetch_order_count_window(period_start, period_end, merchant_ids):
...
@@ -716,6 +716,73 @@ def fetch_order_count_window(period_start, period_end, merchant_ids):
return
result
return
result
def
_month_last_day
(
d
):
"""返回 d 所在月份的最后一天(date)。"""
if
d
is
None
:
return
None
if
d
.
month
==
12
:
return
date
(
d
.
year
+
1
,
1
,
1
)
-
timedelta
(
days
=
1
)
return
date
(
d
.
year
,
d
.
month
+
1
,
1
)
-
timedelta
(
days
=
1
)
# 售后订单数(用户 2026-10-16 指定口径):主订单 a + 支付单 b(pay_status=2) + 客户单 c(sales_type=1),
# 按商户 GROUP BY COUNT(0),得到「延保+权益」合并总数(不分拆)。商户号/时间为入参,其余参数固定。
BAIC_AFTER_SALE_ORDER_SQL
=
"""
SELECT a.merchant_id, COUNT(0) AS order_cnt
FROM dj_car_life_goods_order a
LEFT JOIN dj_car_life_pay_order b
ON b.order_code = a.order_code AND b.deleted_flag='N' AND b.pay_status=2
LEFT JOIN dj_car_life_goods_order_customer c
ON c.order_code = a.order_code AND c.deleted_flag='N'
WHERE a.deleted_flag='N'
AND a.merchant_id IN :merchant_ids
AND a.scene_type=0 AND a.business_type=2 AND a.status=4
AND b.success_date >= :date_from
AND b.success_date < :date_to
AND c.sales_type=1
GROUP BY a.merchant_id
"""
def
fetch_after_sale_order_count
(
period_start
,
period_end
,
merchant_ids
):
"""查询各门店「售后订单数」(延保+权益合并,dj_smartcarlife)。
用户 2026-10-16 指定口径:主订单 a LEFT JOIN 支付单 b(pay_status=2)
LEFT JOIN 客户单 c(sales_type=1),按商户分组 COUNT(0)。
其余谓词(scene_type=0 / business_type=2 / status=4 / deleted_flag / sales_type=1)固定。
:param period_start: 周期开始日期(date,含当天)
:param period_end: 周期结束日期(date,含当天 → 查询上界取次日)
:param merchant_ids: 门店 D+ 编码集合(store_code)
:return: {merchant_id(str): 订单数(int)};失败/无数据返回 {}(不致命)。
"""
result
=
{}
ids
=
[
int
(
c
)
for
c
in
{
str
(
c
)
.
strip
()
for
c
in
merchant_ids
if
c
not
in
(
None
,
''
)}
if
str
(
c
)
.
strip
()
.
isdigit
()]
if
not
ids
or
not
period_start
or
not
period_end
:
return
result
date_from
=
period_start
.
strftime
(
'
%
Y-
%
m-
%
d'
)
date_to
=
(
period_end
+
timedelta
(
days
=
1
))
.
strftime
(
'
%
Y-
%
m-
%
d'
)
engine
=
_get_biz_engine
()
try
:
with
engine
.
connect
()
as
conn
:
stmt
=
text
(
BAIC_AFTER_SALE_ORDER_SQL
)
.
bindparams
(
bindparam
(
'merchant_ids'
,
expanding
=
True
))
rows
=
conn
.
execute
(
stmt
,
{
'merchant_ids'
:
ids
,
'date_from'
:
date_from
,
'date_to'
:
date_to
},
)
.
fetchall
()
except
Exception
as
e
:
logger
.
warning
(
f
"[北汽] 售后订单数查询失败: {e}"
)
return
result
for
r
in
rows
:
mid
,
cnt
=
r
[
0
],
r
[
1
]
if
mid
is
None
or
cnt
is
None
:
continue
result
[
str
(
mid
)]
=
int
(
cnt
)
logger
.
info
(
f
"[北汽] 售后订单数:周期 [{date_from}, {date_to}) 命中 {len(result)}/{len(ids)} 家门店"
)
return
result
def
compute_baic_week_ends
(
month
,
cutoff_date
):
def
compute_baic_week_ends
(
month
,
cutoff_date
):
"""计算北汽报表周度评灯节点的结束日期(**独立规则,勿与通用+权益混用**)。
"""计算北汽报表周度评灯节点的结束日期(**独立规则,勿与通用+权益混用**)。
...
@@ -1258,7 +1325,7 @@ class BaicSpecialProcessor:
...
@@ -1258,7 +1325,7 @@ class BaicSpecialProcessor:
is_deleted
=
0
,
is_deleted
=
0
,
stat_month
=
stat_month
,
stat_month
=
stat_month
,
project_name
=
'北汽'
,
project_name
=
'北汽'
,
brand
=
'
北汽
'
,
brand
=
'
极狐/越野
'
,
store_cnt
=
len
(
detail_objs
),
store_cnt
=
len
(
detail_objs
),
# 源文件末尾汇总段(品牌级手工上传):售后门店数 / 售后场景开通率
# 源文件末尾汇总段(品牌级手工上传):售后门店数 / 售后场景开通率
after_sale_store_cnt
=
_to_int
(
summary_vals
.
get
(
'after_sale_store_cnt'
)),
after_sale_store_cnt
=
_to_int
(
summary_vals
.
get
(
'after_sale_store_cnt'
)),
...
@@ -1340,6 +1407,14 @@ class BaicSpecialProcessor:
...
@@ -1340,6 +1407,14 @@ class BaicSpecialProcessor:
cutoff_date_obj
=
_to_date
(
cutoff_date
)
cutoff_date_obj
=
_to_date
(
cutoff_date
)
week_nodes
=
compute_baic_week_ends
(
month
,
cutoff_date_obj
)
week_nodes
=
compute_baic_week_ends
(
month
,
cutoff_date_obj
)
store_codes
=
[
d
.
store_code
for
d
in
details
if
d
.
store_code
]
store_codes
=
[
d
.
store_code
for
d
in
details
if
d
.
store_code
]
# ---- 售后订单数(J 列,品牌汇总·本月段):用户 2026-10-16 新口径 ----
# 主订单 a + 支付单 b(pay_status=2) + 客户单 c(sales_type=1),按商户 COUNT(0) 得合并总数(不分延保/权益)。
# K/M(整车延保/原厂权益成交数)仍走旧 biz_map 子订单拆分逻辑(用户明确保持旧逻辑)。
_after_sale_total
=
0
if
month_start_obj
and
store_codes
:
_aso
=
fetch_after_sale_order_count
(
month_start_obj
,
_month_last_day
(
month_start_obj
),
store_codes
)
_after_sale_total
=
sum
(
_aso
.
values
())
window_biz_by_node
=
[]
# 与 week_nodes 同序:{store_code(str): {biz: cnt}}
window_biz_by_node
=
[]
# 与 week_nodes 同序:{store_code(str): {biz: cnt}}
for
(
_lbl
,
_end
)
in
week_nodes
:
for
(
_lbl
,
_end
)
in
week_nodes
:
if
month_start_obj
and
_end
:
if
month_start_obj
and
_end
:
...
@@ -1457,13 +1532,17 @@ class BaicSpecialProcessor:
...
@@ -1457,13 +1532,17 @@ class BaicSpecialProcessor:
ws
.
cell
(
r_a
,
col
)
.
fill
=
(
ws
.
cell
(
r_a
,
col
)
.
fill
=
(
PatternFill
(
start_color
=
light_fill
,
end_color
=
light_fill
,
fill_type
=
'solid'
)
PatternFill
(
start_color
=
light_fill
,
end_color
=
light_fill
,
fill_type
=
'solid'
)
if
light_fill
else
PatternFill
(
fill_type
=
'none'
))
if
light_fill
else
PatternFill
(
fill_type
=
'none'
))
# ---- 回写 baic_special_month_data:W1/W2/W3 周度字段(与 Excel 同口径)----
# ---- 回写 baic_special_month_data:截止目前 + W1/W2/W3 周度字段(与 Excel 同口径)----
# j=0 为「截止目前」节点,无对应 w0 列 → 跳过;j=1/2/3 → w1/w2/w3。
# j=0 为「截止目前」节点 → 落到单列表 comprehensive_penetration / current_light;
# w{n}_order_cnt 该周累计(延保+权益)订单数(无门店编码→None,无法统计)
# j=1/2/3 → w1/w2/w3。
# w{n}_new_delivery_cnt 该周结束日快照交付数(无快照→None)
# *_order_cnt 该周累计(延保+权益)订单数(无门店编码→None,无法统计)
# w{n}_comprehensive_penetration 综合渗透(分母缺失→None)
# *_new_delivery_cnt 该周结束日快照交付数(无快照→None)
# w{n}_light_level 灯级(绿/黄/红;无法评估→None)
# *_comprehensive_penetration 综合渗透(分母缺失→None)
if
j
>=
1
:
# *_light_level 灯级(绿/黄/红;无法评估→None)
if
j
==
0
:
setattr
(
d
,
'comprehensive_penetration'
,
pen_node
)
setattr
(
d
,
'current_light'
,
light_txt
)
else
:
setattr
(
d
,
f
'w{j}_order_cnt'
,
(
comb_node
if
scode
else
None
))
setattr
(
d
,
f
'w{j}_order_cnt'
,
(
comb_node
if
scode
else
None
))
setattr
(
d
,
f
'w{j}_new_delivery_cnt'
,
setattr
(
d
,
f
'w{j}_new_delivery_cnt'
,
(
int
(
node_delivery
)
if
node_delivery
is
not
None
else
None
))
(
int
(
node_delivery
)
if
node_delivery
is
not
None
else
None
))
...
@@ -1624,7 +1703,7 @@ class BaicSpecialProcessor:
...
@@ -1624,7 +1703,7 @@ class BaicSpecialProcessor:
r'本月\s*\S*'
,
f
'本月 {month_label}'
,
str
(
lbl
))
r'本月\s*\S*'
,
f
'本月 {month_label}'
,
str
(
lbl
))
data_row
=
cur_month_row
+
2
data_row
=
cur_month_row
+
2
_set_cell
(
ws
,
data_row
,
1
,
'北汽'
)
# 项目
_set_cell
(
ws
,
data_row
,
1
,
'北汽'
)
# 项目
_set_cell
(
ws
,
data_row
,
2
,
'
北汽'
)
# 品牌(框架轮填北汽;D+ 接入后改极狐/越野
)
_set_cell
(
ws
,
data_row
,
2
,
'
极狐/越野'
)
# 品牌(北汽极狐 + 北京越野 固定汇总显示
)
_set_cell
(
ws
,
data_row
,
3
,
n
)
# 门店数
_set_cell
(
ws
,
data_row
,
3
,
n
)
# 门店数
# 品牌汇总本月段:与合计行同口径聚合(D~N 系统/计算列均填,O~R 环比框架轮留空)
# 品牌汇总本月段:与合计行同口径聚合(D~N 系统/计算列均填,O~R 环比框架轮留空)
_set_cell
(
ws
,
data_row
,
4
,
total_current
)
# D 产值(万)= 截止目前产值合计
_set_cell
(
ws
,
data_row
,
4
,
total_current
)
# D 产值(万)= 截止目前产值合计
...
@@ -1635,7 +1714,7 @@ class BaicSpecialProcessor:
...
@@ -1635,7 +1714,7 @@ class BaicSpecialProcessor:
if
summary
:
if
summary
:
_set_cell
(
ws
,
data_row
,
8
,
summary
.
after_sale_store_cnt
)
_set_cell
(
ws
,
data_row
,
8
,
summary
.
after_sale_store_cnt
)
_set_cell
(
ws
,
data_row
,
9
,
_dec_to_float
(
summary
.
after_sale_scene_open_rate
))
_set_cell
(
ws
,
data_row
,
9
,
_dec_to_float
(
summary
.
after_sale_scene_open_rate
))
_set_cell
(
ws
,
data_row
,
10
,
_
combined_total
)
# J 售后订单数 = 延保+权益成交合计
_set_cell
(
ws
,
data_row
,
10
,
_
after_sale_total
)
# J 售后订单数 = 新口径合并总数(主订单+客户单 sales_type=1)
_set_cell
(
ws
,
data_row
,
11
,
ext_total
)
# K 整车延保成交数 = 延保订单合计
_set_cell
(
ws
,
data_row
,
11
,
ext_total
)
# K 整车延保成交数 = 延保订单合计
_set_cell
(
ws
,
data_row
,
12
,
_penetration
(
ext_total
,
total_delivery
))
# L 整车延保渗透率
_set_cell
(
ws
,
data_row
,
12
,
_penetration
(
ext_total
,
total_delivery
))
# L 整车延保渗透率
_set_cell
(
ws
,
data_row
,
13
,
rights_total
)
# M 原厂权益成交数 = 权益订单合计
_set_cell
(
ws
,
data_row
,
13
,
rights_total
)
# M 原厂权益成交数 = 权益订单合计
...
@@ -1658,7 +1737,8 @@ class BaicSpecialProcessor:
...
@@ -1658,7 +1737,8 @@ class BaicSpecialProcessor:
_prev_details
=
[]
_prev_details
=
[]
_prev_ext
=
_prev_rights
=
0
_prev_ext
=
_prev_rights
=
0
_prev_delivery
=
_prev_visit
=
_prev_current
=
0
_prev_delivery
=
_prev_visit
=
_prev_current
=
0
_prev_visit_rate
=
_prev_combined
=
None
_prev_visit_rate
=
None
_prev_after_sale_total
=
0
_prev_store_cnt
=
0
_prev_store_cnt
=
0
_prev_summary
=
None
_prev_summary
=
None
if
prev_month
:
if
prev_month
:
...
@@ -1683,7 +1763,13 @@ class BaicSpecialProcessor:
...
@@ -1683,7 +1763,13 @@ class BaicSpecialProcessor:
_prev_visit
=
sum
((
d
.
visit_cnt
or
0
)
for
d
in
_prev_details
)
_prev_visit
=
sum
((
d
.
visit_cnt
or
0
)
for
d
in
_prev_details
)
_prev_current
=
sum
((
_dec_to_float
(
d
.
output_current_wan
)
or
0
)
for
d
in
_prev_details
)
_prev_current
=
sum
((
_dec_to_float
(
d
.
output_current_wan
)
or
0
)
for
d
in
_prev_details
)
_prev_visit_rate
=
(
_prev_visit
/
_prev_delivery
)
if
_prev_delivery
else
None
_prev_visit_rate
=
(
_prev_visit
/
_prev_delivery
)
if
_prev_delivery
else
None
_prev_combined
=
_prev_ext
+
_prev_rights
# 上月段售后订单数(J):同样改用新口径(主订单+客户单 sales_type=1,合并总数)
_prev_store_codes
=
[
d
.
store_code
for
d
in
_prev_details
if
d
.
store_code
]
_prev_after_sale_total
=
0
if
_prev_store_codes
:
_pme
=
_month_last_day
(
date
(
_py
,
_pm
,
1
))
_paso
=
fetch_after_sale_order_count
(
date
(
_py
,
_pm
,
1
),
_pme
,
_prev_store_codes
)
_prev_after_sale_total
=
sum
(
_paso
.
values
())
_prev_store_cnt
=
len
(
_prev_details
)
_prev_store_cnt
=
len
(
_prev_details
)
_prev_summary
=
(
BaicSpecialMonthSummaryMetrics
.
query
_prev_summary
=
(
BaicSpecialMonthSummaryMetrics
.
query
.
filter_by
(
task_id
=
_prev_task
.
id
,
is_deleted
=
0
)
.
filter_by
(
task_id
=
_prev_task
.
id
,
is_deleted
=
0
)
...
@@ -1718,7 +1804,7 @@ class BaicSpecialProcessor:
...
@@ -1718,7 +1804,7 @@ class BaicSpecialProcessor:
if
_prev_details
:
if
_prev_details
:
# 上月段数据行:用上月真实任务数据回填(与上月结果表「本月」汇总同口径)
# 上月段数据行:用上月真实任务数据回填(与上月结果表「本月」汇总同口径)
_set_cell
(
ws
,
_p_row
,
1
,
'北汽'
)
# 项目
_set_cell
(
ws
,
_p_row
,
1
,
'北汽'
)
# 项目
_set_cell
(
ws
,
_p_row
,
2
,
'
北汽'
)
# 品牌
_set_cell
(
ws
,
_p_row
,
2
,
'
极狐/越野'
)
# 品牌(极狐/越野 固定汇总显示)
_set_cell
(
ws
,
_p_row
,
3
,
_prev_store_cnt
)
# 门店数
_set_cell
(
ws
,
_p_row
,
3
,
_prev_store_cnt
)
# 门店数
_set_cell
(
ws
,
_p_row
,
4
,
_prev_current
)
# D 产值(万)
_set_cell
(
ws
,
_p_row
,
4
,
_prev_current
)
# D 产值(万)
_set_cell
(
ws
,
_p_row
,
5
,
_prev_delivery
)
# E 交付数
_set_cell
(
ws
,
_p_row
,
5
,
_prev_delivery
)
# E 交付数
...
@@ -1727,7 +1813,7 @@ class BaicSpecialProcessor:
...
@@ -1727,7 +1813,7 @@ class BaicSpecialProcessor:
if
_prev_summary
:
if
_prev_summary
:
_set_cell
(
ws
,
_p_row
,
8
,
_prev_summary
.
after_sale_store_cnt
)
_set_cell
(
ws
,
_p_row
,
8
,
_prev_summary
.
after_sale_store_cnt
)
_set_cell
(
ws
,
_p_row
,
9
,
_dec_to_float
(
_prev_summary
.
after_sale_scene_open_rate
))
_set_cell
(
ws
,
_p_row
,
9
,
_dec_to_float
(
_prev_summary
.
after_sale_scene_open_rate
))
_set_cell
(
ws
,
_p_row
,
10
,
_prev_
combined
)
# J 售后订单数
_set_cell
(
ws
,
_p_row
,
10
,
_prev_
after_sale_total
)
# J 售后订单数(新口径合并总数)
_set_cell
(
ws
,
_p_row
,
11
,
_prev_ext
)
# K 整车延保成交数
_set_cell
(
ws
,
_p_row
,
11
,
_prev_ext
)
# K 整车延保成交数
_set_cell
(
ws
,
_p_row
,
12
,
_penetration
(
_prev_ext
,
_prev_delivery
))
# L 整车延保渗透率
_set_cell
(
ws
,
_p_row
,
12
,
_penetration
(
_prev_ext
,
_prev_delivery
))
# L 整车延保渗透率
_set_cell
(
ws
,
_p_row
,
13
,
_prev_rights
)
# M 原厂权益成交数
_set_cell
(
ws
,
_p_row
,
13
,
_prev_rights
)
# M 原厂权益成交数
...
@@ -1739,6 +1825,51 @@ class BaicSpecialProcessor:
...
@@ -1739,6 +1825,51 @@ class BaicSpecialProcessor:
for
_c
in
range
(
1
,
19
):
for
_c
in
range
(
1
,
19
):
_set_cell
(
ws
,
_p_row
,
_c
,
None
)
_set_cell
(
ws
,
_p_row
,
_c
,
None
)
# ---- 同步回填 baic_special_month_summary_metrics(与「品牌汇总·本月」段同口径)----
# 历史问题:该表仅写 project_name/brand/store_cnt/售后门店数/售后场景开通率/售后订单数,
# 产值/交付/触客/渗透/环比 等列一直留空(未落库)。现用品牌汇总段已算出的聚合值回填,
# 使 DB 汇总表与结果表「品牌汇总·本月」段完全一致(generate_result_excel 是唯一能拿到
# 全量聚合值 + 上月对比值的入口,故在此统一回写,避免与 _save 重复取数)。
_sm_update
=
{
BaicSpecialMonthSummaryMetrics
.
output_wan
:
total_current
,
BaicSpecialMonthSummaryMetrics
.
delivery_cnt
:
total_delivery
,
BaicSpecialMonthSummaryMetrics
.
pre_sale_visit_cnt
:
total_visit
,
BaicSpecialMonthSummaryMetrics
.
pre_sale_visit_rate
:
_visit_rate_total
,
BaicSpecialMonthSummaryMetrics
.
after_sale_order_cnt
:
_after_sale_total
,
BaicSpecialMonthSummaryMetrics
.
vehicle_ext_warranty_deal_cnt
:
ext_total
,
BaicSpecialMonthSummaryMetrics
.
vehicle_ext_warranty_penetration_rate
:
_penetration
(
ext_total
,
total_delivery
),
BaicSpecialMonthSummaryMetrics
.
original_rights_deal_cnt
:
rights_total
,
BaicSpecialMonthSummaryMetrics
.
original_rights_penetration_rate
:
_penetration
(
rights_total
,
total_delivery
),
}
if
_prev_details
:
_cur_open_rate
=
_dec_to_float
(
summary
.
after_sale_scene_open_rate
)
if
summary
else
None
_prev_open_rate
=
(
_dec_to_float
(
_prev_summary
.
after_sale_scene_open_rate
)
if
_prev_summary
else
None
)
_sm_update
[
BaicSpecialMonthSummaryMetrics
.
after_sale_scene_open_rate_mom_pp
]
=
\
_diff
(
_cur_open_rate
,
_prev_open_rate
)
_sm_update
[
BaicSpecialMonthSummaryMetrics
.
pre_sale_visit_rate_mom_pp
]
=
\
_diff
(
_visit_rate_total
,
_prev_visit_rate
)
_sm_update
[
BaicSpecialMonthSummaryMetrics
.
vehicle_ext_warranty_penetration_rate_mom_pp
]
=
\
_diff
(
_penetration
(
ext_total
,
total_delivery
),
_penetration
(
_prev_ext
,
_prev_delivery
))
_sm_update
[
BaicSpecialMonthSummaryMetrics
.
original_rights_penetration_rate_mom_pp
]
=
\
_diff
(
_penetration
(
rights_total
,
total_delivery
),
_penetration
(
_prev_rights
,
_prev_delivery
))
else
:
# 无上月真实任务 → 环比列清空(与结果表 O~R 一致)
_sm_update
[
BaicSpecialMonthSummaryMetrics
.
after_sale_scene_open_rate_mom_pp
]
=
None
_sm_update
[
BaicSpecialMonthSummaryMetrics
.
pre_sale_visit_rate_mom_pp
]
=
None
_sm_update
[
BaicSpecialMonthSummaryMetrics
.
vehicle_ext_warranty_penetration_rate_mom_pp
]
=
None
_sm_update
[
BaicSpecialMonthSummaryMetrics
.
original_rights_penetration_rate_mom_pp
]
=
None
_sm_n
=
BaicSpecialMonthSummaryMetrics
.
query
.
filter_by
(
task_id
=
task
.
id
,
is_deleted
=
0
)
.
update
(
_sm_update
,
synchronize_session
=
False
)
db
.
session
.
commit
()
logger
.
info
(
f
"[北汽] 汇总指标表回填 {_sm_n} 行(task {task.id}):"
f
"产值={total_current} 交付={total_delivery} 触客={total_visit} "
f
"延保成交={ext_total} 权益成交={rights_total}"
)
# ---- sheet 重命名(月份占位替换)----
# ---- sheet 重命名(月份占位替换)----
try
:
try
:
ws
.
title
=
f
"{month_label}页签-字段取值说明"
ws
.
title
=
f
"{month_label}页签-字段取值说明"
...
...
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