import os import json from sqlor.dbpools import DBPools async def save_ai_config(request_data: dict, org_id: str) -> dict: """保存AI配置""" dbp = await getDBP(org_id) # 检查是否已存在配置 check_sql = "SELECT id FROM contract_ai_config WHERE org_id = %(org_id)s" check_result = await sor.doQuery(check_sql, {'org_id': org_id}) if check_result: # 更新现有配置 sql = """ UPDATE contract_ai_config SET ai_service_url = %(ai_service_url)s, api_key = %(api_key)s, updated_at = NOW() WHERE org_id = %(org_id)s """ else: # 插入新配置 sql = """ INSERT INTO contract_ai_config (id, ai_service_url, api_key, org_id, created_at, updated_at) VALUES (%(id)s, %(ai_service_url)s, %(api_key)s, %(org_id)s, NOW(), NOW()) """ request_data['id'] = str(uuid.uuid4()).replace('-', '') request_data['org_id'] = org_id await sor.doTransaction([{'sql': sql, 'params': request_data}]) return {'success': True, 'message': 'AI配置保存成功'} # 在 init.py 中添加这个函数的暴露