From 00cc714db6e003593bff0ae5edaa623d9b08f3b4 Mon Sep 17 00:00:00 2001 From: jacobxu666 Date: Tue, 17 Nov 2026 12:26:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=A5=96=E6=B1=A0=E4=B8=8E=E7=AC=AC?= =?UTF-8?q?=E5=9B=9B=E8=BD=AE=E5=85=AC=E7=A4=BA=E6=89=8B=E6=9C=BA=E5=8F=B7?= =?UTF-8?q?=E7=9C=9F=E5=80=BC=E5=9B=9E=E5=88=B7(UAT)=E2=80=94=E2=80=94?= =?UTF-8?q?=E4=BB=A5=E7=AC=AC=E4=BA=8C=E6=89=B9=E5=8E=9F=E5=A7=8B=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=B8=BA=E6=BA=90,=E5=AD=98=E9=87=8F515673=E8=A1=8C?= =?UTF-8?q?=E6=8E=A9=E7=A0=81=E5=9B=9E=E5=88=B7+25215=E7=BD=AENULL,?= =?UTF-8?q?=E5=85=AC=E7=A4=BA1827=E8=A1=8C=E5=88=B7=E6=98=8E=E6=96=87,?= =?UTF-8?q?=E5=8F=8C=E5=A4=87=E4=BB=BD=E8=A1=A8=E5=8F=AF=E5=9B=9E=E6=BB=9A?= =?UTF-8?q?;=E7=99=BD=E5=90=8D=E5=8D=95=E5=8F=A3=E5=BE=84=E5=90=8C11-10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/tools_refill_phone_20261117.py | 123 ++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 docs/tools_refill_phone_20261117.py diff --git a/docs/tools_refill_phone_20261117.py b/docs/tools_refill_phone_20261117.py new file mode 100644 index 0000000..9c64309 --- /dev/null +++ b/docs/tools_refill_phone_20261117.py @@ -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 已清理;备份表保留待业务确认后清理')