user.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "github.com/mhaya/game/game_cluster/internal/constant"
  6. "github.com/mhaya/game/game_cluster/internal/mdb"
  7. "github.com/mhaya/game/game_cluster/internal/mdb/models"
  8. "github.com/mhaya/game/game_cluster/nodes/adminapi/entity"
  9. "go.mongodb.org/mongo-driver/bson"
  10. )
  11. func GetUserCount() *entity.UserCountResp {
  12. collection := mdb.MDB.Collection(constant.CNameAccount)
  13. // 统计当日注册 和总注册人数
  14. // 获取今天开始的时间戳
  15. now := time.Now()
  16. startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
  17. endOfDay := startOfDay.Add(24 * time.Hour).Add(-1 * time.Second)
  18. // 构建查询条件 - 如果查询值为空那就不添加查询条件
  19. filter := bson.M{}
  20. // 统计当日注册 和总注册人数
  21. filter["JoinTime"] = bson.M{
  22. "$gte": startOfDay.Unix(),
  23. "$lte": endOfDay.Unix(),
  24. }
  25. // 获取注册总数
  26. totalCount, err := collection.CountDocuments(context.Background(), bson.M{})
  27. if err != nil {
  28. return nil
  29. }
  30. count, err := mdb.MDB.Collection(constant.CNameAccount).CountDocuments(context.Background(), filter)
  31. if err != nil {
  32. return nil
  33. }
  34. return &entity.UserCountResp{
  35. TotalReg: totalCount,
  36. ToDayReg: count,
  37. }
  38. }
  39. // GetUserList 获取用户列表 并且可以根据用户ID获取下级用户
  40. func GetUserList(userId string) []*entity.UserListResp {
  41. collection := mdb.MDB.Collection(constant.CNamePlayer)
  42. filter := bson.M{}
  43. if userId != "" {
  44. filter["pid"] = userId
  45. }
  46. cursor, err := collection.Find(context.Background(), filter)
  47. if err != nil {
  48. return nil
  49. }
  50. defer cursor.Close(context.Background())
  51. var result []*entity.UserListResp
  52. for cursor.Next(context.Background()) {
  53. var account *models.Player
  54. if err := cursor.Decode(&account); err != nil {
  55. return nil
  56. }
  57. result = append(result, &entity.UserListResp{
  58. UserName: account.UserName,
  59. UserNameMaybe: account.UserNameMaybe,
  60. NickName: account.NickName,
  61. Avatar: account.Avatar,
  62. Birthday: account.Birthday,
  63. Gender: account.Gender,
  64. Level: account.Level,
  65. Rank: account.Rank,
  66. Exp: account.Exp,
  67. JoinTime: account.JoinTime,
  68. JoinIP: account.JoinIP,
  69. LoginTime: account.LoginTime,
  70. LoginIP: account.LoginIP,
  71. Status: account.Status,
  72. PrevTime: account.PrevTime,
  73. Successions: account.Successions,
  74. MaxSuccessions: account.MaxSuccessions,
  75. IsRobot: account.IsRobot,
  76. IsVip: account.IsVip,
  77. IsLeader: account.IsLeader,
  78. IsFirstBindingXID: account.IsFirstBindingXID,
  79. XID: account.XID,
  80. OpenId: account.OpenId,
  81. Pid: account.Pid,
  82. Mobile: account.Mobile,
  83. Email: account.Email,
  84. TonWall: account.TonWall,
  85. UpdateTime: account.UpdateTime,
  86. CreateTime: account.CreateTime,
  87. RollDay: account.RollDay,
  88. DailyRefreshTime: account.DailyRefreshTime,
  89. HourRefreshTime: account.HourRefreshTime,
  90. WeeklyRefreshTime: account.WeeklyRefreshTime,
  91. LoginFailure: account.LoginFailure,
  92. IsCashOut: account.IsCashOut,
  93. })
  94. }
  95. return result
  96. }