package model import ( "context" "mygo/utils" "time" "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" ) type GameUser struct { ID primitive.ObjectID `json:"_id" bson:"_id"` UID int `json:"uid" bson:"uid"` ParentID string `json:"parent_id" bson:"parent_id"` Nickname string `json:"nickname" bson:"nickname"` Avatar string `json:"avatar" bson:"avatar"` OnlineStatus int `json:"online_status" bson:"online_status"` MaxLevel int `json:"max_level" bson:"max_level"` Regionid int `json:"regionid" bson:"regionid"` Serverid int `json:"serverid" bson:"serverid"` AccountStatus int `json:"account_status" bson:"account_status"` AgentStatus int `json:"agent_status" bson:"agent_status"` Money float64 `json:"money" bson:"money"` FreezeReason string `json:"freeze_reason" bson:"freeze_reason"` FreezeDate string `json:"freeze_date" bson:"freeze_date"` LastLoginTime string `json:"last_login_time" bson:"last_login_time"` LastLogoutTime string `json:"last_logout_time" bson:"last_logout_time"` Created_at string `json:"created_at" bson:"created_at"` Updated_at string `json:"updated_at" bson:"updated_at"` } func GetGameUserList(collection *mongo.Collection, uid int64, offset int64, limit int64) ([]GameUser, int64, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() opts := options.Find().SetLimit(limit).SetSkip(offset).SetSort(bson.D{{Key: "max_level", Value: -1}}) var filter = bson.D{} if uid > 0 { filter = bson.D{{Key: "uid", Value: uid}} } cur, err := collection.Find(ctx, filter, opts) if err != nil { return nil, 0, err } defer cur.Close(ctx) users := []GameUser{} err = cur.All(context.Background(), &users) if err != nil { return nil, 0, err } count, err := collection.CountDocuments(ctx, filter) if err != nil { return nil, 0, err } return users, count, nil } func SetAccountStatus(collection *mongo.Collection, uid int64, status int64, freeze_season, freeze_date string) error { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() filter := bson.D{{Key: "uid", Value: uid}} update := bson.D{{Key: "$set", Value: bson.D{{Key: "account_status", Value: status}, {Key: "freeze_reason", Value: freeze_season}, {Key: "freeze_date", Value: freeze_date}}}} _, err := collection.UpdateOne(ctx, filter, update) if err != nil { return err } return nil } func GetOnlineUserCount(collection *mongo.Collection) (int64, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var filter = bson.D{{Key: "online_status", Value: 1}} count, err := collection.CountDocuments(ctx, filter) if err != nil { return 0, err } return count, nil } func GetAgentChildList(collection *mongo.Collection, agent_id string, offset int64, limit int64) ([]GameUser, int64, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() opts := options.Find().SetLimit(limit).SetSkip(offset).SetSort(bson.D{{Key: "max_level", Value: -1}}) var filter = bson.D{{Key: "parent_id", Value: agent_id}} cur, err := collection.Find(ctx, filter, opts) if err != nil { return nil, 0, err } defer cur.Close(ctx) users := []GameUser{} err = cur.All(ctx, &users) if err != nil { return nil, 0, err } count, err := collection.CountDocuments(ctx, filter) if err != nil { return nil, 0, err } return users, count, nil } func GetAgentChildCount(collection *mongo.Collection, agent_id string) (int64, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() var filter = bson.D{{Key: "parent_id", Value: agent_id}} count, err := collection.CountDocuments(ctx, filter) if err != nil { return 0, err } return count, nil } func GetTodayCreateCount(collection *mongo.Collection) (int64, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() today := time.Now().Format("2006-01-02") filter := bson.M{"created_at": bson.M{"$regex": primitive.Regex{Pattern: "^" + today}}} count, err := collection.CountDocuments(ctx, filter) if err != nil { return 0, err } return count, nil } func GetTotalCreateCount(collection *mongo.Collection) (int64, error) { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() filter := bson.D{} count, err := collection.CountDocuments(ctx, filter) if err != nil { return 0, err } return count, nil } func CountGameUsersByRegionIDsAndTimeRange(collection *mongo.Collection, ids []int, start_time, end_time string) (map[int]int, error) { query := bson.M{} if len(ids) > 0 { query["regionid"] = bson.M{"$in": ids} } if start_time != "" { startTime, err := utils.ParseTimeString(start_time) if err != nil { return nil, err } query["created_at"] = bson.M{"$gte": startTime} } if end_time != "" { endTime, err := utils.ParseTimeString(end_time) if err != nil { return nil, err } if _, ok := query["created_at"]; !ok { query["created_at"] = bson.M{} } query["created_at"].(bson.M)["$lte"] = endTime } matchStage := bson.M{"$match": query} groupStage := bson.M{ "$group": bson.M{ "_id": "$regionid", "user_count": bson.M{"$sum": 1}, }, } pipeline := []bson.M{ matchStage, groupStage, } cursor, err := collection.Aggregate(context.Background(), pipeline, options.Aggregate().SetAllowDiskUse(true)) if err != nil { return nil, err } defer cursor.Close(context.Background()) userCounts := make(map[int]int) for cursor.Next(context.Background()) { var result struct { RegionID int `bson:"_id"` UserCount int `bson:"user_count"` } if err := cursor.Decode(&result); err != nil { return nil, err } userCounts[result.RegionID] = result.UserCount } return userCounts, nil } func CountGameOnlineUsersByRegionIDs(collection *mongo.Collection, ids []int) (map[int]int, error) { query := bson.M{} if len(ids) > 0 { query["regionid"] = bson.M{"$in": ids} } query["online_status"] = bson.M{"$eq": 1} matchStage := bson.M{"$match": query} groupStage := bson.M{ "$group": bson.M{ "_id": "$regionid", "user_count": bson.M{"$sum": 1}, }, } pipeline := []bson.M{ matchStage, groupStage, } cursor, err := collection.Aggregate(context.Background(), pipeline, options.Aggregate().SetAllowDiskUse(true)) if err != nil { return nil, err } defer cursor.Close(context.Background()) userCounts := make(map[int]int) for cursor.Next(context.Background()) { var result struct { RegionId int `bson:"_id"` UserCount int `bson:"user_count"` } if err := cursor.Decode(&result); err != nil { return nil, err } userCounts[result.RegionId] = result.UserCount } return userCounts, nil } func CountGameUsersByServerIDsAndTimeRange(collection *mongo.Collection, ids []int, start_time, end_time string) (map[int]int, error) { query := bson.M{} if len(ids) > 0 { query["serverid"] = bson.M{"$in": ids} } if start_time != "" { startTime, err := utils.ParseTimeString(start_time) if err != nil { return nil, err } query["created_at"] = bson.M{"$gte": startTime} } if end_time != "" { endTime, err := utils.ParseTimeString(end_time) if err != nil { return nil, err } if _, ok := query["created_at"]; !ok { query["created_at"] = bson.M{} } query["created_at"].(bson.M)["$lte"] = endTime } matchStage := bson.M{"$match": query} groupStage := bson.M{ "$group": bson.M{ "_id": "$serverid", "user_count": bson.M{"$sum": 1}, }, } pipeline := []bson.M{ matchStage, groupStage, } cursor, err := collection.Aggregate(context.Background(), pipeline, options.Aggregate().SetAllowDiskUse(true)) if err != nil { return nil, err } defer cursor.Close(context.Background()) userCounts := make(map[int]int) for cursor.Next(context.Background()) { var result struct { ServerId int `bson:"_id"` UserCount int `bson:"user_count"` } if err := cursor.Decode(&result); err != nil { return nil, err } userCounts[result.ServerId] = result.UserCount } return userCounts, nil } func CountGameOnlineUsersByServerIDs(collection *mongo.Collection, ids []int) (map[int]int, error) { query := bson.M{} if len(ids) > 0 { query["serverid"] = bson.M{"$in": ids} } query["online_status"] = bson.M{"$eq": 1} matchStage := bson.M{"$match": query} groupStage := bson.M{ "$group": bson.M{ "_id": "$serverid", "user_count": bson.M{"$sum": 1}, }, } pipeline := []bson.M{ matchStage, groupStage, } cursor, err := collection.Aggregate(context.Background(), pipeline, options.Aggregate().SetAllowDiskUse(true)) if err != nil { return nil, err } defer cursor.Close(context.Background()) userCounts := make(map[int]int) for cursor.Next(context.Background()) { var result struct { ServerId int `bson:"_id"` UserCount int `bson:"user_count"` } if err := cursor.Decode(&result); err != nil { return nil, err } userCounts[result.ServerId] = result.UserCount } return userCounts, nil }