test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. "os"
  12. "strconv"
  13. "time"
  14. )
  15. type logData struct {
  16. LogTime time.Time `json:"logTime" gorm:"column:logTime;"`
  17. Type int `json:"Type"`
  18. }
  19. func calculateUserOnlineTime(logData []logData) int64 {
  20. var lastLog int64
  21. var isStart bool
  22. var onlineTimeTotal int64
  23. for k, v := range logData {
  24. logTime := v.LogTime.Unix()
  25. //如果跟上一次的记录隔的时间超过6分钟,就认为是之前断线了,属于无效数据
  26. if k > 0 && logData[k-1].LogTime.Unix()+60*6 < logTime {
  27. isStart = false
  28. continue
  29. }
  30. if v.Type == 1 && isStart == false {
  31. isStart = true
  32. lastLog = v.LogTime.Unix()
  33. continue
  34. }
  35. if v.Type == 1 && isStart == true {
  36. //logTime := v.LogTime.Unix()
  37. onlineTimeTotal = onlineTimeTotal + (logTime - lastLog)
  38. lastLog = logTime
  39. //如果下一次的心跳,间隔时间大于6分钟了,就可以认为,这次就算是结束了
  40. if k+1 == len(logData) || logData[k+1].LogTime.Unix() > logTime+60*6 {
  41. isStart = false
  42. }
  43. continue
  44. }
  45. if v.Type == 2 && isStart == true {
  46. //logTime := v.LogTime.Unix()
  47. onlineTimeTotal = onlineTimeTotal + (logTime - lastLog)
  48. isStart = false
  49. continue
  50. }
  51. }
  52. return onlineTimeTotal
  53. }
  54. func SetUserBehaviorBak(c *gin.Context) {
  55. //读取出来user数据
  56. var users []model.User
  57. global.App.DB.Table("user").
  58. Where("pf", "=", "tt").
  59. Where("gid", "=", "linkup").
  60. Where("createdAt", ">=", "2024-11-12").
  61. Where("createdAt", "<=", "2024-11-13").Scan(&users)
  62. collection := global.App.MongoDB.Database("chunhao").Collection("adRelated")
  63. for _, user := range users {
  64. //查询出reids中的openId
  65. userIdKeys := fmt.Sprintf("%s:%s:%s:%v", user.Gid, user.Pf, config.Get("app.user_table_id"), strconv.Itoa(user.UserId))
  66. userIdData, _ := global.App.Redis.HGetAll(context.Background(), userIdKeys).Result()
  67. openId := userIdData["openId"]
  68. //openId = strconv.Itoa(user.UserId)
  69. //查询出在线时长数据
  70. var LogData []logData
  71. global.App.DB.Table("user_online").
  72. Where("pf", user.Pf).
  73. Where("gid", user.Gid).
  74. Where("userId", user.UserId).Scan(&LogData)
  75. duration := calculateUserOnlineTime(LogData)
  76. //查询出看广告数据
  77. var req_count int64
  78. var exp_count int64
  79. global.App.DB.Table("user_see_ads").
  80. Where("pf", user.Pf).
  81. Where("gid", user.Gid).
  82. Where("userId", user.UserId).Count(&req_count)
  83. global.App.DB.Table("user_see_ads").
  84. Where("pf", user.Pf).
  85. Where("gid", user.Gid).
  86. Where("adState", 2).
  87. Where("userId", user.UserId).Count(&exp_count)
  88. //查询出启动次数
  89. var start_num int64
  90. global.App.DB.Table("user_login").
  91. Where("pf", user.Pf).
  92. Where("gid", user.Gid).
  93. Where("userId", user.UserId).Count(&start_num)
  94. //存入mongo
  95. adData := struct {
  96. Id string `bson:"_id" json:"_id"`
  97. UserId string `bson:"userId" json:"userId"`
  98. Aid int64 `json:"aid" bson:"aid"` //广告的推广计划id,即广告ID,广告平台配置落地页参数
  99. Cid string `json:"cid" bson:"cid"` //openid的用户点击aid广告进入游戏时的唯一标识,广告平台提供
  100. Pid int64 `json:"pid" bson:"pid"` //广告的项目id(仅巨量引擎存在,腾讯广告时不存在该值),广告平台配置落地页参数
  101. CreateTime int64 `bson:"create_time"` //当前计划的创建时间
  102. StartNum int `bson:"startNum" json:"startNum"` //启动次数
  103. Revenue float32 `bson:"revenue" json:"revenue"` //当日预估收益
  104. Duration int64 `bson:"duration" json:"duration"` //当日在线时长
  105. ReqCount int `bson:"req_count" json:"req_count"` //当日的激励视频广告请求次数
  106. ExpCount int `bson:"exp_count" json:"exp_count"` //当日的激励视频广告曝光次数
  107. }{
  108. UserId: user.Gid + "|" + user.Pf + "|" + openId,
  109. Id: user.Gid + "|" + user.Pf + "|" + openId + "|" + "7436301890621997000",
  110. Aid: 7436301890621997000,
  111. Pid: 0,
  112. Cid: "",
  113. Duration: duration,
  114. StartNum: int(start_num),
  115. Revenue: 0,
  116. ReqCount: int(req_count),
  117. ExpCount: int(exp_count),
  118. CreateTime: user.CreatedAt.Unix(),
  119. }
  120. collection.InsertOne(context.Background(), adData)
  121. fmt.Println(user.Gid+"|"+user.Pf+"|"+openId, "数据写入完成\n")
  122. }
  123. }
  124. func DeleteUserBehaviorBak(c *gin.Context) {
  125. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  126. filter := bson.M{"create_time": bson.M{"$gt": 1}}
  127. collection.DeleteMany(context.Background(), filter)
  128. response.Success(c, gin.H{})
  129. }
  130. func GetUserNickName(c *gin.Context) {
  131. //读取user表
  132. var count int64
  133. err := global.App.DB.Table("user").Count(&count).Error
  134. if err != nil {
  135. response.Fail(c, 1001, err.Error())
  136. return
  137. }
  138. var i int64
  139. for i = 1; i <= count; i++ {
  140. var user model.User
  141. err = global.App.DB.Table("user").Where("id", i).Find(&user).Error
  142. if err != nil {
  143. response.Fail(c, 1001, err.Error())
  144. return
  145. }
  146. if user.Pf == "web" {
  147. //web跳过
  148. continue
  149. }
  150. userKey := user.Gid + ":" + user.Pf + ":" + config.Get("app.user_table_key") + user.OpenId
  151. userData, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
  152. if err != nil {
  153. global.App.Log.Error("GetUserData err")
  154. response.Fail(c, 1003, "GetUserData err"+err.Error())
  155. return
  156. }
  157. if userData["head"] != "" {
  158. //有意义的数据
  159. //data := struct {
  160. // ID int `json:"id" gorm:"not null;"`
  161. // Head string `json:"head" gorm:"not null;"`
  162. // NickName string `json:"nickName" gorm:"not null;column:nickName;"`
  163. //}{
  164. // Head: userData["head"],
  165. // NickName: userData["nickName"],
  166. //}
  167. //err = global.App.DB.Table("nickname").Create(&data).Error
  168. //if err != nil {
  169. // response.Fail(c, 1003, "nickname err"+err.Error())
  170. // return
  171. //}
  172. }
  173. }
  174. response.Success(c, gin.H{})
  175. }
  176. // 定义一个函数来执行任务
  177. func WriteDBDataToFile(c *gin.Context) {
  178. // 打开文件准备写入
  179. file, err := os.Create("./nickname.txt")
  180. if err != nil {
  181. response.Fail(c, 1001, fmt.Errorf("无法创建文件: %v", err))
  182. }
  183. defer file.Close()
  184. // 查询数据库
  185. var data []struct {
  186. ID int `json:"id" gorm:"not null;"`
  187. Head string `json:"head" gorm:"not null;"`
  188. NickName string `json:"nickName" gorm:"not null;column:nickName;"`
  189. }
  190. global.App.DB.Table("nickname").Scan(&data)
  191. // 写入列名到文件(可选)
  192. for _, v := range data {
  193. fmt.Println(v)
  194. fmt.Fprintf(file, "%s\t%s\n", v.NickName, v.Head)
  195. }
  196. response.Success(c, gin.H{})
  197. }