1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141 |
- package v1
- import (
- "context"
- "designs/app/common/request"
- "designs/app/common/response"
- "designs/global"
- "designs/service"
- "designs/utils"
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "go.mongodb.org/mongo-driver/v2/bson"
- "go.mongodb.org/mongo-driver/v2/mongo/options"
- "math"
- "math/big"
- "time"
- )
- // 总览
- func Summary(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- Pf string `form:"pf" json:"pf" binding:"required"`
- }{})
- //查询用户总数
- var userCount int64
- err := global.App.DB.Table("user").
- Where("gid", form.Gid).
- Where("pf", form.Pf).
- Count(&userCount).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //查询近七日活跃总数
- now := time.Now()
- sevenDayAgo := now.AddDate(0, 0, -7)
- thirtyDayAgo := now.AddDate(0, 0, -30)
- var activeUserCount7 int64
- err = global.App.DB.Table("user_login").
- Where("gid", form.Gid).
- Where("pf", form.Pf).
- Where("loginTime", ">=", sevenDayAgo).
- Where("loginTime", "<=", now).
- Distinct("userId").
- Count(&activeUserCount7).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- var activeUserCount30 int64
- err = global.App.DB.Table("user_login").
- Where("gid", form.Gid).
- Where("pf", form.Pf).
- Where("loginTime", ">=", thirtyDayAgo).
- Where("loginTime", "<=", now).
- Distinct("userId").
- Count(&activeUserCount30).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //从redis中读取缓存
- var avgTimeString string
- avgTimeKey := fmt.Sprintf("%s|%s|avgTime", form.Gid, form.Pf)
- avgTimeString, _ = global.App.Redis.Get(context.Background(), avgTimeKey).Result()
- if avgTimeString == "" {
- //查询 近7日单设备日均使用时长
- res, err := service.UserOnlineSummary(form.Gid, form.Pf, "", sevenDayAgo.Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05"))
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- var avgTime int
- for _, v := range res {
- avgTime = avgTime + int(v)
- }
- if avgTime != 0 {
- avgTime = int(math.Round(float64(avgTime / len(res))))
- avgTimeString = utils.TimeStampToMDS(avgTime)
- } else {
- avgTimeString = "00.00"
- }
- global.App.Redis.Set(context.Background(), avgTimeKey, avgTimeString, time.Second*3000)
- }
- response.Success(c, gin.H{
- "data": map[string]interface{}{
- "userCount": userCount,
- "activeUserCount7": activeUserCount7,
- "activeUserCount30": activeUserCount30,
- "activeUserCount7Time": avgTimeString,
- },
- })
- }
- // 时段分布
- func TimeDistributionData(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- Pf string `form:"pf" json:"pf" binding:"required"`
- Type int `form:"type" json:"type" binding:"required"`
- }{})
- var data interface{}
- if form.Type == 1 {
- //新增用户
- todayTimeDistribution, yesterdayTimeDistribution, yesterdayCount, todayCount, yesterdayThisTimeCount, err := service.GetRegisterTimeDistribution(form.Pf, form.Gid)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data = map[string]interface{}{
- "today": todayTimeDistribution,
- "yesterday": yesterdayTimeDistribution,
- "yesterdayCount": yesterdayCount,
- "yesterdayThisTimeCount": yesterdayThisTimeCount,
- "todayCount": todayCount,
- }
- } else if form.Type == 2 {
- //活跃设备
- todayTimeDistribution, yesterdayTimeDistribution, yesterdayCount, todayCount, yesterdayThisTimeCount, err := service.GetActiveTimeDistribution(form.Pf, form.Gid)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data = map[string]interface{}{
- "today": todayTimeDistribution,
- "yesterday": yesterdayTimeDistribution,
- "yesterdayCount": yesterdayCount,
- "yesterdayThisTimeCount": yesterdayThisTimeCount,
- "todayCount": todayCount,
- }
- } else if form.Type == 3 {
- //启动次数
- todayTimeDistribution, yesterdayTimeDistribution, yesterdayCount, todayCount, yesterdayThisTimeCount, err := service.GetActionDistribution(form.Pf, form.Gid)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data = map[string]interface{}{
- "today": todayTimeDistribution,
- "yesterday": yesterdayTimeDistribution,
- "yesterdayCount": yesterdayCount,
- "yesterdayThisTimeCount": yesterdayThisTimeCount,
- "todayCount": todayCount,
- }
- } else {
- response.Fail(c, 1003, "type错误")
- return
- }
- response.Success(c, gin.H{
- "data": data,
- })
- }
- // 30日趋势
- func MouthDistributionData(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- Pf string `form:"pf" json:"pf" binding:"required"`
- Type int `form:"type" json:"type" binding:"required"`
- }{})
- now := time.Now()
- EndTime := now.AddDate(0, 0, 1).Format("2006-01-02")
- StartTime := now.AddDate(0, 0, -29).Format("2006-01-02")
- data := make(map[string]interface{})
- if form.Type == 1 {
- //查询新增设备趋势图
- TimeDistribution, count, avg, err := service.GetRegisterDayDistribution(form.Pf, form.Gid, StartTime, EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data["timeDistribution"] = TimeDistribution
- data["count"] = count
- data["avg"] = avg
- } else if form.Type == 2 {
- //查询活跃用户趋势图
- TimeDistribution, count, avg, err := service.GetActiveDayDistribution(form.Pf, form.Gid, StartTime, EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data["timeDistribution"] = TimeDistribution
- data["count"] = count
- data["avg"] = avg
- } else if form.Type == 3 {
- //查询启动次数
- TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, StartTime, EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data["timeDistribution"] = TimeDistribution
- data["count"] = count
- data["avg"] = avg
- } else if form.Type == 4 {
- //查询单用户使用时长
- TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, StartTime, EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data["timeDistribution"] = TimeDistribution
- data["count"] = count
- data["avg"] = avg
- } else if form.Type == 5 {
- //查询用户留存率
- //todo
- } else {
- response.Fail(c, 1003, "type错误")
- return
- }
- response.Success(c, gin.H{
- "data": data,
- })
- }
- // 用户趋势 总览
- func UserTrendsOverview(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- Pf string `form:"pf" json:"pf" binding:"required"`
- StartTime string `form:"startTime" json:"startTime" binding:"required"`
- EndTime string `form:"endTime" json:"endTime" binding:"required"`
- }{})
- //查询用户新增
- var registerCount int64
- err := global.App.DB.Table("user").
- Where("gid", form.Gid).
- Where("pf", form.Pf).
- Where("createdAt", ">=", form.StartTime).
- Where("createdAt", "<=", form.EndTime).
- Count(®isterCount).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //查询活跃设备
- var activeCount int64
- err = global.App.DB.Table("user_online").
- Where("gid", form.Gid).
- Where("pf", form.Pf).
- Where("logTime", ">=", form.StartTime).
- Where("logTime", "<=", form.EndTime).
- Distinct("userId").Count(&activeCount).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //查询启动次数
- var loginCount int64
- err = global.App.DB.Table("user_login").
- Where("gid", form.Gid).
- Where("pf", form.Pf).
- Where("loginTime", ">=", form.StartTime).
- Where("loginTime", "<=", form.EndTime).
- Count(&loginCount).Error
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //查询平均启动时长
- //查询活跃用户月趋势图
- _, _, activeTime, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //查询 DAU/MAU
- //todo 查询方式需要更新
- dauMau := 0.3
- response.Success(c, gin.H{
- "data": map[string]interface{}{
- "registerCount": registerCount,
- "activeCount": activeCount,
- "loginCount": loginCount,
- "activeTime": activeTime,
- "dauMau": dauMau,
- },
- })
- }
- // 数据趋势
- func DataTrades(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- Pf string `form:"pf" json:"pf" binding:"required"`
- StartTime string `form:"startTime" json:"startTime" binding:"required"`
- EndTime string `form:"endTime" json:"endTime" binding:"required"`
- Type int `form:"type" json:"type" binding:"required"`
- }{})
- data := make(map[string]interface{})
- form.EndTime = form.EndTime + " 23:59:59"
- if form.Type == 1 {
- //查询新增设备趋势图
- TimeDistribution, count, avg, err := service.GetRegisterDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data["imeDistribution"] = TimeDistribution
- data["count"] = count
- data["avg"] = avg
- } else if form.Type == 2 {
- //查询活跃用户趋势图
- TimeDistribution, count, avg, err := service.GetActiveDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data["imeDistribution"] = TimeDistribution
- data["count"] = count
- data["avg"] = avg
- } else if form.Type == 3 {
- //查询活跃用户周趋势图
- TimeDistribution, count, avg, err := service.GetActiveWeekDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data["imeDistribution"] = TimeDistribution
- data["count"] = count
- data["avg"] = avg
- } else if form.Type == 4 {
- //查询活跃用户月趋势图
- TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data["imeDistribution"] = TimeDistribution
- data["count"] = count
- data["avg"] = avg
- } else if form.Type == 5 {
- //查询启动次数
- TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data["imeDistribution"] = TimeDistribution
- data["count"] = count
- data["avg"] = avg
- } else if form.Type == 6 {
- //查询平均启动时间
- TimeDistribution, count, avg, err := service.UserOnlineSummaryByDay(form.Gid, form.Pf, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- data["imeDistribution"] = TimeDistribution
- data["count"] = count
- data["avg"] = avg
- } else {
- response.Fail(c, 1003, "type 错误")
- return
- }
- response.Success(c, gin.H{
- "data": data,
- })
- }
- // 数据趋势的整合
- func DataTradesDetail(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- Pf string `form:"pf" json:"pf" binding:"required"`
- StartTime string `form:"startTime" json:"startTime" binding:"required"`
- EndTime string `form:"endTime" json:"endTime" binding:"required"`
- }{})
- form.EndTime = form.EndTime + " 23:59:59"
- type dayData struct {
- NewUser int `json:"newUser"`
- ActiveUser int `json:"activeUser"`
- ActiveUserWeek int `json:"activeUserWeek"`
- ActiveUserMouth int `json:"activeUserMouth"`
- ActiveStart int `json:"activeStart"`
- AvgTime int `json:"avgTime"`
- }
- var data = make(map[string]dayData)
- //查询新增设备趋势图
- NewUser, _, _, err := service.GetRegisterDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //查询活跃用户趋势图
- ActiveUser, _, _, err := service.GetActiveDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //查询活跃用户周趋势图
- ActiveUserWeek, _, _, err := service.GetActiveWeekDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //查询活跃用户月趋势图
- ActiveUserMouth, _, _, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //查询启动次数
- ActiveStart, _, _, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- //查询平均启动时间
- AvgTime, _, _, err := service.UserOnlineSummaryByDay(form.Gid, form.Pf, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- for k := range AvgTime {
- data[k] = dayData{
- NewUser: NewUser[k],
- ActiveUser: ActiveUser[k],
- ActiveUserWeek: ActiveUserWeek[k],
- ActiveUserMouth: ActiveUserMouth[k],
- ActiveStart: ActiveStart[k],
- AvgTime: AvgTime[k],
- }
- }
- response.Success(c, gin.H{
- "data": data,
- })
- }
- func RemainDataBydDay(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- Pf string `form:"pf" json:"pf" binding:"required"`
- StartTime string `form:"startTime" json:"startTime" binding:"required"`
- EndTime string `form:"endTime" json:"endTime" binding:"required"`
- Type int `form:"type" json:"type" binding:"required"`
- }{})
- data, err := service.RemainDataBydDay(form.Type, form.Pf, form.Gid, form.StartTime, form.EndTime)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- response.Success(c, gin.H{
- "data": data,
- })
- }
- 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" json:"gid"`
- Pf string `bson:"pf" json:"pf"`
- OpenId string `bson:"openId" json:"openId"`
- RelatedAid int64 `bson:"relatedAid" json:"relatedAid"` //目前关联的aid
- TotalDuration int `bson:"totalDuration" json:"totalDuration"`
- TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
- TotalAdEposedCount int `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
- CreateDate string `bson:"createDate" json:"createDate"`
- CreateTime int `json:"createTime" bson:"createTime"`
- StartNum int `bson:"startNum" json:"startNum"`
- ActiveStatus bool `bson:"activeStatus" json:"activeStatus"` //激活状态
- ConversionStatus bool `bson:"conversionStatus" json:"conversionStatus"` //转化状态
- RemainData map[string]string `json:"remainData" bson:"remainData"` //留存数据
- }
- type AdRelated struct {
- Id string `bson:"_id" json:"_id"`
- UserId string `bson:"userId" json:"userId"`
- Aid int64 `json:"aid" bson:"aid"` //广告的推广计划id,即广告ID,广告平台配置落地页参数
- Cid string `json:"cid" bson:"cid"` //openid的用户点击aid广告进入游戏时的唯一标识,广告平台提供
- Pid int64 `json:"pid" bson:"pid"` //广告的项目id(仅巨量引擎存在,腾讯广告时不存在该值),广告平台配置落地页参数
- CreateTime int64 `bson:"create_time" json:"createTime"` //当前计划的创建时间
- StartNum int `bson:"startNum" json:"startNum"` //启动次数
- Revenue float32 `bson:"revenue" json:"revenue"` //当日预估收益
- Duration int64 `bson:"duration" json:"duration"` //当日在线时长
- ReqCount int `bson:"req_count" json:"req_count"` //当日的激励视频广告请求次数
- ExpCount int `bson:"exp_count" json:"exp_count"` //当日的激励视频广告曝光次数
- }
- type BehaviorFilter struct {
- Gt int `json:"gt"`
- Gte int `json:"gte"`
- Lte int `json:"lte"`
- Lt int `json:"lt"`
- Ne int `json:"ne"`
- //$gt 大于
- //$gte:大于或等于
- //$lt:小于
- //$lte:小于或等于
- //$ne:不等于
- }
- func BehaviorList(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:""`
- Offset int `form:"offset" json:"offset" binding:""`
- Limit int `form:"limit" json:"limit" binding:""`
- ActiveStatus string `form:"activeStatus" json:"activeStatus" binding:""` //all true false
- ConversionStatus string `form:"conversionStatus" json:"conversionStatus" binding:""` //all true false
- AdFromCount interface{} `form:"adFromCount" json:"adFromCount" binding:""`
- TotalDuration interface{} `form:"totalDuration" json:"totalDuration" binding:""`
- TotalAdReqCount interface{} `form:"totalAdReqCount" json:"totalAdReqCount" binding:""`
- TotalAdEposedCount interface{} `form:"totalAdEposedCount" json:"totalAdEposedCount" binding:""`
- CreateTime interface{} `form:"createTime" json:"createTime" binding:""`
- StartNum interface{} `form:"startNum" json:"startNum" binding:""`
- }{})
- collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
- ctx := context.Background()
- filter := bson.M{"gid": form.Gid}
- if form.Pf != "" {
- filter["pf"] = form.Pf
- }
- if form.OpenId != "" {
- filter["openId"] = bson.M{"$regex": form.OpenId}
- }
- if form.ActiveStatus == "true" {
- filter["activeStatus"] = true
- } else if form.ActiveStatus == "false" {
- filter["activeStatus"] = false
- }
- if form.ActiveStatus == "true" {
- filter["activeStatus"] = true
- } else if form.ActiveStatus == "false" {
- filter["activeStatus"] = false
- }
- if form.AdFromCount != nil {
- fmt.Println(form.AdFromCount)
- marsh, _ := json.Marshal(form.AdFromCount)
- var adFromCount BehaviorFilter
- json.Unmarshal(marsh, &adFromCount)
- filters := bson.M{}
- if adFromCount.Gt != 0 {
- filters["$gt"] = adFromCount.Gt
- }
- if adFromCount.Gte != 0 {
- filters["$gte"] = adFromCount.Gte
- }
- if adFromCount.Lte != 0 {
- filters["$lte"] = adFromCount.Lte
- }
- if adFromCount.Lt != 0 {
- filters["$lt"] = adFromCount.Lt
- }
- if adFromCount.Ne != 0 {
- filters["$ne"] = adFromCount.Ne
- }
- if len(filters) > 0 {
- filter["adFromCount"] = filters
- }
- }
- if form.TotalAdReqCount != nil {
- marsh, _ := json.Marshal(form.TotalAdReqCount)
- var totalAdReqCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdReqCount)
- filters := bson.M{}
- if totalAdReqCount.Gt != 0 {
- filters["$gt"] = totalAdReqCount.Gt
- }
- if totalAdReqCount.Gte != 0 {
- filters["$gte"] = totalAdReqCount.Gte
- }
- if totalAdReqCount.Lte != 0 {
- filters["$lte"] = totalAdReqCount.Lte
- }
- if totalAdReqCount.Lt != 0 {
- filters["$lt"] = totalAdReqCount.Lt
- }
- if totalAdReqCount.Ne != 0 {
- filters["$ne"] = totalAdReqCount.Ne
- }
- if len(filters) > 0 {
- filter["totalAdReqCount"] = filters
- }
- }
- if form.TotalAdEposedCount != nil {
- marsh, _ := json.Marshal(form.TotalAdEposedCount)
- var totalAdEposedCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdEposedCount)
- filters := bson.M{}
- if totalAdEposedCount.Gt != 0 {
- filters["$gt"] = totalAdEposedCount.Gt
- }
- if totalAdEposedCount.Gte != 0 {
- filters["$gte"] = totalAdEposedCount.Gte
- }
- if totalAdEposedCount.Lte != 0 {
- filters["$lte"] = totalAdEposedCount.Lte
- }
- if totalAdEposedCount.Lt != 0 {
- filters["$lt"] = totalAdEposedCount.Lt
- }
- if totalAdEposedCount.Ne != 0 {
- filters["$ne"] = totalAdEposedCount.Ne
- }
- if len(filters) > 0 {
- filter["totalAdEposedCount"] = filters
- }
- }
- if form.CreateTime != nil {
- marsh, _ := json.Marshal(form.CreateTime)
- var totalAdEposedCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdEposedCount)
- filters := bson.M{}
- if totalAdEposedCount.Gt != 0 {
- filters["$gt"] = totalAdEposedCount.Gt
- }
- if totalAdEposedCount.Gte != 0 {
- filters["$gte"] = totalAdEposedCount.Gte
- }
- if totalAdEposedCount.Lte != 0 {
- filters["$lte"] = totalAdEposedCount.Lte
- }
- if totalAdEposedCount.Lt != 0 {
- filters["$lt"] = totalAdEposedCount.Lt
- }
- if totalAdEposedCount.Ne != 0 {
- filters["$ne"] = totalAdEposedCount.Ne
- }
- if len(filters) > 0 {
- filter["createTime"] = filters
- }
- }
- if form.StartNum != nil {
- marsh, _ := json.Marshal(form.StartNum)
- var totalAdEposedCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdEposedCount)
- filters := bson.M{}
- if totalAdEposedCount.Gt != 0 {
- filters["$gt"] = totalAdEposedCount.Gt
- }
- if totalAdEposedCount.Gte != 0 {
- filters["$gte"] = totalAdEposedCount.Gte
- }
- if totalAdEposedCount.Lte != 0 {
- filters["$lte"] = totalAdEposedCount.Lte
- }
- if totalAdEposedCount.Lt != 0 {
- filters["$lt"] = totalAdEposedCount.Lt
- }
- if totalAdEposedCount.Ne != 0 {
- filters["$ne"] = totalAdEposedCount.Ne
- }
- if len(filters) > 0 {
- filter["startNum"] = filters
- }
- }
- if form.TotalDuration != nil {
- marsh, _ := json.Marshal(form.TotalDuration)
- var totalAdEposedCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdEposedCount)
- filters := bson.M{}
- if totalAdEposedCount.Gt != 0 {
- filters["$gt"] = totalAdEposedCount.Gt
- }
- if totalAdEposedCount.Gte != 0 {
- filters["$gte"] = totalAdEposedCount.Gte
- }
- if totalAdEposedCount.Lte != 0 {
- filters["$lte"] = totalAdEposedCount.Lte
- }
- if totalAdEposedCount.Lt != 0 {
- filters["$lt"] = totalAdEposedCount.Lt
- }
- if totalAdEposedCount.Ne != 0 {
- filters["$ne"] = totalAdEposedCount.Ne
- }
- if len(filters) > 0 {
- filter["totalDuration"] = filters
- }
- }
- option := options.Find()
- option.SetLimit(int64(form.Limit))
- option.SetSkip(int64(form.Offset))
- cur, err := collection.Find(ctx, filter, option)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- count, err := collection.CountDocuments(ctx, filter)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- var data []UserBehavior
- err = cur.All(ctx, &data)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- response.Success(c, gin.H{
- "data": data,
- "count": count,
- })
- }
- func AdRelatedList(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:"search" json:"search" binding:""`
- Offset int `form:"offset" json:"offset" binding:""`
- Limit int `form:"limit" json:"limit" binding:""`
- Pid string `form:"pid" json:"pid" binding:""`
- Aid int `form:"aid" json:"aid" binding:""`
- Cid string `form:"cid" json:"cid" binding:""`
- CreateTime interface{} `form:"createTime" json:"createTime" binding:""`
- StartNum interface{} `form:"startNum" json:"startNum" binding:""`
- Revenue interface{} `form:"revenue" json:"revenue" binding:""`
- Duration interface{} `form:"duration" json:"duration" binding:""`
- ReqCount interface{} `form:"reqCount" json:"reqCount" binding:""`
- ExpCount interface{} `form:"expCount" json:"expCount" binding:""`
- }{})
- collection := global.App.MongoDB.Database("chunhao").Collection("adRelated")
- ctx := context.Background()
- filter := bson.M{}
- if form.Pid != "" {
- filter["pid"] = form.Pid
- }
- if form.Aid != 0 {
- filter["aid"] = form.Aid
- }
- if form.Cid != "" {
- filter["cid"] = form.Cid
- }
- if form.CreateTime != nil {
- marsh, _ := json.Marshal(form.CreateTime)
- var totalAdEposedCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdEposedCount)
- filters := bson.M{}
- if totalAdEposedCount.Gt != 0 {
- filters["$gt"] = totalAdEposedCount.Gt
- }
- if totalAdEposedCount.Gte != 0 {
- filters["$gte"] = totalAdEposedCount.Gte
- }
- if totalAdEposedCount.Lte != 0 {
- filters["$lte"] = totalAdEposedCount.Lte
- }
- if totalAdEposedCount.Lt != 0 {
- filters["$lt"] = totalAdEposedCount.Lt
- }
- if totalAdEposedCount.Ne != 0 {
- filters["$ne"] = totalAdEposedCount.Ne
- }
- if len(filters) > 0 {
- filter["createTime"] = filters
- }
- }
- if form.StartNum != nil {
- marsh, _ := json.Marshal(form.StartNum)
- var totalAdEposedCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdEposedCount)
- filters := bson.M{}
- if totalAdEposedCount.Gt != 0 {
- filters["$gt"] = totalAdEposedCount.Gt
- }
- if totalAdEposedCount.Gte != 0 {
- filters["$gte"] = totalAdEposedCount.Gte
- }
- if totalAdEposedCount.Lte != 0 {
- filters["$lte"] = totalAdEposedCount.Lte
- }
- if totalAdEposedCount.Lt != 0 {
- filters["$lt"] = totalAdEposedCount.Lt
- }
- if totalAdEposedCount.Ne != 0 {
- filters["$ne"] = totalAdEposedCount.Ne
- }
- if len(filters) > 0 {
- filter["startNum"] = filters
- }
- }
- if form.Revenue != nil {
- marsh, _ := json.Marshal(form.Revenue)
- var totalAdEposedCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdEposedCount)
- filters := bson.M{}
- if totalAdEposedCount.Gt != 0 {
- filters["$gt"] = totalAdEposedCount.Gt
- }
- if totalAdEposedCount.Gte != 0 {
- filters["$gte"] = totalAdEposedCount.Gte
- }
- if totalAdEposedCount.Lte != 0 {
- filters["$lte"] = totalAdEposedCount.Lte
- }
- if totalAdEposedCount.Lt != 0 {
- filters["$lt"] = totalAdEposedCount.Lt
- }
- if totalAdEposedCount.Ne != 0 {
- filters["$ne"] = totalAdEposedCount.Ne
- }
- if len(filters) > 0 {
- filter["revenue"] = filters
- }
- }
- if form.Duration != nil {
- marsh, _ := json.Marshal(form.Duration)
- var totalAdEposedCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdEposedCount)
- filters := bson.M{}
- if totalAdEposedCount.Gt != 0 {
- filters["$gt"] = totalAdEposedCount.Gt
- }
- if totalAdEposedCount.Gte != 0 {
- filters["$gte"] = totalAdEposedCount.Gte
- }
- if totalAdEposedCount.Lte != 0 {
- filters["$lte"] = totalAdEposedCount.Lte
- }
- if totalAdEposedCount.Lt != 0 {
- filters["$lt"] = totalAdEposedCount.Lt
- }
- if totalAdEposedCount.Ne != 0 {
- filters["$ne"] = totalAdEposedCount.Ne
- }
- if len(filters) > 0 {
- filter["duration"] = filters
- }
- }
- if form.ReqCount != nil {
- marsh, _ := json.Marshal(form.ReqCount)
- var totalAdEposedCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdEposedCount)
- filters := bson.M{}
- if totalAdEposedCount.Gt != 0 {
- filters["$gt"] = totalAdEposedCount.Gt
- }
- if totalAdEposedCount.Gte != 0 {
- filters["$gte"] = totalAdEposedCount.Gte
- }
- if totalAdEposedCount.Lte != 0 {
- filters["$lte"] = totalAdEposedCount.Lte
- }
- if totalAdEposedCount.Lt != 0 {
- filters["$lt"] = totalAdEposedCount.Lt
- }
- if totalAdEposedCount.Ne != 0 {
- filters["$ne"] = totalAdEposedCount.Ne
- }
- if len(filters) > 0 {
- filter["reqCount"] = filters
- }
- }
- if form.ExpCount != nil {
- marsh, _ := json.Marshal(form.ExpCount)
- var totalAdEposedCount BehaviorFilter
- json.Unmarshal(marsh, &totalAdEposedCount)
- filters := bson.M{}
- if totalAdEposedCount.Gt != 0 {
- filters["$gt"] = totalAdEposedCount.Gt
- }
- if totalAdEposedCount.Gte != 0 {
- filters["$gte"] = totalAdEposedCount.Gte
- }
- if totalAdEposedCount.Lte != 0 {
- filters["$lte"] = totalAdEposedCount.Lte
- }
- if totalAdEposedCount.Lt != 0 {
- filters["$lt"] = totalAdEposedCount.Lt
- }
- if totalAdEposedCount.Ne != 0 {
- filters["$ne"] = totalAdEposedCount.Ne
- }
- if len(filters) > 0 {
- filter["expCount"] = filters
- }
- }
- option := options.Find()
- option.SetLimit(int64(form.Limit))
- option.SetSkip(int64(form.Offset))
- cur, err := collection.Find(ctx, filter, option)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- count, err := collection.CountDocuments(ctx, filter)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- var data []AdRelated
- err = cur.All(ctx, &data)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- response.Success(c, gin.H{
- "data": data,
- "count": count,
- })
- }
- // ConversionCondition 转化条件
- type ConversionCondition struct {
- Id string `bson:"_id" json:"id"`
- Gid string `bson:"gid" json:"gid"`
- Pid *big.Int `bson:"pid" json:"pid"`
- Aid *big.Int `bson:"aid" json:"aid"`
- Type string `bson:"type" json:"type"`
- StartNum int `bson:"start_num" json:"start_num"` //启动次数
- EstimatedRevenue float32 `bson:"revenue" json:"revenue"` //当日预估收益
- Duration int64 `bson:"duration" json:"duration"` //当日在线时长
- ReqRewardedAd int `bson:"req_count" json:"req_count"` //当日的激励视频广告请求次数
- ExpRewardedAd int `bson:"exp_count" json:"exp_count"` //当日的激励视频广告曝光次数
- }
- func SetGameCondition(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- Pid string `form:"pid" json:"pid" binding:""`
- Aid string `form:"aid" json:"aid" binding:""`
- Type string `form:"type" json:"type" binding:"required"`
- StartNum int `form:"start_num" json:"start_num" binding:""`
- EstimatedRevenue float32 `form:"revenue" json:"revenue" binding:""`
- Duration int64 `form:"duration" json:"duration" binding:""`
- ReqRewardedAd int `form:"req_count" json:"req_count" binding:""`
- ExpRewardedAd int `form:"exp_count" json:"exp_count" binding:""`
- }{})
- id := fmt.Sprintf("%s|%s|%s|%s", form.Gid, form.Pid, form.Aid, form.Type)
- PidInt := new(big.Int)
- PidInt.SetString(form.Pid, 10)
- AidInt := new(big.Int)
- AidInt.SetString(form.Aid, 10)
- collection := global.App.MongoDB.Database("chunhao").Collection("conversionCondition")
- filter := bson.M{"_id": id}
- var conversionCondition ConversionCondition
- err := collection.FindOne(context.TODO(), filter).Decode(&conversionCondition)
- //if err != nil {
- // response.Fail(c, 1002, err.Error())
- // return
- //}
- if conversionCondition.Id != "" {
- //存在,更新
- update := bson.M{
- "$set": struct {
- StartNum int `bson:"start_num"` //启动次数
- EstimatedRevenue float32 `bson:"revenue"` //当日预估收益
- Duration int64 `bson:"duration"` //当日在线时长
- ReqRewardedAd int `bson:"req_count"` //当日的激励视频广告请求次数
- ExpRewardedAd int `bson:"exp_count"` //当日的激励视频广告曝光次数
- }{
- StartNum: form.StartNum,
- EstimatedRevenue: form.EstimatedRevenue,
- Duration: form.Duration,
- ReqRewardedAd: form.ReqRewardedAd,
- ExpRewardedAd: form.ExpRewardedAd,
- },
- }
- _, err = collection.UpdateOne(context.TODO(), filter, update)
- if err != nil {
- response.Fail(c, 1003, err.Error())
- return
- }
- } else {
- //不存在,新增
- insert := ConversionCondition{
- Id: id,
- Pid: PidInt,
- Aid: AidInt,
- Gid: form.Gid,
- Type: form.Type,
- StartNum: form.StartNum,
- EstimatedRevenue: form.EstimatedRevenue,
- Duration: form.Duration,
- ReqRewardedAd: form.ReqRewardedAd,
- ExpRewardedAd: form.ExpRewardedAd,
- }
- _, err = collection.InsertOne(context.TODO(), insert)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- }
- response.Success(c, gin.H{})
- }
- func GameConditionList(c *gin.Context) {
- form := request.Check(c, &struct {
- Gid string `form:"gid" json:"gid" binding:"required"`
- Pid int64 `form:"pid" json:"pid" binding:""`
- Aid int64 `form:"aid" json:"aid" binding:""`
- Type string `form:"type" json:"type" binding:""`
- Offset int `form:"offset" json:"offset" binding:""`
- Limit int `form:"limit" json:"limit" binding:"required"`
- }{})
- collection := global.App.MongoDB.Database("chunhao").Collection("conversionCondition")
- filter := bson.M{"gid": form.Gid, "type": form.Type}
- if form.Type != "" {
- filter["type"] = form.Type
- }
- if form.Pid != 0 {
- filter["pid"] = form.Pid
- }
- if form.Aid != 0 {
- filter["aid"] = form.Aid
- }
- ctx := context.Background()
- option := options.Find()
- option.SetLimit(int64(form.Limit))
- option.SetSkip(int64(form.Offset))
- cur, err := collection.Find(ctx, filter, option)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- count, err := collection.CountDocuments(ctx, filter)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- var data []ConversionCondition
- err = cur.All(ctx, &data)
- if err != nil {
- response.Fail(c, 1001, err.Error())
- return
- }
- response.Success(c, gin.H{
- "data": data,
- "count": count,
- })
- }
|