123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- package v1
- import (
- "context"
- "designs/app/common/request"
- "designs/app/common/response"
- "designs/global"
- "designs/service"
- "encoding/json"
- "errors"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/v2/bson"
- "strconv"
- "time"
- )
- 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,
- })
- }
- func FindUserBehavior(id string) *UserBehavior {
- filter := bson.M{"_id": id}
- var result 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) error {
- id := gid + "|" + pf + "|" + openId
- Behavior := FindUserBehavior(id)
- if Behavior != nil {
- return errors.New("该用户数据已经存在")
- }
- behavior := UserBehavior{
- Id: id,
- Gid: gid,
- Pf: pf,
- OpenId: openId,
- AdData: nil,
- AdFromCount: 0,
- TotalDuration: 0,
- TotalAdReqCount: 0,
- TotalAdEposedCount: 0,
- }
- //新增
- collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
- _, err := collection.InsertOne(context.Background(), behavior)
- if err != nil {
- return err
- }
- return nil
- }
- // 更新UserBehavior 在线时间
- func UpdateUserBehaviorDuration(gid string, pf string, openId string, now time.Time, types int) error {
- id := gid + "|" + pf + "|" + openId
- Behavior := FindUserBehavior(id)
- if Behavior == nil {
- return errors.New("该用户数据未保存,无法计算在线时间")
- }
- lastTime, err := global.App.Redis.Get(context.Background(), id+"|online").Result()
- if err != nil {
- return err
- }
- //当状态为在线时,刷新redis数据,状态为2是离线,不刷新
- if types == 1 {
- global.App.Redis.Set(context.Background(), id+"|online", now.Unix(), 300)
- } else {
- global.App.Redis.Del(context.Background(), id+"|online")
- }
- //如果redis中没有查到在线状态,那就视为刚刚登录,不增加时间
- if lastTime == "" {
- return nil
- }
- //计算时间差
- lastTimeUnix, _ := strconv.ParseInt(lastTime, 10, 64)
- duration := now.Unix() - lastTimeUnix
- var adData interface{}
- if Behavior.AdData != nil {
- //存在adData
- var newAdData AdData
- oldAdDatas, _ := json.Marshal(Behavior.AdData)
- json.Unmarshal(oldAdDatas, &newAdData)
- newAdData.Duration = newAdData.Duration + duration
- adData = newAdData
- }
- //更新到MongoDB 中
- update := bson.M{
- "$set": struct {
- AdData interface{} `bson:"adData" json:"adData"`
- TotalDuration int `bson:"duration" json:"duration"`
- }{
- AdData: adData,
- 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
- }
- 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 adData interface{}
- if Behavior.AdData != nil {
- //存在adData
- var newAdData AdData
- oldAdDatas, _ := json.Marshal(Behavior.AdData)
- json.Unmarshal(oldAdDatas, &newAdData)
- newAdData.AdReqCount++
- if adsState == 2 {
- newAdData.AdEposedcount++
- }
- adData = newAdData
- }
- var AdReqCount int
- var AdEposedcount int
- var AdFormCount int
- if adsState == 0 {
- //展示不成功
- AdReqCount = 1
- AdFormCount = 1
- } else if adsState == 1 {
- //展示成功
- AdReqCount = 1
- AdFormCount = 1
- } else if adsState == 2 {
- //展示成功并且看完
- AdReqCount = 1
- AdFormCount = 1
- AdEposedcount = 1
- }
- //更新到MongoDB 中
- update := bson.M{
- "$set": struct {
- AdData interface{} `bson:"adData" json:"adData"`
- AdFromCount int `bson:"adFromCount" json:"adFromCount"`
- TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
- TotalAdEposedCount int `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
- }{
- AdData: adData,
- AdFromCount: Behavior.AdFromCount + AdFormCount,
- TotalAdReqCount: Behavior.TotalAdReqCount + AdReqCount,
- TotalAdEposedCount: Behavior.TotalAdEposedCount + AdEposedcount,
- },
- }
- _, err := collection.UpdateOne(context.Background(), filter, update)
- if err != nil {
- return err
- }
- return nil
- }
|