package service import ( "context" "time" "github.com/mhaya/game/game_cluster/internal/constant" "github.com/mhaya/game/game_cluster/internal/mdb" "github.com/mhaya/game/game_cluster/internal/mdb/models" "github.com/mhaya/game/game_cluster/nodes/adminapi/entity" "go.mongodb.org/mongo-driver/bson" ) func GetUserCount() *entity.UserCountResp { collection := mdb.MDB.Collection(constant.CNameAccount) // 统计当日注册 和总注册人数 // 获取今天开始的时间戳 now := time.Now() startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Second) // 构建查询条件 - 如果查询值为空那就不添加查询条件 filter := bson.M{} // 统计当日注册 和总注册人数 filter["JoinTime"] = bson.M{ "$gte": startOfDay.Unix(), "$lte": endOfDay.Unix(), } // 获取注册总数 totalCount, err := collection.CountDocuments(context.Background(), bson.M{}) if err != nil { return nil } count, err := mdb.MDB.Collection(constant.CNameAccount).CountDocuments(context.Background(), filter) if err != nil { return nil } return &entity.UserCountResp{ TotalReg: totalCount, ToDayReg: count, } } // GetUserList 获取用户列表 并且可以根据用户ID获取下级用户 func GetUserList(userId string) []*entity.UserListResp { collection := mdb.MDB.Collection(constant.CNamePlayer) filter := bson.M{} if userId != "" { filter["pid"] = userId } cursor, err := collection.Find(context.Background(), filter) if err != nil { return nil } defer cursor.Close(context.Background()) var result []*entity.UserListResp for cursor.Next(context.Background()) { var account *models.Player if err := cursor.Decode(&account); err != nil { return nil } result = append(result, &entity.UserListResp{ UserName: account.UserName, UserNameMaybe: account.UserNameMaybe, NickName: account.NickName, Avatar: account.Avatar, Birthday: account.Birthday, Gender: account.Gender, Level: account.Level, Rank: account.Rank, Exp: account.Exp, JoinTime: account.JoinTime, JoinIP: account.JoinIP, LoginTime: account.LoginTime, LoginIP: account.LoginIP, Status: account.Status, PrevTime: account.PrevTime, Successions: account.Successions, MaxSuccessions: account.MaxSuccessions, IsRobot: account.IsRobot, IsVip: account.IsVip, IsLeader: account.IsLeader, IsFirstBindingXID: account.IsFirstBindingXID, XID: account.XID, OpenId: account.OpenId, Pid: account.Pid, Mobile: account.Mobile, Email: account.Email, TonWall: account.TonWall, UpdateTime: account.UpdateTime, CreateTime: account.CreateTime, RollDay: account.RollDay, DailyRefreshTime: account.DailyRefreshTime, HourRefreshTime: account.HourRefreshTime, WeeklyRefreshTime: account.WeeklyRefreshTime, LoginFailure: account.LoginFailure, IsCashOut: account.IsCashOut, }) } return result }