"""修复：重新采集 erp_schema 中 field_count=0 的表结构"""
import sys, os, json, urllib.request, logging, time
from os import getenv
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app.database import get_session_local
from sqlalchemy import text

logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger("schema_fix")

API_BASE = "http://127.0.0.1:8300/api/v1"
API_KEY = os.getenv("ERP_API_KEY", "erp-gateway-key-bhwl-2026")
HEADERS = {"X-API-Key": API_KEY}

def api_get(path):
    req = urllib.request.Request(f"{API_BASE}{path}", headers=HEADERS)
    with urllib.request.urlopen(req, timeout=30) as resp:
        return json.loads(resp.read().decode())

db = get_session_local()()

rows = db.execute(text("SELECT table_name FROM erp_schema WHERE field_count = 0")).fetchall()
to_fix = [r[0] for r in rows]
logger.info(f"需重新采集: {len(to_fix)} 张表")

fixed, errors = 0, 0
for i, tn in enumerate(to_fix):
    try:
        data = api_get(f"/query?table={tn}&limit=1")
        cols = data.get("columns", [])
        total = data.get("total", 0)
        fj = json.dumps([{"name": c} for c in cols], ensure_ascii=False)
        db.execute(text("UPDATE erp_schema SET total_rows=:tr, field_count=:fc, fields_json=:fj, updated_at=NOW() WHERE table_name=:tn"),
                   {"tn": tn, "tr": total, "fc": len(cols), "fj": fj})
        db.commit()
        fixed += 1
    except Exception as e:
        errors += 1
        logger.warning(f"  {tn}: {e}")
    if (i+1) % 50 == 0:
        logger.info(f"进度: {i+1}/{len(to_fix)} 已修{fixed} 错误{errors}")
    time.sleep(0.1)

db.close()
logger.info(f"完成! 成功:{fixed} 错误:{errors}")
