user_view_statistics.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package services
  2. import (
  3. "context"
  4. "hongze/hongze_task/models"
  5. "hongze/hongze_task/utils"
  6. "time"
  7. )
  8. // StatisticsUserView 汇总统计表
  9. func StatisticsUserView(cont context.Context) (err error) {
  10. dateTime := time.Now().AddDate(0, 0, -1)
  11. dateStr := dateTime.Format(utils.FormatDate)
  12. count, err := models.GetUserViewStatisticsCount(dateStr)
  13. if err != nil {
  14. return
  15. }
  16. if count > 0 {
  17. return
  18. }
  19. {
  20. mobileViewMap := make(map[string]*models.UserViewMobileTotalSlice)
  21. list, tmpErr := models.GetCountUserViewHistoryByMobiles(dateStr)
  22. if tmpErr != nil {
  23. err = tmpErr
  24. return
  25. }
  26. for _, v := range list {
  27. mobileViewMap[v.Mobile] = v
  28. }
  29. list, tmpErr = models.GetReportViewMaxTimeByMobiles(dateStr)
  30. if tmpErr != nil {
  31. err = tmpErr
  32. return
  33. }
  34. for _, v := range list {
  35. if userViewStatistics, ok := mobileViewMap[v.Mobile]; ok {
  36. userViewStatistics.Total += v.Total
  37. if v.ViewTime.After(userViewStatistics.ViewTime) {
  38. userViewStatistics.ViewTime = v.ViewTime
  39. }
  40. } else {
  41. mobileViewMap[v.Mobile] = v
  42. }
  43. }
  44. list, tmpErr = models.GetAdvisoryCountUserViewHistoryByMobiles(dateStr)
  45. if tmpErr != nil {
  46. err = tmpErr
  47. return
  48. }
  49. for _, v := range list {
  50. if userViewStatistics, ok := mobileViewMap[v.Mobile]; ok {
  51. userViewStatistics.Total += v.Total
  52. if v.ViewTime.After(userViewStatistics.ViewTime) {
  53. userViewStatistics.ViewTime = v.ViewTime
  54. }
  55. } else {
  56. mobileViewMap[v.Mobile] = v
  57. }
  58. }
  59. if len(mobileViewMap) > 0 {
  60. tmpList := make([]*models.UserViewStatistics, 0)
  61. for mobile, userViewStatistics := range mobileViewMap {
  62. item := &models.UserViewStatistics{
  63. Mobile: mobile,
  64. RealName: userViewStatistics.RealName,
  65. CompanyName: userViewStatistics.CompanyName,
  66. ViewNum: userViewStatistics.Total,
  67. LastViewTime: userViewStatistics.ViewTime,
  68. Date: dateTime,
  69. CreateTime: time.Now(),
  70. }
  71. tmpList = append(tmpList, item)
  72. }
  73. //最后出来后,如果还有数据未插入,那么再去批量插入数据库
  74. if len(tmpList) > 0 {
  75. _ = models.AddMultiUserViewStatistics(tmpList)
  76. }
  77. }
  78. }
  79. //邮箱
  80. {
  81. emailViewMap := make(map[string]*models.UserViewEmailTotalSlice)
  82. list, tmpErr := models.GetCountUserViewHistoryByEmails(dateStr)
  83. if tmpErr != nil {
  84. err = tmpErr
  85. return
  86. }
  87. for _, v := range list {
  88. emailViewMap[v.Email] = v
  89. }
  90. list, tmpErr = models.GetReportViewMaxTimeByEmails(dateStr)
  91. if tmpErr != nil {
  92. err = tmpErr
  93. return
  94. }
  95. for _, v := range list {
  96. if userViewStatistics, ok := emailViewMap[v.Email]; ok {
  97. userViewStatistics.Total += v.Total
  98. if v.ViewTime.After(userViewStatistics.ViewTime) {
  99. userViewStatistics.ViewTime = v.ViewTime
  100. }
  101. } else {
  102. emailViewMap[v.Email] = v
  103. }
  104. }
  105. list, tmpErr = models.GetAdvisoryCountUserViewHistoryByEmails(dateStr)
  106. if tmpErr != nil {
  107. err = tmpErr
  108. return
  109. }
  110. for _, v := range list {
  111. if userViewStatistics, ok := emailViewMap[v.Email]; ok {
  112. userViewStatistics.Total += v.Total
  113. if v.ViewTime.After(userViewStatistics.ViewTime) {
  114. userViewStatistics.ViewTime = v.ViewTime
  115. }
  116. } else {
  117. emailViewMap[v.Email] = v
  118. }
  119. }
  120. if len(emailViewMap) > 0 {
  121. tmpList := make([]*models.UserViewStatistics, 0)
  122. for email, userViewStatistics := range emailViewMap {
  123. item := &models.UserViewStatistics{
  124. Email: email,
  125. RealName: userViewStatistics.RealName,
  126. CompanyName: userViewStatistics.CompanyName,
  127. ViewNum: userViewStatistics.Total,
  128. LastViewTime: userViewStatistics.ViewTime,
  129. Date: dateTime,
  130. CreateTime: time.Now(),
  131. }
  132. tmpList = append(tmpList, item)
  133. }
  134. //最后出来后,如果还有数据未插入,那么再去批量插入数据库
  135. if len(tmpList) > 0 {
  136. _ = models.AddMultiUserViewStatistics(tmpList)
  137. }
  138. }
  139. }
  140. return
  141. }