synthesis.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. package service
  2. import (
  3. "context"
  4. "errors"
  5. "log"
  6. "math"
  7. "sort"
  8. "strconv"
  9. "time"
  10. mhayaTime "github.com/mhaya/extend/time"
  11. "github.com/mhaya/game/game_cluster/internal/constant"
  12. "github.com/mhaya/game/game_cluster/internal/mdb"
  13. "github.com/mhaya/game/game_cluster/internal/mdb/models"
  14. "github.com/mhaya/game/game_cluster/nodes/webadmin/entity"
  15. "github.com/spf13/cast"
  16. "go.mongodb.org/mongo-driver/bson"
  17. "go.mongodb.org/mongo-driver/bson/primitive"
  18. "go.mongodb.org/mongo-driver/mongo"
  19. "go.mongodb.org/mongo-driver/mongo/options"
  20. )
  21. type Synthesis struct {
  22. db *mongo.Database
  23. }
  24. func NewSynthesis() *Synthesis {
  25. return &Synthesis{
  26. db: mdb.MDB,
  27. }
  28. }
  29. func (s *Synthesis) FindMDBUserLogDaily(req *entity.UserLogDailyReq) ([]entity.UserLogDailyResp, error) {
  30. // 定义上下文
  31. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  32. defer cancel()
  33. // 指定集合
  34. collection := mdb.MDB.Collection("playerDailyRecord")
  35. // 构建查询条件 - 如果查询值为空那就不添加查询条件
  36. filter := bson.M{}
  37. if req.StartTime != 0 {
  38. filter["daily"] = bson.M{
  39. "$gte": req.StartTime,
  40. "$lte": req.EndTime,
  41. }
  42. }
  43. if req.Platform != "" && req.Platform != "all" {
  44. filter["platform"] = req.Platform
  45. }
  46. if req.Channel != "" {
  47. filter["channel"] = req.Channel
  48. }
  49. // 分页参数
  50. skip := (req.Page - 1) * req.Size
  51. req.Total = 0
  52. // 计算总数
  53. count, err := collection.CountDocuments(ctx, filter)
  54. if err != nil {
  55. return nil, err
  56. }
  57. req.Total = count
  58. // 执行查询
  59. opts := options.Find()
  60. opts.SetSkip(int64(skip))
  61. opts.SetLimit(int64(req.Size))
  62. opts.SetSort(bson.D{{"daily", -1}})
  63. cursor, err := collection.Find(ctx, filter, opts)
  64. if err != nil {
  65. return nil, err
  66. }
  67. defer cursor.Close(ctx)
  68. // 解析查询结果
  69. var results []entity.UserLogDailyResp
  70. for cursor.Next(ctx) {
  71. var result entity.UserLogDailyResp
  72. err := cursor.Decode(&result)
  73. if err != nil {
  74. return nil, err
  75. }
  76. results = append(results, result)
  77. }
  78. // 同一天的数据全部放到platform= ALl 且数据累加
  79. // 如果没有platform=all 的那就新增一个 同一个时间段只能有一个 platform=all
  80. // 将同一天的数据累加到platform=all的记录中
  81. allPlatformRecordMap := make(map[int64]*entity.UserLogDailyResp)
  82. for _, result := range results {
  83. allPlatformRecordMap[result.Timestamp] = &entity.UserLogDailyResp{
  84. Timestamp: result.Timestamp,
  85. Platform: "all",
  86. }
  87. }
  88. for _, v := range results {
  89. if v.Timestamp == allPlatformRecordMap[v.Timestamp].Timestamp {
  90. allPlatformRecordMap[v.Timestamp].Registered += v.Registered
  91. allPlatformRecordMap[v.Timestamp].LoggedIn += v.LoggedIn
  92. allPlatformRecordMap[v.Timestamp].NewActive += v.NewActive
  93. allPlatformRecordMap[v.Timestamp].OldActive += v.OldActive
  94. allPlatformRecordMap[v.Timestamp].TotalPoints += v.TotalPoints
  95. allPlatformRecordMap[v.Timestamp].UProduced += v.UProduced
  96. allPlatformRecordMap[v.Timestamp].UCashout += v.UCashout
  97. allPlatformRecordMap[v.Timestamp].NewLogin += v.NewLogin
  98. allPlatformRecordMap[v.Timestamp].OldLogin += v.OldLogin
  99. }
  100. }
  101. // 替换原有结果
  102. for _, record := range allPlatformRecordMap {
  103. results = append(results, *record)
  104. }
  105. return results, nil
  106. }
  107. // FindUserLogTotal 查询总提现数量,总注册人数,
  108. func (s *Synthesis) FindMDBUserLogTotal() (*entity.UserLogTotalResp, error) {
  109. collection := mdb.MDB.Collection("playerDailyRecord")
  110. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  111. defer cancel()
  112. filter := bson.M{}
  113. filter["daily"] = bson.M{
  114. "$gte": 0,
  115. }
  116. cursor, err := collection.Find(ctx, filter, nil)
  117. if err != nil {
  118. return nil, err
  119. }
  120. defer cursor.Close(ctx)
  121. // 解析查询结果
  122. var results []entity.UserLogDailyResp
  123. for cursor.Next(ctx) {
  124. var result entity.UserLogDailyResp
  125. err := cursor.Decode(&result)
  126. if err != nil {
  127. return nil, err
  128. }
  129. results = append(results, result)
  130. }
  131. registerCount := 0
  132. var uCashoutCount float64
  133. for _, v := range results {
  134. registerCount += v.Registered
  135. uCashoutCount += v.UCashout
  136. }
  137. return &entity.UserLogTotalResp{
  138. Registered: registerCount,
  139. UCashout: uCashoutCount,
  140. }, nil
  141. }
  142. // FindUserLogHistory 近30天每天的U提现 新注册用户数
  143. func (s *Synthesis) FindUserLogHistory() ([]*entity.UserLogHistoryResp, error) {
  144. collection := mdb.MDB.Collection("playerDailyRecord")
  145. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  146. defer cancel()
  147. filter := bson.M{}
  148. filter["daily"] = bson.M{
  149. "$gte": mhayaTime.Now().StartOfDay().Add(-30 * 24 * time.Hour).Unix(),
  150. "$lte": mhayaTime.Now().Unix(),
  151. }
  152. cursor, err := collection.Find(ctx, filter, nil)
  153. if err != nil {
  154. return nil, err
  155. }
  156. defer cursor.Close(ctx)
  157. // 解析查询结果
  158. var results []*entity.UserLogHistoryResp
  159. for cursor.Next(ctx) {
  160. var result *entity.UserLogHistoryResp
  161. err := cursor.Decode(&result)
  162. if err != nil {
  163. return nil, err
  164. }
  165. results = append(results, result)
  166. }
  167. // 合并每日的数据
  168. allPlatformRecordMap := make(map[int64]*entity.UserLogHistoryResp)
  169. for _, result := range results {
  170. item, exist := allPlatformRecordMap[result.Timestamp]
  171. if !exist {
  172. allPlatformRecordMap[result.Timestamp] = &entity.UserLogHistoryResp{
  173. Timestamp: result.Timestamp,
  174. Registered: result.Registered,
  175. UCashout: result.UCashout,
  176. }
  177. continue
  178. }
  179. item.Registered += result.Registered
  180. item.UCashout += result.UCashout
  181. allPlatformRecordMap[result.Timestamp] = item
  182. }
  183. results = make([]*entity.UserLogHistoryResp, 0, len(allPlatformRecordMap))
  184. for _, v := range allPlatformRecordMap {
  185. results = append(results, v)
  186. }
  187. return results, nil
  188. }
  189. // FindWithdrawal 根据请求查询提现记录
  190. func (s *Synthesis) FindWithdrawal(req *entity.UserWithdrawalReq) ([]*entity.UserWithdrawalResp, int64, error) {
  191. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  192. defer cancel()
  193. collection := mdb.MDB.Collection(constant.CNameCashOutRecord)
  194. // 构建过滤器
  195. filter := bson.M{}
  196. if req.UserName != "" {
  197. filter["userName"] = req.UserName
  198. }
  199. if req.NickName != "" {
  200. filter["nickName"] = bson.M{"$regex": escapeRegex(req.NickName), "$options": "i"}
  201. }
  202. if req.ID != "" {
  203. filter["_id"], _ = primitive.ObjectIDFromHex(req.ID)
  204. }
  205. if req.StartTime != 0 {
  206. filter["createAt"] = bson.M{
  207. "$gte": req.StartTime,
  208. "$lte": req.EndTime,
  209. }
  210. }
  211. if req.Withdrawal != "" {
  212. withdrawal, err := strconv.Atoi(req.Withdrawal)
  213. if err != nil {
  214. return nil, 0, err
  215. }
  216. filter["withdrawal"] = withdrawal
  217. }
  218. if req.Address != "" {
  219. filter["address"] = req.Address
  220. }
  221. if req.Status != "" {
  222. status, err := strconv.Atoi(req.Status)
  223. if err != nil {
  224. return nil, 0, err
  225. }
  226. filter["status"] = status
  227. }
  228. // 设置分页选项
  229. findOptions := options.Find()
  230. findOptions.SetSkip(int64((req.Page - 1) * req.Size))
  231. findOptions.SetLimit(int64(req.Size))
  232. findOptions.SetSort(bson.D{{"createAt", -1}})
  233. // 获取总数total
  234. count, err := collection.CountDocuments(ctx, filter)
  235. if err != nil {
  236. return nil, 0, err
  237. }
  238. // 查询数据
  239. var results []*entity.UserWithdrawalResp
  240. cursor, err := collection.Find(ctx, filter, findOptions)
  241. if err != nil {
  242. return nil, 0, err
  243. }
  244. defer cursor.Close(ctx)
  245. // 解析结果
  246. for cursor.Next(ctx) {
  247. var result entity.UserWithdrawalResp
  248. if err := cursor.Decode(&result); err != nil {
  249. return nil, 0, err
  250. }
  251. results = append(results, &result)
  252. }
  253. if err := cursor.Err(); err != nil {
  254. return nil, 0, err
  255. }
  256. return results, count, nil
  257. }
  258. // WithdrawalStatus 更新提现状态
  259. func (s *Synthesis) WithdrawalStatus(req *entity.UserWithdrawalStatus) error {
  260. // ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  261. // defer cancel()
  262. collection := mdb.MDB.Collection(constant.CNameCashOutRecord)
  263. // 更新条件
  264. updateCondition := bson.M{"userName": req.UserName}
  265. // 更新内容
  266. updateContent := bson.M{"$set": bson.M{"status": req.Status}}
  267. // 设置更新选项
  268. updateOptions := options.Update() // 设置 upsert 选项
  269. // 执行更新操作
  270. _, err := collection.UpdateOne(context.TODO(), updateCondition, updateContent, updateOptions)
  271. if err != nil {
  272. return err
  273. }
  274. return nil
  275. }
  276. // WithdrawalStatusBatch 更新提现状态
  277. func (s *Synthesis) WithdrawalStatusBatch(req *entity.UserWithdrawalStatusBatch) error {
  278. collection := mdb.MDB.Collection(constant.CNameCashOutRecord)
  279. if len(req.ID) == 0 {
  280. return errors.New("id 不能为空")
  281. }
  282. for _, id := range req.ID {
  283. objID := primitive.ObjectID{}
  284. objID, _ = primitive.ObjectIDFromHex(id)
  285. updateCondition := bson.M{"_id": objID}
  286. updateContent := bson.M{}
  287. withdrawal := models.CashOutRecord{}
  288. err := collection.FindOne(context.TODO(), updateCondition).Decode(&withdrawal)
  289. if err != nil {
  290. log.Printf("查询提现记录失败: %v", err)
  291. continue
  292. }
  293. if req.Withdrawal != 0 {
  294. if withdrawal.Status == 1 {
  295. updateContent = bson.M{"$set": bson.M{"withdrawal": req.Withdrawal}}
  296. } else {
  297. continue
  298. }
  299. }
  300. if req.Status > 0 {
  301. if withdrawal.Status != 0 {
  302. continue
  303. }
  304. updateContent = bson.M{"$set": bson.M{"status": req.Status}}
  305. }
  306. updateOptions := options.Update().SetUpsert(true)
  307. _, err = collection.UpdateOne(context.TODO(), updateCondition, updateContent, updateOptions)
  308. if err != nil {
  309. continue
  310. }
  311. }
  312. return nil
  313. }
  314. // FindUserCountryCount 查询用户国家分布
  315. //
  316. // 返回值为 UserCountryResp 的切片和错误。
  317. func (s *Synthesis) FindUserCountryCount() ([]*entity.UserCountryResp, error) {
  318. // 选择数据库和集合
  319. collection := mdb.MDB.Collection("playerCountryStat")
  320. // 定义聚合管道
  321. // 定义聚合管道
  322. pipeline := []bson.D{
  323. {
  324. {Key: "$project", Value: bson.D{
  325. {Key: "playerRegisterCountry", Value: bson.D{{Key: "$objectToArray", Value: "$playerRegisterCountry"}}},
  326. }},
  327. },
  328. {
  329. {Key: "$unwind", Value: "$playerRegisterCountry"},
  330. },
  331. {
  332. {Key: "$group", Value: bson.D{
  333. {Key: "_id", Value: "$playerRegisterCountry.k"},
  334. {Key: "totalValue", Value: bson.D{{Key: "$sum", Value: "$playerRegisterCountry.v"}}},
  335. }},
  336. },
  337. {
  338. {Key: "$project", Value: bson.D{
  339. {Key: "_id", Value: 0},
  340. {Key: "countryKey", Value: "$_id"},
  341. {Key: "totalValue", Value: 1},
  342. }},
  343. },
  344. }
  345. // 执行聚合查询
  346. cursor, err := collection.Aggregate(context.TODO(), pipeline)
  347. if err != nil {
  348. return nil, err
  349. }
  350. defer cursor.Close(context.TODO())
  351. // 遍历查询结果
  352. var results []bson.M
  353. if err := cursor.All(context.TODO(), &results); err != nil {
  354. return nil, err
  355. }
  356. var totalIPCount int64
  357. var data []*entity.UserCountryResp
  358. // 将结果转换为 UserCountryResp
  359. for _, r := range results {
  360. var resp entity.UserCountryResp
  361. resp.Country = r["countryKey"].(string)
  362. resp.IPCount = int(r["totalValue"].(int32))
  363. totalIPCount += int64(resp.IPCount)
  364. data = append(data, &resp)
  365. }
  366. for _, v := range data {
  367. // 保留小数点后两位
  368. v.Percentage = math.Round(float64(v.IPCount)/float64(totalIPCount)*10000) / 100
  369. }
  370. // 根据阈值过滤结果
  371. otherCount := 0
  372. otherPercentage := 0.00
  373. filteredResults := make([]*entity.UserCountryResp, 0)
  374. threshold := 1.00
  375. for _, r := range data {
  376. if r.Percentage >= threshold {
  377. filteredResults = append(filteredResults, r)
  378. // 保留小数点后两位
  379. r.Percentage = math.Round(r.Percentage*100) / 100
  380. otherPercentage += r.Percentage
  381. } else {
  382. otherCount += r.IPCount
  383. }
  384. }
  385. // 将其他国家添加到过滤后的结果中
  386. if otherCount > 0 {
  387. p := 100.00 - math.Round(otherPercentage*100)/100
  388. filteredResults = append(filteredResults, &entity.UserCountryResp{
  389. Country: "other",
  390. IPCount: otherCount,
  391. Percentage: math.Round(p*100) / 100,
  392. })
  393. }
  394. return filteredResults, nil
  395. }
  396. // FindUserRetention UserRetentionResp 用户留存率
  397. // 1. 获取指定日期范围内的注册用户数量
  398. // 2. 获取指定日期范围内的活跃用户数量
  399. // 3. 计算留存率
  400. // 4. 返回结果
  401. func (s *Synthesis) FindUserRetention(req *entity.UserRetentionReq) ([]*entity.UserRetentionResp, error) {
  402. playerPreserve := models.GetPlayerPreserve(req.StartTime, req.EndTime)
  403. // 查询数据
  404. var results []*entity.UserRetentionResp
  405. for key, v := range playerPreserve {
  406. var resp entity.UserRetentionResp
  407. var retention entity.Retention
  408. for _, vv := range v {
  409. if vv.ID == 1 {
  410. retention.Day1 = entity.DayRetention{
  411. LoggedIn: vv.Ratio,
  412. LoginDate: key,
  413. }
  414. }
  415. if vv.ID == 3 {
  416. retention.Day3 = entity.DayRetention{
  417. LoggedIn: vv.Ratio,
  418. LoginDate: key,
  419. }
  420. }
  421. if vv.ID == 7 {
  422. retention.Day7 = entity.DayRetention{
  423. LoggedIn: vv.Ratio,
  424. LoginDate: key,
  425. }
  426. }
  427. if vv.ID == 14 {
  428. retention.Day14 = entity.DayRetention{
  429. LoggedIn: vv.Ratio,
  430. LoginDate: key,
  431. }
  432. }
  433. if vv.ID == 30 {
  434. retention.Day30 = entity.DayRetention{
  435. LoggedIn: vv.Ratio,
  436. LoginDate: key,
  437. }
  438. }
  439. }
  440. resp.RegistrationDate = key
  441. resp.RetentionData = retention
  442. results = append(results, &resp)
  443. }
  444. return results, nil
  445. }
  446. // FindUserLevel 用户等级统计
  447. func (s *Synthesis) FindUserLevel() ([]*entity.UserLevelCountResp, error) {
  448. ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
  449. defer cancel()
  450. collection := mdb.MDB.Collection("playerLevelStat")
  451. // 查询所有文档
  452. cursor, err := collection.Find(ctx, bson.M{})
  453. if err != nil {
  454. return nil, err
  455. }
  456. defer cursor.Close(ctx)
  457. var results []*entity.UserLevelCountResp
  458. var reData []*models.PlayerLevelStat
  459. for cursor.Next(ctx) {
  460. var result models.PlayerLevelStat
  461. if err := cursor.Decode(&result); err != nil {
  462. return nil, err
  463. }
  464. reData = append(reData, &result)
  465. }
  466. if err := cursor.Err(); err != nil {
  467. return nil, err
  468. }
  469. dataMap := make(map[string]int)
  470. for _, v := range reData {
  471. for key, v1 := range v.ServerLevel {
  472. dataMap[key] += v1
  473. }
  474. }
  475. keys := make([]string, 0, len(dataMap))
  476. for k := range dataMap {
  477. keys = append(keys, k)
  478. }
  479. sort.Strings(keys)
  480. for _, v := range keys {
  481. results = append(results, &entity.UserLevelCountResp{
  482. Level: cast.ToInt(v),
  483. UserCount: dataMap[v],
  484. })
  485. }
  486. return results, nil
  487. }