| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136 |
- 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"
- "strconv"
- "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 string `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"] = bson.M{"$regex": form.Pid}
- }
- if form.Aid != "" {
- filter["adData.aid"] = form.Aid
- }
- if form.Cid != "" {
- filter["adData.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 int64 `bson:"pid" json:"pid"`
- Aid int64 `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 int64 `form:"pid" json:"pid" binding:""`
- Aid int64 `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, strconv.Itoa(int(form.Pid)), strconv.Itoa(int(form.Aid)), form.Type)
- 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: form.Pid,
- Aid: form.Aid,
- 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,
- })
- }
|