activity_view_log.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package activity
  2. import (
  3. "hongze/hongze_yb/models/tables/yb_activity"
  4. "hongze/hongze_yb/models/tables/yb_activity_view_log"
  5. "time"
  6. )
  7. // GetUnRead 获取未读数
  8. func GetUnRead( activityIds []int, userId uint64) (unRead int, err error) {
  9. firstDay := "2022-01-01"
  10. newActivityList, err := yb_activity.GetActivityByIDsAndDate(activityIds, firstDay)
  11. if err != nil {
  12. return
  13. }
  14. var newIds []int
  15. activityMap := make(map[int]bool)
  16. if len(newActivityList) > 0 {
  17. for _, v := range newActivityList {
  18. newIds = append(newIds, v.ActivityID)
  19. activityMap[v.ActivityID] = true
  20. }
  21. //获取用户的浏览记录
  22. viewActivityIds, tErr := yb_activity_view_log.GetByActivityIdsAndUserIdAndTime(newIds, userId, firstDay)
  23. if tErr != nil {
  24. err = tErr
  25. return
  26. }
  27. //过滤已经浏览过的活动ID
  28. if len(viewActivityIds) > 0 {
  29. for _, item := range viewActivityIds {
  30. if _, ok := activityMap[item]; ok {
  31. delete(activityMap, item)
  32. }
  33. }
  34. }
  35. unRead = len(activityMap)
  36. }
  37. return
  38. }
  39. // BatchInsertActivityView 批量新增报告已读记录
  40. func BatchInsertActivityView(activityIds []int, userId uint64) (num int, err error) {
  41. firstDay := "2022-01-01"
  42. newActivityList, err := yb_activity.GetActivityByIDsAndDate(activityIds, firstDay)
  43. if err != nil {
  44. return
  45. }
  46. var newIds []int
  47. activityMap := make(map[int]bool)
  48. if len(newActivityList) > 0 {
  49. for _, v := range newActivityList {
  50. newIds = append(newIds, v.ActivityID)
  51. activityMap[v.ActivityID] = true
  52. }
  53. //获取用户的浏览记录
  54. viewActivityIds, tErr := yb_activity_view_log.GetByActivityIdsAndUserIdAndTime(newIds, userId, firstDay)
  55. if tErr != nil {
  56. err = tErr
  57. return
  58. }
  59. //过滤已经浏览过的活动ID
  60. if len(viewActivityIds) > 0 {
  61. for _, item := range viewActivityIds {
  62. if _, ok := activityMap[item]; ok {
  63. delete(activityMap, item)
  64. }
  65. }
  66. }
  67. var logs []*yb_activity_view_log.YbActivityViewLog
  68. for k, _ := range activityMap{
  69. temp := new(yb_activity_view_log.YbActivityViewLog)
  70. temp.ActivityId = k
  71. temp.UserId = userId
  72. temp.CreateTime = time.Now()
  73. logs = append(logs, temp)
  74. }
  75. tErr = yb_activity_view_log.CreateInBatches(logs)
  76. if tErr != nil {
  77. err = tErr
  78. return
  79. }
  80. num = len(logs)
  81. }
  82. return
  83. }