verify_alias_accum.py
3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
"""验证:标准名/别名映射接入后,装修摊销与门店耗材的累加是否兼容。"""
import pymysql
import clean_budget_achievement as cb
from config import Config
from clean_budget_achievement import load_alias_maps, to_standard, strip_if_dplus
conn = pymysql.connect(
host=Config.MYSQL_HOST, port=Config.MYSQL_PORT, user=Config.MYSQL_USER,
password=Config.MYSQL_PASSWORD, database=Config.MYSQL_DB,
charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
load_alias_maps(conn)
m4 = cb._ALIAS_MAPS['4s'] # alias -> standard
my = cb._ALIAS_MAPS['yq']
# 反向:标准名集合
std4 = set(m4.values())
stdy = set(my.values())
# 1) 库内 2026-06 预算行,检查别名泄漏(traffic_name/park_store 是某别名但没归并到标准名)
sql = ("SELECT id, business_type, traffic_name, entity, brand, park_store, service_provider, gmv, "
"gmv_ratio, gmv_ratio_park, renovation_amortization, store_consumables "
"FROM biz_budget_achieve WHERE stat_month=%s ORDER BY id")
with conn.cursor() as cur:
cur.execute(sql, ('2026-06',))
rows = cur.fetchall()
leak_tn = [] # traffic_name 是别名(出现在 m4 的 key 中)但未归并
leak_ps = []
for r in rows:
tn = (r['traffic_name'] or '').strip()
ps = (r['park_store'] or '').strip()
if tn in m4 and m4[tn] != tn:
leak_tn.append((r['id'], tn, m4[tn]))
if ps in my and my[ps] != ps:
leak_ps.append((r['id'], ps, my[ps]))
print('== 1. 别名泄漏检查(库内行仍存别名、未归并到标准名)==')
print(' traffic_name 泄漏:', leak_tn if leak_tn else '无')
print(' park_store 泄漏:', leak_ps if leak_ps else '无')
# 2) 重复 6 维 key 检查(合并是否彻底)
from collections import Counter
keys = Counter((r['traffic_name'], r['business_type'], r['entity'] if 'entity' in r else None,
r['brand'], r['park_store'], r['service_provider']) for r in rows)
# entity 字段不在 select,用 business_type+traffic+park 粗查
keys2 = Counter((r['business_type'], r['traffic_name'], r['brand'], r['park_store'], r['service_provider']) for r in rows)
dups = {k: c for k, c in keys2.items() if c > 1}
print('== 2. 近似重复维度 key(bt+tn+brand+ps+sp)==')
print(' 重复组:', dups if dups else '无')
# 3) 累加对账:装修摊销池合计 / 门店耗材池合计 是否精确
ren_total = sum(float(r['renovation_amortization'] or 0) for r in rows)
cons_total = sum(float(r['store_consumables'] or 0) for r in rows)
print('== 3. 累加总额 ==')
print(f' renovation_amortization 合计: {ren_total:.2f}')
print(f' store_consumables 合计: {cons_total:.2f}')
print(f' 两列均非 NULL 行数: ren={sum(1 for r in rows if r["renovation_amortization"] is not None)}, '
f'cons={sum(1 for r in rows if r["store_consumables"] is not None)}')
# 4) gmv_ratio / gmv_ratio_park 组内合计是否=1
from collections import defaultdict
g_sum = defaultdict(float); gp_sum = defaultdict(float)
g_ctr = defaultdict(int); gp_ctr = defaultdict(int)
for r in rows:
if r['gmv_ratio'] is not None:
g_sum[(r['business_type'], r['traffic_name'])] += float(r['gmv_ratio'])
g_ctr[(r['business_type'], r['traffic_name'])] += 1
if r['gmv_ratio_park'] is not None:
gp_sum[(r['business_type'], r['park_store'])] += float(r['gmv_ratio_park'])
gp_ctr[(r['business_type'], r['park_store'])] += 1
bad_g = {k: v for k, v in g_sum.items() if abs(v - 1.0) > 1e-6}
bad_gp = {k: v for k, v in gp_sum.items() if abs(v - 1.0) > 1e-6}
print('== 4. 占比组和对账 ==')
print(f' gmv_ratio 组总数={len(g_sum)},偏差>1e-6 的组: {bad_g if bad_g else "无"}')
print(f' gmv_ratio_park 组总数={len(gp_sum)},偏差>1e-6 的组: {bad_gp if bad_gp else "无"}')
# 5) 标准名覆盖率:明细来源的别名是否能归并
print('== 5. 字典规模 ==')
print(f' merchant_4s 别名条目={len(m4)},标准名={len(std4)}')
print(f' merchant_yq 别名条目={len(my)},标准名={len(stdy)}')
conn.close()