|
|
|
@ -46,6 +46,7 @@ client = AsyncOpenAI(
|
|
|
|
|
base_url=Config.MODEL_API_URL,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def lifespan(app: FastAPI):
|
|
|
|
|
# 抑制HTTPS相关警告
|
|
|
|
|
warnings.filterwarnings('ignore', message='Connecting to .* using TLS with verify_certs=False is insecure')
|
|
|
|
@ -173,25 +174,31 @@ async def get_tree_data():
|
|
|
|
|
await conn.ping()
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
await cur.execute("""
|
|
|
|
|
SELECT id, title, parent_id, is_leaf,
|
|
|
|
|
prerequisite, related
|
|
|
|
|
FROM knowledge_points
|
|
|
|
|
ORDER BY parent_id, id
|
|
|
|
|
""")
|
|
|
|
|
SELECT id,
|
|
|
|
|
title,
|
|
|
|
|
parent_id,
|
|
|
|
|
is_leaf,
|
|
|
|
|
prerequisite,
|
|
|
|
|
related
|
|
|
|
|
FROM knowledge_points
|
|
|
|
|
ORDER BY parent_id, id
|
|
|
|
|
""")
|
|
|
|
|
rows = await cur.fetchall()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 构建节点映射
|
|
|
|
|
nodes = {}
|
|
|
|
|
for row in rows:
|
|
|
|
|
prerequisite_data = json.loads(row[4]) if row[4] else []
|
|
|
|
|
# 转换先修知识格式
|
|
|
|
|
if isinstance(prerequisite_data, list) and len(prerequisite_data) > 0 and isinstance(prerequisite_data[0], dict):
|
|
|
|
|
if isinstance(prerequisite_data, list) and len(prerequisite_data) > 0 and isinstance(prerequisite_data[0],
|
|
|
|
|
dict):
|
|
|
|
|
# 已经是新格式
|
|
|
|
|
prerequisites = prerequisite_data
|
|
|
|
|
else:
|
|
|
|
|
# 转换为新格式
|
|
|
|
|
prerequisites = [{"id": str(id), "title": title} for id, title in (prerequisite_data or [])] if prerequisite_data else None
|
|
|
|
|
|
|
|
|
|
prerequisites = [{"id": str(id), "title": title} for id, title in
|
|
|
|
|
(prerequisite_data or [])] if prerequisite_data else None
|
|
|
|
|
|
|
|
|
|
nodes[row[0]] = {
|
|
|
|
|
"id": row[0],
|
|
|
|
|
"title": row[1],
|
|
|
|
@ -201,7 +208,7 @@ async def get_tree_data():
|
|
|
|
|
"related": json.loads(row[5]) if row[5] and len(json.loads(row[5])) > 0 else None,
|
|
|
|
|
"open": True
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 构建树形结构
|
|
|
|
|
tree_data = []
|
|
|
|
|
for node_id, node in nodes.items():
|
|
|
|
@ -213,39 +220,52 @@ async def get_tree_data():
|
|
|
|
|
if "children" not in nodes[parent_id]:
|
|
|
|
|
nodes[parent_id]["children"] = []
|
|
|
|
|
nodes[parent_id]["children"].append(node)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {"code": 0, "data": tree_data}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
return {"code": 1, "msg": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/api/update-prerequisites")
|
|
|
|
|
async def update_prerequisites(request: fastapi.Request):
|
|
|
|
|
@app.post("/api/update-knowledge")
|
|
|
|
|
async def update_knowledge(request: fastapi.Request):
|
|
|
|
|
try:
|
|
|
|
|
data = await request.json()
|
|
|
|
|
node_id = data.get('node_id')
|
|
|
|
|
prerequisites = data.get('prerequisites', [])
|
|
|
|
|
|
|
|
|
|
knowledge = data.get('knowledge', [])
|
|
|
|
|
update_type = data.get('update_type', 'prerequisite') # 默认为先修知识
|
|
|
|
|
|
|
|
|
|
if not node_id:
|
|
|
|
|
raise ValueError("Missing node_id")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mysql_pool = await init_mysql_pool()
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping()
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"""
|
|
|
|
|
UPDATE knowledge_points
|
|
|
|
|
SET prerequisite = %s
|
|
|
|
|
WHERE id = %s
|
|
|
|
|
""",
|
|
|
|
|
(json.dumps([{"id": p["id"], "title": p["title"]} for p in prerequisites]), node_id)
|
|
|
|
|
)
|
|
|
|
|
if update_type == 'prerequisite':
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"""
|
|
|
|
|
UPDATE knowledge_points
|
|
|
|
|
SET prerequisite = %s
|
|
|
|
|
WHERE id = %s
|
|
|
|
|
""",
|
|
|
|
|
(json.dumps([{"id": p["id"], "title": p["title"]} for p in knowledge], ensure_ascii=False),
|
|
|
|
|
node_id)
|
|
|
|
|
)
|
|
|
|
|
else: # related knowledge
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"""
|
|
|
|
|
UPDATE knowledge_points
|
|
|
|
|
SET related = %s
|
|
|
|
|
WHERE id = %s
|
|
|
|
|
""",
|
|
|
|
|
(json.dumps([{"id": p["id"], "title": p["title"]} for p in knowledge], ensure_ascii=False),
|
|
|
|
|
node_id)
|
|
|
|
|
)
|
|
|
|
|
await conn.commit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {"code": 0, "msg": "更新成功"}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"更新先修知识失败: {str(e)}")
|
|
|
|
|
logger.error(f"更新知识失败: {str(e)}")
|
|
|
|
|
return {"code": 1, "msg": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|