#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""一次性规范化: 把 gov_raw.industry 非规范值(中文标签/数字码/空串)统一为英文 id 体系。
规范集合: 13个英文 id + other。非规范值用 classify_industry(title) 重算(与回填同一套规则)。
"""
import sqlite3, sys
sys.path.insert(0, '/root/gov_crawler')
from search_app import classify_industry

DB = '/mnt/data/search.db'
CANONICAL = {'power','terminals','transmission','production','altfuel','hpi','cpi',
             'metals','pulp','food','logistics','manufacturing','pharma','other'}

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

placeholders = ','.join('?' * len(CANONICAL))
rows = c.execute(
    "SELECT id, title, industry FROM gov_raw "
    "WHERE industry NOT IN ({ph}) OR industry IS NULL OR industry = ''".format(ph=placeholders),
    *[list(CANONICAL)]
).fetchall()
print('非规范行数: {}'.format(len(rows)))

changed = 0
batch = []
for rid, title, old in rows:
    new = classify_industry(title or '')
    if new != old:
        batch.append((new, rid))
        changed += 1
    if len(batch) >= 5000:
        conn.executemany('UPDATE gov_raw SET industry=? WHERE id=?', batch)
        batch = []
        print('  已处理 {} ...'.format(changed))
if batch:
    conn.executemany('UPDATE gov_raw SET industry=? WHERE id=?', batch)
conn.close()
print('完成: {} 行已统一为规范行业值'.format(changed))
