#!/usr/bin/env python3
"""
将本地导出的zhaoqing SQL导入服务器DB并同步FTS
用法: 
  1. scp /tmp/zhaoqing_import.sql root@1.94.217.116:/tmp/
  2. python3 import_zhaoqing.py
"""
import subprocess, sys, re

DB = '/mnt/data/search.db'
SQL_FILE = '/tmp/zhaoqing_import.sql'

with open(SQL_FILE, 'r', encoding='utf-8') as f:
    sql_content = f.read()

# Count INSERT statements
inserts = re.findall(r'INSERT OR IGNORE INTO gov_raw', sql_content)
print(f"待导入: {len(inserts)} 条")

# 分批导入（防止事务太大）
BATCH_SIZE = 50
lines = sql_content.split('\n')
current_batch = []
added = 0

for line in lines:
    current_batch.append(line)
    if 'INSERT OR IGNORE INTO gov_raw' in line:
        current_batch.append('SELECT CASE WHEN changes() > 0 THEN last_insert_rowid() ELSE 0 END;')
        full_sql = '\n'.join(current_batch)
        try:
            result = subprocess.run(
                ['sqlite3', DB],
                input=full_sql,
                capture_output=True, text=True, timeout=30
            )
            if result.returncode == 0:
                out = result.stdout.strip()
                if out:
                    try:
                        rowid = int(out.strip())
                    except ValueError:
                        rowid = 0
                    if rowid > 0:
                        fts_sql = f"""INSERT OR IGNORE INTO gov_search (rowid, title, site_name, summary)
SELECT rowid, title, site_name, summary FROM gov_raw WHERE rowid = {rowid};
"""
                        subprocess.run(['sqlite3', DB], input=fts_sql,
                                     capture_output=True, text=True, timeout=30)
                        added += 1
            else:
                if 'UNIQUE constraint' not in result.stderr:
                    print(f"  [ERR] {result.stderr[:100]}")
        except Exception as e:
            print(f"  [ERR] {e}")
        
        if added % 50 == 0 and added > 0:
            print(f"  已导入: {added}")
        
        current_batch = []

print(f"\n导入完成: {added} 条 (已跳过重复)")
