修复存会话背景问题

This commit is contained in:
junleea 2025-03-25 15:12:24 +08:00
parent 746df74b83
commit c242bbbd60
1 changed files with 9 additions and 2 deletions

View File

@ -2,7 +2,9 @@ package dao
import ( import (
"StuAcaWorksAI/proto" "StuAcaWorksAI/proto"
"encoding/json"
"gorm.io/gorm" "gorm.io/gorm"
"log"
) )
type Session struct { type Session struct {
@ -57,10 +59,15 @@ func FindSessionByUserID(userID int) []Session {
// 更新会话的名字 // 更新会话的名字
func UpdateSessionByID(id int, userId int, name string, context []int) error { func UpdateSessionByID(id int, userId int, name string, context []int) error {
var res *gorm.DB var res *gorm.DB
contextJson, err := json.Marshal(context)
if err != nil {
log.Println("update session json marshal error:", err)
}
if proto.Config.SERVER_SQL_LOG { if proto.Config.SERVER_SQL_LOG {
res = DB.Debug().Model(&Session{}).Where("id = ? AND user_id = ?", id, userId).Updates(Session{Name: name, Context: context}) res = DB.Debug().Model(&Session{}).Where("id = ? AND user_id = ?", id, userId).Updates(Session{Name: name}).Update("context", contextJson)
} else { } else {
res = DB.Model(&Session{}).Where("id = ? AND user_id = ?", id, userId).Updates(Session{Name: name, Context: context}) res = DB.Model(&Session{}).Where("id = ? AND user_id = ?", id, userId).Updates(Session{Name: name, Context: nil}).Update("context", contextJson)
} }
return res.Error return res.Error
} }