#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# qc_del_govsearch.py — 手写 DELETE FROM gov_search 台账（分类）
import json, os, re
D = '/root/gov_crawler/'
rows = []
for f in sorted(os.listdir(D)):
    if not (f.startswith('crawl_') and f.endswith('.py')):
        continue
    try:
        src = open(D + f, encoding='utf-8').read()
    except Exception:
        continue
    lines = src.split('\n')
    for i, ln in enumerate(lines, 1):
        if 'DELETE' not in ln.upper() or 'gov_search' not in ln:
            continue
        stripped = ln.strip()
        commented = stripped.startswith('#')
        stmt = ' '.join(stripped.split())[:150]
        if re.search(r'WHERE\s+site_name\s*=\s*\?', stmt, re.I):
            kind = 'site_name 整站'
        elif re.search(r'rowid\s+IN', stmt, re.I):
            kind = 'rowid 子集'
        elif re.search(r'WHERE\s+page_url', stmt, re.I):
            kind = 'page_url 单条'
        else:
            kind = '其他'
        # 看该脚本插入 gov_raw 用的模式
        mode = '?'
        if re.search(r'INSERT\s+OR\s+REPLACE\s+INTO\s+gov_raw', src, re.I):
            mode = 'OR REPLACE'
        elif re.search(r'INSERT\s+OR\s+IGNORE\s+INTO\s+gov_raw', src, re.I):
            mode = 'OR IGNORE'
        elif re.search(r'INSERT\s+INTO\s+gov_raw', src, re.I):
            mode = 'INSERT'
        rows.append(dict(f=f, L=i, kind=kind, commented=commented, mode=mode, stmt=stmt))
json.dump(rows, open(D + 'qc_out/qc_del_govsearch.json', 'w', encoding='utf-8'), ensure_ascii=False, indent=1)
print('总命中: %d 处 / %d 个脚本' % (len(rows), len(set(r['f'] for r in rows))))
print()
from collections import Counter
print('按类型:', dict(Counter(r['kind'] for r in rows)))
print('按是否已注释:', dict(Counter('已注释' if r['commented'] else '生效中' for r in rows)))
print('按插入模式:', dict(Counter(r['mode'] for r in rows)))
print()
print('=== 生效中的「site_name 整站」删除（可安全去除，前 20 条）===')
n = 0
for r in rows:
    if r['kind'] == 'site_name 整站' and not r['commented']:
        n += 1
        if n <= 20:
            print('  %-32s L%-4d %s  | %s' % (r['f'][:32], r['L'], r['mode'], r['stmt'][:88]))
print('  ... 生效中的 site_name 整站合计: %d' % n)
print()
print('=== 生效中的「rowid 子集 / 其他」（需逐条看）===')
for r in rows:
    if r['kind'] in ('rowid 子集', 'page_url 单条', '其他') and not r['commented']:
        print('  %-32s L%-4d [%s] %s' % (r['f'][:32], r['L'], r['kind'], r['stmt'][:88]))
