#!/usr/bin/env python3
"""backfill_industry_v2.py — 用新 classify_industry(英文id+长度优先) 重算存量行业分类
只处理 industry='other' 的行（其他已分类的不动，避免覆盖人工/既有正确分类）。
分块处理避免锁库。用法: python3 backfill_industry_v2.py
"""
import sqlite3
import sys
sys.path.insert(0, '/root/gov_crawler')
import search_app as sa

DB = '/mnt/data/search.db'
BATCH = 5000

conn = sqlite3.connect(DB, timeout=60, isolation_level=None)
conn.execute('PRAGMA busy_timeout=30000')
c = conn.cursor()

# 只取 other 行 id+title
c.execute("SELECT COUNT(*) FROM gov_raw WHERE industry='other'")
total_other = c.fetchone()[0]
print(f'other 总数: {total_other}')

c.execute("SELECT id, title FROM gov_raw WHERE industry='other'")
rows = c.fetchall()
changed = 0
batch = []
for rid, title in rows:
    new_ind = sa.classify_industry(title or '')
    if new_ind and new_ind != 'other':
        batch.append((new_ind, rid))
        changed += 1
    if len(batch) >= BATCH:
        conn.executemany('UPDATE gov_raw SET industry=? WHERE id=?', batch)
        batch = []
        print(f'  已回填 {changed}...')
if batch:
    conn.executemany('UPDATE gov_raw SET industry=? WHERE id=?', batch)
conn.close()
print(f'完成: {changed} 条从 other 重新分类')
