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.
66 lines
2.3 KiB
66 lines
2.3 KiB
1 month ago
|
from sqlalchemy import desc
|
||
|
from sqlalchemy.orm import Session
|
||
|
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
|
||
|
from utils.time_format_util import list_format_datetime
|
||
|
|
||
|
|
||
|
class AiChatDao:
|
||
|
"""
|
||
|
菜单管理模块数据库操作层
|
||
|
"""
|
||
|
|
||
|
@classmethod
|
||
|
def get_ai_session_list(cls, db: Session,session_id: str, user_id: int):
|
||
|
session_list = db.query(AiChatSession)\
|
||
|
.filter(AiChatSession.user == user_id, AiChatSession.session_id != session_id) \
|
||
|
.order_by(desc(AiChatSession.time)) \
|
||
|
.limit(20).all()
|
||
|
return session_list
|
||
|
|
||
|
@classmethod
|
||
|
def get_ai_chat_list(cls, db: Session, session_id: str, user_id: int):
|
||
|
chat_list = db.query(AiChatHistory) \
|
||
|
.filter(AiChatHistory.user == user_id, AiChatHistory.session_id == session_id) \
|
||
|
.order_by(AiChatHistory.time).all()
|
||
|
return chat_list
|
||
|
|
||
|
@classmethod
|
||
|
def get_ai_chat_by_id(cls, session_id: str, db: Session, user_id: int):
|
||
|
chat_list = db.query(AiChatSession) \
|
||
|
.filter(AiChatSession.user == user_id, AiChatSession.session_id == session_id).first()
|
||
|
return chat_list
|
||
|
|
||
|
@classmethod
|
||
|
def add_ai_chat_session(cls, session_id: str, session_name: str, time: str, db: Session, user_id: int):
|
||
|
chat_session = AiChatSession()
|
||
|
chat_session.session_id = session_id
|
||
|
chat_session.session_name = session_name
|
||
|
chat_session.time = time
|
||
|
chat_session.user = user_id
|
||
|
db.add(chat_session)
|
||
|
db.flush()
|
||
|
return chat_session
|
||
|
|
||
|
@classmethod
|
||
|
def add_ai_chat_history(cls, chat: AiChatHistory, db: Session):
|
||
|
db.add(chat)
|
||
|
db.flush()
|
||
|
return chat
|
||
|
|
||
|
@classmethod
|
||
|
def delete_chat_session(cls, db: Session, session_id: str, user_id: int):
|
||
|
db.query(AiChatHistory) \
|
||
|
.filter(AiChatHistory.session_id == session_id) \
|
||
|
.delete()
|
||
|
db.query(AiChatSession) \
|
||
|
.filter(AiChatSession.session_id == session_id) \
|
||
|
.delete()
|
||
|
|
||
|
@classmethod
|
||
|
def update_ai_chat_history(cls, update_chat: AiChatModel, db: Session):
|
||
|
db.query(AiChatHistory) \
|
||
|
.filter(AiChatHistory.chat_id == update_chat.chat_id) \
|
||
|
.update(dict(update_chat))
|