test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package v1
  2. import (
  3. "context"
  4. "designs/config"
  5. "designs/global"
  6. "designs/model"
  7. "designs/response"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "go.mongodb.org/mongo-driver/v2/bson"
  11. "strconv"
  12. "time"
  13. )
  14. type logData struct {
  15. LogTime time.Time `json:"logTime" gorm:"column:logTime;"`
  16. Type int `json:"Type"`
  17. }
  18. func calculateUserOnlineTime(logData []logData) int64 {
  19. var lastLog int64
  20. var isStart bool
  21. var onlineTimeTotal int64
  22. for k, v := range logData {
  23. logTime := v.LogTime.Unix()
  24. //如果跟上一次的记录隔的时间超过6分钟,就认为是之前断线了,属于无效数据
  25. if k > 0 && logData[k-1].LogTime.Unix()+60*6 < logTime {
  26. isStart = false
  27. continue
  28. }
  29. if v.Type == 1 && isStart == false {
  30. isStart = true
  31. lastLog = v.LogTime.Unix()
  32. continue
  33. }
  34. if v.Type == 1 && isStart == true {
  35. //logTime := v.LogTime.Unix()
  36. onlineTimeTotal = onlineTimeTotal + (logTime - lastLog)
  37. lastLog = logTime
  38. //如果下一次的心跳,间隔时间大于6分钟了,就可以认为,这次就算是结束了
  39. if k+1 == len(logData) || logData[k+1].LogTime.Unix() > logTime+60*6 {
  40. isStart = false
  41. }
  42. continue
  43. }
  44. if v.Type == 2 && isStart == true {
  45. //logTime := v.LogTime.Unix()
  46. onlineTimeTotal = onlineTimeTotal + (logTime - lastLog)
  47. isStart = false
  48. continue
  49. }
  50. }
  51. return onlineTimeTotal
  52. }
  53. func SetUserBehaviorBak(c *gin.Context) {
  54. //读取出来user数据
  55. var users []model.User
  56. global.App.DB.Table("user").
  57. Where("pf", "=", "tt").
  58. Where("gid", "=", "linkup").
  59. Where("createdAt", ">=", "2024-11-12").
  60. Where("createdAt", "<=", "2024-11-13").Scan(&users)
  61. collection := global.App.MongoDB.Database("chunhao").Collection("adRelated")
  62. for _, user := range users {
  63. //查询出reids中的openId
  64. userIdKeys := fmt.Sprintf("%s:%s:%s:%v", user.Gid, user.Pf, config.Get("app.user_table_id"), strconv.Itoa(user.UserId))
  65. userIdData, _ := global.App.Redis.HGetAll(context.Background(), userIdKeys).Result()
  66. openId := userIdData["openId"]
  67. //openId = strconv.Itoa(user.UserId)
  68. //查询出在线时长数据
  69. var LogData []logData
  70. global.App.DB.Table("user_online").
  71. Where("pf", user.Pf).
  72. Where("gid", user.Gid).
  73. Where("userId", user.UserId).Scan(&LogData)
  74. duration := calculateUserOnlineTime(LogData)
  75. //查询出看广告数据
  76. var req_count int64
  77. var exp_count int64
  78. global.App.DB.Table("user_see_ads").
  79. Where("pf", user.Pf).
  80. Where("gid", user.Gid).
  81. Where("userId", user.UserId).Count(&req_count)
  82. global.App.DB.Table("user_see_ads").
  83. Where("pf", user.Pf).
  84. Where("gid", user.Gid).
  85. Where("adState", 2).
  86. Where("userId", user.UserId).Count(&exp_count)
  87. //查询出启动次数
  88. var start_num int64
  89. global.App.DB.Table("user_login").
  90. Where("pf", user.Pf).
  91. Where("gid", user.Gid).
  92. Where("userId", user.UserId).Count(&start_num)
  93. //存入mongo
  94. adData := struct {
  95. Id string `bson:"_id" json:"_id"`
  96. UserId string `bson:"userId" json:"userId"`
  97. Aid int64 `json:"aid" bson:"aid"` //广告的推广计划id,即广告ID,广告平台配置落地页参数
  98. Cid string `json:"cid" bson:"cid"` //openid的用户点击aid广告进入游戏时的唯一标识,广告平台提供
  99. Pid int64 `json:"pid" bson:"pid"` //广告的项目id(仅巨量引擎存在,腾讯广告时不存在该值),广告平台配置落地页参数
  100. CreateTime int64 `bson:"create_time"` //当前计划的创建时间
  101. StartNum int `bson:"startNum" json:"startNum"` //启动次数
  102. Revenue float32 `bson:"revenue" json:"revenue"` //当日预估收益
  103. Duration int64 `bson:"duration" json:"duration"` //当日在线时长
  104. ReqCount int `bson:"req_count" json:"req_count"` //当日的激励视频广告请求次数
  105. ExpCount int `bson:"exp_count" json:"exp_count"` //当日的激励视频广告曝光次数
  106. }{
  107. UserId: user.Gid + "|" + user.Pf + "|" + openId,
  108. Id: user.Gid + "|" + user.Pf + "|" + openId + "|" + "7436301890621997000",
  109. Aid: 7436301890621997000,
  110. Pid: 0,
  111. Cid: "",
  112. Duration: duration,
  113. StartNum: int(start_num),
  114. Revenue: 0,
  115. ReqCount: int(req_count),
  116. ExpCount: int(exp_count),
  117. CreateTime: user.CreatedAt.Unix(),
  118. }
  119. collection.InsertOne(context.Background(), adData)
  120. fmt.Println(user.Gid+"|"+user.Pf+"|"+openId, "数据写入完成\n")
  121. }
  122. }
  123. func DeleteUserBehaviorBak(c *gin.Context) {
  124. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  125. filter := bson.M{"create_time": bson.M{"$gt": 1}}
  126. collection.DeleteMany(context.Background(), filter)
  127. response.Success(c, gin.H{})
  128. }