behavior.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package v1
  2. import (
  3. "context"
  4. "designs/app/common/request"
  5. "designs/app/common/response"
  6. "designs/global"
  7. "github.com/gin-gonic/gin"
  8. "go.mongodb.org/mongo-driver/v2/bson"
  9. )
  10. type AdData struct {
  11. Pid string `json:"pid"`
  12. Aid string `json:"aid"`
  13. Cid string `json:"cid"`
  14. ReportUrl string `json:"reportUrl"`
  15. Reported bool `json:"reported"`
  16. Duration int64 `json:"duration"`
  17. AdReqCount uint8 `json:"adReqCount"`
  18. AdEposedcount uint8 `json:"adEposedcount"`
  19. CreateTime int `json:"createTime"`
  20. }
  21. type UserBehavior struct {
  22. Id string `bson:"_id,omitempty"`
  23. Gid string `bson:"gid"`
  24. Pf string `bson:"pf"`
  25. OpenId string `bson:"openId"`
  26. AdData interface{} `bson:"adData"`
  27. AdFromCount int `bson:"adFromCount"`
  28. TotalDuration int `bson:"totalDuration"`
  29. TotalAdReqCount int `bson:"totalAdReqCount"`
  30. TotalAdEposedCount int `bson:"totalAdEposedCount"`
  31. }
  32. func ReceiveUserBehavior(c *gin.Context) {
  33. form := request.Check(c, &struct {
  34. Gid string `form:"gid" json:"gid" binding:"required"`
  35. Pf string `form:"pf" json:"pf" binding:"required"`
  36. OpenId string `form:"openId" json:"openId" binding:"required"`
  37. AdData interface{} `form:"adData" json:"adData" binding:""`
  38. AdFromCount int `form:"adFromCount" json:"adFromCount" binding:""`
  39. TotalDuration int `form:"totalDuration" json:"totalDuration" binding:""`
  40. TotalAdReqCount int `form:"totalAdReqCount" json:"totalAdReqCount" binding:""`
  41. TotalAdEposedCount int `form:"totalAdEposedCount" json:"totalAdEposedCount" binding:""`
  42. }{})
  43. behavior := UserBehavior{
  44. Id: form.Gid + "|" + form.Pf + "|" + form.OpenId,
  45. Gid: form.Gid,
  46. Pf: form.Pf,
  47. OpenId: form.OpenId,
  48. AdData: form.AdData,
  49. AdFromCount: form.AdFromCount,
  50. TotalDuration: form.TotalDuration,
  51. TotalAdReqCount: form.TotalAdReqCount,
  52. TotalAdEposedCount: form.TotalAdEposedCount,
  53. }
  54. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  55. //
  56. filter := bson.M{"_id": behavior.Id}
  57. result := make(map[string]interface{})
  58. err := collection.FindOne(context.Background(), filter).Decode(&result)
  59. if len(result) == 0 {
  60. //新增
  61. _, err = collection.InsertOne(context.Background(), behavior)
  62. if err != nil {
  63. response.Fail(c, 1003, "写入数据失败"+err.Error())
  64. return
  65. }
  66. } else {
  67. //更新到MongoDB 中
  68. update := bson.M{
  69. "$set": struct {
  70. Gid string `bson:"gid"`
  71. Pf string `bson:"pf"`
  72. OpenId string `bson:"openId"`
  73. AdData interface{} `bson:"adData"`
  74. AdFromCount int `bson:"adFromCount"`
  75. TotalDuration int `bson:"totalDuration"`
  76. TotalAdReqCount int `bson:"totalAdReqCount"`
  77. TotalAdEposedCount int `bson:"totalAdEposedCount"`
  78. }{
  79. Gid: behavior.Gid,
  80. Pf: behavior.Pf,
  81. OpenId: behavior.OpenId,
  82. AdData: behavior.AdData,
  83. AdFromCount: behavior.AdFromCount,
  84. TotalDuration: behavior.TotalDuration,
  85. TotalAdReqCount: behavior.TotalAdReqCount,
  86. TotalAdEposedCount: behavior.TotalAdEposedCount,
  87. },
  88. }
  89. _, err = collection.UpdateOne(context.Background(), filter, update)
  90. if err != nil {
  91. response.Fail(c, 1003, "写入数据失败"+err.Error())
  92. return
  93. }
  94. }
  95. response.Success(c, gin.H{})
  96. }
  97. func CheckUserBehavior(c *gin.Context) {
  98. gid := c.GetString("gid")
  99. pf := c.GetString("pf")
  100. openId := c.GetString("openid")
  101. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  102. //
  103. filter := bson.M{"_id": gid + "|" + pf + "|" + openId}
  104. result := make(map[string]interface{})
  105. collection.FindOne(context.Background(), filter).Decode(&result)
  106. if len(result) == 0 {
  107. response.Success(c, gin.H{
  108. "data": nil,
  109. })
  110. } else {
  111. response.Success(c, gin.H{
  112. "data": result,
  113. })
  114. }
  115. }