article_history.go 3.8 KB

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