userBehavior.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. package v1
  2. import (
  3. "context"
  4. "designs/app/common/request"
  5. "designs/app/common/response"
  6. "designs/global"
  7. "designs/service"
  8. "designs/utils"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "go.mongodb.org/mongo-driver/v2/bson"
  13. "go.mongodb.org/mongo-driver/v2/mongo/options"
  14. "math"
  15. "math/big"
  16. "time"
  17. )
  18. // 总览
  19. func Summary(c *gin.Context) {
  20. form := request.Check(c, &struct {
  21. Gid string `form:"gid" json:"gid" binding:"required"`
  22. Pf string `form:"pf" json:"pf" binding:"required"`
  23. }{})
  24. //查询用户总数
  25. var userCount int64
  26. err := global.App.DB.Table("user").
  27. Where("gid", form.Gid).
  28. Where("pf", form.Pf).
  29. Count(&userCount).Error
  30. if err != nil {
  31. response.Fail(c, 1001, err.Error())
  32. return
  33. }
  34. //查询近七日活跃总数
  35. now := time.Now()
  36. sevenDayAgo := now.AddDate(0, 0, -7)
  37. thirtyDayAgo := now.AddDate(0, 0, -30)
  38. var activeUserCount7 int64
  39. err = global.App.DB.Table("user_login").
  40. Where("gid", form.Gid).
  41. Where("pf", form.Pf).
  42. Where("loginTime", ">=", sevenDayAgo).
  43. Where("loginTime", "<=", now).
  44. Distinct("userId").
  45. Count(&activeUserCount7).Error
  46. if err != nil {
  47. response.Fail(c, 1001, err.Error())
  48. return
  49. }
  50. var activeUserCount30 int64
  51. err = global.App.DB.Table("user_login").
  52. Where("gid", form.Gid).
  53. Where("pf", form.Pf).
  54. Where("loginTime", ">=", thirtyDayAgo).
  55. Where("loginTime", "<=", now).
  56. Distinct("userId").
  57. Count(&activeUserCount30).Error
  58. if err != nil {
  59. response.Fail(c, 1001, err.Error())
  60. return
  61. }
  62. //从redis中读取缓存
  63. var avgTimeString string
  64. avgTimeKey := fmt.Sprintf("%s|%s|avgTime", form.Gid, form.Pf)
  65. avgTimeString, _ = global.App.Redis.Get(context.Background(), avgTimeKey).Result()
  66. if avgTimeString == "" {
  67. //查询 近7日单设备日均使用时长
  68. res, err := service.UserOnlineSummary(form.Gid, form.Pf, "", sevenDayAgo.Format("2006-01-02 15:04:05"), now.Format("2006-01-02 15:04:05"))
  69. if err != nil {
  70. response.Fail(c, 1001, err.Error())
  71. return
  72. }
  73. var avgTime int
  74. for _, v := range res {
  75. avgTime = avgTime + int(v)
  76. }
  77. if avgTime != 0 {
  78. avgTime = int(math.Round(float64(avgTime / len(res))))
  79. avgTimeString = utils.TimeStampToMDS(avgTime)
  80. } else {
  81. avgTimeString = "00.00"
  82. }
  83. global.App.Redis.Set(context.Background(), avgTimeKey, avgTimeString, time.Second*3000)
  84. }
  85. response.Success(c, gin.H{
  86. "data": map[string]interface{}{
  87. "userCount": userCount,
  88. "activeUserCount7": activeUserCount7,
  89. "activeUserCount30": activeUserCount30,
  90. "activeUserCount7Time": avgTimeString,
  91. },
  92. })
  93. }
  94. // 时段分布
  95. func TimeDistributionData(c *gin.Context) {
  96. form := request.Check(c, &struct {
  97. Gid string `form:"gid" json:"gid" binding:"required"`
  98. Pf string `form:"pf" json:"pf" binding:"required"`
  99. Type int `form:"type" json:"type" binding:"required"`
  100. }{})
  101. var data interface{}
  102. if form.Type == 1 {
  103. //新增用户
  104. todayTimeDistribution, yesterdayTimeDistribution, yesterdayCount, todayCount, yesterdayThisTimeCount, err := service.GetRegisterTimeDistribution(form.Pf, form.Gid)
  105. if err != nil {
  106. response.Fail(c, 1001, err.Error())
  107. return
  108. }
  109. data = map[string]interface{}{
  110. "today": todayTimeDistribution,
  111. "yesterday": yesterdayTimeDistribution,
  112. "yesterdayCount": yesterdayCount,
  113. "yesterdayThisTimeCount": yesterdayThisTimeCount,
  114. "todayCount": todayCount,
  115. }
  116. } else if form.Type == 2 {
  117. //活跃设备
  118. todayTimeDistribution, yesterdayTimeDistribution, yesterdayCount, todayCount, yesterdayThisTimeCount, err := service.GetActiveTimeDistribution(form.Pf, form.Gid)
  119. if err != nil {
  120. response.Fail(c, 1001, err.Error())
  121. return
  122. }
  123. data = map[string]interface{}{
  124. "today": todayTimeDistribution,
  125. "yesterday": yesterdayTimeDistribution,
  126. "yesterdayCount": yesterdayCount,
  127. "yesterdayThisTimeCount": yesterdayThisTimeCount,
  128. "todayCount": todayCount,
  129. }
  130. } else if form.Type == 3 {
  131. //启动次数
  132. todayTimeDistribution, yesterdayTimeDistribution, yesterdayCount, todayCount, yesterdayThisTimeCount, err := service.GetActionDistribution(form.Pf, form.Gid)
  133. if err != nil {
  134. response.Fail(c, 1001, err.Error())
  135. return
  136. }
  137. data = map[string]interface{}{
  138. "today": todayTimeDistribution,
  139. "yesterday": yesterdayTimeDistribution,
  140. "yesterdayCount": yesterdayCount,
  141. "yesterdayThisTimeCount": yesterdayThisTimeCount,
  142. "todayCount": todayCount,
  143. }
  144. } else {
  145. response.Fail(c, 1003, "type错误")
  146. return
  147. }
  148. response.Success(c, gin.H{
  149. "data": data,
  150. })
  151. }
  152. // 30日趋势
  153. func MouthDistributionData(c *gin.Context) {
  154. form := request.Check(c, &struct {
  155. Gid string `form:"gid" json:"gid" binding:"required"`
  156. Pf string `form:"pf" json:"pf" binding:"required"`
  157. Type int `form:"type" json:"type" binding:"required"`
  158. }{})
  159. now := time.Now()
  160. EndTime := now.AddDate(0, 0, 1).Format("2006-01-02")
  161. StartTime := now.AddDate(0, 0, -29).Format("2006-01-02")
  162. data := make(map[string]interface{})
  163. if form.Type == 1 {
  164. //查询新增设备趋势图
  165. TimeDistribution, count, avg, err := service.GetRegisterDayDistribution(form.Pf, form.Gid, StartTime, EndTime)
  166. if err != nil {
  167. response.Fail(c, 1001, err.Error())
  168. return
  169. }
  170. data["timeDistribution"] = TimeDistribution
  171. data["count"] = count
  172. data["avg"] = avg
  173. } else if form.Type == 2 {
  174. //查询活跃用户趋势图
  175. TimeDistribution, count, avg, err := service.GetActiveDayDistribution(form.Pf, form.Gid, StartTime, EndTime)
  176. if err != nil {
  177. response.Fail(c, 1001, err.Error())
  178. return
  179. }
  180. data["timeDistribution"] = TimeDistribution
  181. data["count"] = count
  182. data["avg"] = avg
  183. } else if form.Type == 3 {
  184. //查询启动次数
  185. TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, StartTime, EndTime)
  186. if err != nil {
  187. response.Fail(c, 1001, err.Error())
  188. return
  189. }
  190. data["timeDistribution"] = TimeDistribution
  191. data["count"] = count
  192. data["avg"] = avg
  193. } else if form.Type == 4 {
  194. //查询单用户使用时长
  195. TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, StartTime, EndTime)
  196. if err != nil {
  197. response.Fail(c, 1001, err.Error())
  198. return
  199. }
  200. data["timeDistribution"] = TimeDistribution
  201. data["count"] = count
  202. data["avg"] = avg
  203. } else if form.Type == 5 {
  204. //查询用户留存率
  205. //todo
  206. } else {
  207. response.Fail(c, 1003, "type错误")
  208. return
  209. }
  210. response.Success(c, gin.H{
  211. "data": data,
  212. })
  213. }
  214. // 用户趋势 总览
  215. func UserTrendsOverview(c *gin.Context) {
  216. form := request.Check(c, &struct {
  217. Gid string `form:"gid" json:"gid" binding:"required"`
  218. Pf string `form:"pf" json:"pf" binding:"required"`
  219. StartTime string `form:"startTime" json:"startTime" binding:"required"`
  220. EndTime string `form:"endTime" json:"endTime" binding:"required"`
  221. }{})
  222. //查询用户新增
  223. var registerCount int64
  224. err := global.App.DB.Table("user").
  225. Where("gid", form.Gid).
  226. Where("pf", form.Pf).
  227. Where("createdAt", ">=", form.StartTime).
  228. Where("createdAt", "<=", form.EndTime).
  229. Count(&registerCount).Error
  230. if err != nil {
  231. response.Fail(c, 1001, err.Error())
  232. return
  233. }
  234. //查询活跃设备
  235. var activeCount int64
  236. err = global.App.DB.Table("user_online").
  237. Where("gid", form.Gid).
  238. Where("pf", form.Pf).
  239. Where("logTime", ">=", form.StartTime).
  240. Where("logTime", "<=", form.EndTime).
  241. Distinct("userId").Count(&activeCount).Error
  242. if err != nil {
  243. response.Fail(c, 1001, err.Error())
  244. return
  245. }
  246. //查询启动次数
  247. var loginCount int64
  248. err = global.App.DB.Table("user_login").
  249. Where("gid", form.Gid).
  250. Where("pf", form.Pf).
  251. Where("loginTime", ">=", form.StartTime).
  252. Where("loginTime", "<=", form.EndTime).
  253. Count(&loginCount).Error
  254. if err != nil {
  255. response.Fail(c, 1001, err.Error())
  256. return
  257. }
  258. //查询平均启动时长
  259. //查询活跃用户月趋势图
  260. _, _, activeTime, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  261. if err != nil {
  262. response.Fail(c, 1001, err.Error())
  263. return
  264. }
  265. //查询 DAU/MAU
  266. //todo 查询方式需要更新
  267. dauMau := 0.3
  268. response.Success(c, gin.H{
  269. "data": map[string]interface{}{
  270. "registerCount": registerCount,
  271. "activeCount": activeCount,
  272. "loginCount": loginCount,
  273. "activeTime": activeTime,
  274. "dauMau": dauMau,
  275. },
  276. })
  277. }
  278. // 数据趋势
  279. func DataTrades(c *gin.Context) {
  280. form := request.Check(c, &struct {
  281. Gid string `form:"gid" json:"gid" binding:"required"`
  282. Pf string `form:"pf" json:"pf" binding:"required"`
  283. StartTime string `form:"startTime" json:"startTime" binding:"required"`
  284. EndTime string `form:"endTime" json:"endTime" binding:"required"`
  285. Type int `form:"type" json:"type" binding:"required"`
  286. }{})
  287. data := make(map[string]interface{})
  288. form.EndTime = form.EndTime + " 23:59:59"
  289. if form.Type == 1 {
  290. //查询新增设备趋势图
  291. TimeDistribution, count, avg, err := service.GetRegisterDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  292. if err != nil {
  293. response.Fail(c, 1001, err.Error())
  294. return
  295. }
  296. data["imeDistribution"] = TimeDistribution
  297. data["count"] = count
  298. data["avg"] = avg
  299. } else if form.Type == 2 {
  300. //查询活跃用户趋势图
  301. TimeDistribution, count, avg, err := service.GetActiveDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  302. if err != nil {
  303. response.Fail(c, 1001, err.Error())
  304. return
  305. }
  306. data["imeDistribution"] = TimeDistribution
  307. data["count"] = count
  308. data["avg"] = avg
  309. } else if form.Type == 3 {
  310. //查询活跃用户周趋势图
  311. TimeDistribution, count, avg, err := service.GetActiveWeekDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  312. if err != nil {
  313. response.Fail(c, 1001, err.Error())
  314. return
  315. }
  316. data["imeDistribution"] = TimeDistribution
  317. data["count"] = count
  318. data["avg"] = avg
  319. } else if form.Type == 4 {
  320. //查询活跃用户月趋势图
  321. TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  322. if err != nil {
  323. response.Fail(c, 1001, err.Error())
  324. return
  325. }
  326. data["imeDistribution"] = TimeDistribution
  327. data["count"] = count
  328. data["avg"] = avg
  329. } else if form.Type == 5 {
  330. //查询启动次数
  331. TimeDistribution, count, avg, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  332. if err != nil {
  333. response.Fail(c, 1001, err.Error())
  334. return
  335. }
  336. data["imeDistribution"] = TimeDistribution
  337. data["count"] = count
  338. data["avg"] = avg
  339. } else if form.Type == 6 {
  340. //查询平均启动时间
  341. TimeDistribution, count, avg, err := service.UserOnlineSummaryByDay(form.Gid, form.Pf, form.StartTime, form.EndTime)
  342. if err != nil {
  343. response.Fail(c, 1001, err.Error())
  344. return
  345. }
  346. data["imeDistribution"] = TimeDistribution
  347. data["count"] = count
  348. data["avg"] = avg
  349. } else {
  350. response.Fail(c, 1003, "type 错误")
  351. return
  352. }
  353. response.Success(c, gin.H{
  354. "data": data,
  355. })
  356. }
  357. // 数据趋势的整合
  358. func DataTradesDetail(c *gin.Context) {
  359. form := request.Check(c, &struct {
  360. Gid string `form:"gid" json:"gid" binding:"required"`
  361. Pf string `form:"pf" json:"pf" binding:"required"`
  362. StartTime string `form:"startTime" json:"startTime" binding:"required"`
  363. EndTime string `form:"endTime" json:"endTime" binding:"required"`
  364. }{})
  365. form.EndTime = form.EndTime + " 23:59:59"
  366. type dayData struct {
  367. NewUser int `json:"newUser"`
  368. ActiveUser int `json:"activeUser"`
  369. ActiveUserWeek int `json:"activeUserWeek"`
  370. ActiveUserMouth int `json:"activeUserMouth"`
  371. ActiveStart int `json:"activeStart"`
  372. AvgTime int `json:"avgTime"`
  373. }
  374. var data = make(map[string]dayData)
  375. //查询新增设备趋势图
  376. NewUser, _, _, err := service.GetRegisterDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  377. if err != nil {
  378. response.Fail(c, 1001, err.Error())
  379. return
  380. }
  381. //查询活跃用户趋势图
  382. ActiveUser, _, _, err := service.GetActiveDayDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  383. if err != nil {
  384. response.Fail(c, 1001, err.Error())
  385. return
  386. }
  387. //查询活跃用户周趋势图
  388. ActiveUserWeek, _, _, err := service.GetActiveWeekDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  389. if err != nil {
  390. response.Fail(c, 1001, err.Error())
  391. return
  392. }
  393. //查询活跃用户月趋势图
  394. ActiveUserMouth, _, _, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  395. if err != nil {
  396. response.Fail(c, 1001, err.Error())
  397. return
  398. }
  399. //查询启动次数
  400. ActiveStart, _, _, err := service.GetActiveMouthDistribution(form.Pf, form.Gid, form.StartTime, form.EndTime)
  401. if err != nil {
  402. response.Fail(c, 1001, err.Error())
  403. return
  404. }
  405. //查询平均启动时间
  406. AvgTime, _, _, err := service.UserOnlineSummaryByDay(form.Gid, form.Pf, form.StartTime, form.EndTime)
  407. if err != nil {
  408. response.Fail(c, 1001, err.Error())
  409. return
  410. }
  411. for k := range AvgTime {
  412. data[k] = dayData{
  413. NewUser: NewUser[k],
  414. ActiveUser: ActiveUser[k],
  415. ActiveUserWeek: ActiveUserWeek[k],
  416. ActiveUserMouth: ActiveUserMouth[k],
  417. ActiveStart: ActiveStart[k],
  418. AvgTime: AvgTime[k],
  419. }
  420. }
  421. response.Success(c, gin.H{
  422. "data": data,
  423. })
  424. }
  425. func RemainDataBydDay(c *gin.Context) {
  426. form := request.Check(c, &struct {
  427. Gid string `form:"gid" json:"gid" binding:"required"`
  428. Pf string `form:"pf" json:"pf" binding:"required"`
  429. StartTime string `form:"startTime" json:"startTime" binding:"required"`
  430. EndTime string `form:"endTime" json:"endTime" binding:"required"`
  431. Type int `form:"type" json:"type" binding:"required"`
  432. }{})
  433. data, err := service.RemainDataBydDay(form.Type, form.Pf, form.Gid, form.StartTime, form.EndTime)
  434. if err != nil {
  435. response.Fail(c, 1001, err.Error())
  436. return
  437. }
  438. response.Success(c, gin.H{
  439. "data": data,
  440. })
  441. }
  442. type AdData struct {
  443. Pid string `json:"pid"`
  444. Aid string `json:"aid"`
  445. Cid string `json:"cid"`
  446. ReportUrl string `json:"reportUrl"`
  447. Reported bool `json:"reported"`
  448. Duration int64 `json:"duration"`
  449. AdReqCount uint8 `json:"adReqCount"`
  450. AdEposedcount uint8 `json:"adEposedcount"`
  451. CreateTime int `json:"createTime"`
  452. }
  453. type UserBehavior struct {
  454. Id string `bson:"_id,omitempty"`
  455. Gid string `bson:"gid" json:"gid"`
  456. Pf string `bson:"pf" json:"pf"`
  457. OpenId string `bson:"openId" json:"openId"`
  458. RelatedAid int64 `bson:"relatedAid" json:"relatedAid"` //目前关联的aid
  459. TotalDuration int `bson:"totalDuration" json:"totalDuration"`
  460. TotalAdReqCount int `bson:"totalAdReqCount" json:"totalAdReqCount"`
  461. TotalAdEposedCount int `bson:"totalAdEposedCount" json:"totalAdEposedCount"`
  462. CreateDate string `bson:"createDate" json:"createDate"`
  463. CreateTime int `json:"createTime" bson:"createTime"`
  464. StartNum int `bson:"startNum" json:"startNum"`
  465. ActiveStatus bool `bson:"activeStatus" json:"activeStatus"` //激活状态
  466. ConversionStatus bool `bson:"conversionStatus" json:"conversionStatus"` //转化状态
  467. RemainData map[string]string `json:"remainData" bson:"remainData"` //留存数据
  468. }
  469. type AdRelated struct {
  470. Id string `bson:"_id" json:"_id"`
  471. UserId string `bson:"userId" json:"userId"`
  472. Aid int64 `json:"aid" bson:"aid"` //广告的推广计划id,即广告ID,广告平台配置落地页参数
  473. Cid string `json:"cid" bson:"cid"` //openid的用户点击aid广告进入游戏时的唯一标识,广告平台提供
  474. Pid int64 `json:"pid" bson:"pid"` //广告的项目id(仅巨量引擎存在,腾讯广告时不存在该值),广告平台配置落地页参数
  475. CreateTime int64 `bson:"create_time" json:"createTime"` //当前计划的创建时间
  476. StartNum int `bson:"startNum" json:"startNum"` //启动次数
  477. Revenue float32 `bson:"revenue" json:"revenue"` //当日预估收益
  478. Duration int64 `bson:"duration" json:"duration"` //当日在线时长
  479. ReqCount int `bson:"req_count" json:"req_count"` //当日的激励视频广告请求次数
  480. ExpCount int `bson:"exp_count" json:"exp_count"` //当日的激励视频广告曝光次数
  481. }
  482. type BehaviorFilter struct {
  483. Gt int `json:"gt"`
  484. Gte int `json:"gte"`
  485. Lte int `json:"lte"`
  486. Lt int `json:"lt"`
  487. Ne int `json:"ne"`
  488. //$gt 大于
  489. //$gte:大于或等于
  490. //$lt:小于
  491. //$lte:小于或等于
  492. //$ne:不等于
  493. }
  494. func BehaviorList(c *gin.Context) {
  495. form := request.Check(c, &struct {
  496. Gid string `form:"gid" json:"gid" binding:"required"`
  497. Pf string `form:"pf" json:"pf" binding:"required"`
  498. OpenId string `form:"openId" json:"openId" binding:""`
  499. Offset int `form:"offset" json:"offset" binding:""`
  500. Limit int `form:"limit" json:"limit" binding:""`
  501. ActiveStatus string `form:"activeStatus" json:"activeStatus" binding:""` //all true false
  502. ConversionStatus string `form:"conversionStatus" json:"conversionStatus" binding:""` //all true false
  503. AdFromCount interface{} `form:"adFromCount" json:"adFromCount" binding:""`
  504. TotalDuration interface{} `form:"totalDuration" json:"totalDuration" binding:""`
  505. TotalAdReqCount interface{} `form:"totalAdReqCount" json:"totalAdReqCount" binding:""`
  506. TotalAdEposedCount interface{} `form:"totalAdEposedCount" json:"totalAdEposedCount" binding:""`
  507. CreateTime interface{} `form:"createTime" json:"createTime" binding:""`
  508. StartNum interface{} `form:"startNum" json:"startNum" binding:""`
  509. }{})
  510. collection := global.App.MongoDB.Database("chunhao").Collection("userBehavior")
  511. ctx := context.Background()
  512. filter := bson.M{"gid": form.Gid}
  513. if form.Pf != "" {
  514. filter["pf"] = form.Pf
  515. }
  516. if form.OpenId != "" {
  517. filter["openId"] = bson.M{"$regex": form.OpenId}
  518. }
  519. if form.ActiveStatus == "true" {
  520. filter["activeStatus"] = true
  521. } else if form.ActiveStatus == "false" {
  522. filter["activeStatus"] = false
  523. }
  524. if form.ActiveStatus == "true" {
  525. filter["activeStatus"] = true
  526. } else if form.ActiveStatus == "false" {
  527. filter["activeStatus"] = false
  528. }
  529. if form.AdFromCount != nil {
  530. fmt.Println(form.AdFromCount)
  531. marsh, _ := json.Marshal(form.AdFromCount)
  532. var adFromCount BehaviorFilter
  533. json.Unmarshal(marsh, &adFromCount)
  534. filters := bson.M{}
  535. if adFromCount.Gt != 0 {
  536. filters["$gt"] = adFromCount.Gt
  537. }
  538. if adFromCount.Gte != 0 {
  539. filters["$gte"] = adFromCount.Gte
  540. }
  541. if adFromCount.Lte != 0 {
  542. filters["$lte"] = adFromCount.Lte
  543. }
  544. if adFromCount.Lt != 0 {
  545. filters["$lt"] = adFromCount.Lt
  546. }
  547. if adFromCount.Ne != 0 {
  548. filters["$ne"] = adFromCount.Ne
  549. }
  550. if len(filters) > 0 {
  551. filter["adFromCount"] = filters
  552. }
  553. }
  554. if form.TotalAdReqCount != nil {
  555. marsh, _ := json.Marshal(form.TotalAdReqCount)
  556. var totalAdReqCount BehaviorFilter
  557. json.Unmarshal(marsh, &totalAdReqCount)
  558. filters := bson.M{}
  559. if totalAdReqCount.Gt != 0 {
  560. filters["$gt"] = totalAdReqCount.Gt
  561. }
  562. if totalAdReqCount.Gte != 0 {
  563. filters["$gte"] = totalAdReqCount.Gte
  564. }
  565. if totalAdReqCount.Lte != 0 {
  566. filters["$lte"] = totalAdReqCount.Lte
  567. }
  568. if totalAdReqCount.Lt != 0 {
  569. filters["$lt"] = totalAdReqCount.Lt
  570. }
  571. if totalAdReqCount.Ne != 0 {
  572. filters["$ne"] = totalAdReqCount.Ne
  573. }
  574. if len(filters) > 0 {
  575. filter["totalAdReqCount"] = filters
  576. }
  577. }
  578. if form.TotalAdEposedCount != nil {
  579. marsh, _ := json.Marshal(form.TotalAdEposedCount)
  580. var totalAdEposedCount BehaviorFilter
  581. json.Unmarshal(marsh, &totalAdEposedCount)
  582. filters := bson.M{}
  583. if totalAdEposedCount.Gt != 0 {
  584. filters["$gt"] = totalAdEposedCount.Gt
  585. }
  586. if totalAdEposedCount.Gte != 0 {
  587. filters["$gte"] = totalAdEposedCount.Gte
  588. }
  589. if totalAdEposedCount.Lte != 0 {
  590. filters["$lte"] = totalAdEposedCount.Lte
  591. }
  592. if totalAdEposedCount.Lt != 0 {
  593. filters["$lt"] = totalAdEposedCount.Lt
  594. }
  595. if totalAdEposedCount.Ne != 0 {
  596. filters["$ne"] = totalAdEposedCount.Ne
  597. }
  598. if len(filters) > 0 {
  599. filter["totalAdEposedCount"] = filters
  600. }
  601. }
  602. if form.CreateTime != nil {
  603. marsh, _ := json.Marshal(form.CreateTime)
  604. var totalAdEposedCount BehaviorFilter
  605. json.Unmarshal(marsh, &totalAdEposedCount)
  606. filters := bson.M{}
  607. if totalAdEposedCount.Gt != 0 {
  608. filters["$gt"] = totalAdEposedCount.Gt
  609. }
  610. if totalAdEposedCount.Gte != 0 {
  611. filters["$gte"] = totalAdEposedCount.Gte
  612. }
  613. if totalAdEposedCount.Lte != 0 {
  614. filters["$lte"] = totalAdEposedCount.Lte
  615. }
  616. if totalAdEposedCount.Lt != 0 {
  617. filters["$lt"] = totalAdEposedCount.Lt
  618. }
  619. if totalAdEposedCount.Ne != 0 {
  620. filters["$ne"] = totalAdEposedCount.Ne
  621. }
  622. if len(filters) > 0 {
  623. filter["createTime"] = filters
  624. }
  625. }
  626. if form.StartNum != nil {
  627. marsh, _ := json.Marshal(form.StartNum)
  628. var totalAdEposedCount BehaviorFilter
  629. json.Unmarshal(marsh, &totalAdEposedCount)
  630. filters := bson.M{}
  631. if totalAdEposedCount.Gt != 0 {
  632. filters["$gt"] = totalAdEposedCount.Gt
  633. }
  634. if totalAdEposedCount.Gte != 0 {
  635. filters["$gte"] = totalAdEposedCount.Gte
  636. }
  637. if totalAdEposedCount.Lte != 0 {
  638. filters["$lte"] = totalAdEposedCount.Lte
  639. }
  640. if totalAdEposedCount.Lt != 0 {
  641. filters["$lt"] = totalAdEposedCount.Lt
  642. }
  643. if totalAdEposedCount.Ne != 0 {
  644. filters["$ne"] = totalAdEposedCount.Ne
  645. }
  646. if len(filters) > 0 {
  647. filter["startNum"] = filters
  648. }
  649. }
  650. if form.TotalDuration != nil {
  651. marsh, _ := json.Marshal(form.TotalDuration)
  652. var totalAdEposedCount BehaviorFilter
  653. json.Unmarshal(marsh, &totalAdEposedCount)
  654. filters := bson.M{}
  655. if totalAdEposedCount.Gt != 0 {
  656. filters["$gt"] = totalAdEposedCount.Gt
  657. }
  658. if totalAdEposedCount.Gte != 0 {
  659. filters["$gte"] = totalAdEposedCount.Gte
  660. }
  661. if totalAdEposedCount.Lte != 0 {
  662. filters["$lte"] = totalAdEposedCount.Lte
  663. }
  664. if totalAdEposedCount.Lt != 0 {
  665. filters["$lt"] = totalAdEposedCount.Lt
  666. }
  667. if totalAdEposedCount.Ne != 0 {
  668. filters["$ne"] = totalAdEposedCount.Ne
  669. }
  670. if len(filters) > 0 {
  671. filter["totalDuration"] = filters
  672. }
  673. }
  674. option := options.Find()
  675. option.SetLimit(int64(form.Limit))
  676. option.SetSkip(int64(form.Offset))
  677. cur, err := collection.Find(ctx, filter, option)
  678. if err != nil {
  679. response.Fail(c, 1001, err.Error())
  680. return
  681. }
  682. count, err := collection.CountDocuments(ctx, filter)
  683. if err != nil {
  684. response.Fail(c, 1001, err.Error())
  685. return
  686. }
  687. var data []UserBehavior
  688. err = cur.All(ctx, &data)
  689. if err != nil {
  690. response.Fail(c, 1001, err.Error())
  691. return
  692. }
  693. response.Success(c, gin.H{
  694. "data": data,
  695. "count": count,
  696. })
  697. }
  698. func AdRelatedList(c *gin.Context) {
  699. form := request.Check(c, &struct {
  700. //Gid string `form:"gid" json:"gid" binding:"required"`
  701. //Pf string `form:"pf" json:"pf" binding:"required"`
  702. //OpenId string `form:"search" json:"search" binding:""`
  703. Offset int `form:"offset" json:"offset" binding:""`
  704. Limit int `form:"limit" json:"limit" binding:""`
  705. Pid string `form:"pid" json:"pid" binding:""`
  706. Aid int `form:"aid" json:"aid" binding:""`
  707. Cid string `form:"cid" json:"cid" binding:""`
  708. CreateTime interface{} `form:"createTime" json:"createTime" binding:""`
  709. StartNum interface{} `form:"startNum" json:"startNum" binding:""`
  710. Revenue interface{} `form:"revenue" json:"revenue" binding:""`
  711. Duration interface{} `form:"duration" json:"duration" binding:""`
  712. ReqCount interface{} `form:"reqCount" json:"reqCount" binding:""`
  713. ExpCount interface{} `form:"expCount" json:"expCount" binding:""`
  714. }{})
  715. collection := global.App.MongoDB.Database("chunhao").Collection("adRelated")
  716. ctx := context.Background()
  717. filter := bson.M{}
  718. if form.Pid != "" {
  719. filter["pid"] = form.Pid
  720. }
  721. if form.Aid != 0 {
  722. filter["aid"] = form.Aid
  723. }
  724. if form.Cid != "" {
  725. filter["cid"] = form.Cid
  726. }
  727. if form.CreateTime != nil {
  728. marsh, _ := json.Marshal(form.CreateTime)
  729. var totalAdEposedCount BehaviorFilter
  730. json.Unmarshal(marsh, &totalAdEposedCount)
  731. filters := bson.M{}
  732. if totalAdEposedCount.Gt != 0 {
  733. filters["$gt"] = totalAdEposedCount.Gt
  734. }
  735. if totalAdEposedCount.Gte != 0 {
  736. filters["$gte"] = totalAdEposedCount.Gte
  737. }
  738. if totalAdEposedCount.Lte != 0 {
  739. filters["$lte"] = totalAdEposedCount.Lte
  740. }
  741. if totalAdEposedCount.Lt != 0 {
  742. filters["$lt"] = totalAdEposedCount.Lt
  743. }
  744. if totalAdEposedCount.Ne != 0 {
  745. filters["$ne"] = totalAdEposedCount.Ne
  746. }
  747. if len(filters) > 0 {
  748. filter["createTime"] = filters
  749. }
  750. }
  751. if form.StartNum != nil {
  752. marsh, _ := json.Marshal(form.StartNum)
  753. var totalAdEposedCount BehaviorFilter
  754. json.Unmarshal(marsh, &totalAdEposedCount)
  755. filters := bson.M{}
  756. if totalAdEposedCount.Gt != 0 {
  757. filters["$gt"] = totalAdEposedCount.Gt
  758. }
  759. if totalAdEposedCount.Gte != 0 {
  760. filters["$gte"] = totalAdEposedCount.Gte
  761. }
  762. if totalAdEposedCount.Lte != 0 {
  763. filters["$lte"] = totalAdEposedCount.Lte
  764. }
  765. if totalAdEposedCount.Lt != 0 {
  766. filters["$lt"] = totalAdEposedCount.Lt
  767. }
  768. if totalAdEposedCount.Ne != 0 {
  769. filters["$ne"] = totalAdEposedCount.Ne
  770. }
  771. if len(filters) > 0 {
  772. filter["startNum"] = filters
  773. }
  774. }
  775. if form.Revenue != nil {
  776. marsh, _ := json.Marshal(form.Revenue)
  777. var totalAdEposedCount BehaviorFilter
  778. json.Unmarshal(marsh, &totalAdEposedCount)
  779. filters := bson.M{}
  780. if totalAdEposedCount.Gt != 0 {
  781. filters["$gt"] = totalAdEposedCount.Gt
  782. }
  783. if totalAdEposedCount.Gte != 0 {
  784. filters["$gte"] = totalAdEposedCount.Gte
  785. }
  786. if totalAdEposedCount.Lte != 0 {
  787. filters["$lte"] = totalAdEposedCount.Lte
  788. }
  789. if totalAdEposedCount.Lt != 0 {
  790. filters["$lt"] = totalAdEposedCount.Lt
  791. }
  792. if totalAdEposedCount.Ne != 0 {
  793. filters["$ne"] = totalAdEposedCount.Ne
  794. }
  795. if len(filters) > 0 {
  796. filter["revenue"] = filters
  797. }
  798. }
  799. if form.Duration != nil {
  800. marsh, _ := json.Marshal(form.Duration)
  801. var totalAdEposedCount BehaviorFilter
  802. json.Unmarshal(marsh, &totalAdEposedCount)
  803. filters := bson.M{}
  804. if totalAdEposedCount.Gt != 0 {
  805. filters["$gt"] = totalAdEposedCount.Gt
  806. }
  807. if totalAdEposedCount.Gte != 0 {
  808. filters["$gte"] = totalAdEposedCount.Gte
  809. }
  810. if totalAdEposedCount.Lte != 0 {
  811. filters["$lte"] = totalAdEposedCount.Lte
  812. }
  813. if totalAdEposedCount.Lt != 0 {
  814. filters["$lt"] = totalAdEposedCount.Lt
  815. }
  816. if totalAdEposedCount.Ne != 0 {
  817. filters["$ne"] = totalAdEposedCount.Ne
  818. }
  819. if len(filters) > 0 {
  820. filter["duration"] = filters
  821. }
  822. }
  823. if form.ReqCount != nil {
  824. marsh, _ := json.Marshal(form.ReqCount)
  825. var totalAdEposedCount BehaviorFilter
  826. json.Unmarshal(marsh, &totalAdEposedCount)
  827. filters := bson.M{}
  828. if totalAdEposedCount.Gt != 0 {
  829. filters["$gt"] = totalAdEposedCount.Gt
  830. }
  831. if totalAdEposedCount.Gte != 0 {
  832. filters["$gte"] = totalAdEposedCount.Gte
  833. }
  834. if totalAdEposedCount.Lte != 0 {
  835. filters["$lte"] = totalAdEposedCount.Lte
  836. }
  837. if totalAdEposedCount.Lt != 0 {
  838. filters["$lt"] = totalAdEposedCount.Lt
  839. }
  840. if totalAdEposedCount.Ne != 0 {
  841. filters["$ne"] = totalAdEposedCount.Ne
  842. }
  843. if len(filters) > 0 {
  844. filter["reqCount"] = filters
  845. }
  846. }
  847. if form.ExpCount != nil {
  848. marsh, _ := json.Marshal(form.ExpCount)
  849. var totalAdEposedCount BehaviorFilter
  850. json.Unmarshal(marsh, &totalAdEposedCount)
  851. filters := bson.M{}
  852. if totalAdEposedCount.Gt != 0 {
  853. filters["$gt"] = totalAdEposedCount.Gt
  854. }
  855. if totalAdEposedCount.Gte != 0 {
  856. filters["$gte"] = totalAdEposedCount.Gte
  857. }
  858. if totalAdEposedCount.Lte != 0 {
  859. filters["$lte"] = totalAdEposedCount.Lte
  860. }
  861. if totalAdEposedCount.Lt != 0 {
  862. filters["$lt"] = totalAdEposedCount.Lt
  863. }
  864. if totalAdEposedCount.Ne != 0 {
  865. filters["$ne"] = totalAdEposedCount.Ne
  866. }
  867. if len(filters) > 0 {
  868. filter["expCount"] = filters
  869. }
  870. }
  871. option := options.Find()
  872. option.SetLimit(int64(form.Limit))
  873. option.SetSkip(int64(form.Offset))
  874. cur, err := collection.Find(ctx, filter, option)
  875. if err != nil {
  876. response.Fail(c, 1001, err.Error())
  877. return
  878. }
  879. count, err := collection.CountDocuments(ctx, filter)
  880. if err != nil {
  881. response.Fail(c, 1001, err.Error())
  882. return
  883. }
  884. var data []AdRelated
  885. err = cur.All(ctx, &data)
  886. if err != nil {
  887. response.Fail(c, 1001, err.Error())
  888. return
  889. }
  890. response.Success(c, gin.H{
  891. "data": data,
  892. "count": count,
  893. })
  894. }
  895. // ConversionCondition 转化条件
  896. type ConversionCondition struct {
  897. Id string `bson:"_id" json:"id"`
  898. Gid string `bson:"gid" json:"gid"`
  899. Pid *big.Int `bson:"pid" json:"pid"`
  900. Aid *big.Int `bson:"aid" json:"aid"`
  901. Type string `bson:"type" json:"type"`
  902. StartNum int `bson:"start_num" json:"start_num"` //启动次数
  903. EstimatedRevenue float32 `bson:"revenue" json:"revenue"` //当日预估收益
  904. Duration int64 `bson:"duration" json:"duration"` //当日在线时长
  905. ReqRewardedAd int `bson:"req_count" json:"req_count"` //当日的激励视频广告请求次数
  906. ExpRewardedAd int `bson:"exp_count" json:"exp_count"` //当日的激励视频广告曝光次数
  907. }
  908. func SetGameCondition(c *gin.Context) {
  909. form := request.Check(c, &struct {
  910. Gid string `form:"gid" json:"gid" binding:"required"`
  911. Pid string `form:"pid" json:"pid" binding:""`
  912. Aid string `form:"aid" json:"aid" binding:""`
  913. Type string `form:"type" json:"type" binding:"required"`
  914. StartNum int `form:"start_num" json:"start_num" binding:""`
  915. EstimatedRevenue float32 `form:"revenue" json:"revenue" binding:""`
  916. Duration int64 `form:"duration" json:"duration" binding:""`
  917. ReqRewardedAd int `form:"req_count" json:"req_count" binding:""`
  918. ExpRewardedAd int `form:"exp_count" json:"exp_count" binding:""`
  919. }{})
  920. id := fmt.Sprintf("%s|%s|%s|%s", form.Gid, form.Pid, form.Aid, form.Type)
  921. PidInt := new(big.Int)
  922. PidInt.SetString(form.Pid, 10)
  923. AidInt := new(big.Int)
  924. AidInt.SetString(form.Aid, 10)
  925. collection := global.App.MongoDB.Database("chunhao").Collection("conversionCondition")
  926. filter := bson.M{"_id": id}
  927. var conversionCondition ConversionCondition
  928. err := collection.FindOne(context.TODO(), filter).Decode(&conversionCondition)
  929. //if err != nil {
  930. // response.Fail(c, 1002, err.Error())
  931. // return
  932. //}
  933. if conversionCondition.Id != "" {
  934. //存在,更新
  935. update := bson.M{
  936. "$set": struct {
  937. StartNum int `bson:"start_num"` //启动次数
  938. EstimatedRevenue float32 `bson:"revenue"` //当日预估收益
  939. Duration int64 `bson:"duration"` //当日在线时长
  940. ReqRewardedAd int `bson:"req_count"` //当日的激励视频广告请求次数
  941. ExpRewardedAd int `bson:"exp_count"` //当日的激励视频广告曝光次数
  942. }{
  943. StartNum: form.StartNum,
  944. EstimatedRevenue: form.EstimatedRevenue,
  945. Duration: form.Duration,
  946. ReqRewardedAd: form.ReqRewardedAd,
  947. ExpRewardedAd: form.ExpRewardedAd,
  948. },
  949. }
  950. _, err = collection.UpdateOne(context.TODO(), filter, update)
  951. if err != nil {
  952. response.Fail(c, 1003, err.Error())
  953. return
  954. }
  955. } else {
  956. //不存在,新增
  957. insert := ConversionCondition{
  958. Id: id,
  959. Pid: PidInt,
  960. Aid: AidInt,
  961. Gid: form.Gid,
  962. Type: form.Type,
  963. StartNum: form.StartNum,
  964. EstimatedRevenue: form.EstimatedRevenue,
  965. Duration: form.Duration,
  966. ReqRewardedAd: form.ReqRewardedAd,
  967. ExpRewardedAd: form.ExpRewardedAd,
  968. }
  969. _, err = collection.InsertOne(context.TODO(), insert)
  970. if err != nil {
  971. response.Fail(c, 1001, err.Error())
  972. return
  973. }
  974. }
  975. response.Success(c, gin.H{})
  976. }
  977. func GameConditionList(c *gin.Context) {
  978. form := request.Check(c, &struct {
  979. Gid string `form:"gid" json:"gid" binding:"required"`
  980. Pid int64 `form:"pid" json:"pid" binding:""`
  981. Aid int64 `form:"aid" json:"aid" binding:""`
  982. Type string `form:"type" json:"type" binding:""`
  983. Offset int `form:"offset" json:"offset" binding:""`
  984. Limit int `form:"limit" json:"limit" binding:"required"`
  985. }{})
  986. collection := global.App.MongoDB.Database("chunhao").Collection("conversionCondition")
  987. filter := bson.M{"gid": form.Gid, "type": form.Type}
  988. if form.Type != "" {
  989. filter["type"] = form.Type
  990. }
  991. if form.Pid != 0 {
  992. filter["pid"] = form.Pid
  993. }
  994. if form.Aid != 0 {
  995. filter["aid"] = form.Aid
  996. }
  997. ctx := context.Background()
  998. option := options.Find()
  999. option.SetLimit(int64(form.Limit))
  1000. option.SetSkip(int64(form.Offset))
  1001. cur, err := collection.Find(ctx, filter, option)
  1002. if err != nil {
  1003. response.Fail(c, 1001, err.Error())
  1004. return
  1005. }
  1006. count, err := collection.CountDocuments(ctx, filter)
  1007. if err != nil {
  1008. response.Fail(c, 1001, err.Error())
  1009. return
  1010. }
  1011. var data []ConversionCondition
  1012. err = cur.All(ctx, &data)
  1013. if err != nil {
  1014. response.Fail(c, 1001, err.Error())
  1015. return
  1016. }
  1017. response.Success(c, gin.H{
  1018. "data": data,
  1019. "count": count,
  1020. })
  1021. }