user.go 4.8 KB

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