|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
import logging
|
|
|
|
|
from typing import Optional, Dict
|
|
|
|
|
from typing import Optional, Dict, List
|
|
|
|
|
|
|
|
|
|
from aiomysql import create_pool
|
|
|
|
|
from WxMini.Milvus.Config.MulvusConfig import *
|
|
|
|
@ -147,59 +147,6 @@ async def update_risk(mysql_pool, person_id, risk_memo):
|
|
|
|
|
logger.warning(f"未找到 person_id={person_id} 的记录。")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 查询有风险的聊天记录
|
|
|
|
|
async def get_risk_chat_log_page(mysql_pool, risk_flag, page=1, page_size=10):
|
|
|
|
|
"""
|
|
|
|
|
查询有风险的聊天记录,并按 id 降序分页
|
|
|
|
|
:param mysql_pool: MySQL 连接池
|
|
|
|
|
:param risk_flag: 风险标志
|
|
|
|
|
:param page: 当前页码
|
|
|
|
|
:param page_size: 每页记录数
|
|
|
|
|
:return: 分页数据
|
|
|
|
|
"""
|
|
|
|
|
offset = (page - 1) * page_size
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
async with conn.cursor() as cur:
|
|
|
|
|
# 查询总记录数
|
|
|
|
|
await cur.execute(
|
|
|
|
|
"SELECT COUNT(*) FROM t_chat_log WHERE risk_flag = %s", (risk_flag,)
|
|
|
|
|
)
|
|
|
|
|
total = (await cur.fetchone())[0]
|
|
|
|
|
logger.info(f"总记录数: {total}")
|
|
|
|
|
|
|
|
|
|
# 查询分页数据
|
|
|
|
|
query = (
|
|
|
|
|
"SELECT id, person_id, user_input, model_response, audio_url, duration, create_time, risk_memo "
|
|
|
|
|
"FROM t_chat_log WHERE risk_flag = %s ORDER BY id DESC LIMIT %s OFFSET %s"
|
|
|
|
|
)
|
|
|
|
|
params = (risk_flag, page_size, offset)
|
|
|
|
|
logger.debug(f"执行查询: {query % params}") # 打印 SQL 查询
|
|
|
|
|
|
|
|
|
|
await cur.execute(query, params)
|
|
|
|
|
records = await cur.fetchall()
|
|
|
|
|
logger.debug(f"查询结果: {records}") # 打印查询结果
|
|
|
|
|
|
|
|
|
|
# 将查询结果转换为字典列表
|
|
|
|
|
result = [
|
|
|
|
|
{
|
|
|
|
|
"id": record[0],
|
|
|
|
|
"person_id": record[1],
|
|
|
|
|
"user_input": record[2],
|
|
|
|
|
"model_response": record[3],
|
|
|
|
|
"audio_url": record[4],
|
|
|
|
|
"duration": record[5],
|
|
|
|
|
"create_time": record[6].strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
|
"risk_memo": record[7]
|
|
|
|
|
}
|
|
|
|
|
for record in records
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"data": result,
|
|
|
|
|
"total": total,
|
|
|
|
|
"page": page,
|
|
|
|
|
"page_size": page_size
|
|
|
|
|
}
|
|
|
|
|
# 查询用户信息
|
|
|
|
|
async def get_user_by_login_name(mysql_pool, login_name: str) -> Optional[Dict]:
|
|
|
|
|
"""
|
|
|
|
@ -218,4 +165,47 @@ async def get_user_by_login_name(mysql_pool, login_name: str) -> Optional[Dict]:
|
|
|
|
|
|
|
|
|
|
# 将元组转换为字典
|
|
|
|
|
columns = [column[0] for column in cursor.description]
|
|
|
|
|
return dict(zip(columns, row))
|
|
|
|
|
return dict(zip(columns, row))
|
|
|
|
|
|
|
|
|
|
# 查询聊天记录
|
|
|
|
|
async def get_chat_logs_by_risk_flag(mysql_pool, risk_flag: int, offset: int, page_size: int) -> (List[Dict], int):
|
|
|
|
|
"""
|
|
|
|
|
根据风险标志查询聊天记录
|
|
|
|
|
:param pool: MySQL 连接池
|
|
|
|
|
:param risk_flag: 风险标志
|
|
|
|
|
:param offset: 分页偏移量
|
|
|
|
|
:param page_size: 每页记录数
|
|
|
|
|
:return: 聊天记录列表和总记录数
|
|
|
|
|
"""
|
|
|
|
|
async with mysql_pool.acquire() as conn:
|
|
|
|
|
async with conn.cursor() as cursor:
|
|
|
|
|
# 查询符合条件的记录
|
|
|
|
|
sql = """
|
|
|
|
|
SELECT
|
|
|
|
|
tcl.id, tcl.user_input, tcl.model_response, tcl.audio_url, tcl.duration,
|
|
|
|
|
tcl.create_time, tcl.risk_flag, tcl.risk_memo, tcl.risk_result,
|
|
|
|
|
tbp.*
|
|
|
|
|
FROM t_chat_log AS tcl
|
|
|
|
|
INNER JOIN t_base_person AS tbp ON tcl.person_id = tbp.person_id
|
|
|
|
|
WHERE tcl.risk_flag = %s
|
|
|
|
|
LIMIT %s OFFSET %s
|
|
|
|
|
"""
|
|
|
|
|
await cursor.execute(sql, (risk_flag, page_size, offset))
|
|
|
|
|
rows = await cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
# 查询总记录数
|
|
|
|
|
count_sql = """
|
|
|
|
|
SELECT COUNT(*)
|
|
|
|
|
FROM t_chat_log AS tcl
|
|
|
|
|
INNER JOIN t_base_person AS tbp ON tcl.person_id = tbp.person_id
|
|
|
|
|
WHERE tcl.risk_flag = %s
|
|
|
|
|
"""
|
|
|
|
|
await cursor.execute(count_sql, (risk_flag,))
|
|
|
|
|
total = (await cursor.fetchone())[0]
|
|
|
|
|
|
|
|
|
|
# 将元组转换为字典
|
|
|
|
|
if rows:
|
|
|
|
|
columns = [column[0] for column in cursor.description]
|
|
|
|
|
logs = [dict(zip(columns, row)) for row in rows]
|
|
|
|
|
return logs, total
|
|
|
|
|
return [], 0
|