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 7e0fc25f
authored
Aug 10, 2026
by
周立明
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
流量费(GMV口径)到嘀加分润等字段的解析及入库
1 parent
70371d95
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
114 additions
and
32 deletions
.workbuddy/memory/2026-08-10.md
data-process/clean_budget_achievement.py
data-process/parsers/settlement_parser.py
.workbuddy/memory/2026-08-10.md
View file @
7e0fc25
...
...
@@ -35,3 +35,23 @@
验证(2026-06):654 行全量重写;订单数合计 23326 与 Excel 明细累计精确一致;GMV/成本/收入与原始明细累计一致(仅 decimal(15,2) 逐行舍入 <0.02 偏差);
本月占比 651 行非空、3 组 GMV=0 写 NULL;上月占比 654 行全 NULL(2026-05 未处理,待跑完 2026-05 后重跑 2026-06 自动回填)。
比值抽查与 ROUND(成本/GMV×100,2) 精确一致。
## 第三步:补齐 9 个指标(流量费/服务费相关 + 成本(收入口径)/流量费(收入口径)/嘀加分润)
-
字段:traffic_fee_gmv(R流量方-分润)、traffic_fee_gmv_ratio_current/previous、service_fee(V服务商-分润)、
service_fee_ratio_current/previous、cost_revenue(Q商品成本-嘀加 + AG应付-商品成本)、traffic_fee_revenue(AH应付-流量费)、
dijia_profit_sharing(=GMV-成本GMV-流量费GMV-服务费)。
-
`settlement_parser.generate_budget_metrics`
扩展聚合上述 5 个源单元格,并算两个本月占比;上月占比从
`prev_metrics`
取。
-
`clean_budget_achievement`
:
`query_previous_month_metrics`
增加 SUM(traffic_fee_gmv)、SUM(service_fee);
`save_budget_rows`
扩为 24 列(2 控制列+7 维+15 指标),DELETE+INSERT 幂等。
-
修复过一次 INSERT 占位符数量少 1 的 bug(23 vs 24),已改正。
⚠️ 异常发现与处理:本步首次成功写库后,发现
`biz_budget_achieve`
中多出一整批
`stat_month='2026-05'`
、task_id=16 的 654 行,
其维度分组/gmv/成本与 2026-06 完全相同、9 个新字段为 NULL、自增 id(655-1308) 早于 2026-06(1963-2616),且任务表无 2026-05 任务、表无触发器。
判定为把 2026-06 数据错标成 2026-05 的历史异常重复数据(会污染"上月占比",导致 2026-06 上月占比被误算成 0.00)。
已执行
`DELETE FROM biz_budget_achieve WHERE stat_month='2026-05' AND task_id=16`
(654 行)清理,并重跑 2026-06,
使上月占比正确回退为 NULL(当前表仅剩 2026-06 / 654 行)。脚本只写 task
[
'month'
]
、无触发器,该异常不会复现。
校验(2026-06,9 指标):traffic_fee_gmv=41622228.25、service_fee=8348903.87、cost_revenue=10975009.39、
traffic_fee_revenue=13760287.16、dijia_profit_sharing=12148718.83,与 Excel 明细累计差异均 ≤0.04(decimal(15,2) 舍入);
上月占比 654 全 NULL,本月流量费占比 651 非空、3 组 GMV=0 写 NULL。
data-process/clean_budget_achievement.py
View file @
7e0fc25
...
...
@@ -9,17 +9,21 @@
→ 解析并按维度分组
→ 写入 biz_budget_achieve
本期写入字段(维度 7 个 + 金额
6 个,共 13
个):
本期写入字段(维度 7 个 + 金额
15 个,共 22
个):
task_id、stat_month、is_multi_category、traffic_name、business_type、
entity、brand、park_store、service_provider、
order_count、gmv、revenue、cost_gmv、
cost_gmv_ratio_current、cost_gmv_ratio_previous
cost_gmv_ratio_current、cost_gmv_ratio_previous、
traffic_fee_gmv、traffic_fee_gmv_ratio_current、traffic_fee_gmv_ratio_previous、
service_fee、service_fee_ratio_current、service_fee_ratio_previous、
cost_revenue、traffic_fee_revenue、dijia_profit_sharing
说明:
- 金额指标按「业务类型+主体+流量方+品牌+园区店+服务商」维度累计汇总;
- cost_gmv_ratio_current = 成本(GMV口径)/GMV*100(保留 2 位);
- cost_gmv_ratio_previous = 上月同维度比值,取自 biz_budget_achieve 中上一统计月的数据,
若上月数据尚未处理则写 NULL,待上月跑完后重跑本脚本即可自动回填。
- 各 *_ratio_current = 对应金额/GMV*100(保留 2 位);
- 各 *_ratio_previous = 上月同维度比值,取自 biz_budget_achieve 中上一统计月的数据,
若上月数据尚未处理则写 NULL,待上月跑完后重跑本脚本即可自动回填;
- dijia_profit_sharing = GMV - 成本(GMV口径) - 流量费(GMV口径) - 服务费。
用法:
python clean_budget_achievement.py --data-table-type budget_achievement --month 2026-06
...
...
@@ -119,17 +123,18 @@ def prev_month_str(month):
def
query_previous_month_metrics
(
conn
,
prev_month
):
"""查询上一统计月 biz_budget_achieve 中各维度组的成本
(GMV口径)与 GMV
累计值。
"""查询上一统计月 biz_budget_achieve 中各维度组的成本
/GMV/流量费/服务费
累计值。
用于计算
cost_gmv_ratio_previous(上月占比)。按 6 维度聚合,
返回 dict:
key=(traffic_name, business_type, entity, brand, park_store, service_provider),
value={'cost_gmv':float, 'gmv
':float}。
用于计算
各"上月占比"字段。按 6 维度聚合,返回 dict:
key=(traffic_name, business_type, entity, brand, park_store, service_provider),
value={'cost_gmv':float, 'gmv':float, 'traffic_fee_gmv':float, 'service_fee
':float}。
若上月数据尚未处理(查无记录),返回空 dict,对应比值写 None。
"""
sql
=
(
'SELECT traffic_name, business_type, entity, brand, park_store, service_provider, '
'SUM(cost_gmv) AS cost_gmv, SUM(gmv) AS gmv '
'SUM(cost_gmv) AS cost_gmv, SUM(gmv) AS gmv, '
'SUM(traffic_fee_gmv) AS traffic_fee_gmv, SUM(service_fee) AS service_fee '
'FROM biz_budget_achieve '
'WHERE stat_month =
%
s '
'GROUP BY traffic_name, business_type, entity, brand, park_store, service_provider'
...
...
@@ -143,6 +148,8 @@ def query_previous_month_metrics(conn, prev_month):
result
[
key
]
=
{
'cost_gmv'
:
float
(
r
[
'cost_gmv'
]
or
0
),
'gmv'
:
float
(
r
[
'gmv'
]
or
0
),
'traffic_fee_gmv'
:
float
(
r
[
'traffic_fee_gmv'
]
or
0
),
'service_fee'
:
float
(
r
[
'service_fee'
]
or
0
),
}
return
result
...
...
@@ -150,16 +157,19 @@ def query_previous_month_metrics(conn, prev_month):
def
save_budget_rows
(
conn
,
task_id
,
stat_month
,
rows
):
"""写入预算达成数据表:同一任务重跑先清空旧数据,再批量插入(单事务)。
写入字段:维度 7 个 + 金额 6 个(order_count/gmv/revenue/cost_gmv/
cost_gmv_ratio_current/cost_gmv_ratio_previous)。
写入字段(共 24 列:含 task_id/stat_month 2 个控制列 + 维度 7 个 + 金额指标 15 个)。
"""
insert_sql
=
(
f
'INSERT INTO {TARGET_TABLE} '
'(task_id, stat_month, is_multi_category, traffic_name, business_type, '
' entity, brand, park_store, service_provider, '
' order_count, gmv, revenue, cost_gmv, '
' cost_gmv_ratio_current, cost_gmv_ratio_previous) '
'VALUES (
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s)'
' cost_gmv_ratio_current, cost_gmv_ratio_previous, '
' traffic_fee_gmv, traffic_fee_gmv_ratio_current, traffic_fee_gmv_ratio_previous, '
' service_fee, service_fee_ratio_current, service_fee_ratio_previous, '
' cost_revenue, traffic_fee_revenue, dijia_profit_sharing) '
'VALUES (
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s, '
'
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s,
%
s)'
)
params
=
[
(
...
...
@@ -178,6 +188,15 @@ def save_budget_rows(conn, task_id, stat_month, rows):
r
[
'cost_gmv'
],
r
[
'cost_gmv_ratio_current'
],
r
[
'cost_gmv_ratio_previous'
],
r
[
'traffic_fee_gmv'
],
r
[
'traffic_fee_gmv_ratio_current'
],
r
[
'traffic_fee_gmv_ratio_previous'
],
r
[
'service_fee'
],
r
[
'service_fee_ratio_current'
],
r
[
'service_fee_ratio_previous'
],
r
[
'cost_revenue'
],
r
[
'traffic_fee_revenue'
],
r
[
'dijia_profit_sharing'
],
)
for
r
in
rows
]
...
...
@@ -256,7 +275,9 @@ def process_task(conn, task, dry_run=False):
deleted
,
inserted
=
save_budget_rows
(
conn
,
task_id
,
stat_month
,
rows
)
log
(
f
' 写入 {TARGET_TABLE}:清理旧数据 {deleted} 行,新增 {inserted} 行'
f
'(含金额指标 6 列,本月占比非空 {sum(1 for r in rows if r["cost_gmv_ratio_current"] is not None)} 行)'
)
f
'(本月成本占比非空 {sum(1 for r in rows if r["cost_gmv_ratio_current"] is not None)} 行'
f
',本月流量费占比非空 {sum(1 for r in rows if r["traffic_fee_gmv_ratio_current"] is not None)} 行'
f
',本月服务费占比非空 {sum(1 for r in rows if r["service_fee_ratio_current"] is not None)} 行)'
)
update_clean_status
(
conn
,
file_row
[
'id'
],
CLEAN_DONE
)
return
inserted
except
Exception
:
...
...
data-process/parsers/settlement_parser.py
View file @
7e0fc25
...
...
@@ -272,22 +272,32 @@ class SettlementSummaryParser:
def
generate_budget_metrics
(
self
,
prev_metrics
=
None
):
"""生成「预算达成数据表」维度 + 金额指标(维度同前 7 列,金额列累计汇总)。
金额指标来源(模板 I~N 列定义):
- order_count 订单数 ← 结算数据「升级数量」按维度累计
- gmv GMV ← 结算数据「订单金额」按维度累计
- revenue 收入 ← 结算数据「应收-总应收(按对账)」按维度累计
- cost_gmv 成本(GMV口径) ← 结算数据「商品成本-总成本」按维度累计
- cost_gmv_ratio_current 本月占比 ← 成本(GMV口径)/GMV*100,保留 2 位
- cost_gmv_ratio_previous 上月占比 ← 上月「成本(GMV口径)/GMV*100」,
prev_metrics 提供(key = 6 维),
缺失则写 None(待上月数据处理后重跑回填)
金额指标来源(模板 O~W 列定义):
- order_count 订单数 ← 结算数据「升级数量」按维度累计
- gmv GMV ← 结算数据「订单金额」按维度累计
- revenue 收入 ← 结算数据「应收-总应收(按对账)」按维度累计
- cost_gmv 成本(GMV口径) ← 结算数据「商品成本-总成本」按维度累计
- cost_gmv_ratio_current 本月成本占比 ← 成本(GMV口径)/GMV*100,保留 2 位
- cost_gmv_ratio_previous 上月成本占比 ← 上月同口径比值(prev_metrics 提供)
- traffic_fee_gmv 流量费(GMV口径) ← 结算数据「流量方-分润」累计
- traffic_fee_gmv_ratio_current 本月流量费占比 ← 流量费(GMV口径)/GMV*100
- traffic_fee_gmv_ratio_previous 上月流量费占比 ← 上月同口径比值
- service_fee 服务费 ← 结算数据「服务商-分润」累计
- service_fee_ratio_current 本月服务费占比 ← 服务费/GMV*100
- service_fee_ratio_previous 上月服务费占比 ← 上月同口径比值
- cost_revenue 成本(收入口径) ← 结算数据「商品成本-嘀加」+「应付-商品成本」累计
- traffic_fee_revenue 流量费(收入口径) ← 结算数据「应付-流量费」累计
- dijia_profit_sharing 嘀加分润 ← GMV - 成本(GMV口径) - 流量费(GMV口径) - 服务费
Args:
prev_metrics: dict,key=(traffic_name, business_type, entity, brand,
park_store, service_provider),value={'cost_gmv':float,'gmv':float}。
park_store, service_provider),
value={'cost_gmv':float, 'gmv':float,
'traffic_fee_gmv':float, 'service_fee':float}。
缺失则对应上月占比写 None(待上月数据处理后重跑回填)。
Returns:
list[dict]: 含
13 个
字段的明细行。
list[dict]: 含字段的明细行。
"""
if
not
self
.
records
:
self
.
parse
()
...
...
@@ -307,13 +317,22 @@ class SettlementSummaryParser:
)
if
key
not
in
groups
:
groups
[
key
]
=
[]
sums
[
key
]
=
{
'order_count'
:
0.0
,
'gmv'
:
0.0
,
'revenue'
:
0.0
,
'cost_gmv'
:
0.0
}
sums
[
key
]
=
{
'order_count'
:
0.0
,
'gmv'
:
0.0
,
'revenue'
:
0.0
,
'cost_gmv'
:
0.0
,
'traffic_fee_gmv'
:
0.0
,
'service_fee'
:
0.0
,
'cost_dijia'
:
0.0
,
'payable_cost'
:
0.0
,
'payable_traffic'
:
0.0
,
}
groups
[
key
]
.
append
(
rec
)
m
=
sums
[
key
]
m
[
'order_count'
]
+=
_to_num
(
rec
[
'upgrade_count'
])
or
0.0
m
[
'gmv'
]
+=
_to_num
(
rec
[
'order_amount'
])
or
0.0
m
[
'revenue'
]
+=
_to_num
(
rec
[
'receivable_total'
])
or
0.0
m
[
'cost_gmv'
]
+=
_to_num
(
rec
[
'cost_total'
])
or
0.0
m
[
'traffic_fee_gmv'
]
+=
_to_num
(
rec
[
'traffic_profit'
])
or
0.0
m
[
'service_fee'
]
+=
_to_num
(
rec
[
'provider_profit'
])
or
0.0
m
[
'cost_dijia'
]
+=
_to_num
(
rec
[
'cost_dijia'
])
or
0.0
m
[
'payable_cost'
]
+=
_to_num
(
rec
[
'payable_cost'
])
or
0.0
m
[
'payable_traffic'
]
+=
_to_num
(
rec
[
'payable_traffic'
])
or
0.0
traffic_business_types
=
defaultdict
(
set
)
for
business_type
,
_entity
,
traffic_party
,
_brand
,
_park
,
_sp
in
groups
:
...
...
@@ -326,13 +345,26 @@ class SettlementSummaryParser:
gmv
=
round
(
m
[
'gmv'
],
2
)
revenue
=
round
(
m
[
'revenue'
],
2
)
cost_gmv
=
round
(
m
[
'cost_gmv'
],
2
)
ratio_current
=
_ratio
(
cost_gmv
,
gmv
)
ratio_previous
=
None
traffic_fee_gmv
=
round
(
m
[
'traffic_fee_gmv'
],
2
)
service_fee
=
round
(
m
[
'service_fee'
],
2
)
cost_revenue
=
round
(
m
[
'cost_dijia'
]
+
m
[
'payable_cost'
],
2
)
traffic_fee_revenue
=
round
(
m
[
'payable_traffic'
],
2
)
dijia_profit_sharing
=
round
(
gmv
-
cost_gmv
-
traffic_fee_gmv
-
service_fee
,
2
)
cost_ratio_cur
=
_ratio
(
cost_gmv
,
gmv
)
cost_ratio_prev
=
None
tf_ratio_cur
=
_ratio
(
traffic_fee_gmv
,
gmv
)
tf_ratio_prev
=
None
sf_ratio_cur
=
_ratio
(
service_fee
,
gmv
)
sf_ratio_prev
=
None
if
prev_metrics
:
pv
=
prev_metrics
.
get
(
(
traffic_party
,
business_type
,
entity
,
brand
,
park_store
,
sp
))
if
pv
:
ratio_previous
=
_ratio
(
pv
[
'cost_gmv'
],
pv
[
'gmv'
])
cost_ratio_prev
=
_ratio
(
pv
[
'cost_gmv'
],
pv
[
'gmv'
])
tf_ratio_prev
=
_ratio
(
pv
[
'traffic_fee_gmv'
],
pv
[
'gmv'
])
sf_ratio_prev
=
_ratio
(
pv
[
'service_fee'
],
pv
[
'gmv'
])
rows
.
append
({
'is_multi_category'
:
'是'
if
len
(
traffic_business_types
[
traffic_party
])
>
1
else
'否'
,
'traffic_name'
:
traffic_party
,
...
...
@@ -345,8 +377,17 @@ class SettlementSummaryParser:
'gmv'
:
gmv
,
'revenue'
:
revenue
,
'cost_gmv'
:
cost_gmv
,
'cost_gmv_ratio_current'
:
ratio_current
,
'cost_gmv_ratio_previous'
:
ratio_previous
,
'cost_gmv_ratio_current'
:
cost_ratio_cur
,
'cost_gmv_ratio_previous'
:
cost_ratio_prev
,
'traffic_fee_gmv'
:
traffic_fee_gmv
,
'traffic_fee_gmv_ratio_current'
:
tf_ratio_cur
,
'traffic_fee_gmv_ratio_previous'
:
tf_ratio_prev
,
'service_fee'
:
service_fee
,
'service_fee_ratio_current'
:
sf_ratio_cur
,
'service_fee_ratio_previous'
:
sf_ratio_prev
,
'cost_revenue'
:
cost_revenue
,
'traffic_fee_revenue'
:
traffic_fee_revenue
,
'dijia_profit_sharing'
:
dijia_profit_sharing
,
})
return
rows
...
...
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