From d221c8a439b281c00a8f29907abbd5a8db11035c Mon Sep 17 00:00:00 2001 From: jacobxu666 Date: Tue, 17 Nov 2026 12:41:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=A5=96=E6=B1=A0=E8=A1=A5=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E7=AC=AC=E4=BA=8C=E6=89=B925-40=E5=B2=81=E6=9C=AA?= =?UTF-8?q?=E5=85=A5=E6=B1=A017195=E4=BA=BA(UAT)=E2=80=94=E2=80=94?= =?UTF-8?q?=E5=8F=91=E5=8D=A1=E5=9C=B0=E5=8C=BA=E7=94=A8=E4=B8=9A=E5=8A=A1?= =?UTF-8?q?=E8=A1=A5=E5=85=85=E5=88=97(=E4=B8=8E=E5=AD=98=E9=87=8F100%?= =?UTF-8?q?=E4=B8=80=E8=87=B4=E5=B7=B2=E9=AA=8C),=E6=89=8B=E6=9C=BA?= =?UTF-8?q?=E7=99=BD=E5=90=8D=E5=8D=95=E5=8F=A3=E5=BE=84=E9=9D=9E=E6=B3=95?= =?UTF-8?q?=E7=BD=AENULL=E5=85=B1825,=E5=8E=BF=E5=8C=BA=E5=85=A8=E8=A6=86?= =?UTF-8?q?=E7=9B=96;574570=E2=86=92591765?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/tools_import_batch2_20261117.py | 82 ++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 docs/tools_import_batch2_20261117.py diff --git a/docs/tools_import_batch2_20261117.py b/docs/tools_import_batch2_20261117.py new file mode 100644 index 0000000..ded25f5 --- /dev/null +++ b/docs/tools_import_batch2_20261117.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +# xuchao: 第二批25-40岁未入池人员补导入抽奖池(2026-11-17 用户指令"补导入池") +# 用法: python3 docs/tools_import_batch2_20261117.py # 干跑,只算不写 +# python3 docs/tools_import_batch2_20261117.py --execute # 执行入库(UAT=生产) +# 源: ~/Downloads/第二批年龄段25-40岁抽奖数据 1.xlsx(业务补充发卡地区版;该列与池存量县区100%一致已验证) +# 口径: 目标=文件ID不在池的17,195行;county=发卡地区;证号=明文转掩码(6+8星+4); +# 手机=白名单号段11位转掩码,非法/空→NULL(与当日真值回刷口径一致,不剔行); +# batch_no=0, won=0。回滚: DELETE FROM hhrs_lottery_pool WHERE created_time >= '' AND id BETWEEN 254 AND 3121236(先核数应=17195)。 +# 连接参数复用 12301-dscp/docs/tools/settle_recompute_check.py 的 SETTLE_ENV=uat 分支。 +import sys, os, re +import pandas as pd + +EXECUTE = '--execute' in sys.argv +SRC = os.path.expanduser('~/Downloads/第二批年龄段25-40岁抽奖数据.xlsx') +EXPECT = 17195 + +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 phone_mask(p): + return (p[:3] + '****' + p[7:11]) if re.fullmatch(r'\d{11}', p) and p[:3] in allowed else None + +def id_mask(i): + assert re.fullmatch(r'\d{17}[\dXx]', i), f'证号格式异常: {i}' + return i[:6] + '********' + i[14:] + +F = pd.read_excel(SRC, dtype=str).fillna('') +F.columns = ['ID', '身份证', '姓名', '手机', '发卡地区'] +assert F['ID'].astype(int).is_unique and len(F) == 532872 + +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 COUNT(*) c FROM hhrs_lottery_pool") +before = cur.fetchone()['c'] +cur.execute("SELECT id FROM hhrs_lottery_pool") +pool_ids = {r['id'] for r in cur.fetchall()} + +only = F[~F['ID'].astype(int).isin(pool_ids)].copy() +assert len(only) == EXPECT, f'待导入 {len(only)} != {EXPECT},口径变化中止' +assert only['发卡地区'].ne('').all(), '存在空发卡地区' +rows = [(int(w['ID']), w['姓名'], w['发卡地区'], 0, id_mask(w['身份证']), phone_mask(w['手机']), 0) + for _, w in only.iterrows()] +n_ph = sum(1 for r in rows if r[5]) +print(f'待导入 {len(rows)} 行: 有效手机掩码 {n_ph}, 置NULL {len(rows)-n_ph}') + +if not EXECUTE: + print('干跑通过,未写入。执行请加 --execute') + raise SystemExit + +cur.execute("SELECT NOW() n") +t0 = cur.fetchone()['n'] +sql = ("INSERT INTO hhrs_lottery_pool " + "(id, member_name, county, batch_no, member_identity_no, member_phone, won) " + "VALUES (%s,%s,%s,%s,%s,%s,%s)") +for i in range(0, len(rows), 2000): + cur.executemany(sql, rows[i:i + 2000]) +cur.execute("SELECT COUNT(*) c FROM hhrs_lottery_pool WHERE created_time >= %s", (t0,)) +n_new = cur.fetchone()['c'] +cur.execute("SELECT COUNT(*) c FROM hhrs_lottery_pool") +n_all = cur.fetchone()['c'] +cur.execute("SELECT COUNT(*) c FROM hhrs_lottery_pool WHERE created_time >= %s AND county IS NULL", (t0,)) +n_nocounty = cur.fetchone()['c'] +print(f'自检: 本批 {n_new}(应{EXPECT}) 全表 {n_all}(应{before+EXPECT}) 本批无县区 {n_nocounty}(应0) t0={t0}') +if n_new == EXPECT and n_all == before + EXPECT and n_nocounty == 0: + conn.commit() + print('COMMIT 成功:', before, '->', n_all) +else: + conn.rollback() + print('自检未通过,已 ROLLBACK') + raise SystemExit(1)