user_llm_chat.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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() (err error) {
  43. o := global.DbMap[utils.DbNameAI]
  44. err = o.Create(u).Error
  45. return
  46. }
  47. func (u *UserLlmChat) RenameChatSession() (err error) {
  48. o := global.DbMap[utils.DbNameAI]
  49. var exists bool
  50. err = o.Model(&u).Select("1").Where("id = ?", u.Id).Scan(&exists).Error
  51. if err != nil {
  52. return
  53. }
  54. if !exists {
  55. err = errors.New("当前会话不存在")
  56. return
  57. }
  58. err = o.Select("chat_title").Updates(u).Error
  59. return
  60. }
  61. func GetUserChatList(userId int, monDay, toDay string) (chatList []UserLlmChatListItem, err error) {
  62. o := global.DbMap[utils.DbNameAI]
  63. 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
  64. 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`
  65. err = o.Raw(sql, userId, monDay, toDay).Find(&chatList).Error
  66. return
  67. }