conversation_repository.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package repository
  2. import (
  3. "errors"
  4. "github.com/2930134478/AI-CS/backend/models"
  5. "gorm.io/gorm"
  6. )
  7. // ConversationRepository 封装与会话相关的数据库操作。
  8. type ConversationRepository struct {
  9. db *gorm.DB
  10. }
  11. // NewConversationRepository 创建会话仓库实例。
  12. func NewConversationRepository(db *gorm.DB) *ConversationRepository {
  13. return &ConversationRepository{db: db}
  14. }
  15. // FindOpenByVisitorID 查询访客当前未关闭的会话。
  16. func (r *ConversationRepository) FindOpenByVisitorID(visitorID uint) (*models.Conversation, error) {
  17. var conv models.Conversation
  18. err := r.db.Where("visitor_id = ? AND status != ?", visitorID, "closed").
  19. Order("created_at desc").
  20. First(&conv).Error
  21. if err != nil {
  22. return nil, err
  23. }
  24. return &conv, nil
  25. }
  26. // Create 创建新的会话记录。
  27. func (r *ConversationRepository) Create(conv *models.Conversation) error {
  28. return r.db.Create(conv).Error
  29. }
  30. // UpdateFields 更新会话的指定字段。
  31. func (r *ConversationRepository) UpdateFields(id uint, values map[string]interface{}) error {
  32. if len(values) == 0 {
  33. return nil
  34. }
  35. return r.db.Model(&models.Conversation{}).Where("id = ?", id).Updates(values).Error
  36. }
  37. // GetByID 根据主键查询会话。
  38. func (r *ConversationRepository) GetByID(id uint) (*models.Conversation, error) {
  39. var conv models.Conversation
  40. if err := r.db.First(&conv, id).Error; err != nil {
  41. return nil, err
  42. }
  43. return &conv, nil
  44. }
  45. // ListActive 返回所有未关闭的会话。
  46. func (r *ConversationRepository) ListActive() ([]models.Conversation, error) {
  47. var conversations []models.Conversation
  48. if err := r.db.Where("status != ?", "closed").
  49. Order("updated_at desc").
  50. Find(&conversations).Error; err != nil {
  51. return nil, err
  52. }
  53. return conversations, nil
  54. }
  55. // ListByIDs 根据多个 ID 批量查询会话。
  56. func (r *ConversationRepository) ListByIDs(ids []uint) ([]models.Conversation, error) {
  57. if len(ids) == 0 {
  58. return []models.Conversation{}, nil
  59. }
  60. var conversations []models.Conversation
  61. if err := r.db.Where("id IN ? AND status != ?", ids, "closed").
  62. Order("updated_at desc").
  63. Find(&conversations).Error; err != nil {
  64. return nil, err
  65. }
  66. return conversations, nil
  67. }
  68. // SearchByIDOrVisitorLike 根据会话 ID 或访客 ID 进行模糊搜索。
  69. func (r *ConversationRepository) SearchByIDOrVisitorLike(pattern string) ([]models.Conversation, error) {
  70. var conversations []models.Conversation
  71. if err := r.db.Where("CAST(id AS CHAR) LIKE ? OR CAST(visitor_id AS CHAR) LIKE ?", pattern, pattern).
  72. Find(&conversations).Error; err != nil {
  73. return nil, err
  74. }
  75. return conversations, nil
  76. }
  77. // AssignAgent 为会话分配客服。
  78. func (r *ConversationRepository) AssignAgent(conversationID uint, agentID uint) error {
  79. result := r.db.Model(&models.Conversation{}).
  80. Where("id = ?", conversationID).
  81. Updates(map[string]interface{}{
  82. "agent_id": agentID,
  83. })
  84. if result.Error != nil {
  85. return result.Error
  86. }
  87. if result.RowsAffected == 0 {
  88. return gorm.ErrRecordNotFound
  89. }
  90. return nil
  91. }
  92. // UpdateStatus 更新会话状态。
  93. func (r *ConversationRepository) UpdateStatus(conversationID uint, status string) error {
  94. if status == "" {
  95. return errors.New("status cannot be empty")
  96. }
  97. return r.db.Model(&models.Conversation{}).
  98. Where("id = ?", conversationID).
  99. Update("status", status).Error
  100. }