package v1 import ( "context" "designs/app/common/request" "designs/app/common/response" "designs/global" "github.com/gin-gonic/gin" "go.mongodb.org/mongo-driver/v2/bson" ) type AdData struct { Pid string `json:"pid"` Aid string `json:"aid"` Cid string `json:"cid"` ReportUrl string `json:"reportUrl"` Reported bool `json:"reported"` Duration int64 `json:"duration"` AdReqCount uint8 `json:"adReqCount"` AdEposedcount uint8 `json:"adEposedcount"` CreateTime int `json:"createTime"` } type UserBehavior struct { Id string `bson:"_id,omitempty"` Gid string `bson:"gid"` Pf string `bson:"pf"` OpenId string `bson:"openId"` AdData interface{} `bson:"adData"` AdFromCount int `bson:"adFromCount"` TotalDuration int `bson:"totalDuration"` TotalAdReqCount int `bson:"totalAdReqCount"` TotalAdEposedCount int `bson:"totalAdEposedCount"` } func ReceiveUserBehavior(c *gin.Context) { form := request.Check(c, &struct { Gid string `form:"gid" json:"gid" binding:"required"` Pf string `form:"pf" json:"pf" binding:"required"` OpenId string `form:"openId" json:"openId" binding:"required"` AdData interface{} `form:"adData" json:"adData" binding:""` AdFromCount int `form:"adFromCount" json:"adFromCount" binding:""` TotalDuration int `form:"totalDuration" json:"totalDuration" binding:""` TotalAdReqCount int `form:"totalAdReqCount" json:"totalAdReqCount" binding:""` TotalAdEposedCount int `form:"totalAdEposedCount" json:"totalAdEposedCount" binding:""` }{}) behavior := UserBehavior{ Id: form.Gid + "|" + form.Pf + "|" + form.OpenId, Gid: form.Gid, Pf: form.Pf, OpenId: form.OpenId, AdData: form.AdData, AdFromCount: form.AdFromCount, TotalDuration: form.TotalDuration, TotalAdReqCount: form.TotalAdReqCount, TotalAdEposedCount: form.TotalAdEposedCount, } collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior") // filter := bson.M{"_id": behavior.Id} result := make(map[string]interface{}) err := collection.FindOne(context.Background(), filter).Decode(&result) if len(result) == 0 { //新增 _, err = collection.InsertOne(context.Background(), behavior) if err != nil { response.Fail(c, 1003, "写入数据失败"+err.Error()) return } } else { //更新到MongoDB 中 update := bson.M{ "$set": struct { Gid string `bson:"gid"` Pf string `bson:"pf"` OpenId string `bson:"openId"` AdData interface{} `bson:"adData"` AdFromCount int `bson:"adFromCount"` TotalDuration int `bson:"totalDuration"` TotalAdReqCount int `bson:"totalAdReqCount"` TotalAdEposedCount int `bson:"totalAdEposedCount"` }{ Gid: behavior.Gid, Pf: behavior.Pf, OpenId: behavior.OpenId, AdData: behavior.AdData, AdFromCount: behavior.AdFromCount, TotalDuration: behavior.TotalDuration, TotalAdReqCount: behavior.TotalAdReqCount, TotalAdEposedCount: behavior.TotalAdEposedCount, }, } _, err = collection.UpdateOne(context.Background(), filter, update) if err != nil { response.Fail(c, 1003, "写入数据失败"+err.Error()) return } } response.Success(c, gin.H{}) } func CheckUserBehavior(c *gin.Context) { gid := c.GetString("gid") pf := c.GetString("pf") openId := c.GetString("openid") collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior") // filter := bson.M{"_id": gid + "|" + pf + "|" + openId} result := make(map[string]interface{}) collection.FindOne(context.Background(), filter).Decode(&result) if len(result) == 0 { response.Success(c, gin.H{ "data": nil, }) } else { response.Success(c, gin.H{ "data": result, }) } }