article_history.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/utils"
  7. "strconv"
  8. "time"
  9. )
  10. func GetArticleHistoryByUser(articleIds []int, user *models.WxUserItem) (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) + `) AND user_id = ?`
  25. pars = append(pars, articleIds, user.UserId)
  26. list, err := models.GetCygxArticleHistoryRecordNewpvList(condition, pars)
  27. if err != nil {
  28. return
  29. }
  30. mapResp = make(map[int]int, 0)
  31. for _, v := range list {
  32. mapResp[v.ArticleId]++
  33. }
  34. return
  35. }
  36. // 记录用户文章浏览记录
  37. func ArticleHistory(articleId int, user *models.WxUserItem) (err error) {
  38. defer func() {
  39. if err != nil {
  40. go utils.SendAlarmMsg("记录用户文章浏览记录,失败"+err.Error(), 2)
  41. }
  42. }()
  43. uid := user.UserId
  44. key := "CYGX_ARTICLE_" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid)
  45. if !utils.Rc.IsExist(key) {
  46. //新增浏览记录
  47. //这个表貌似没怎么用了,暂时保留记录
  48. record := new(models.CygxArticleViewRecord)
  49. record.UserId = uid
  50. record.ArticleId = articleId
  51. record.CreateTime = time.Now()
  52. record.Mobile = user.Mobile
  53. record.Email = user.Email
  54. record.CompanyId = user.CompanyId
  55. record.CompanyName = user.CompanyName
  56. _, e := models.AddCygxArticleViewRecord(record)
  57. if e != nil {
  58. err = errors.New("AddCygxArticleViewRecord, Err: " + e.Error())
  59. return
  60. }
  61. e = models.ModifyReportLastViewTime(uid)
  62. if e != nil {
  63. err = errors.New("ModifyReportLastViewTime, Err: " + e.Error())
  64. return
  65. }
  66. utils.Rc.Put(key, 1, 2*time.Second)
  67. }
  68. return
  69. }
  70. // 记录用户文章浏览记录带时长
  71. func ArticleHistoryStopTime(articleId, stopTime, outType int, user *models.WxUserItem) (err error) {
  72. defer func() {
  73. if err != nil {
  74. go utils.SendAlarmMsg("记录用户文章浏览记录带时长,失败"+err.Error(), 2)
  75. }
  76. }()
  77. if stopTime < 3 {
  78. return
  79. }
  80. uid := user.UserId
  81. key := "CYGX_ARTICLE_PV" + strconv.Itoa(articleId) + "_" + strconv.Itoa(uid) + "_" + strconv.Itoa(user.CompanyId) + "_" + strconv.Itoa(outType)
  82. if !utils.Rc.IsExist(key) {
  83. record := new(models.CygxArticleHistoryRecordNewpv)
  84. record.UserId = uid
  85. record.ArticleId = articleId
  86. record.CreateTime = time.Now().Add(-time.Second * time.Duration(stopTime))
  87. record.ModifyTime = time.Now()
  88. record.Mobile = user.Mobile
  89. record.Email = user.Email
  90. record.CompanyId = user.CompanyId
  91. record.CompanyName = user.CompanyName
  92. record.StopTime = stopTime
  93. record.OutType = outType
  94. record.Source = "MOBILE"
  95. newId, e := models.AddCygxArticleViewRecordNewpv(record)
  96. if e != nil {
  97. err = errors.New("AddCygxArticleViewRecordNewpv, Err: " + e.Error())
  98. return
  99. }
  100. recordRedis := new(ReportViewRecord)
  101. recordRedis.UserId = user.UserId
  102. recordRedis.ReportId = articleId
  103. recordRedis.Mobile = user.Mobile
  104. recordRedis.Email = user.Email
  105. recordRedis.RealName = user.RealName
  106. recordRedis.CompanyName = user.CompanyName
  107. recordRedis.StopTime = stopTime
  108. recordRedis.OutId = int(newId)
  109. recordRedis.CreateTime = time.Now()
  110. go PushViewRecordNewRedisData(recordRedis, user.CompanyId)
  111. utils.Rc.Put(key, 1, 2*time.Second)
  112. }
  113. go ArticleHistoryUserLabelLogAdd(articleId, uid)
  114. return
  115. }