user_llm_chat.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package llm
  2. import (
  3. "errors"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "time"
  7. )
  8. type UserLlmChat struct {
  9. Id int `gorm:"primaryKey;autoIncrement;comment:会话主键"`
  10. UserId int `gorm:"comment:用户id"`
  11. ChatTitle string `gorm:"comment:会话标题"`
  12. IsDeleted int `gorm:"comment:是否删除"`
  13. CreatedTime time.Time `gorm:"comment:创建时间"`
  14. UpdateTime time.Time `gorm:"autoUpdateTime;comment:更新时间"`
  15. }
  16. func (u *UserLlmChat) TableName() string {
  17. return "user_llm_chat"
  18. }
  19. func (u *UserLlmChat) CreateChatSession() (err error) {
  20. o := global.DbMap[utils.DbNameAI]
  21. err = o.Create(u).Error
  22. return
  23. }
  24. func (u *UserLlmChat) RenameChatSession() (err error) {
  25. o := global.DbMap[utils.DbNameAI]
  26. var exists bool
  27. err = o.Model(&u).Select("1").Where("id = ?", u.Id).Scan(&exists).Error
  28. if err != nil {
  29. return
  30. }
  31. if !exists {
  32. err = errors.New("当前会话不存在")
  33. return
  34. }
  35. err = o.Select("chat_title").Updates(u).Error
  36. return
  37. }
  38. func GetUserChatList(userId int, monDay, toDay string) (chatList []UserLlmChat, err error) {
  39. o := global.DbMap[utils.DbNameAI]
  40. sql := `select ulc.id ,ulc.chat_title,ulc.created_time,COUNT(ucr.id) AS record_count from user_llm_chat ulc left join user_chat_record ucr
  41. ON ucr.chat_id = ulc.id where ulc.user_id=? and ? BETWEEN ? and ? GROUP BY ulc.id`
  42. err = o.Raw(sql, userId, utils.GenerateQuerySql(utils.ToDate, &utils.QueryParam{Column: "ulc.created_time"}), monDay, toDay).Find(&chatList).Error
  43. return
  44. }