123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- 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() (chatId int, err error) {
- o := global.DbMap[utils.DbNameAI]
- err = o.Create(u).Error
- if err != nil {
- return
- }
- chatId = u.Id
- 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 (u *UserLlmChat) DeleteChatSession() (err error) {
- o := global.DbMap[utils.DbNameAI]
- err = o.Select("is_deleted").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 ? AND is_deleted=0 GROUP BY ulc.id order by ulc.created_time desc`
- err = o.Raw(sql, userId, monDay, toDay).Find(&chatList).Error
- return
- }
|