article_history.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_clpt/models"
  6. "hongze/hongze_clpt/utils"
  7. "strconv"
  8. "time"
  9. )
  10. // 记录用户文章浏览记录
  11. func ArticleHistory(articleId int, user *models.WxUserItem) (err error) {
  12. defer func() {
  13. if err != nil {
  14. go utils.SendAlarmMsg("记录用户文章浏览记录,失败"+err.Error(), 2)
  15. }
  16. }()
  17. uid := user.UserId
  18. key := "CYGX_ARTICLE_" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
  19. if !utils.Rc.IsExist(key) {
  20. //新增浏览记录
  21. //这个表貌似没怎么用了,暂时保留记录
  22. record := new(models.CygxArticleViewRecord)
  23. record.UserId = uid
  24. record.ArticleId = articleId
  25. record.CreateTime = time.Now()
  26. record.Mobile = user.Mobile
  27. record.Email = user.Email
  28. record.CompanyId = user.CompanyId
  29. record.CompanyName = user.CompanyName
  30. _, e := models.AddCygxArticleViewRecord(record)
  31. if e != nil {
  32. err = errors.New("AddCygxArticleViewRecord, Err: " + e.Error())
  33. return
  34. }
  35. e = models.ModifyReportLastViewTime(uid)
  36. if e != nil {
  37. err = errors.New("ModifyReportLastViewTime, Err: " + e.Error())
  38. return
  39. }
  40. utils.Rc.Put(key, 1, 2*time.Second)
  41. }
  42. return
  43. }
  44. // 记录用户文章浏览记录带时长
  45. func ArticleHistoryStopTime(articleId, stopTime, outType int, user *models.WxUserItem) (err error) {
  46. defer func() {
  47. if err != nil {
  48. go utils.SendAlarmMsg("记录用户文章浏览记录带时长,失败"+err.Error(), 2)
  49. }
  50. }()
  51. if stopTime < 3 {
  52. return
  53. }
  54. uid := user.UserId
  55. key := "CYGX_ARTICLE_PV" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid) + "_" + strconv.Itoa(user.CompanyId) + "_" + strconv.Itoa(outType)
  56. if !utils.Rc.IsExist(key) {
  57. record := new(models.CygxArticleHistoryRecordNewpv)
  58. record.UserId = uid
  59. record.ArticleId = articleId
  60. record.CreateTime = time.Now().Add(-time.Second * time.Duration(stopTime))
  61. record.ModifyTime = time.Now()
  62. record.Mobile = user.Mobile
  63. record.Email = user.Email
  64. record.CompanyId = user.CompanyId
  65. record.CompanyName = user.CompanyName
  66. record.StopTime = stopTime
  67. record.OutType = outType
  68. record.RegisterPlatform = utils.REGISTER_PLATFORM
  69. record.Source = "WEB"
  70. newId, e := models.AddCygxArticleViewRecordNewpv(record)
  71. if e != nil {
  72. err = errors.New("AddCygxArticleViewRecordNewpv, Err: " + e.Error())
  73. return
  74. }
  75. recordRedis := new(ReportViewRecord)
  76. recordRedis.UserId = user.UserId
  77. recordRedis.ReportId = articleId
  78. recordRedis.Mobile = user.Mobile
  79. recordRedis.Email = user.Email
  80. recordRedis.RealName = user.RealName
  81. recordRedis.CompanyName = user.CompanyName
  82. recordRedis.StopTime = stopTime
  83. recordRedis.OutId = int(newId)
  84. recordRedis.CreateTime = record.CreateTime
  85. go PushViewRecordNewRedisData(recordRedis, user.CompanyId)
  86. utils.Rc.Put(key, 1, 2*time.Second)
  87. }
  88. go ArticleHistoryUserLabelLogAdd(articleId, uid)
  89. return
  90. }
  91. // GetArticleHistoryByArticleId 获取文章阅读 pv map
  92. func GetArticleHistoryByArticleId(articleIds []int) (mapResp map[int]int) {
  93. var err error
  94. defer func() {
  95. if err != nil {
  96. fmt.Println(err)
  97. go utils.SendAlarmMsg("获取用户的阅读数据,信息失败,GetArticleHistoryByArticleId Err:"+err.Error(), 3)
  98. }
  99. }()
  100. lenIds := len(articleIds)
  101. if lenIds == 0 {
  102. return
  103. }
  104. var condition string
  105. var pars []interface{}
  106. condition = ` AND article_id IN (` + utils.GetOrmInReplace(lenIds) + `) `
  107. pars = append(pars, articleIds)
  108. listCy, e := models.GetCygxArticleHistoryRecordNewpvListPvCy(condition, pars)
  109. if e != nil {
  110. err = errors.New("GetCygxArticleHistoryRecordNewpvListPvCy, Err: " + e.Error())
  111. return
  112. }
  113. mapResp = make(map[int]int, 0)
  114. for _, v := range listCy {
  115. mapResp[v.ArticleId] = v.Pv
  116. }
  117. listCl, e := models.GetCygxArticleHistoryRecordNewpvListPvCl(condition, pars)
  118. if e != nil {
  119. err = errors.New("GetCygxArticleHistoryRecordNewpvListPvCy, Err: " + e.Error())
  120. return
  121. }
  122. for _, v := range listCl {
  123. mapResp[v.ArticleId] += v.Pv
  124. }
  125. return
  126. }