123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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,
- })
- }
- }
|