// Copyright © 2023 OpenIM open source community. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package chat import ( "context" "time" "github.com/openimsdk/tools/db/mongoutil" "github.com/openimsdk/tools/db/pagination" "github.com/openimsdk/tools/errs" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" "git.imall.cloud/openim/chat/pkg/common/db/table/chat" ) func NewScheduledTask(db *mongo.Database) (chat.ScheduledTaskInterface, error) { coll := db.Collection("scheduled_tasks") _, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{ { Keys: bson.D{ {Key: "user_id", Value: 1}, {Key: "create_time", Value: -1}, }, }, { Keys: bson.D{ {Key: "user_id", Value: 1}, {Key: "status", Value: 1}, }, }, }) if err != nil { return nil, errs.Wrap(err) } return &ScheduledTask{coll: coll}, nil } type ScheduledTask struct { coll *mongo.Collection } func (o *ScheduledTask) Create(ctx context.Context, tasks ...*chat.ScheduledTask) error { for _, task := range tasks { if task.ID == "" { task.ID = primitive.NewObjectID().Hex() } if task.CreateTime.IsZero() { task.CreateTime = time.Now() } if task.UpdateTime.IsZero() { task.UpdateTime = time.Now() } if task.Status == 0 { task.Status = 1 // 默认为启用状态 } } return mongoutil.InsertMany(ctx, o.coll, tasks) } func (o *ScheduledTask) Take(ctx context.Context, taskID string) (*chat.ScheduledTask, error) { return mongoutil.FindOne[*chat.ScheduledTask](ctx, o.coll, bson.M{"_id": taskID}) } func (o *ScheduledTask) FindByUserID(ctx context.Context, userID string, pagination pagination.Pagination) (int64, []*chat.ScheduledTask, error) { filter := bson.M{ "user_id": userID, } return mongoutil.FindPage[*chat.ScheduledTask](ctx, o.coll, filter, pagination, options.Find().SetSort(bson.D{{Key: "create_time", Value: -1}})) } func (o *ScheduledTask) FindAll(ctx context.Context, pagination pagination.Pagination) (int64, []*chat.ScheduledTask, error) { filter := bson.M{} return mongoutil.FindPage[*chat.ScheduledTask](ctx, o.coll, filter, pagination, options.Find().SetSort(bson.D{{Key: "create_time", Value: -1}})) } func (o *ScheduledTask) Update(ctx context.Context, taskID string, data map[string]any) error { if len(data) == 0 { return nil } data["update_time"] = time.Now() return mongoutil.UpdateOne(ctx, o.coll, bson.M{"_id": taskID}, bson.M{"$set": data}, false) } func (o *ScheduledTask) Delete(ctx context.Context, taskIDs []string) error { if len(taskIDs) == 0 { return nil } _, err := o.coll.DeleteMany(ctx, bson.M{"_id": bson.M{"$in": taskIDs}}) return errs.Wrap(err) }