user_llm_chat.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. type UserLlmChatListItem struct {
  17. Id int `gorm:"primaryKey;autoIncrement;comment:会话主键"`
  18. UserId int `gorm:"comment:用户id"`
  19. ChatTitle string `gorm:"comment:会话标题"`
  20. CreatedTime time.Time `gorm:"comment:创建时间"`
  21. RecordCount int `gorm:"comment:会话记录数"`
  22. }
  23. type UserLlmChatListViewItem struct {
  24. Id int `gorm:"primaryKey;autoIncrement;comment:会话主键"`
  25. UserId int `gorm:"comment:用户id"`
  26. ChatTitle string `gorm:"comment:会话标题"`
  27. CreatedTime string `gorm:"comment:创建时间"`
  28. RecordCount int `gorm:"comment:会话记录数"`
  29. }
  30. func CovertItemToView(item UserLlmChatListItem) UserLlmChatListViewItem {
  31. return UserLlmChatListViewItem{
  32. Id: item.Id,
  33. UserId: item.UserId,
  34. ChatTitle: item.ChatTitle,
  35. CreatedTime: item.CreatedTime.Format(utils.FormatDateTime),
  36. RecordCount: item.RecordCount,
  37. }
  38. }
  39. func (u *UserLlmChat) TableName() string {
  40. return "user_llm_chat"
  41. }
  42. func (u *UserLlmChat) CreateChatSession() (chatId int, err error) {
  43. o := global.DbMap[utils.DbNameAI]
  44. err = o.Create(u).Error
  45. if err != nil {
  46. return
  47. }
  48. chatId = u.Id
  49. return
  50. }
  51. func (u *UserLlmChat) RenameChatSession() (err error) {
  52. o := global.DbMap[utils.DbNameAI]
  53. var exists bool
  54. err = o.Model(&u).Select("1").Where("id = ?", u.Id).Scan(&exists).Error
  55. if err != nil {
  56. return
  57. }
  58. if !exists {
  59. err = errors.New("当前会话不存在")
  60. return
  61. }
  62. err = o.Select("chat_title").Updates(u).Error
  63. return
  64. }
  65. func (u *UserLlmChat) DeleteChatSession() (err error) {
  66. o := global.DbMap[utils.DbNameAI]
  67. err = o.Select("is_deleted").Updates(u).Error
  68. return
  69. }
  70. func GetUserChatList(userId int, monDay, toDay string) (chatList []UserLlmChatListItem, err error) {
  71. o := global.DbMap[utils.DbNameAI]
  72. 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
  73. 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`
  74. err = o.Raw(sql, userId, monDay, toDay).Find(&chatList).Error
  75. return
  76. }