package model import ( "context" "time" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type ServerItem struct { ID int64 `json:"id" bson:"_id"` ServerId int64 `json:"server_id" bson:"server_id"` Status int64 `json:"status" bson:"status"` ActivityId int64 `json:"activity_id" bson:"activity_id"` ActivityEndTime string `json:"activity_end_time" bson:"activity_end_time"` CreatedAt string `json:"created_at" bson:"created_at"` } type RespServerItem struct { ID int64 `json:"id"` ServerId int64 `json:"server_name"` Status int64 `json:"server_id"` UserCount int64 `json:"user_count"` OnlineCount int64 `json:"online_count"` ActivityId int64 `json:"activity_id"` ActivityEndTime string `json:"activity_end_time"` CreatedAt string `json:"created_at"` } func GetServerItemList(collection *mongo.Collection, server_id int64, offset int64, limit int64) ([]ServerItem, int64, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() filter := bson.D{{Key: "server_id", Value: server_id}} opts := options.Find().SetLimit(limit).SetSkip(offset).SetSort(bson.D{{Key: "_id", Value: 1}}) cur, err := collection.Find(ctx, filter, opts) if err != nil { return nil, 0, err } defer cur.Close(ctx) list := []ServerItem{} err = cur.All(context.Background(), &list) if err != nil { return nil, 0, err } count, err := collection.CountDocuments(ctx, filter) if err != nil { return nil, 0, err } return list, count, nil } func GetServerItemCount(collection *mongo.Collection, server_id int64) (int64, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() filter := bson.D{{Key: "server_id", Value: server_id}} count, err := collection.CountDocuments(ctx, filter) if err != nil { return 0, err } return count, nil } func UpdateServer(collection *mongo.Collection) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() filter := bson.D{{Key: "status", Value: 1}} update := bson.D{{Key: "$set", Value: bson.D{{Key: "status", Value: 2}}}} _, err := collection.UpdateMany(ctx, filter, update) if err != nil { return err } return nil } func AddServer(collection *mongo.Collection, docs []interface{}) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() _, err := collection.InsertMany(ctx, docs) if err != nil { return err } return nil }