92 lines
2.1 KiB
Go
92 lines
2.1 KiB
Go
package adser
|
|
|
|
import (
|
|
"91porn-server/app/appg"
|
|
"91porn-server/common/constant"
|
|
"91porn-server/common/version"
|
|
"91porn-server/models/s/useradverstatmod"
|
|
"91porn-server/models/v/usermod"
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
func CanRecord(user *usermod.User) bool {
|
|
if user.SysType == constant.SysTypeAndroid &&
|
|
//user.OriVer != "" &&
|
|
user.AdGroup.InABC() {
|
|
return VersionCompare(user.OriVer, constant.AdGroupClientVersion)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func VersionCompare(v1, v2 string) bool {
|
|
ver1, err := version.New(v1)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
ver2, err := version.New(v2)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return ver1.GTE(ver2)
|
|
}
|
|
|
|
func UpsertAdStat(ctx context.Context, user *usermod.User, now time.Time, incAppClick, incAdClick int64, payAmount int64) error {
|
|
if !CanRecord(user) && appg.Conf.Base.Env != "test" {
|
|
return errors.New("user not eligible for ad stat recording")
|
|
}
|
|
local, _ := time.LoadLocation("Asia/Shanghai")
|
|
|
|
// 把 day 归一化到当天 0 点,保证和索引一致
|
|
now = now.In(local)
|
|
day := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, local)
|
|
|
|
filter := bson.M{
|
|
"userId": user.UID,
|
|
"date": day,
|
|
}
|
|
|
|
inc := bson.M{}
|
|
if incAppClick != 0 {
|
|
inc["appClick"] = incAppClick
|
|
}
|
|
if incAdClick != 0 {
|
|
inc["adClick"] = incAdClick
|
|
}
|
|
deltaTotal := incAppClick + incAdClick
|
|
if deltaTotal != 0 {
|
|
inc["totalClick"] = deltaTotal
|
|
}
|
|
if payAmount != 0 {
|
|
inc["payCount"] = 1
|
|
inc["payTotal"] = payAmount
|
|
}
|
|
createdAt := user.CreatedAt.In(local)
|
|
regDay := time.Date(createdAt.Year(), createdAt.Month(), createdAt.Day(), 0, 0, 0, 0, createdAt.Location())
|
|
|
|
update := bson.M{
|
|
// 只在插入时写入的字段
|
|
"$setOnInsert": bson.M{
|
|
"userId": user.UID,
|
|
"date": day,
|
|
"createdAt": now,
|
|
"districtCode": user.DistrictCode,
|
|
"sysType": user.SysType,
|
|
"adGroup": user.AdGroup,
|
|
"regDay": regDay,
|
|
"version": user.OriVer,
|
|
},
|
|
// 每次更新都要改的字段
|
|
"$set": bson.M{
|
|
"updatedAt": now,
|
|
},
|
|
}
|
|
if len(inc) > 0 {
|
|
update["$inc"] = inc
|
|
}
|
|
return useradverstatmod.UpsertOne(filter, update)
|
|
}
|