userBehavior.go 39 KB

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