behavior.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package v1
  2. import (
  3. "context"
  4. "designs/app/common/request"
  5. "designs/app/common/response"
  6. "designs/global"
  7. "designs/service"
  8. "encoding/json"
  9. "github.com/gin-gonic/gin"
  10. "go.mongodb.org/mongo-driver/v2/bson"
  11. )
  12. type AdData struct {
  13. Pid string `json:"pid" bson:"pid"`
  14. Aid string `json:"aid" bson:"aid"`
  15. Cid string `json:"cid" bson:"cid"`
  16. //ReportUrl string `json:"reportUrl" bson:"reportUrl"`
  17. Reported bool `json:"reported" bson:"reported"`
  18. Duration int64 `json:"duration" bson:"duration"`
  19. AdReqCount uint8 `json:"adReqCount" bson:"adReqCount"`
  20. AdEposedcount uint8 `json:"adEposedcount" bson:"adEposedcount"`
  21. CreateTime int `json:"createTime" bson:"createTime"`
  22. }
  23. type UserBehavior struct {
  24. Id string `bson:"_id,omitempty"`
  25. Gid string `bson:"gid" json:"gid"`
  26. Pf string `bson:"pf" json:"pf"`
  27. OpenId string `bson:"openId" json:"openId"`
  28. AdData interface{} `bson:"adData" json:"adData"`
  29. AdFromCount int `bson:"adFromCount" json:"adFromCount"`
  30. TotalDuration int `bson:"totalDuration" json:"totalDuration"`
  31. TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
  32. TotalAdEposedCount int `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
  33. }
  34. func ReceiveUserBehavior(c *gin.Context) {
  35. form := request.Check(c, &struct {
  36. Gid string `form:"gid" json:"gid" binding:"required"`
  37. Pf string `form:"pf" json:"pf" binding:"required"`
  38. OpenId string `form:"openId" json:"openId" binding:"required"`
  39. AdData interface{} `form:"adData" json:"adData" binding:""`
  40. AdFromCount int `form:"adFromCount" json:"adFromCount" binding:""`
  41. TotalDuration int `form:"totalDuration" json:"totalDuration" binding:""`
  42. TotalAdReqCount int `form:"totalAdReqCount" json:"totalAdReqCount" binding:""`
  43. TotalAdEposedCount int `form:"totalAdEposedCount" json:"totalAdEposedCount" binding:""`
  44. }{})
  45. //if form.AdData != nil {
  46. // var AdData map[string]interface{}
  47. //}
  48. behavior := UserBehavior{
  49. Id: form.Gid + "|" + form.Pf + "|" + form.OpenId,
  50. Gid: form.Gid,
  51. Pf: form.Pf,
  52. OpenId: form.OpenId,
  53. AdData: form.AdData,
  54. AdFromCount: form.AdFromCount,
  55. TotalDuration: form.TotalDuration,
  56. TotalAdReqCount: form.TotalAdReqCount,
  57. TotalAdEposedCount: form.TotalAdEposedCount,
  58. }
  59. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  60. filter := bson.M{"_id": behavior.Id}
  61. result := make(map[string]interface{})
  62. err := collection.FindOne(context.Background(), filter).Decode(&result)
  63. if len(result) == 0 {
  64. //新增
  65. _, err = collection.InsertOne(context.Background(), behavior)
  66. if err != nil {
  67. response.Fail(c, 1003, "写入数据失败"+err.Error())
  68. return
  69. }
  70. } else {
  71. var newAdData AdData
  72. adData, _ := json.Marshal(form.AdData)
  73. json.Unmarshal(adData, &newAdData)
  74. var oldAdData AdData
  75. oldAdDatas, _ := json.Marshal(result["adData"])
  76. json.Unmarshal(oldAdDatas, &oldAdData)
  77. newAdData.Reported = oldAdData.Reported
  78. //更新到MongoDB 中
  79. update := bson.M{
  80. "$set": struct {
  81. Gid string `bson:"gid" json:"gid"`
  82. Pf string `bson:"pf" json:"pf"`
  83. OpenId string `bson:"openId" json:"openId"`
  84. AdData interface{} `bson:"adData" json:"adData"`
  85. AdFromCount int `bson:"adFromCount" json:"adFromCount"`
  86. TotalDuration int `bson:"totalDuration" json:"totalDuration"`
  87. TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
  88. TotalAdEposedCount int `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
  89. }{
  90. Gid: behavior.Gid,
  91. Pf: behavior.Pf,
  92. OpenId: behavior.OpenId,
  93. AdData: newAdData,
  94. AdFromCount: behavior.AdFromCount,
  95. TotalDuration: behavior.TotalDuration,
  96. TotalAdReqCount: behavior.TotalAdReqCount,
  97. TotalAdEposedCount: behavior.TotalAdEposedCount,
  98. },
  99. }
  100. _, err = collection.UpdateOne(context.Background(), filter, update)
  101. if err != nil {
  102. response.Fail(c, 1003, "写入数据失败"+err.Error())
  103. return
  104. }
  105. }
  106. //如果是从广告获取进来的用户,则需要检测是否上传
  107. if form.AdData != nil {
  108. res := service.OceanReportAction(behavior.Id)
  109. if res.Code != 0 {
  110. //上传出现了问题,或者并没有上传
  111. global.App.Log.Error(res.Code, res.Message)
  112. }
  113. }
  114. response.Success(c, gin.H{})
  115. }
  116. func CheckUserBehavior(c *gin.Context) {
  117. gid := c.GetString("gid")
  118. pf := c.GetString("pf")
  119. openId := c.GetString("openid")
  120. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  121. //
  122. filter := bson.M{"_id": gid + "|" + pf + "|" + openId}
  123. result := make(map[string]interface{})
  124. collection.FindOne(context.Background(), filter).Decode(&result)
  125. if len(result) == 0 {
  126. response.Success(c, gin.H{
  127. "data": nil,
  128. })
  129. } else {
  130. response.Success(c, gin.H{
  131. "data": result,
  132. })
  133. }
  134. }
  135. func UpdateUserBehavior(c *gin.Context) {
  136. //查出所有需要更新的数据
  137. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  138. filters := bson.M{}
  139. filters["adData"] = bson.M{"$exists": true}
  140. var result []map[string]interface{}
  141. cur, err := collection.Find(context.Background(), filters)
  142. if err != nil {
  143. response.Fail(c, 1001, err.Error())
  144. }
  145. err = cur.All(context.Background(), &result)
  146. if err != nil {
  147. response.Fail(c, 1001, err.Error())
  148. return
  149. }
  150. for _, v := range result {
  151. var adData map[string]interface{}
  152. oldAdDatas, _ := json.Marshal(v["adData"])
  153. json.Unmarshal(oldAdDatas, &adData)
  154. if adData["createtime"] != nil {
  155. //错误数据,需要修正
  156. filter := bson.M{"_id": v["_id"]}
  157. //
  158. adData["createTime"] = adData["createtime"]
  159. adData["adReqCount"] = adData["adreqcount"]
  160. adData["adEposedcount"] = adData["adeposedcount"]
  161. adData["reportUrl"] = adData["reporturl"]
  162. delete(adData, "createtime")
  163. delete(adData, "adreqcount")
  164. delete(adData, "adeposedcount")
  165. delete(adData, "reporturl")
  166. //更新到MongoDB 中
  167. update := bson.M{
  168. "$set": struct {
  169. AdData map[string]interface{} `bson:"adData" json:"adData"`
  170. }{
  171. AdData: adData,
  172. },
  173. }
  174. _, err = collection.UpdateOne(context.Background(), filter, update)
  175. if err != nil {
  176. response.Fail(c, 1003, "写入数据失败"+err.Error())
  177. return
  178. }
  179. }
  180. }
  181. defer cur.Close(context.Background())
  182. //更新成正确的数据
  183. response.Success(c, gin.H{
  184. "data": result,
  185. })
  186. }