synthesis.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. "github.com/mhaya/game/game_cluster/internal/mdb"
  6. "github.com/mhaya/game/game_cluster/internal/mdb/models"
  7. "github.com/mhaya/game/game_cluster/nodes/webadmin/entity"
  8. "github.com/mhaya/game/game_cluster/nodes/webadmin/model"
  9. "github.com/spf13/cast"
  10. "go.mongodb.org/mongo-driver/bson"
  11. "go.mongodb.org/mongo-driver/mongo"
  12. "go.mongodb.org/mongo-driver/mongo/options"
  13. )
  14. type Synthesis struct {
  15. db *mongo.Database
  16. }
  17. func NewSynthesis() *Synthesis {
  18. return &Synthesis{
  19. db: mdb.MDB,
  20. }
  21. }
  22. func (s *Synthesis) FindMDBUserLogDaily(req *entity.UserLogDailyReq) ([]entity.UserLogDailyResp, error) {
  23. // 定义上下文
  24. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  25. defer cancel()
  26. // 指定集合
  27. collection := mdb.MDB.Collection("playerDailyRecord")
  28. // 构建查询条件 - 如果查询值为空那就不添加查询条件
  29. filter := bson.M{}
  30. if req.StartTime != 0 {
  31. filter["timestamp"] = bson.M{
  32. "$gte": req.StartTime,
  33. "$lte": req.EndTime,
  34. }
  35. }
  36. if req.Platform != "" && req.Platform != "all" {
  37. filter["platform"] = req.Platform
  38. }
  39. if req.Channel != "" {
  40. filter["channel"] = req.Channel
  41. }
  42. // 分页参数
  43. skip := (req.Page - 1) * req.Size
  44. // 执行查询
  45. opts := options.Find()
  46. opts.SetSkip(int64(skip))
  47. opts.SetLimit(int64(req.Size))
  48. cursor, err := collection.Find(ctx, filter, opts)
  49. if err != nil {
  50. return nil, err
  51. }
  52. defer cursor.Close(ctx)
  53. // 解析查询结果
  54. var results []entity.UserLogDailyResp
  55. for cursor.Next(ctx) {
  56. var result entity.UserLogDailyResp
  57. err := cursor.Decode(&result)
  58. if err != nil {
  59. return nil, err
  60. }
  61. results = append(results, result)
  62. }
  63. // 同一天的数据全部放到platform= ALl 且数据累加
  64. // 如果没有platform=all 的那就新增一个 同一个时间段只能有一个 platform=all
  65. // 将同一天的数据累加到platform=all的记录中
  66. allPlatformRecordMap := make(map[int64]*entity.UserLogDailyResp)
  67. for _, result := range results {
  68. allPlatformRecordMap[result.Timestamp] = &entity.UserLogDailyResp{
  69. Timestamp: result.Timestamp,
  70. Platform: "all",
  71. }
  72. }
  73. for _, v := range results {
  74. if v.Timestamp == allPlatformRecordMap[v.Timestamp].Timestamp {
  75. allPlatformRecordMap[v.Timestamp].Registered += v.Registered
  76. allPlatformRecordMap[v.Timestamp].LoggedIn += v.LoggedIn
  77. allPlatformRecordMap[v.Timestamp].NewActive += v.NewActive
  78. allPlatformRecordMap[v.Timestamp].OldActive += v.OldActive
  79. allPlatformRecordMap[v.Timestamp].TotalPoints += v.TotalPoints
  80. allPlatformRecordMap[v.Timestamp].UProduced += v.UProduced
  81. allPlatformRecordMap[v.Timestamp].UCashout += v.UCashout
  82. allPlatformRecordMap[v.Timestamp].NewLogin += v.NewLogin
  83. allPlatformRecordMap[v.Timestamp].OldLogin += v.OldLogin
  84. }
  85. }
  86. // 替换原有结果
  87. for _, record := range allPlatformRecordMap {
  88. results = append(results, *record)
  89. }
  90. return results, nil
  91. }
  92. // FindWithdrawal 根据请求查询提现记录
  93. func (s *Synthesis) FindWithdrawal(req *entity.UserWithdrawalReq) ([]*entity.UserWithdrawalResp, error) {
  94. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  95. defer cancel()
  96. collection := mdb.MDB.Collection("CashOutRecord")
  97. // 构建过滤器
  98. filter := bson.M{}
  99. if req.UserName != "" {
  100. filter["userName"] = req.UserName
  101. }
  102. if req.NickName != "" {
  103. filter["nickName"] = req.NickName
  104. }
  105. if req.StartTime > 0 && req.EndTime > 0 {
  106. filter["createdAt"] = bson.M{"$gte": time.Unix(req.StartTime, 0), "$lte": time.Unix(req.EndTime, 0)}
  107. } else if req.StartTime > 0 {
  108. filter["createdAt"] = bson.M{"$gte": time.Unix(req.StartTime, 0)}
  109. } else if req.EndTime > 0 {
  110. filter["createdAt"] = bson.M{"$lte": time.Unix(req.EndTime, 0)}
  111. }
  112. // 设置分页选项
  113. findOptions := options.Find()
  114. findOptions.SetSkip(int64((req.Page - 1) * req.Size))
  115. findOptions.SetLimit(int64(req.Size))
  116. // 查询数据
  117. var results []*entity.UserWithdrawalResp
  118. cursor, err := collection.Find(ctx, filter, findOptions)
  119. if err != nil {
  120. return nil, err
  121. }
  122. defer cursor.Close(ctx)
  123. // 解析结果
  124. for cursor.Next(ctx) {
  125. var result entity.UserWithdrawalResp
  126. if err := cursor.Decode(&result); err != nil {
  127. return nil, err
  128. }
  129. results = append(results, &result)
  130. }
  131. if err := cursor.Err(); err != nil {
  132. return nil, err
  133. }
  134. return results, nil
  135. }
  136. // WithdrawalStatus 更新提现状态
  137. func (s *Synthesis) WithdrawalStatus(req *entity.UserWithdrawalStatus) error {
  138. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  139. defer cancel()
  140. collection := mdb.MDB.Collection("CashOutRecord")
  141. _, err := collection.UpdateOne(ctx, bson.M{"userName": req.UserName}, bson.M{"$set": bson.M{"status": req.Status}})
  142. if err != nil {
  143. return err
  144. }
  145. return nil
  146. }
  147. // FindUserCountryCount 查询用户国家分布
  148. //
  149. // 返回值为 UserCountryResp 的切片和错误。
  150. func (s *Synthesis) FindUserCountryCount() ([]*entity.UserCountryResp, error) {
  151. // 设置超时的上下文
  152. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  153. defer cancel()
  154. // 获取数据库的集合
  155. collection := s.db.Collection("user_country_count")
  156. // 设置百分比阈值
  157. threshold := 1
  158. // 执行聚合查询
  159. var results []*entity.UserCountryResp
  160. cursor, err := collection.Find(ctx, bson.M{}, options.Find())
  161. if err != nil {
  162. return nil, err
  163. }
  164. defer cursor.Close(ctx)
  165. // 解析结果
  166. for cursor.Next(ctx) {
  167. var result model.UserCountryCount
  168. if err := cursor.Decode(&result); err != nil {
  169. return nil, err
  170. }
  171. results = append(results, &entity.UserCountryResp{
  172. Country: result.Country,
  173. IPCount: result.IPCount,
  174. Percentage: result.Percentage,
  175. })
  176. }
  177. if err := cursor.Err(); err != nil {
  178. return nil, err
  179. }
  180. // 根据阈值过滤结果
  181. otherCount := 0
  182. otherPercentage := 0
  183. filteredResults := make([]*entity.UserCountryResp, 0)
  184. for _, r := range results {
  185. if r.Percentage >= threshold {
  186. filteredResults = append(filteredResults, r)
  187. } else {
  188. otherCount += r.IPCount
  189. otherPercentage += r.Percentage
  190. }
  191. }
  192. // 将其他国家添加到过滤后的结果中
  193. if otherCount > 0 {
  194. filteredResults = append(filteredResults, &entity.UserCountryResp{
  195. Country: "other",
  196. IPCount: otherCount,
  197. })
  198. }
  199. return filteredResults, nil
  200. }
  201. // FindUserRetention UserRetentionResp 用户留存率
  202. // 1. 获取指定日期范围内的注册用户数量
  203. // 2. 获取指定日期范围内的活跃用户数量
  204. // 3. 计算留存率
  205. // 4. 返回结果
  206. func (s *Synthesis) FindUserRetention(req *entity.UserRetentionReq) ([]*entity.UserRetentionResp, error) {
  207. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  208. defer cancel()
  209. collection := s.db.Collection("user_reg_count")
  210. // 构建过滤器
  211. filter := bson.M{}
  212. if req.StartTime != 0 {
  213. filter["registration_date"] = bson.M{
  214. "$gte": req.StartTime,
  215. "$lte": req.EndTime,
  216. } // 日期范围
  217. }
  218. // 设置分页选项
  219. findOptions := options.Find()
  220. findOptions.SetSkip(int64((req.Page - 1) * req.Size))
  221. findOptions.SetLimit(int64(req.Size))
  222. // 查询数据
  223. var results []*entity.UserRetentionResp
  224. cursor, err := collection.Find(ctx, filter, findOptions)
  225. if err != nil {
  226. return nil, err
  227. }
  228. defer cursor.Close(ctx)
  229. // 解析结果
  230. for cursor.Next(ctx) {
  231. var result model.UserRetentionData
  232. if err := cursor.Decode(&result); err != nil {
  233. return nil, err
  234. }
  235. results = append(results, &entity.UserRetentionResp{
  236. UserRetentionData: &result,
  237. })
  238. }
  239. if err := cursor.Err(); err != nil {
  240. return nil, err
  241. }
  242. return results, nil
  243. }
  244. // FindUserLevel 用户等级统计
  245. func (s *Synthesis) FindUserLevel() ([]*entity.UserLevelCountResp, error) {
  246. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  247. defer cancel()
  248. collection := mdb.MDB.Collection("playerLevelStat")
  249. // 查询所有文档
  250. cursor, err := collection.Find(ctx, bson.M{})
  251. if err != nil {
  252. return nil, err
  253. }
  254. defer cursor.Close(ctx)
  255. var results []*entity.UserLevelCountResp
  256. var reData []*models.PlayerLevelStat
  257. for cursor.Next(ctx) {
  258. var result models.PlayerLevelStat
  259. if err := cursor.Decode(&result); err != nil {
  260. return nil, err
  261. }
  262. reData = append(reData, &result)
  263. }
  264. if err := cursor.Err(); err != nil {
  265. return nil, err
  266. }
  267. dataMap := make(map[string]int)
  268. for _, v := range reData {
  269. for key, v1 := range v.ServerLevel {
  270. dataMap[key] += v1
  271. }
  272. }
  273. for k, v := range dataMap {
  274. results = append(results, &entity.UserLevelCountResp{
  275. Level: cast.ToInt(k),
  276. UserCount: v,
  277. })
  278. }
  279. return results, nil
  280. }