package llm import ( "errors" "eta/eta_api/global" "eta/eta_api/utils" "time" ) type UserLlmChat struct { Id int `gorm:"primaryKey;autoIncrement;comment:会话主键"` UserId int `gorm:"comment:用户id"` ChatTitle string `gorm:"comment:会话标题"` IsDeleted int `gorm:"comment:是否删除"` CreatedTime time.Time `gorm:"comment:创建时间"` UpdateTime time.Time `gorm:"autoUpdateTime;comment:更新时间"` } type UserLlmChatListItem struct { Id int `gorm:"primaryKey;autoIncrement;comment:会话主键"` UserId int `gorm:"comment:用户id"` ChatTitle string `gorm:"comment:会话标题"` CreatedTime time.Time `gorm:"comment:创建时间"` RecordCount int `gorm:"comment:会话记录数"` } type UserLlmChatListViewItem struct { Id int `gorm:"primaryKey;autoIncrement;comment:会话主键"` UserId int `gorm:"comment:用户id"` ChatTitle string `gorm:"comment:会话标题"` CreatedTime string `gorm:"comment:创建时间"` RecordCount int `gorm:"comment:会话记录数"` } func CovertItemToView(item UserLlmChatListItem) UserLlmChatListViewItem { return UserLlmChatListViewItem{ Id: item.Id, UserId: item.UserId, ChatTitle: item.ChatTitle, CreatedTime: item.CreatedTime.Format(utils.FormatDateTime), RecordCount: item.RecordCount, } } func (u *UserLlmChat) TableName() string { return "user_llm_chat" } func (u *UserLlmChat) CreateChatSession() (err error) { o := global.DbMap[utils.DbNameAI] err = o.Create(u).Error return } func (u *UserLlmChat) RenameChatSession() (err error) { o := global.DbMap[utils.DbNameAI] var exists bool err = o.Model(&u).Select("1").Where("id = ?", u.Id).Scan(&exists).Error if err != nil { return } if !exists { err = errors.New("当前会话不存在") return } err = o.Select("chat_title").Updates(u).Error return } func GetUserChatList(userId int, monDay, toDay string) (chatList []UserLlmChatListItem, err error) { o := global.DbMap[utils.DbNameAI] sql := `select ulc.id AS id ,ulc.user_id as user_id,ulc.chat_title as chat_title,ulc.created_time,COUNT(ucr.id) AS record_count from user_llm_chat ulc left join user_chat_record ucr ON ucr.chat_id = ulc.id where ulc.user_id=? and ` + utils.GenerateQuerySql(utils.ToDate, &utils.QueryParam{Column: "ulc.created_time"}) + ` BETWEEN ? and ? GROUP BY ulc.id order by ulc.created_time desc` err = o.Raw(sql, userId, monDay, toDay).Find(&chatList).Error return }