synthesis.go 16 KB

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