抽奖池与第四轮公示手机号真值回刷(UAT)——以第二批原始数据为源,存量515673行掩码回刷+25215置NULL,公示1827行刷明文,双备份表可回滚;白名单口径同11-10
This commit is contained in:
@@ -0,0 +1,123 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# xuchao: 真值回刷——抽奖池存量手机列(掩码)+第四轮公示手机(明文),源=《第二批年龄段25-40岁抽奖数据》(ID=池主键)
|
||||||
|
# 用法: python3 docs/tools_refill_phone_20261117.py # 干跑:算变更量+建备份表,不改数据
|
||||||
|
# python3 docs/tools_refill_phone_20261117.py --execute # 执行回刷(UAT=生产)
|
||||||
|
# 口径(2026-11-17 用户确认"回刷池和公示"):
|
||||||
|
# 池 member_phone = 文件明文转掩码(前3+****+后4),文件无手机→NULL;仅动存量行(created_time='2026-10-15 09:57:17');
|
||||||
|
# hhrs_winner 第四轮(round_id=857957849313349) member_phone = 文件明文原样(与二三轮明文先例一致,接口出参自行脱敏);
|
||||||
|
# 新批11-10的58,893行/173行公示不在本文件覆盖内,待业务另导该批原始明文后二次回刷。
|
||||||
|
# 备份: hhrs_lottery_pool_phonebak_20261117(存量id+改前phone) / hhrs_winner_r4phone_bak_20261117(第四轮整行);
|
||||||
|
# 验证无误后由业务确认可清理(仿《备份表清理台账》处置)。
|
||||||
|
import sys, os, re
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
EXECUTE = '--execute' in sys.argv
|
||||||
|
SRC = '/Users/jacobxu/Downloads/第二批年龄段25-40岁抽奖数据.xlsx'
|
||||||
|
LEGACY_CT = '2026-10-15 09:57:17'
|
||||||
|
R4 = 857957849313349
|
||||||
|
|
||||||
|
F = pd.read_excel(SRC, dtype=str).fillna('')
|
||||||
|
F.columns = ['ID', '身份证', '姓名', '手机']
|
||||||
|
assert F['ID'].astype(int).is_unique and len(F) == 532872, '文件ID应唯一且532,872行'
|
||||||
|
ph_ok = F['手机'].str.fullmatch(r'\d{11}').sum()
|
||||||
|
ph_empty = (F['手机'] == '').sum()
|
||||||
|
ph_other = len(F) - ph_ok - ph_empty
|
||||||
|
print(f'文件 {len(F)} 行: 手机11位 {ph_ok}, 空 {ph_empty}, 其他形态 {ph_other}')
|
||||||
|
|
||||||
|
# 11位还需号段在白名单(沿用 11-10 用户定口径),否则与脏值同等对待→NULL
|
||||||
|
allowed = set()
|
||||||
|
allowed.update(f'13{d}' for d in range(10))
|
||||||
|
allowed.update(['140', '141', '144', '145', '146', '147', '148', '149'])
|
||||||
|
allowed.update(f'15{d}' for d in range(10) if d != 4)
|
||||||
|
allowed.update(['162', '165', '166', '167'])
|
||||||
|
allowed.update(['170', '171', '172', '173', '175', '176', '177', '178'])
|
||||||
|
allowed.update(f'18{d}' for d in range(10))
|
||||||
|
allowed.update(f'19{d}' for d in range(10) if d != 4)
|
||||||
|
|
||||||
|
def clean(p):
|
||||||
|
"""合法=11位且号段白名单,返回原明文;否则 None(入库为NULL)"""
|
||||||
|
return p if re.fullmatch(r'\d{11}', p) and p[:3] in allowed else None
|
||||||
|
|
||||||
|
def mask(p):
|
||||||
|
return p[:3] + '****' + p[7:11] if p else None
|
||||||
|
|
||||||
|
fmap = dict(zip(F['ID'].astype(int), F['手机'].map(clean)))
|
||||||
|
n_valid = sum(1 for v in fmap.values() if v)
|
||||||
|
print(f'白名单口径下有效明文 {n_valid} 行, 其余 {len(fmap)-n_valid} 行将按NULL入库')
|
||||||
|
|
||||||
|
os.environ['SETTLE_ENV'] = 'uat'
|
||||||
|
path = '/Users/jacobxu/Documents/Project/12301-dscp/docs/tools/settle_recompute_check.py'
|
||||||
|
src = open(path).read()
|
||||||
|
head = src.split("cur = conn.cursor(pymysql.cursors.DictCursor)", 1)[0] + "cur = conn.cursor(pymysql.cursors.DictCursor)"
|
||||||
|
ns = {'__name__': 'env'}
|
||||||
|
exec(compile(head, path, 'exec'), ns)
|
||||||
|
conn, cur = ns['conn'], ns['cur']
|
||||||
|
|
||||||
|
# 池存量现状
|
||||||
|
cur.execute("SELECT id, member_phone FROM hhrs_lottery_pool WHERE created_time=%s", (LEGACY_CT,))
|
||||||
|
legacy = {r['id']: r['member_phone'] for r in cur.fetchall()}
|
||||||
|
print('池存量行数:', len(legacy))
|
||||||
|
inter = sorted(set(legacy) & set(fmap))
|
||||||
|
assert len(inter) == len(legacy), '文件未完全覆盖存量,中止'
|
||||||
|
pool_changes = [(i, mask(fmap[i])) for i in inter if mask(fmap[i]) != legacy[i]]
|
||||||
|
n_null = sum(1 for _, v in pool_changes if v is None)
|
||||||
|
print(f'池需变更 {len(pool_changes)} 行(其中置NULL {n_null})')
|
||||||
|
|
||||||
|
# 第四轮公示现状
|
||||||
|
cur.execute("SELECT id, pool_id, member_phone FROM hhrs_winner WHERE round_id=%s", (R4,))
|
||||||
|
r4rows = cur.fetchall()
|
||||||
|
win_changes = [(r['id'], fmap.get(r['pool_id'])) for r in r4rows if r['pool_id'] in fmap]
|
||||||
|
print(f'公示第四轮共 {len(r4rows)} 行,本文件覆盖 {len(win_changes)} 行需刷为明文/NULL')
|
||||||
|
|
||||||
|
if not EXECUTE:
|
||||||
|
print('干跑完成,未改动。执行请加 --execute')
|
||||||
|
raise SystemExit
|
||||||
|
|
||||||
|
# ---- 备份表(DDL隐式提交,先行) ----
|
||||||
|
cur.execute("SHOW TABLES LIKE 'hhrs_lottery_pool_phonebak_20261117'")
|
||||||
|
if not cur.fetchall():
|
||||||
|
cur.execute(f"CREATE TABLE hhrs_lottery_pool_phonebak_20261117 AS "
|
||||||
|
f"SELECT id, member_phone FROM hhrs_lottery_pool WHERE created_time='{LEGACY_CT}'")
|
||||||
|
cur.execute("SELECT COUNT(*) c FROM hhrs_lottery_pool_phonebak_20261117")
|
||||||
|
print('池备份表行数:', cur.fetchone()['c'])
|
||||||
|
cur.execute("SHOW TABLES LIKE 'hhrs_winner_r4phone_bak_20261117'")
|
||||||
|
if not cur.fetchall():
|
||||||
|
cur.execute(f"CREATE TABLE hhrs_winner_r4phone_bak_20261117 AS SELECT * FROM hhrs_winner WHERE round_id={R4}")
|
||||||
|
|
||||||
|
# ---- 池:staging 批量 UPDATE ----
|
||||||
|
cur.execute("DROP TABLE IF EXISTS hhrs_stg_phone_20261117")
|
||||||
|
cur.execute("CREATE TABLE hhrs_stg_phone_20261117 (id BIGINT PRIMARY KEY, new_phone VARCHAR(20))")
|
||||||
|
vals = [(i, v) for i, v in pool_changes]
|
||||||
|
sql = "INSERT INTO hhrs_stg_phone_20261117 (id, new_phone) VALUES (%s,%s)"
|
||||||
|
for i in range(0, len(vals), 2000):
|
||||||
|
cur.executemany(sql, vals[i:i + 2000])
|
||||||
|
|
||||||
|
cur.execute("START TRANSACTION")
|
||||||
|
cur.execute("UPDATE hhrs_lottery_pool p JOIN hhrs_stg_phone_20261117 s ON p.id=s.id "
|
||||||
|
f"SET p.member_phone=s.new_phone WHERE p.created_time='{LEGACY_CT}' AND NOT (p.member_phone <=> s.new_phone)")
|
||||||
|
n_pool = cur.rowcount
|
||||||
|
# 公示:逐行(1,827 行,量小)
|
||||||
|
sqlw = "UPDATE hhrs_winner SET member_phone=%s WHERE id=%s"
|
||||||
|
cur.executemany(sqlw, [(v, i) for i, v in win_changes])
|
||||||
|
n_win = cur.rowcount
|
||||||
|
# 事务内自检
|
||||||
|
cur.execute("SELECT COUNT(*) c FROM hhrs_lottery_pool")
|
||||||
|
all_rows = cur.fetchone()['c']
|
||||||
|
cur.execute(f"SELECT COUNT(*) c FROM hhrs_lottery_pool WHERE created_time='{LEGACY_CT}' AND member_phone IS NULL")
|
||||||
|
nulls_after = cur.fetchone()['c']
|
||||||
|
cur.execute(f"SELECT COUNT(*) c FROM hhrs_lottery_pool WHERE created_time='{LEGACY_CT}' "
|
||||||
|
"AND member_phone REGEXP '^[0-9]{3}\\\\*{4}[0-9]{4}$'")
|
||||||
|
fmt_ok = cur.fetchone()['c']
|
||||||
|
cur.execute(f"SELECT COUNT(*) c FROM hhrs_winner WHERE round_id={R4} AND member_phone REGEXP '^[0-9]{{11}}$'")
|
||||||
|
plain_ok = cur.fetchone()['c']
|
||||||
|
print(f'自检: 池更新 {n_pool}(应{len(pool_changes)}) | 公示更新 {n_win}(应{len(win_changes)}) | '
|
||||||
|
f'全表 {all_rows}(应574570) | 存量NULL {nulls_after}(应{n_null}) | 存量掩码格式 {fmt_ok} | 公示明文 {plain_ok}')
|
||||||
|
if n_pool == len(pool_changes) and n_win == len(win_changes) and all_rows == 574570:
|
||||||
|
conn.commit()
|
||||||
|
print('COMMIT 成功')
|
||||||
|
else:
|
||||||
|
conn.rollback()
|
||||||
|
print('自检未通过,已 ROLLBACK')
|
||||||
|
raise SystemExit(1)
|
||||||
|
cur.execute("DROP TABLE hhrs_stg_phone_20261117")
|
||||||
|
print('staging 已清理;备份表保留待业务确认后清理')
|
||||||
Reference in New Issue
Block a user