userBehavior.go 32 KB

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