#!/usr/bin/env python3
"""
完整修复ERP动态API SQL模板 - 最终版本
使用实际存在的字段和正确的SQL语法
"""

import pymysql
import os

MYSQL_HOST = os.getenv('MYSQL_HOST', '192.168.120.60')
MYSQL_PORT = int(os.getenv('MYSQL_PORT', '3306'))
MYSQL_DB = os.getenv('MYSQL_DB', 'hzhub')
MYSQL_USER = os.getenv('MYSQL_USERNAME', 'root')
MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD', 'hzhub123')

print(f"连接MySQL: {MYSQL_HOST}:{MYSQL_PORT}/{MYSQL_DB}")

conn = pymysql.connect(
    host=MYSQL_HOST,
    port=MYSQL_PORT,
    database=MYSQL_DB,
    user=MYSQL_USER,
    password=MYSQL_PASSWORD,
    charset='utf8mb4'
)

cursor = conn.cursor()

# 完整修复的SQL模板（使用实际存在的字段）
fixed_configs = {
    1: {  # api_id = 1 (客户列表查询) - 简化版本，不支持筛选
        'api_name': '客户列表查询（基础版）',
        'api_desc': '查询客户档案列表（基础分页查询，暂不支持筛选）',
        'sql_template': '''
SELECT CLTCODE AS customerCode,
  CLTNAME AS customerName,
  COMPANY_ID AS companyCode,
  COMPANY_NAME AS companyName,
  BRAND AS brand,
  BRANDNAME AS brandName,
  LINKMAN AS contactName,
  AREAID AS salesAreaCode,
  AREANAME AS salesAreaName,
  SALESID_T AS salesPersonCode,
  SALESNAME_T AS salesPersonName,
  TEL1 AS phone,
  EMAIL AS email,
  ISSTOP AS isStop
FROM SCLTGENERAL
        ''',
        'result_type': 'LIST',
        'support_pagination': 1,
    },
    2: {  # api_id = 2 (客户详情查询)
        'sql_template': '''
SELECT CLTCODE AS customerCode,
  CLTNAME AS customerName,
  COMPANY_ID AS companyCode,
  COMPANY_NAME AS companyName,
  BRAND AS brand,
  BRANDNAME AS brandName,
  LINKMAN AS contactName,
  AREAID AS salesAreaCode,
  AREANAME AS salesAreaName,
  SALESID_T AS salesPersonCode,
  SALESNAME_T AS salesPersonName,
  SALEDOCID AS saleDocCode,
  SALEDOCNAME AS saleDocName,
  CLTPRICENO AS pricePlanCode,
  CLTPRICENAME AS pricePlanName,
  CLTTYPE AS customerType,
  STREET AS address,
  TEL1 AS phone,
  EMAIL AS email,
  SDORGID AS sdOrgCode,
  SDORGNAME AS sdOrgName,
  province,
  city,
  ISSTOP AS isStop
FROM SCLTGENERAL
WHERE CLTCODE = #{customerCode}
        ''',
    },
    3: {  # api_id = 3 (销区列表) - 使用Customer表的AREAID字段
        'api_name': '销区列表查询',
        'api_desc': '从客户档案表提取销区列表',
        'sql_template': '''
SELECT DISTINCT AREAID AS salesAreaCode,
  AREANAME AS salesAreaName
FROM SCLTGENERAL
WHERE AREAID IS NOT NULL
  AND AREANAME IS NOT NULL
ORDER BY AREAID
        ''',
        'source_table': 'SCLTGENERAL',
        'source_table_comment': '客户档案主表',
    },
}

print("\n开始完整修复SQL模板...")

for api_id, updates in fixed_configs.items():
    print(f"\n修复API ID {api_id}:")

    # 构建UPDATE语句
    update_fields = []
    update_values = []

    if 'api_name' in updates:
        update_fields.append('api_name = %s')
        update_values.append(updates['api_name'])
        print(f"  ✓ API名称: {updates['api_name']}")

    if 'api_desc' in updates:
        update_fields.append('api_desc = %s')
        update_values.append(updates['api_desc'])
        print(f"  ✓ API描述: {updates['api_desc']}")

    if 'sql_template' in updates:
        update_fields.append('sql_template = %s')
        update_values.append(updates['sql_template'].strip())
        print(f"  ✓ SQL模板已更新")

    if 'source_table' in updates:
        update_fields.append('source_table = %s')
        update_values.append(updates['source_table'])

    if 'source_table_comment' in updates:
        update_fields.append('source_table_comment = %s')
        update_values.append(updates['source_table_comment'])

    # 添加更新时间
    update_fields.append('update_time = NOW()')

    # 执行UPDATE
    sql = f"UPDATE erp_api_config SET {', '.join(update_fields)} WHERE api_id = %s"
    cursor.execute(sql, update_values + [api_id])

conn.commit()

# 清理参数表（api_id=1的客户列表参数太多，简化）
print("\n清理冗余参数...")
cursor.execute("DELETE FROM erp_api_param WHERE api_id = 1")
print("  ✓ 已清空客户列表的旧参数")

# 重新插入简化参数（只保留分页参数）
simplified_params = [
    (1, 'pageNum', '页码', 'Integer', 'QUERY', 0, '1', 1),
    (1, 'pageSize', '页大小', 'Integer', 'QUERY', 0, '10', 2),
]

for api_id, param_name, param_desc, param_type, param_position, is_required, default_value, sort in simplified_params:
    cursor.execute("""
        INSERT INTO erp_api_param (
            api_id, param_name, param_desc, param_type,
            param_position, is_required, default_value, sort,
            create_time
        ) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, NOW())
    """, (api_id, param_name, param_desc, param_type, param_position, is_required, default_value, sort))

print("  ✓ 已插入2个简化参数（分页参数）")

conn.commit()

# 显示修复后的配置
print("\n验证修复结果:")
cursor.execute("""
    SELECT api_id, api_name, api_path, result_type, support_pagination, source_table
    FROM erp_api_config
    ORDER BY api_id
""")

for row in cursor.fetchall():
    api_id, name, path, result_type, pagination, source_table = row
    print(f"  [{api_id}] {name} - {path} ({result_type}, 分页={pagination}, 表={source_table or 'N/A'})")

# 统计参数
cursor.execute("SELECT api_id, COUNT(*) FROM erp_api_param GROUP BY api_id ORDER BY api_id")
params_count = cursor.fetchall()
print("\n参数统计:")
for api_id, count in params_count:
    print(f"  API {api_id}: {count}个参数")

cursor.close()
conn.close()

print("\n✅ ERP动态API完整修复完成！")
print("\n下一步测试:")
print("  1. 重启ERP服务（SQL模板已更新，立即生效）")
print("  2. 测试所有API:")
print("     curl 'http://192.168.120.60:8082/erp/dynamic/v1/customer/brands'")
print("     curl 'http://192.168.120.60:8082/erp/dynamic/v1/customer/sales-areas'")
print("     curl 'http://192.168.120.60:8082/erp/dynamic/v1/customer/list?pageNum=1&pageSize=10'")
print("     curl 'http://192.168.120.60:8082/erp/dynamic/v1/customer/detail?customerCode=C00001'")
print("  3. 验证前端管理界面:")
print("     http://192.168.120.60:5666/erp/api")