| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package mongo
- import (
- "context"
- "fwh-toolbox-server/database"
- "fwh-toolbox-server/domain"
- "time"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/mongo/options"
- )
- const CollectionName_AiChatSession = "AiChatSessions"
- // UpsertAiChatSession 创建或更新会话记录
- func UpsertAiChatSession(sess domain.AiChatSession) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_AiChatSession)
- filter := bson.M{
- "userId": sess.UserId,
- "sessionId": sess.SessionId,
- }
- update := bson.M{
- "$set": bson.M{
- "title": sess.Title,
- "isActive": sess.IsActive,
- "lastActive": sess.LastActive,
- "updatedAt": time.Now(),
- },
- "$setOnInsert": bson.M{
- "userId": sess.UserId,
- "sessionId": sess.SessionId,
- "createdAt": sess.CreatedAt,
- },
- }
- opts := options.Update().SetUpsert(true)
- _, err := collection.UpdateOne(ctx, filter, update, opts)
- return err
- }
- // GetAiChatSessionsByUser 获取用户的所有会话(按 lastActive 倒序)
- func GetAiChatSessionsByUser(userId string) ([]domain.AiChatSession, error) {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_AiChatSession)
- filter := bson.M{"userId": userId}
- findOpts := options.Find().
- SetSort(bson.D{{Key: "lastActive", Value: -1}}).
- SetLimit(50)
- cursor, err := collection.Find(ctx, filter, findOpts)
- if err != nil {
- return nil, err
- }
- defer cursor.Close(ctx)
- var sessions []domain.AiChatSession
- if err := cursor.All(ctx, &sessions); err != nil {
- return nil, err
- }
- return sessions, nil
- }
- // UpdateAiChatSessionTitle 更新会话标题
- func UpdateAiChatSessionTitle(userId string, sessionId string, title string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_AiChatSession)
- filter := bson.M{"userId": userId, "sessionId": sessionId}
- update := bson.M{
- "$set": bson.M{
- "title": title,
- "updatedAt": time.Now(),
- },
- }
- _, err := collection.UpdateOne(ctx, filter, update)
- return err
- }
- // UpdateAiChatSessionActive 更新会话的活跃状态
- func UpdateAiChatSessionActive(userId string, sessionId string, isActive bool) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_AiChatSession)
- filter := bson.M{"userId": userId, "sessionId": sessionId}
- update := bson.M{
- "$set": bson.M{
- "isActive": isActive,
- "lastActive": time.Now(),
- "updatedAt": time.Now(),
- },
- }
- _, err := collection.UpdateOne(ctx, filter, update)
- return err
- }
- // DeactivateAllAiChatSessions 将用户所有会话设为非活跃
- func DeactivateAllAiChatSessions(userId string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_AiChatSession)
- filter := bson.M{"userId": userId, "isActive": true}
- update := bson.M{
- "$set": bson.M{
- "isActive": false,
- "updatedAt": time.Now(),
- },
- }
- _, err := collection.UpdateMany(ctx, filter, update)
- return err
- }
- // DeleteAiChatSession 删除会话记录
- func DeleteAiChatSession(userId string, sessionId string) error {
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- collection := database.GetCollection(CollectionName_AiChatSession)
- filter := bson.M{"userId": userId, "sessionId": sessionId}
- _, err := collection.DeleteOne(ctx, filter)
- return err
- }
|