You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.5 KiB
65 lines
2.5 KiB
3 weeks ago
|
from sqlalchemy import desc, delete, func, select, update
|
||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
from module_admin.entity.do.aichat_do import AiChatHistory
|
||
|
from module_admin.entity.do.aichat_do import AiChatSession
|
||
|
from module_admin.entity.vo.aichat_vo import AiChatModel
|
||
|
|
||
|
|
||
|
class AiChatDao:
|
||
|
"""
|
||
|
菜单管理模块数据库操作层
|
||
|
"""
|
||
|
|
||
|
@classmethod
|
||
|
async def get_ai_session_list(cls, db: AsyncSession, sessionId: str, user_id: int):
|
||
|
session_list = (
|
||
|
await db.execute(
|
||
|
select(AiChatSession).where(AiChatSession.user == user_id, AiChatSession.sessionId != sessionId)
|
||
|
.order_by(desc(AiChatSession.time)).limit(20)
|
||
|
)
|
||
|
).scalars().all()
|
||
|
return session_list
|
||
|
|
||
|
@classmethod
|
||
|
async def get_ai_chat_list(cls, db: AsyncSession, sessionId: str, user_id: int):
|
||
|
chat_list = (
|
||
|
await db.execute(
|
||
|
select(AiChatHistory).where(AiChatHistory.user == user_id, AiChatHistory.sessionId == sessionId)
|
||
|
.order_by(AiChatHistory.time)
|
||
|
)
|
||
|
).scalars().all()
|
||
|
return chat_list
|
||
|
|
||
|
@classmethod
|
||
|
async def get_ai_chat_by_id(cls, sessionId: str, db: AsyncSession, user_id: int):
|
||
|
chat_list = (await db.execute(select(AiChatSession)
|
||
|
.where(AiChatSession.user == user_id,
|
||
|
AiChatSession.sessionId == sessionId))).scalars().first()
|
||
|
return chat_list
|
||
|
|
||
|
@classmethod
|
||
|
async def add_ai_chat_session(cls, sessionId: str, sessionName: str, time: str, db: AsyncSession, user_id: int):
|
||
|
chat_session = AiChatSession()
|
||
|
chat_session.sessionId = sessionId
|
||
|
chat_session.sessionName = sessionName
|
||
|
chat_session.time = time
|
||
|
chat_session.user = user_id
|
||
|
db.add(chat_session)
|
||
|
db.flush()
|
||
|
return chat_session
|
||
|
|
||
|
@classmethod
|
||
|
async def add_ai_chat_history(cls, chat: AiChatHistory, db: AsyncSession):
|
||
|
db.add(chat)
|
||
|
db.flush()
|
||
|
return chat
|
||
|
|
||
|
@classmethod
|
||
|
async def delete_chat_session(cls, db: AsyncSession, sessionId: str, user_id: int):
|
||
|
await db.execute(delete(AiChatHistory).where(AiChatHistory.sessionId == sessionId))
|
||
|
await db.execute(delete(AiChatSession).where(AiChatSession.sessionId == sessionId))
|
||
|
|
||
|
@classmethod
|
||
|
async def update_ai_chat_history(cls, update_chat: AiChatModel, db: AsyncSession):
|
||
|
await db.execute(update(AiChatHistory), [dict(update_chat)])
|