import requests import json def debug_database_goalid(): """ 调试数据库中goalId的问题 """ try: print("=== 调试数据库goalId问题 ===") # 1. 先调用Coze API生成一道新题目 test_data = { "userId": 1, "goalId": 8, # 使用新的goalId "subject": "数学", "knowledgePoint": "基础加法运算", "difficulty": "简单", "type": "选择题", "totalQuantity": 1 } print(f"步骤1: 调用Coze API生成题目,goalId={test_data['goalId']}") response = requests.post( "http://localhost:8080/api/coze/generate-questions", json=test_data, timeout=30 ) if response.status_code == 200: api_result = response.json() print(f"API调用成功: {api_result.get('success', False)}") if api_result.get('success'): questions = api_result.get('questions', []) print(f"生成了 {len(questions)} 道题目") # 2. 查询数据库中goalId=8的所有题目 print(f"\n步骤2: 查询数据库中goalId={test_data['goalId']}的题目") db_response = requests.get( f"http://localhost:8080/api/ai/questions/{test_data['goalId']}", timeout=10 ) if db_response.status_code == 200: db_result = db_response.json() print(f"数据库查询成功,返回数据: {json.dumps(db_result, indent=2, ensure_ascii=False)}") # 3. 分析返回的数据结构 if 'data' in db_result and db_result['data']: questions = db_result['data'] print(f"\n步骤3: 分析返回的题目数据") for i, question in enumerate(questions, 1): print(f"\n题目 {i}:") print(f" - detailId: {question.get('detailId')}") print(f" - goalId: {question.get('goalId')}") print(f" - content: {question.get('content', '')[:50]}...") print(f" - answer: {question.get('answer')}") print(f" - difficulty: {question.get('difficulty')}") print(f" - knowledgePoint: {question.get('knowledgePoint')}") print(f" - contentType: {question.get('contentType')}") # 检查goalId字段 if 'goalId' not in question: print(f" ❌ 缺少goalId字段!") elif question.get('goalId') is None: print(f" ❌ goalId字段为null!") elif question.get('goalId') == test_data['goalId']: print(f" ✅ goalId一致: {question.get('goalId')}") else: print(f" ❌ goalId不一致: 预期={test_data['goalId']}, 实际={question.get('goalId')}") else: print("❌ 数据库中没有找到题目") else: print(f"❌ 数据库查询失败: {db_response.status_code}") print(db_response.text) else: print(f"❌ API调用失败: {api_result.get('message', 'Unknown error')}") else: print(f"❌ API调用失败: {response.status_code}") print(response.text) except requests.exceptions.Timeout: print("❌ 请求超时") except Exception as e: print(f"❌ 请求异常: {e}") if __name__ == "__main__": debug_database_goalid()