72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
package biz
|
|
|
|
import (
|
|
"ai_scheduler/internal/data/impl"
|
|
"ai_scheduler/internal/data/model"
|
|
"ai_scheduler/internal/entitys"
|
|
"context"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
type ChatHistoryBiz struct {
|
|
chatHiRepo *impl.ChatHisImpl
|
|
}
|
|
|
|
func NewChatHistoryBiz(chatHiRepo *impl.ChatHisImpl) *ChatHistoryBiz {
|
|
s := &ChatHistoryBiz{
|
|
chatHiRepo: chatHiRepo,
|
|
}
|
|
//go s.AsyncProcess(context.Background())
|
|
return s
|
|
}
|
|
|
|
// 查询会话历史
|
|
func (s *ChatHistoryBiz) List(ctx context.Context, query *entitys.ChatHistQuery) ([]model.AiChatHi, error) {
|
|
chats, err := s.chatHiRepo.FindAll(
|
|
s.chatHiRepo.WithSessionId(query.SessionID),
|
|
s.chatHiRepo.PaginateScope(query.Page, query.PageSize),
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return chats, nil
|
|
}
|
|
|
|
// 添加会话历史
|
|
func (s *ChatHistoryBiz) Create(ctx context.Context, chat entitys.ChatHistory) error {
|
|
return s.chatHiRepo.Create(&model.AiChatHi{
|
|
SessionID: chat.SessionID,
|
|
Ques: chat.Role.String(),
|
|
Ans: chat.Content,
|
|
})
|
|
}
|
|
|
|
// 更新会话历史内容
|
|
func (s *ChatHistoryBiz) UpdateContent(ctx context.Context, chat *entitys.UseFulRequest) error {
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"his_id": chat.HisId})
|
|
|
|
return s.chatHiRepo.UpdateByCond(&cond, &model.AiChatHi{HisID: chat.HisId, Useful: chat.Useful})
|
|
}
|
|
|
|
// 异步添加会话历史
|
|
//func (s *ChatHistoryBiz) AsyncCreate(ctx context.Context, chat entitys.ChatHistory) {
|
|
// s.chatRepo.AsyncCreate(ctx, model.AiChatHi{
|
|
// SessionID: chat.SessionID,
|
|
// Role: chat.Role.String(),
|
|
// Content: chat.Content,
|
|
// })
|
|
//}
|
|
|
|
// 异步处理会话历史
|
|
//func (s *ChatHistoryBiz) AsyncProcess(ctx context.Context) {
|
|
// s.chatRepo.AsyncProcess(ctx)
|
|
//}
|
|
|
|
func (s *ChatHistoryBiz) Update(ctx context.Context, chat *entitys.UseFulRequest) error {
|
|
cond := builder.NewCond()
|
|
cond = cond.And(builder.Eq{"his_id": chat.HisId})
|
|
return s.chatHiRepo.UpdateByCond(&cond, &model.AiChatHi{HisID: chat.HisId, Useful: chat.Useful})
|
|
}
|