123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- package v1
- import (
- "context"
- "designs/app/common/response"
- "designs/config"
- "designs/global"
- "designs/model"
- "designs/service"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/v2/bson"
- "strconv"
- "time"
- )
- //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,
- })
- }
- func FindUserBehavior(id string) *model.UserBehavior {
- filter := bson.M{"_id": id}
- var result model.UserBehavior
- collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
- err := collection.FindOne(context.Background(), filter).Decode(&result)
- if err != nil {
- return nil
- }
- return &result
- }
- // 新建UserBehavior
- func CreateUserBehavior(gid string, pf string, openId string, now time.Time) error {
- id := gid + "|" + pf + "|" + openId
- Behavior := FindUserBehavior(id)
- if Behavior != nil {
- //更新start_num参数
- collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
- filter := bson.M{"_id": id}
- update := bson.M{"$inc": bson.M{"startNum": 1}}
- _, err := collection.UpdateOne(context.Background(), filter, update)
- if err != nil {
- return err
- }
- //更新关联的广告数据
- filter = bson.M{"userId": id}
- collection = global.App.MongoDB.Database("chunhao").Collection("adRelated")
- update = bson.M{"$inc": bson.M{"startNum": 1}}
- _, err = collection.UpdateMany(context.Background(), filter, update)
- if err != nil {
- return err
- }
- return nil
- } else {
- behavior := model.UserBehavior{
- Id: id,
- Gid: gid,
- Pf: pf,
- OpenId: openId,
- TotalDuration: 0,
- TotalAdReqCount: 0,
- TotalAdEposedCount: 0,
- RelatedAid: 0,
- StartNum: 1,
- CreateTime: int(now.Unix()),
- CreateDate: now.Format("20060102"),
- ActiveStatus: false,
- ConversionStatus: false,
- RemainData: nil,
- }
- //新增
- collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
- _, err := collection.InsertOne(context.Background(), behavior)
- if err != nil {
- return err
- }
- }
- //存储在线数据到redis中
- global.App.Redis.Set(context.Background(), id+"|online", now.Unix(), time.Second*300)
- return nil
- }
- // 更新UserBehavior 在线时间
- func UpdateUserBehaviorDuration(gid string, pf string, openId string, now time.Time, types int) error {
- id := gid + "|" + pf + "|" + openId
- fmt.Println(now.Format("2006-01-02 15:04:05"))
- Behavior := FindUserBehavior(id)
- if Behavior == nil {
- return errors.New("该用户数据未保存,无法计算在线时间")
- }
- lastTime, _ := global.App.Redis.Get(context.Background(), id+"|online").Result()
- //当状态为在线时,刷新redis数据,状态为2是离线,不刷新
- if types == 1 {
- global.App.Redis.Set(context.Background(), id+"|online", now.Unix(), time.Second*300)
- } else {
- global.App.Redis.Del(context.Background(), id+"|online")
- }
- //fmt.Println("types", types)
- //如果redis中没有查到在线状态,那就视为刚刚登录,不增加时间
- if lastTime == "" {
- return nil
- }
- //计算时间差
- lastTimeUnix, _ := strconv.ParseInt(lastTime, 10, 64)
- duration := now.Unix() - lastTimeUnix
- //更新到MongoDB 中
- update := bson.M{
- "$set": struct {
- TotalDuration int `bson:"totalDuration" json:"totalDuration"`
- }{
- TotalDuration: Behavior.TotalDuration + int(duration),
- },
- }
- filter := bson.M{"_id": id}
- collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
- _, err := collection.UpdateOne(context.Background(), filter, update)
- if err != nil {
- return err
- }
- //更新到当日数据中 todo
- //更新到广告关联数据中
- filter = bson.M{"userId": id}
- collection = global.App.MongoDB.Database("chunhao").Collection("adRelated")
- update = bson.M{"$inc": bson.M{"duration": int(duration)}}
- _, err = collection.UpdateMany(context.Background(), filter, update)
- if err != nil {
- return err
- }
- return nil
- }
- // 更新UserBehavior 看广告的数据
- func UpdateUserBehaviorAdInfo(gid string, pf string, openId string, adsState int) error {
- id := gid + "|" + pf + "|" + openId
- collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
- filter := bson.M{"_id": id}
- Behavior := FindUserBehavior(id)
- if Behavior == nil {
- return errors.New("该用户数据未保存,无法累加看广告数据")
- }
- var AdReqCount int
- var AdEposedcount int
- if adsState == 0 {
- //展示不成功
- } else if adsState == 1 {
- //展示成功
- AdReqCount = 1
- } else if adsState == 2 {
- //展示成功并且看完
- AdReqCount = 1
- AdEposedcount = 1
- }
- //更新到总数据中
- update := bson.M{
- "$set": struct {
- TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
- TotalAdEposedCount int `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
- }{
- TotalAdReqCount: Behavior.TotalAdReqCount + AdReqCount,
- TotalAdEposedCount: Behavior.TotalAdEposedCount + AdEposedcount,
- },
- }
- _, err := collection.UpdateOne(context.Background(), filter, update)
- if err != nil {
- return err
- }
- //更新到当日数据中 todo
- //更新到广告关联数据中
- filter = bson.M{"userId": id}
- collection = global.App.MongoDB.Database("chunhao").Collection("adRelated")
- update = bson.M{"$inc": bson.M{"req_count": AdReqCount, "exp_count": AdEposedcount}}
- _, err = collection.UpdateMany(context.Background(), filter, update)
- if err != nil {
- return err
- }
- return nil
- }
- // 上报给巨量对接系统
- func CheckUserActive(gid string, pf string, openId string) {
- request := map[string]interface{}{
- "gid": gid,
- "platform": pf,
- "openId": openId,
- }
- data, err := service.CurlPost(config.Get("app.check_user_active"), request, nil)
- if err != nil {
- global.App.Log.Error("CheckUserActive err: ", err)
- return
- }
- res := make(map[string]interface{})
- err = json.Unmarshal([]byte(data), &res)
- if err != nil {
- global.App.Log.Error("解析返回json错误: ", err)
- }
- fmt.Println(res)
- }
|