userBehavior.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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 != "" && form.TotalAdEposedCount == "" {
  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 != "" && form.TotalAdReqCount == "" {
  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 form.TotalAdEposedCount != "" && form.TotalAdReqCount != "" {
  1199. response.Fail(c, 1002, "筛选条件无效")
  1200. return
  1201. }
  1202. if len(filterList) == 0 {
  1203. filterList = filterListTotalDuration
  1204. }
  1205. for k, filterC := range filterList {
  1206. count, _ := collection.CountDocuments(ctx, filterC)
  1207. data = append(data, resData{
  1208. Name: k,
  1209. Count: int(count),
  1210. })
  1211. }
  1212. response.Success(c, gin.H{
  1213. "data": data,
  1214. })
  1215. }