conversation_repository.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 查询访客当前未关闭的会话(仅 visitor 类型)。
  16. func (r *ConversationRepository) FindOpenByVisitorID(visitorID uint) (*models.Conversation, error) {
  17. var conv models.Conversation
  18. err := r.db.Where("conversation_type = ? AND visitor_id = ? AND status != ?", "visitor", 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. // ListActiveInternalByAgentID 返回某客服的全部未关闭内部对话(知识库测试用)。
  27. func (r *ConversationRepository) ListActiveInternalByAgentID(agentID uint) ([]models.Conversation, error) {
  28. var list []models.Conversation
  29. err := r.db.Where("conversation_type = ? AND agent_id = ? AND status != ?", "internal", agentID, "closed").
  30. Order("updated_at desc").
  31. Find(&list).Error
  32. if err != nil {
  33. return nil, err
  34. }
  35. return list, nil
  36. }
  37. // Create 创建新的会话记录。
  38. func (r *ConversationRepository) Create(conv *models.Conversation) error {
  39. return r.db.Create(conv).Error
  40. }
  41. // UpdateFields 更新会话的指定字段。
  42. func (r *ConversationRepository) UpdateFields(id uint, values map[string]interface{}) error {
  43. if len(values) == 0 {
  44. return nil
  45. }
  46. return r.db.Model(&models.Conversation{}).Where("id = ?", id).Updates(values).Error
  47. }
  48. // GetByID 根据主键查询会话。
  49. func (r *ConversationRepository) GetByID(id uint) (*models.Conversation, error) {
  50. var conv models.Conversation
  51. if err := r.db.First(&conv, id).Error; err != nil {
  52. return nil, err
  53. }
  54. return &conv, nil
  55. }
  56. // ListActive 返回所有未关闭的访客会话(不含 internal)。
  57. func (r *ConversationRepository) ListActive() ([]models.Conversation, error) {
  58. var conversations []models.Conversation
  59. if err := r.db.Where("conversation_type = ? AND status != ?", "visitor", "closed").
  60. Order("updated_at desc").
  61. Find(&conversations).Error; err != nil {
  62. return nil, err
  63. }
  64. return conversations, nil
  65. }
  66. // ListByIDs 根据多个 ID 批量查询会话。
  67. func (r *ConversationRepository) ListByIDs(ids []uint) ([]models.Conversation, error) {
  68. if len(ids) == 0 {
  69. return []models.Conversation{}, nil
  70. }
  71. var conversations []models.Conversation
  72. if err := r.db.Where("id IN ? AND status != ?", ids, "closed").
  73. Order("updated_at desc").
  74. Find(&conversations).Error; err != nil {
  75. return nil, err
  76. }
  77. return conversations, nil
  78. }
  79. // SearchByIDOrVisitorLike 根据会话 ID 或访客 ID 进行模糊搜索。
  80. func (r *ConversationRepository) SearchByIDOrVisitorLike(pattern string) ([]models.Conversation, error) {
  81. var conversations []models.Conversation
  82. if err := r.db.Where("CAST(id AS CHAR) LIKE ? OR CAST(visitor_id AS CHAR) LIKE ?", pattern, pattern).
  83. Find(&conversations).Error; err != nil {
  84. return nil, err
  85. }
  86. return conversations, nil
  87. }
  88. // AssignAgent 为会话分配客服。
  89. func (r *ConversationRepository) AssignAgent(conversationID uint, agentID uint) error {
  90. result := r.db.Model(&models.Conversation{}).
  91. Where("id = ?", conversationID).
  92. Updates(map[string]interface{}{
  93. "agent_id": agentID,
  94. })
  95. if result.Error != nil {
  96. return result.Error
  97. }
  98. if result.RowsAffected == 0 {
  99. return gorm.ErrRecordNotFound
  100. }
  101. return nil
  102. }
  103. // UpdateStatus 更新会话状态。
  104. func (r *ConversationRepository) UpdateStatus(conversationID uint, status string) error {
  105. if status == "" {
  106. return errors.New("status cannot be empty")
  107. }
  108. return r.db.Model(&models.Conversation{}).
  109. Where("id = ?", conversationID).
  110. Update("status", status).Error
  111. }