user.go 3.9 KB

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