#!/usr/bin/env python3 # xuchao: 只读审计——hhrs_lottery_pool.member_phone vs 《第二批年龄段25-40岁抽奖数据.xlsx》(2026-11-18 用户指令"比对手机不一致数量") # 用法: python3 docs/tools_audit_phone_vs_doc_20261118.py # 纯只读,不写库不改文件 # 口径: 池按主键 id 关联文件 ID;池存掩码(前3+****+后4),文件明文按 11-10/11-17 既定白名单口径转掩码后比对; # 文件手机非法/空 → 期望 NULL。分类统计不一致,并抽样展示。 import sys, os, re import pandas as pd SRC = os.path.expanduser('~/Downloads/第二批年龄段25-40岁抽奖数据.xlsx') # 2026-11-18 文件为20列全量发卡明细:按位置取 col0=ID, col1=身份证, col2=姓名, col5=手机, col8=县区 F = pd.read_excel(SRC, dtype=str, usecols=[0, 1, 2, 5, 8]).fillna('') F.columns = ['ID', '身份证', '姓名', '手机', '县区'] print('文件行数:', len(F)) assert F['ID'].astype(int).is_unique, '文件ID不唯一' 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 expect_mask(p): return (p[:3] + '****' + p[7:11]) if re.fullmatch(r'\d{11}', p) and p[:3] in allowed else None fmap = {int(i): expect_mask(p) for i, p in zip(F['ID'], F['手机'])} n_valid = sum(1 for v in fmap.values() if v) print(f'文件 {len(fmap)} 行: 白名单口径有效手机 {n_valid}, 期望NULL {len(fmap)-n_valid}') 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, created_time FROM hhrs_lottery_pool") pool = cur.fetchall() print('池总行数:', len(pool)) covered = [r for r in pool if r['id'] in fmap] uncovered = len(pool) - len(covered) print(f'文件覆盖池 {len(covered)} 行, 未覆盖(不在文件) {uncovered} 行') cats = {'一致': 0, '掩码不同': 0, '池NULL但文件有效': 0, '池有值但文件应NULL': 0} samples = {k: [] for k in cats if k != '一致'} by_ct = {} for r in covered: exp = fmap[r['id']] got = r['member_phone'] if got == exp: cats['一致'] += 1 continue if got is None and exp is not None: k = '池NULL但文件有效' elif got is not None and exp is None: k = '池有值但文件应NULL' else: k = '掩码不同' cats[k] += 1 if len(samples[k]) < 5: samples[k].append((r['id'], got, exp)) for r in covered: ct = str(r['created_time']) by_ct.setdefault(ct, [0, 0]) by_ct[ct][1] += 1 if r['member_phone'] == fmap[r['id']]: by_ct[ct][0] += 1 print('\n=== 比对结果(文件覆盖范围内) ===') for k, v in cats.items(): print(f'{k}: {v}') total_bad = sum(v for k, v in cats.items() if k != '一致') print(f'不一致合计: {total_bad}') print('\n按 created_time 分布(一致/总数):') for ct, (ok, tot) in sorted(by_ct.items()): print(f' {ct}: {ok}/{tot} (不一致 {tot-ok})') print('\n抽样(池id, 池现值, 文件期望掩码):') for k, ss in samples.items(): for s in ss: print(f' [{k}] {s}') conn.close()