83 lines
3.9 KiB
Python
83 lines
3.9 KiB
Python
#!/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 >= '<t0>' 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)
|