| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- package v1
- import (
- "context"
- "designs/app/common/request"
- "designs/app/common/response"
- "designs/global"
- "designs/service"
- "encoding/json"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/v2/bson"
- )
- type AdData struct {
- Pid string `json:"pid" bson:"pid"`
- Aid string `json:"aid" bson:"aid"`
- Cid string `json:"cid" bson:"cid"`
- //ReportUrl string `json:"reportUrl" bson:"reportUrl"`
- Reported bool `json:"reported" bson:"reported"`
- Duration int64 `json:"duration" bson:"duration"`
- AdReqCount uint8 `json:"adReqCount" bson:"adReqCount"`
- AdEposedcount uint8 `json:"adEposedcount" bson:"adEposedcount"`
- CreateTime int `json:"createTime" bson:"createTime"`
- }
- type UserBehavior struct {
- Id string `bson:"_id,omitempty"`
- Gid string `bson:"gid" json:"gid"`
- Pf string `bson:"pf" json:"pf"`
- OpenId string `bson:"openId" json:"openId"`
- AdData interface{} `bson:"adData" json:"adData"`
- AdFromCount int `bson:"adFromCount" json:"adFromCount"`
- TotalDuration int `bson:"totalDuration" json:"totalDuration"`
- TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
- TotalAdEposedCount int `bson:"totalAdEposedCount" json:"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:""`
- }{})
- //if form.AdData != nil {
- // var AdData map[string]interface{}
- //}
- 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 {
- var newAdData AdData
- adData, _ := json.Marshal(form.AdData)
- json.Unmarshal(adData, &newAdData)
- var oldAdData AdData
- oldAdDatas, _ := json.Marshal(result["adData"])
- json.Unmarshal(oldAdDatas, &oldAdData)
- newAdData.Reported = oldAdData.Reported
- //更新到MongoDB 中
- update := bson.M{
- "$set": struct {
- Gid string `bson:"gid" json:"gid"`
- Pf string `bson:"pf" json:"pf"`
- OpenId string `bson:"openId" json:"openId"`
- AdData interface{} `bson:"adData" json:"adData"`
- AdFromCount int `bson:"adFromCount" json:"adFromCount"`
- TotalDuration int `bson:"totalDuration" json:"totalDuration"`
- TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
- TotalAdEposedCount int `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
- }{
- Gid: behavior.Gid,
- Pf: behavior.Pf,
- OpenId: behavior.OpenId,
- AdData: newAdData,
- 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
- }
- }
- //如果是从广告获取进来的用户,则需要检测是否上传
- if form.AdData != nil {
- res := service.OceanReportAction(behavior.Id)
- if res.Code != 0 {
- //上传出现了问题,或者并没有上传
- global.App.Log.Error(res.Code, res.Message)
- }
- }
- 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,
- })
- }
- }
- func UpdateUserBehavior(c *gin.Context) {
- //查出所有需要更新的数据
- collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
- filters := bson.M{}
- filters["adData"] = bson.M{"$exists": true}
- var result []map[string]interface{}
- cur, err := collection.Find(context.Background(), filters)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- }
- err = cur.All(context.Background(), &result)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- for _, v := range result {
- var adData map[string]interface{}
- oldAdDatas, _ := json.Marshal(v["adData"])
- json.Unmarshal(oldAdDatas, &adData)
- if adData["createtime"] != nil {
- //错误数据,需要修正
- filter := bson.M{"_id": v["_id"]}
- //
- adData["createTime"] = adData["createtime"]
- adData["adReqCount"] = adData["adreqcount"]
- adData["adEposedcount"] = adData["adeposedcount"]
- adData["reportUrl"] = adData["reporturl"]
- delete(adData, "createtime")
- delete(adData, "adreqcount")
- delete(adData, "adeposedcount")
- delete(adData, "reporturl")
- //更新到MongoDB 中
- update := bson.M{
- "$set": struct {
- AdData map[string]interface{} `bson:"adData" json:"adData"`
- }{
- AdData: adData,
- },
- }
- _, err = collection.UpdateOne(context.Background(), filter, update)
- if err != nil {
- response.Fail(c, 1003, "写入数据失败"+err.Error())
- return
- }
- }
- }
- defer cur.Close(context.Background())
- //更新成正确的数据
- response.Success(c, gin.H{
- "data": result,
- })
- }
|