|
|
|
@ -28,6 +28,7 @@ async def init_mysql_pool():
|
|
|
|
|
# 保存聊天记录到 MySQL
|
|
|
|
|
async def save_chat_to_mysql(mysql_pool, person_id, prompt, result, audio_url, duration):
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping() # 重置连接
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"INSERT INTO t_chat_log (person_id, user_input, model_response,audio_url,duration,create_time) VALUES (%s, %s, %s, %s, %s,NOW())",
|
|
|
|
@ -39,6 +40,7 @@ async def save_chat_to_mysql(mysql_pool, person_id, prompt, result, audio_url, d
|
|
|
|
|
# 清空表
|
|
|
|
|
async def truncate_chat_log(mysql_pool):
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping() # 重置连接
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
await cur.execute("TRUNCATE TABLE t_chat_log")
|
|
|
|
|
await conn.commit()
|
|
|
|
@ -62,6 +64,7 @@ async def get_chat_log_by_session(mysql_pool, current_user, person_id, page=1, p
|
|
|
|
|
raise ValueError("MySQL 连接池未初始化")
|
|
|
|
|
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping() # 重置连接
|
|
|
|
|
async with conn.cursor(DictCursor) as cur: # 使用 DictCursor
|
|
|
|
|
# 查询总记录数
|
|
|
|
|
await cur.execute(
|
|
|
|
@ -122,6 +125,7 @@ async def get_last_chat_log_id(mysql_pool, person_id):
|
|
|
|
|
:return: 最后一条记录的 id,如果未找到则返回 None
|
|
|
|
|
"""
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping() # 重置连接
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"SELECT id FROM t_chat_log WHERE person_id = %s ORDER BY id DESC LIMIT 1",
|
|
|
|
@ -134,6 +138,7 @@ async def get_last_chat_log_id(mysql_pool, person_id):
|
|
|
|
|
# 更新为危险的记录
|
|
|
|
|
async def update_risk(mysql_pool, person_id, risk_memo):
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping() # 重置连接
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
# 1. 获取此人员的最后一条记录 id
|
|
|
|
|
last_id = await get_last_chat_log_id(mysql_pool, person_id)
|
|
|
|
@ -159,6 +164,7 @@ async def get_user_by_login_name(mysql_pool, login_name: str) -> Optional[Dict]:
|
|
|
|
|
:return: 用户信息(字典形式)
|
|
|
|
|
"""
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping() # 重置连接
|
|
|
|
|
async with conn.cursor() as cursor:
|
|
|
|
|
sql = "SELECT * FROM t_base_person WHERE login_name = %s"
|
|
|
|
|
await cursor.execute(sql, (login_name,))
|
|
|
|
@ -181,6 +187,7 @@ async def get_chat_logs_by_risk_flag(mysql_pool, risk_flag: int, offset: int, pa
|
|
|
|
|
:return: 聊天记录列表和总记录数
|
|
|
|
|
"""
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
await conn.ping() # 重置连接
|
|
|
|
|
async with conn.cursor() as cursor:
|
|
|
|
|
# 查询符合条件的记录
|
|
|
|
|
sql = """
|
|
|
|
|