userBehavior.go 44 KB

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