user.go 4.2 KB

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