article_history.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. if user.CompanyId == 1 {
  82. return
  83. }
  84. defer func() {
  85. if err != nil {
  86. go utils.SendAlarmMsg(fmt.Sprint("记录用户文章浏览记录带时长,失败 ,articleId", articleId, "uid", user.UserId, err.Error()), 2)
  87. }
  88. }()
  89. if stopTime < 3 {
  90. return
  91. }
  92. uid := user.UserId
  93. key := "CYGX_ARTICLE_PV" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid) + "_" + strconv.Itoa(user.CompanyId) + "_" + strconv.Itoa(outType)
  94. if !utils.Rc.IsExist(key) {
  95. record := new(models.CygxArticleHistoryRecordNewpv)
  96. record.UserId = uid
  97. record.ArticleId = articleId
  98. record.CreateTime = time.Now().Add(-time.Second * time.Duration(stopTime))
  99. record.ModifyTime = time.Now()
  100. record.Mobile = user.Mobile
  101. record.Email = user.Email
  102. record.CompanyId = user.CompanyId
  103. record.CompanyName = user.CompanyName
  104. record.StopTime = stopTime
  105. record.OutType = outType
  106. record.RegisterPlatform = utils.REGISTER_PLATFORM
  107. record.Source = "MOBILE"
  108. newId, e := models.AddCygxArticleViewRecordNewpv(record)
  109. if e != nil {
  110. err = errors.New("AddCygxArticleViewRecordNewpv, Err: " + e.Error())
  111. return
  112. }
  113. recordRedis := new(ReportViewRecord)
  114. recordRedis.UserId = user.UserId
  115. recordRedis.ReportId = articleId
  116. recordRedis.Mobile = user.Mobile
  117. recordRedis.Email = user.Email
  118. recordRedis.RealName = user.RealName
  119. recordRedis.CompanyName = user.CompanyName
  120. recordRedis.StopTime = stopTime
  121. recordRedis.ViewTime = record.CreateTime.Format(utils.FormatDateTime)
  122. recordRedis.OutId = int(newId)
  123. //recordRedis.CreateTime = time.Now()
  124. go PushViewRecordNewRedisData(recordRedis, user.CompanyId)
  125. utils.Rc.Put(key, 1, 2*time.Second)
  126. }
  127. go ArticleHistoryUserLabelLogAdd(articleId, uid)
  128. return
  129. }