aiChatRepo.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package mongo
  2. import (
  3. "context"
  4. "fwh-toolbox-server/database"
  5. "fwh-toolbox-server/domain"
  6. "time"
  7. "go.mongodb.org/mongo-driver/bson"
  8. "go.mongodb.org/mongo-driver/mongo/options"
  9. )
  10. const CollectionName_AiChatSession = "AiChatSessions"
  11. // UpsertAiChatSession 创建或更新会话记录
  12. func UpsertAiChatSession(sess domain.AiChatSession) error {
  13. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  14. defer cancel()
  15. collection := database.GetCollection(CollectionName_AiChatSession)
  16. filter := bson.M{
  17. "userId": sess.UserId,
  18. "sessionId": sess.SessionId,
  19. }
  20. update := bson.M{
  21. "$set": bson.M{
  22. "title": sess.Title,
  23. "isActive": sess.IsActive,
  24. "lastActive": sess.LastActive,
  25. "updatedAt": time.Now(),
  26. },
  27. "$setOnInsert": bson.M{
  28. "userId": sess.UserId,
  29. "sessionId": sess.SessionId,
  30. "createdAt": sess.CreatedAt,
  31. },
  32. }
  33. opts := options.Update().SetUpsert(true)
  34. _, err := collection.UpdateOne(ctx, filter, update, opts)
  35. return err
  36. }
  37. // GetAiChatSessionsByUser 获取用户的所有会话(按 lastActive 倒序)
  38. func GetAiChatSessionsByUser(userId string) ([]domain.AiChatSession, error) {
  39. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  40. defer cancel()
  41. collection := database.GetCollection(CollectionName_AiChatSession)
  42. filter := bson.M{"userId": userId}
  43. findOpts := options.Find().
  44. SetSort(bson.D{{Key: "lastActive", Value: -1}}).
  45. SetLimit(50)
  46. cursor, err := collection.Find(ctx, filter, findOpts)
  47. if err != nil {
  48. return nil, err
  49. }
  50. defer cursor.Close(ctx)
  51. var sessions []domain.AiChatSession
  52. if err := cursor.All(ctx, &sessions); err != nil {
  53. return nil, err
  54. }
  55. return sessions, nil
  56. }
  57. // UpdateAiChatSessionTitle 更新会话标题
  58. func UpdateAiChatSessionTitle(userId string, sessionId string, title string) error {
  59. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  60. defer cancel()
  61. collection := database.GetCollection(CollectionName_AiChatSession)
  62. filter := bson.M{"userId": userId, "sessionId": sessionId}
  63. update := bson.M{
  64. "$set": bson.M{
  65. "title": title,
  66. "updatedAt": time.Now(),
  67. },
  68. }
  69. _, err := collection.UpdateOne(ctx, filter, update)
  70. return err
  71. }
  72. // UpdateAiChatSessionActive 更新会话的活跃状态
  73. func UpdateAiChatSessionActive(userId string, sessionId string, isActive bool) error {
  74. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  75. defer cancel()
  76. collection := database.GetCollection(CollectionName_AiChatSession)
  77. filter := bson.M{"userId": userId, "sessionId": sessionId}
  78. update := bson.M{
  79. "$set": bson.M{
  80. "isActive": isActive,
  81. "lastActive": time.Now(),
  82. "updatedAt": time.Now(),
  83. },
  84. }
  85. _, err := collection.UpdateOne(ctx, filter, update)
  86. return err
  87. }
  88. // DeactivateAllAiChatSessions 将用户所有会话设为非活跃
  89. func DeactivateAllAiChatSessions(userId string) error {
  90. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  91. defer cancel()
  92. collection := database.GetCollection(CollectionName_AiChatSession)
  93. filter := bson.M{"userId": userId, "isActive": true}
  94. update := bson.M{
  95. "$set": bson.M{
  96. "isActive": false,
  97. "updatedAt": time.Now(),
  98. },
  99. }
  100. _, err := collection.UpdateMany(ctx, filter, update)
  101. return err
  102. }
  103. // DeleteAiChatSession 删除会话记录
  104. func DeleteAiChatSession(userId string, sessionId string) error {
  105. ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
  106. defer cancel()
  107. collection := database.GetCollection(CollectionName_AiChatSession)
  108. filter := bson.M{"userId": userId, "sessionId": sessionId}
  109. _, err := collection.DeleteOne(ctx, filter)
  110. return err
  111. }